新增实体列映射和实体表映射

This commit is contained in:
falcon 2022-10-13 17:22:17 +08:00
parent e58c98e759
commit cdc0f5f1a0
8 changed files with 181 additions and 50 deletions

View File

@ -0,0 +1,19 @@
using SqlSugar;
using System.ComponentModel.DataAnnotations;
using System.Reflection;
namespace Falcon.SugarApi.DatabaseDefinitions.EntityServices
{
/// <summary>
/// 设置主键
/// </summary>
public class SetupKey : IEntityColumnServices
{
/// <inheritdoc/>
public void SetupColumn(PropertyInfo p, EntityColumnInfo c) {
if (p.TryGetAttribute<KeyAttribute>(out var _)) {
c.IsPrimarykey = true;
}
}
}
}

View File

@ -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
{
/// <summary>
/// 设置长度规则
/// </summary>
public class SetupLength : IEntityColumnServices
{
/// <inheritdoc/>
public void SetupColumn(PropertyInfo p, EntityColumnInfo c) {
var len = new List<int>();
if (p.TryGetAttribute<StringLengthAttribute>(out var sl)) {
len.Add(sl.MaximumLength);
}
if (p.TryGetAttribute<MaxLengthAttribute>(out var la)) {
len.Add(la.Length);
}
if (p.TryGetAttribute<SugarColumn>(out var sc) && sc.Length != 0) {
len.Add(sc.Length);
}
c.Length = len.Max();
}
}
}

View File

@ -0,0 +1,39 @@
using SqlSugar;
using System;
using System.ComponentModel.DataAnnotations;
using System.Reflection;
namespace Falcon.SugarApi.DatabaseDefinitions.EntityServices
{
/// <summary>
/// 设置Nullable
/// </summary>
public class SetupNullable : IEntityColumnServices
{
/// <inheritdoc/>
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<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;
}
}
}

View File

@ -0,0 +1,19 @@
using SqlSugar;
using System;
using System.ComponentModel.DataAnnotations.Schema;
namespace Falcon.SugarApi.DatabaseDefinitions.EntityServices
{
/// <summary>
/// 设置表名服务
/// </summary>
public class TableNameTableService : IEntityTableServices
{
/// <inheritdoc/>
public void SetupTable(Type t, EntityInfo e) {
if (t.TryGetAttribute<TableAttribute>(out var tn)) {
e.DbTableName = tn.Name;
}
}
}
}

View File

@ -0,0 +1,18 @@
using SqlSugar;
using System.Reflection;
namespace Falcon.SugarApi.DatabaseDefinitions
{
/// <summary>
/// 实现实体属性到列的映射服务
/// </summary>
public interface IEntityColumnServices
{
/// <summary>
/// 设置列属性
/// </summary>
/// <param name="p"></param>
/// <param name="c"></param>
void SetupColumn(PropertyInfo p, EntityColumnInfo c);
}
}

View File

@ -0,0 +1,19 @@
using SqlSugar;
using System;
using System.Reflection;
namespace Falcon.SugarApi.DatabaseDefinitions
{
/// <summary>
/// 实现实体类型到表映射服务
/// </summary>
public interface IEntityTableServices
{
/// <summary>
/// 设置表属性
/// </summary>
/// <param name="t">类型</param>
/// <param name="e">实体</param>
void SetupTable(Type t, EntityInfo e);
}
}

View File

@ -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; }
/// <summary>
/// 实体创建服务
/// 表映射服务
/// </summary>
public Action<PropertyInfo, EntityColumnInfo> EntityService
=> this.ConfigureExternalServices.EntityService;
public static List<IEntityTableServices> TableServices { get; set; } = new List<IEntityTableServices>();
/// <summary>
/// 列映射服务
/// </summary>
public static List<IEntityColumnServices> ColumnServices { get; set; } = new List<IEntityColumnServices>();
static SugarConnectionConfig() {
TableServices.Add(new TableNameTableService());
ColumnServices.Add(new SetupKey());
ColumnServices.Add(new SetupLength());
ColumnServices.Add(new SetupNullable());
}
/// <summary>
/// 实例化SugarDb链接配置
/// </summary>
public SugarConnectionConfig() {
this.ConfigureExternalServices ??= new ConfigureExternalServices { };
this.ConfigureExternalServices.EntityService += SetupNullable;
this.ConfigureExternalServices.EntityService += SetupLength;
this.ConfigureExternalServices.EntityNameService = (t, e) => {
foreach (var i in TableServices) {
i.SetupTable(t, e);
}
/// <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;
};
this.ConfigureExternalServices.EntityService = (p, c) => {
foreach (var i in ColumnServices) {
i.SetupColumn(p, c);
}
//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;

View File

@ -21,5 +21,17 @@ namespace Falcon.SugarApi
p = info.GetCustomAttribute<T>();
return p != null;
}
/// <summary>
/// 尝试获取Attribute
/// </summary>
/// <typeparam name="T">Attribute类型</typeparam>
/// <param name="info">属性</param>
/// <param name="p">定义的特性</param>
/// <returns>定义返回True否则False</returns>
public static bool TryGetAttribute<T>([NotNull] this Type info, out T p) where T : Attribute {
p = info.GetCustomAttribute<T>();
return p != null;
}
}
}