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); } } }