重构实体服务。增加MaxLength支持

This commit is contained in:
falcon 2022-10-12 15:30:57 +08:00
parent 9ba6e0c4ac
commit e58c98e759

View File

@ -18,50 +18,64 @@ namespace Falcon.SugarApi.DatabaseDefinitions
/// </summary> /// </summary>
public bool Log { get; set; } public bool Log { get; set; }
/// <summary>
/// 实体创建服务
/// </summary>
public Action<PropertyInfo, EntityColumnInfo> EntityService
=> this.ConfigureExternalServices.EntityService;
/// <summary> /// <summary>
/// 实例化SugarDb链接配置 /// 实例化SugarDb链接配置
/// </summary> /// </summary>
public SugarConnectionConfig() { public SugarConnectionConfig() {
this.ConfigureExternalServices ??= new ConfigureExternalServices { }; this.ConfigureExternalServices ??= new ConfigureExternalServices { };
//设置Nullable this.ConfigureExternalServices.EntityService += SetupNullable;
this.ConfigureExternalServices.EntityService += (p, c) => { this.ConfigureExternalServices.EntityService += SetupLength;
var pt = p.PropertyType; }
//所有类型默认可空
bool na = true; /// <summary>
//字符串默认可空 /// SetNullable
na = pt == typeof(string) ? true : na; /// </summary>
//Nullable<>类型可空 /// <param name="p">属性信息</param>
if (pt.IsGenericType && pt.GetGenericTypeDefinition() == typeof(Nullable<>)) { /// <param name="c">列信息</param>
na = true; public virtual void SetupNullable(PropertyInfo p, EntityColumnInfo c) {
} var pt = p.PropertyType;
//RequiredAttribute标记不可空 //所有类型默认可空
if (p.GetCustomAttribute<RequiredAttribute>() != null) { bool na = true;
na = false; //字符串默认可空
} na = pt == typeof(string) ? true : na;
//主键不可以为空 //Nullable<>类型可空
if (p.TryGetAttribute<KeyAttribute>(out var _)) { if (pt.IsGenericType && pt.GetGenericTypeDefinition() == typeof(Nullable<>)) {
na = false; na = true;
} }
//定义主键不可以为空 //RequiredAttribute标记不可空
if (p.TryGetAttribute<SugarColumn>(out var sc) && sc.IsPrimaryKey) { if (p.GetCustomAttribute<RequiredAttribute>() != null) {
na = false; na = false;
} }
c.IsNullable = na; //主键不可以为空
//var sc = pt.GetCustomAttribute<SugarColumn>(); if (p.TryGetAttribute<KeyAttribute>(out var _)) {
//if (sc != null) { na = false;
// c.IsNullable = sc.IsNullable; }
//} //定义主键不可以为空
//var isNullableTypes = new Type[] { typeof(string) }; if (p.TryGetAttribute<SugarColumn>(out var sc) && sc.IsPrimaryKey) {
//if (isNullableTypes.Contains(pt)) { na = false;
// c.IsNullable = true; }
//} c.IsNullable = na;
//else { }
// c.IsNullable = false; /// <summary>
//} /// Set 长度规则
//if (pt == typeof(string) && pt.GetCustomAttribute<RequiredAttribute>() == null) { /// </summary>
// c.IsNullable = true; /// <param name="p">属性信息</param>
//} /// <param name="c">列信息</param>
}; public virtual void SetupLength(PropertyInfo p, EntityColumnInfo c) {
int len = 0;
if (p.TryGetAttribute<MaxLengthAttribute>(out var la)) {
len = la.Length;
}
if (p.TryGetAttribute<SugarColumn>(out var sc) && sc.Length != 0) {
len = sc.Length;
}
c.Length = len;
} }
private static ICacheService? CacheService = null; private static ICacheService? CacheService = null;
@ -102,7 +116,7 @@ namespace Falcon.SugarApi.DatabaseDefinitions
/// <param name="serialize">序列化实现</param> /// <param name="serialize">序列化实现</param>
public void AddDistributedCache(IDistributedCache cache, IJsonSerialize serialize) { public void AddDistributedCache(IDistributedCache cache, IJsonSerialize serialize) {
this.ConfigureExternalServices ??= new ConfigureExternalServices { }; this.ConfigureExternalServices ??= new ConfigureExternalServices { };
this.ConfigureExternalServices.DataInfoCacheService = new DistributedCache(cache,serialize); this.ConfigureExternalServices.DataInfoCacheService = new DistributedCache(cache, serialize);
} }
} }
} }