正价JsonTypeAttribute特性,标记该特性的数据会已json格式存储在数据库中。

This commit is contained in:
falcon 2023-03-14 16:18:20 +08:00
parent 456969b5dc
commit cf4c66b4cd
4 changed files with 49 additions and 11 deletions

View File

@ -0,0 +1,21 @@
using SqlSugar;
using System.Reflection;
namespace Falcon.SugarApi.DatabaseDefinitions.EntityServices
{
/// <summary>
/// 设置列以Json类型存储。
/// </summary>
public class JsonTypeColumnServices:IEntityColumnServices
{
/// <inheritdoc/>
public void SetupColumn(PropertyInfo p,EntityColumnInfo c) {
if(p.TryGetAttribute<JsonTypeAttribute>(out var a)) {
c.IsJson = true;
if(a.Length >= 0) {
c.Length = a.Length;
}
}
}
}
}

View File

@ -22,7 +22,7 @@ namespace Falcon.SugarApi.DatabaseDefinitions.EntityServices
len.Add(la.Length); len.Add(la.Length);
} }
if(len.Any()) { if(len.Any()) {
c.Length=len.Reduce(c.Length,(m,i) => Math.Max(m,i)); c.Length = len.Reduce(c.Length,(m,i) => Math.Max(m,i));
} }
} }
} }

View File

@ -0,0 +1,16 @@
using System;
namespace Falcon.SugarApi.DatabaseDefinitions
{
/// <summary>
/// 表示该列将以json字符串形式存储
/// </summary>
[AttributeUsage(AttributeTargets.Property,AllowMultiple = false,Inherited = false)]
public class JsonTypeAttribute:Attribute
{
/// <summary>
/// 数据长度。默认4000
/// </summary>
public int Length { get; set; } = 4000;
}
}

View File

@ -13,7 +13,7 @@ namespace Falcon.SugarApi.DatabaseDefinitions
/// <summary> /// <summary>
/// SqlSugar数据库连接配置 /// SqlSugar数据库连接配置
/// </summary> /// </summary>
public class SugarConnectionConfig : ConnectionConfig public class SugarConnectionConfig:ConnectionConfig
{ {
/// <summary> /// <summary>
/// 是否使用log /// 是否使用log
@ -35,6 +35,7 @@ namespace Falcon.SugarApi.DatabaseDefinitions
ColumnServices.Add(new SetupKeyColumnServices()); ColumnServices.Add(new SetupKeyColumnServices());
ColumnServices.Add(new SetupLengthColumnServices()); ColumnServices.Add(new SetupLengthColumnServices());
ColumnServices.Add(new SetupNullableColumnServices()); ColumnServices.Add(new SetupNullableColumnServices());
ColumnServices.Add(new JsonTypeColumnServices());
} }
/// <summary> /// <summary>
@ -42,14 +43,14 @@ namespace Falcon.SugarApi.DatabaseDefinitions
/// </summary> /// </summary>
public SugarConnectionConfig() { public SugarConnectionConfig() {
this.ConfigureExternalServices ??= new ConfigureExternalServices { }; this.ConfigureExternalServices ??= new ConfigureExternalServices { };
this.ConfigureExternalServices.EntityNameService = (t, e) => { this.ConfigureExternalServices.EntityNameService = (t,e) => {
foreach (var i in TableServices) { foreach(var i in TableServices) {
i.SetupTable(t, e); i.SetupTable(t,e);
} }
}; };
this.ConfigureExternalServices.EntityService = (p, c) => { this.ConfigureExternalServices.EntityService = (p,c) => {
foreach (var i in ColumnServices) { foreach(var i in ColumnServices) {
i.SetupColumn(p, c); i.SetupColumn(p,c);
} }
}; };
} }
@ -67,7 +68,7 @@ namespace Falcon.SugarApi.DatabaseDefinitions
/// 通过配置实现Redis缓冲必须单例实现. /// 通过配置实现Redis缓冲必须单例实现.
/// </summary> /// </summary>
public void AddRedisCache(string connectionString) { public void AddRedisCache(string connectionString) {
if (connectionString.IsNullOrEmpty()) { if(connectionString.IsNullOrEmpty()) {
AddRedisCache(); AddRedisCache();
return; return;
} }
@ -88,9 +89,9 @@ namespace Falcon.SugarApi.DatabaseDefinitions
/// </summary> /// </summary>
/// <param name="cache">缓存提供程序</param> /// <param name="cache">缓存提供程序</param>
/// <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);
} }
} }
} }