重构实体服务。增加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>
public bool Log { get; set; }
/// <summary>
/// 实体创建服务
/// </summary>
public Action<PropertyInfo, EntityColumnInfo> EntityService
=> this.ConfigureExternalServices.EntityService;
/// <summary>
/// 实例化SugarDb链接配置
/// </summary>
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<RequiredAttribute>() != null) {
na = false;
}
//主键不可以为空
if (p.TryGetAttribute<KeyAttribute>(out var _)) {
na = false;
}
//定义主键不可以为空
if (p.TryGetAttribute<SugarColumn>(out var sc) && sc.IsPrimaryKey) {
na = false;
}
c.IsNullable = na;
//var sc = pt.GetCustomAttribute<SugarColumn>();
//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<RequiredAttribute>() == null) {
// c.IsNullable = true;
//}
};
this.ConfigureExternalServices.EntityService += SetupNullable;
this.ConfigureExternalServices.EntityService += SetupLength;
}
/// <summary>
/// SetNullable
/// </summary>
/// <param name="p">属性信息</param>
/// <param name="c">列信息</param>
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<RequiredAttribute>() != null) {
na = false;
}
//主键不可以为空
if (p.TryGetAttribute<KeyAttribute>(out var _)) {
na = false;
}
//定义主键不可以为空
if (p.TryGetAttribute<SugarColumn>(out var sc) && sc.IsPrimaryKey) {
na = false;
}
c.IsNullable = na;
}
/// <summary>
/// Set 长度规则
/// </summary>
/// <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;
@ -102,7 +116,7 @@ namespace Falcon.SugarApi.DatabaseDefinitions
/// <param name="serialize">序列化实现</param>
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);
}
}
}