diff --git a/Falcon.SugarApi/DatabaseDefinitions/EntityServices/SetupKey.cs b/Falcon.SugarApi/DatabaseDefinitions/EntityServices/SetupKey.cs
new file mode 100644
index 0000000..3a9250f
--- /dev/null
+++ b/Falcon.SugarApi/DatabaseDefinitions/EntityServices/SetupKey.cs
@@ -0,0 +1,19 @@
+using SqlSugar;
+using System.ComponentModel.DataAnnotations;
+using System.Reflection;
+
+namespace Falcon.SugarApi.DatabaseDefinitions.EntityServices
+{
+ ///
+ /// 设置主键
+ ///
+ public class SetupKey : IEntityColumnServices
+ {
+ ///
+ public void SetupColumn(PropertyInfo p, EntityColumnInfo c) {
+ if (p.TryGetAttribute(out var _)) {
+ c.IsPrimarykey = true;
+ }
+ }
+ }
+}
diff --git a/Falcon.SugarApi/DatabaseDefinitions/EntityServices/SetupLength.cs b/Falcon.SugarApi/DatabaseDefinitions/EntityServices/SetupLength.cs
new file mode 100644
index 0000000..732f968
--- /dev/null
+++ b/Falcon.SugarApi/DatabaseDefinitions/EntityServices/SetupLength.cs
@@ -0,0 +1,29 @@
+using SqlSugar;
+using System.Collections.Generic;
+using System.ComponentModel.DataAnnotations;
+using System.Linq;
+using System.Reflection;
+
+namespace Falcon.SugarApi.DatabaseDefinitions.EntityServices
+{
+ ///
+ /// 设置长度规则
+ ///
+ public class SetupLength : IEntityColumnServices
+ {
+ ///
+ public void SetupColumn(PropertyInfo p, EntityColumnInfo c) {
+ var len = new List();
+ if (p.TryGetAttribute(out var sl)) {
+ len.Add(sl.MaximumLength);
+ }
+ if (p.TryGetAttribute(out var la)) {
+ len.Add(la.Length);
+ }
+ if (p.TryGetAttribute(out var sc) && sc.Length != 0) {
+ len.Add(sc.Length);
+ }
+ c.Length = len.Max();
+ }
+ }
+}
diff --git a/Falcon.SugarApi/DatabaseDefinitions/EntityServices/SetupNullable.cs b/Falcon.SugarApi/DatabaseDefinitions/EntityServices/SetupNullable.cs
new file mode 100644
index 0000000..07c9c87
--- /dev/null
+++ b/Falcon.SugarApi/DatabaseDefinitions/EntityServices/SetupNullable.cs
@@ -0,0 +1,39 @@
+using SqlSugar;
+using System;
+using System.ComponentModel.DataAnnotations;
+using System.Reflection;
+
+namespace Falcon.SugarApi.DatabaseDefinitions.EntityServices
+{
+ ///
+ /// 设置Nullable
+ ///
+ public class SetupNullable : IEntityColumnServices
+ {
+ ///
+ public void SetupColumn(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;
+ }
+ }
+}
diff --git a/Falcon.SugarApi/DatabaseDefinitions/EntityServices/TableNameTableService.cs b/Falcon.SugarApi/DatabaseDefinitions/EntityServices/TableNameTableService.cs
new file mode 100644
index 0000000..5ffb647
--- /dev/null
+++ b/Falcon.SugarApi/DatabaseDefinitions/EntityServices/TableNameTableService.cs
@@ -0,0 +1,19 @@
+using SqlSugar;
+using System;
+using System.ComponentModel.DataAnnotations.Schema;
+
+namespace Falcon.SugarApi.DatabaseDefinitions.EntityServices
+{
+ ///
+ /// 设置表名服务
+ ///
+ public class TableNameTableService : IEntityTableServices
+ {
+ ///
+ public void SetupTable(Type t, EntityInfo e) {
+ if (t.TryGetAttribute(out var tn)) {
+ e.DbTableName = tn.Name;
+ }
+ }
+ }
+}
diff --git a/Falcon.SugarApi/DatabaseDefinitions/IEntityColumnServices.cs b/Falcon.SugarApi/DatabaseDefinitions/IEntityColumnServices.cs
new file mode 100644
index 0000000..721bdfd
--- /dev/null
+++ b/Falcon.SugarApi/DatabaseDefinitions/IEntityColumnServices.cs
@@ -0,0 +1,18 @@
+using SqlSugar;
+using System.Reflection;
+
+namespace Falcon.SugarApi.DatabaseDefinitions
+{
+ ///
+ /// 实现实体属性到列的映射服务
+ ///
+ public interface IEntityColumnServices
+ {
+ ///
+ /// 设置列属性
+ ///
+ ///
+ ///
+ void SetupColumn(PropertyInfo p, EntityColumnInfo c);
+ }
+}
diff --git a/Falcon.SugarApi/DatabaseDefinitions/IEntityTableServices.cs b/Falcon.SugarApi/DatabaseDefinitions/IEntityTableServices.cs
new file mode 100644
index 0000000..3a1379d
--- /dev/null
+++ b/Falcon.SugarApi/DatabaseDefinitions/IEntityTableServices.cs
@@ -0,0 +1,19 @@
+using SqlSugar;
+using System;
+using System.Reflection;
+
+namespace Falcon.SugarApi.DatabaseDefinitions
+{
+ ///
+ /// 实现实体类型到表映射服务
+ ///
+ public interface IEntityTableServices
+ {
+ ///
+ /// 设置表属性
+ ///
+ /// 类型
+ /// 实体
+ void SetupTable(Type t, EntityInfo e);
+ }
+}
diff --git a/Falcon.SugarApi/DatabaseDefinitions/SugarConnectionConfig.cs b/Falcon.SugarApi/DatabaseDefinitions/SugarConnectionConfig.cs
index c5d4749..46fddde 100644
--- a/Falcon.SugarApi/DatabaseDefinitions/SugarConnectionConfig.cs
+++ b/Falcon.SugarApi/DatabaseDefinitions/SugarConnectionConfig.cs
@@ -1,8 +1,10 @@
using Falcon.SugarApi.DatabaseDefinitions.Cache;
+using Falcon.SugarApi.DatabaseDefinitions.EntityServices;
using Falcon.SugarApi.JsonSerialize;
using Microsoft.Extensions.Caching.Distributed;
using SqlSugar;
using System;
+using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Reflection;
@@ -19,63 +21,37 @@ namespace Falcon.SugarApi.DatabaseDefinitions
public bool Log { get; set; }
///
- /// 实体创建服务
+ /// 表映射服务
///
- public Action EntityService
- => this.ConfigureExternalServices.EntityService;
+ public static List TableServices { get; set; } = new List();
+ ///
+ /// 列映射服务
+ ///
+ public static List ColumnServices { get; set; } = new List();
+
+ static SugarConnectionConfig() {
+ TableServices.Add(new TableNameTableService());
+
+ ColumnServices.Add(new SetupKey());
+ ColumnServices.Add(new SetupLength());
+ ColumnServices.Add(new SetupNullable());
+ }
///
/// 实例化SugarDb链接配置
///
public SugarConnectionConfig() {
this.ConfigureExternalServices ??= new ConfigureExternalServices { };
- 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;
+ this.ConfigureExternalServices.EntityNameService = (t, e) => {
+ foreach (var i in TableServices) {
+ i.SetupTable(t, e);
+ }
+ };
+ this.ConfigureExternalServices.EntityService = (p, c) => {
+ foreach (var i in ColumnServices) {
+ i.SetupColumn(p, c);
+ }
+ };
}
private static ICacheService? CacheService = null;
diff --git a/Falcon.SugarApi/TypeExtend.cs b/Falcon.SugarApi/TypeExtend.cs
index 0178c11..a0d4f94 100644
--- a/Falcon.SugarApi/TypeExtend.cs
+++ b/Falcon.SugarApi/TypeExtend.cs
@@ -21,5 +21,17 @@ namespace Falcon.SugarApi
p = info.GetCustomAttribute();
return p != null;
}
+
+ ///
+ /// 尝试获取Attribute
+ ///
+ /// Attribute类型
+ /// 属性
+ /// 定义的特性
+ /// 定义返回True,否则False
+ public static bool TryGetAttribute([NotNull] this Type info, out T p) where T : Attribute {
+ p = info.GetCustomAttribute();
+ return p != null;
+ }
}
}