From cf4c66b4cd3178ae3c9af1af7087963bffeef7c5 Mon Sep 17 00:00:00 2001 From: falcon <9504402@qq.com> Date: Tue, 14 Mar 2023 16:18:20 +0800 Subject: [PATCH] =?UTF-8?q?=E6=AD=A3=E4=BB=B7JsonTypeAttribute=E7=89=B9?= =?UTF-8?q?=E6=80=A7=EF=BC=8C=E6=A0=87=E8=AE=B0=E8=AF=A5=E7=89=B9=E6=80=A7?= =?UTF-8?q?=E7=9A=84=E6=95=B0=E6=8D=AE=E4=BC=9A=E5=B7=B2json=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E5=AD=98=E5=82=A8=E5=9C=A8=E6=95=B0=E6=8D=AE=E5=BA=93?= =?UTF-8?q?=E4=B8=AD=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../EntityServices/JsonTypeColumnServices.cs | 21 +++++++++++++++++++ .../SetupLengthColumnServices.cs | 2 +- .../DatabaseDefinitions/JsonTypeAttribute.cs | 16 ++++++++++++++ .../SugarConnectionConfig.cs | 21 ++++++++++--------- 4 files changed, 49 insertions(+), 11 deletions(-) create mode 100644 Falcon.SugarApi/DatabaseDefinitions/EntityServices/JsonTypeColumnServices.cs create mode 100644 Falcon.SugarApi/DatabaseDefinitions/JsonTypeAttribute.cs diff --git a/Falcon.SugarApi/DatabaseDefinitions/EntityServices/JsonTypeColumnServices.cs b/Falcon.SugarApi/DatabaseDefinitions/EntityServices/JsonTypeColumnServices.cs new file mode 100644 index 0000000..11972f5 --- /dev/null +++ b/Falcon.SugarApi/DatabaseDefinitions/EntityServices/JsonTypeColumnServices.cs @@ -0,0 +1,21 @@ +using SqlSugar; +using System.Reflection; + +namespace Falcon.SugarApi.DatabaseDefinitions.EntityServices +{ + /// + /// 设置列以Json类型存储。 + /// + public class JsonTypeColumnServices:IEntityColumnServices + { + /// + public void SetupColumn(PropertyInfo p,EntityColumnInfo c) { + if(p.TryGetAttribute(out var a)) { + c.IsJson = true; + if(a.Length >= 0) { + c.Length = a.Length; + } + } + } + } +} diff --git a/Falcon.SugarApi/DatabaseDefinitions/EntityServices/SetupLengthColumnServices.cs b/Falcon.SugarApi/DatabaseDefinitions/EntityServices/SetupLengthColumnServices.cs index 7661087..2f653de 100644 --- a/Falcon.SugarApi/DatabaseDefinitions/EntityServices/SetupLengthColumnServices.cs +++ b/Falcon.SugarApi/DatabaseDefinitions/EntityServices/SetupLengthColumnServices.cs @@ -22,7 +22,7 @@ namespace Falcon.SugarApi.DatabaseDefinitions.EntityServices len.Add(la.Length); } if(len.Any()) { - c.Length=len.Reduce(c.Length,(m,i) => Math.Max(m,i)); + c.Length = len.Reduce(c.Length,(m,i) => Math.Max(m,i)); } } } diff --git a/Falcon.SugarApi/DatabaseDefinitions/JsonTypeAttribute.cs b/Falcon.SugarApi/DatabaseDefinitions/JsonTypeAttribute.cs new file mode 100644 index 0000000..ebcd15d --- /dev/null +++ b/Falcon.SugarApi/DatabaseDefinitions/JsonTypeAttribute.cs @@ -0,0 +1,16 @@ +using System; + +namespace Falcon.SugarApi.DatabaseDefinitions +{ + /// + /// 表示该列将以json字符串形式存储 + /// + [AttributeUsage(AttributeTargets.Property,AllowMultiple = false,Inherited = false)] + public class JsonTypeAttribute:Attribute + { + /// + /// 数据长度。默认4000 + /// + public int Length { get; set; } = 4000; + } +} diff --git a/Falcon.SugarApi/DatabaseDefinitions/SugarConnectionConfig.cs b/Falcon.SugarApi/DatabaseDefinitions/SugarConnectionConfig.cs index 5064eeb..316021b 100644 --- a/Falcon.SugarApi/DatabaseDefinitions/SugarConnectionConfig.cs +++ b/Falcon.SugarApi/DatabaseDefinitions/SugarConnectionConfig.cs @@ -13,7 +13,7 @@ namespace Falcon.SugarApi.DatabaseDefinitions /// /// SqlSugar数据库连接配置 /// - public class SugarConnectionConfig : ConnectionConfig + public class SugarConnectionConfig:ConnectionConfig { /// /// 是否使用log @@ -35,6 +35,7 @@ namespace Falcon.SugarApi.DatabaseDefinitions ColumnServices.Add(new SetupKeyColumnServices()); ColumnServices.Add(new SetupLengthColumnServices()); ColumnServices.Add(new SetupNullableColumnServices()); + ColumnServices.Add(new JsonTypeColumnServices()); } /// @@ -42,14 +43,14 @@ namespace Falcon.SugarApi.DatabaseDefinitions /// public SugarConnectionConfig() { this.ConfigureExternalServices ??= new ConfigureExternalServices { }; - this.ConfigureExternalServices.EntityNameService = (t, e) => { - foreach (var i in TableServices) { - i.SetupTable(t, e); + this.ConfigureExternalServices.EntityNameService = (t,e) => { + foreach(var i in TableServices) { + i.SetupTable(t,e); } }; - this.ConfigureExternalServices.EntityService = (p, c) => { - foreach (var i in ColumnServices) { - i.SetupColumn(p, c); + this.ConfigureExternalServices.EntityService = (p,c) => { + foreach(var i in ColumnServices) { + i.SetupColumn(p,c); } }; } @@ -67,7 +68,7 @@ namespace Falcon.SugarApi.DatabaseDefinitions /// 通过配置实现Redis缓冲,必须单例实现. /// public void AddRedisCache(string connectionString) { - if (connectionString.IsNullOrEmpty()) { + if(connectionString.IsNullOrEmpty()) { AddRedisCache(); return; } @@ -88,9 +89,9 @@ namespace Falcon.SugarApi.DatabaseDefinitions /// /// 缓存提供程序 /// 序列化实现 - public void AddDistributedCache(IDistributedCache cache, IJsonSerialize serialize) { + public void AddDistributedCache(IDistributedCache cache,IJsonSerialize serialize) { this.ConfigureExternalServices ??= new ConfigureExternalServices { }; - this.ConfigureExternalServices.DataInfoCacheService = new DistributedCache(cache, serialize); + this.ConfigureExternalServices.DataInfoCacheService = new DistributedCache(cache,serialize); } } }