From e58c98e759064050687f4f0b7278089f3bf21391 Mon Sep 17 00:00:00 2001 From: falcon <9504402@qq.com> Date: Wed, 12 Oct 2022 15:30:57 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84=E5=AE=9E=E4=BD=93=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1=E3=80=82=E5=A2=9E=E5=8A=A0MaxLength=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SugarConnectionConfig.cs | 94 +++++++++++-------- 1 file changed, 54 insertions(+), 40 deletions(-) diff --git a/Falcon.SugarApi/DatabaseDefinitions/SugarConnectionConfig.cs b/Falcon.SugarApi/DatabaseDefinitions/SugarConnectionConfig.cs index 8545594..c5d4749 100644 --- a/Falcon.SugarApi/DatabaseDefinitions/SugarConnectionConfig.cs +++ b/Falcon.SugarApi/DatabaseDefinitions/SugarConnectionConfig.cs @@ -18,50 +18,64 @@ namespace Falcon.SugarApi.DatabaseDefinitions /// public bool Log { get; set; } + /// + /// 实体创建服务 + /// + public Action EntityService + => this.ConfigureExternalServices.EntityService; + /// /// 实例化SugarDb链接配置 /// public SugarConnectionConfig() { this.ConfigureExternalServices ??= new ConfigureExternalServices { }; - //设置Nullable - this.ConfigureExternalServices.EntityService += (p, c) => { - var pt = p.PropertyType; - //所有类型默认可空 - bool na = true; - //字符串默认可空 - na = pt == typeof(string) ? true : na; - //Nullable<>类型可空 - if (pt.IsGenericType && pt.GetGenericTypeDefinition() == typeof(Nullable<>)) { - na = true; - } - //RequiredAttribute标记不可空 - if (p.GetCustomAttribute() != null) { - na = false; - } - //主键不可以为空 - if (p.TryGetAttribute(out var _)) { - na = false; - } - //定义主键不可以为空 - if (p.TryGetAttribute(out var sc) && sc.IsPrimaryKey) { - na = false; - } - c.IsNullable = na; - //var sc = pt.GetCustomAttribute(); - //if (sc != null) { - // c.IsNullable = sc.IsNullable; - //} - //var isNullableTypes = new Type[] { typeof(string) }; - //if (isNullableTypes.Contains(pt)) { - // c.IsNullable = true; - //} - //else { - // c.IsNullable = false; - //} - //if (pt == typeof(string) && pt.GetCustomAttribute() == null) { - // c.IsNullable = true; - //} - }; + this.ConfigureExternalServices.EntityService += SetupNullable; + this.ConfigureExternalServices.EntityService += SetupLength; + } + + /// + /// SetNullable + /// + /// 属性信息 + /// 列信息 + public virtual void SetupNullable(PropertyInfo p, EntityColumnInfo c) { + var pt = p.PropertyType; + //所有类型默认可空 + bool na = true; + //字符串默认可空 + na = pt == typeof(string) ? true : na; + //Nullable<>类型可空 + if (pt.IsGenericType && pt.GetGenericTypeDefinition() == typeof(Nullable<>)) { + na = true; + } + //RequiredAttribute标记不可空 + if (p.GetCustomAttribute() != null) { + na = false; + } + //主键不可以为空 + if (p.TryGetAttribute(out var _)) { + na = false; + } + //定义主键不可以为空 + if (p.TryGetAttribute(out var sc) && sc.IsPrimaryKey) { + na = false; + } + c.IsNullable = na; + } + /// + /// Set 长度规则 + /// + /// 属性信息 + /// 列信息 + public virtual void SetupLength(PropertyInfo p, EntityColumnInfo c) { + int len = 0; + if (p.TryGetAttribute(out var la)) { + len = la.Length; + } + if (p.TryGetAttribute(out var sc) && sc.Length != 0) { + len = sc.Length; + } + c.Length = len; } private static ICacheService? CacheService = null; @@ -102,7 +116,7 @@ namespace Falcon.SugarApi.DatabaseDefinitions /// 序列化实现 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); } } }