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

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

View File

@ -18,13 +18,27 @@ 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;
}
/// <summary>
/// SetNullable
/// </summary>
/// <param name="p">属性信息</param>
/// <param name="c">列信息</param>
public virtual void SetupNullable(PropertyInfo p, EntityColumnInfo c) {
var pt = p.PropertyType; var pt = p.PropertyType;
//所有类型默认可空 //所有类型默认可空
bool na = true; bool na = true;
@ -47,21 +61,21 @@ namespace Falcon.SugarApi.DatabaseDefinitions
na = false; na = false;
} }
c.IsNullable = na; c.IsNullable = na;
//var sc = pt.GetCustomAttribute<SugarColumn>(); }
//if (sc != null) { /// <summary>
// c.IsNullable = sc.IsNullable; /// Set 长度规则
//} /// </summary>
//var isNullableTypes = new Type[] { typeof(string) }; /// <param name="p">属性信息</param>
//if (isNullableTypes.Contains(pt)) { /// <param name="c">列信息</param>
// c.IsNullable = true; public virtual void SetupLength(PropertyInfo p, EntityColumnInfo c) {
//} int len = 0;
//else { if (p.TryGetAttribute<MaxLengthAttribute>(out var la)) {
// c.IsNullable = false; len = la.Length;
//} }
//if (pt == typeof(string) && pt.GetCustomAttribute<RequiredAttribute>() == null) { if (p.TryGetAttribute<SugarColumn>(out var sc) && sc.Length != 0) {
// c.IsNullable = true; len = sc.Length;
//} }
}; c.Length = len;
} }
private static ICacheService? CacheService = null; private static ICacheService? CacheService = null;