- 增加 FreeSql.Extensions.JsonMap FluentApi 扩展方法;#279

This commit is contained in:
28810 2020-04-19 00:09:24 +08:00
parent c0d29b3906
commit a0e18b1a68
6 changed files with 70 additions and 71 deletions

View File

@ -27,6 +27,8 @@ namespace base_entity
[JsonMap] [JsonMap]
public T Config { get; set; } public T Config { get; set; }
public T Config2 { get; set; }
} }
public class Products : BaseEntity<Products, int> public class Products : BaseEntity<Products, int>
@ -89,10 +91,15 @@ namespace base_entity
var items2 = fsql.Select<Products>().Limit(10).OrderByDescending(a => a.CreateTime).ToList(); var items2 = fsql.Select<Products>().Limit(10).OrderByDescending(a => a.CreateTime).ToList();
BaseEntity.Orm.UseJsonMap(); BaseEntity.Orm.UseJsonMap();
BaseEntity.Orm.UseJsonMap();
BaseEntity.Orm.CodeFirst.ConfigEntity<S_SysConfig<TestConfig>>(a =>
{
a.Property(b => b.Config2).JsonMap();
});
new S_SysConfig<TestConfig> { Name = "testkey11", Config = new TestConfig { clicks = 11, title = "testtitle11" } }.Save(); new S_SysConfig<TestConfig> { Name = "testkey11", Config = new TestConfig { clicks = 11, title = "testtitle11" }, Config2 = new TestConfig { clicks = 11, title = "testtitle11" } }.Save();
new S_SysConfig<TestConfig> { Name = "testkey22", Config = new TestConfig { clicks = 22, title = "testtitle22" } }.Save(); new S_SysConfig<TestConfig> { Name = "testkey22", Config = new TestConfig { clicks = 22, title = "testtitle22" }, Config2 = new TestConfig { clicks = 11, title = "testtitle11" } }.Save();
new S_SysConfig<TestConfig> { Name = "testkey33", Config = new TestConfig { clicks = 33, title = "testtitle33" } }.Save(); new S_SysConfig<TestConfig> { Name = "testkey33", Config = new TestConfig { clicks = 33, title = "testtitle33" }, Config2 = new TestConfig { clicks = 11, title = "testtitle11" } }.Save();
var testconfigs11 = S_SysConfig<TestConfig>.Select.ToList(); var testconfigs11 = S_SysConfig<TestConfig>.Select.ToList();
var repo = BaseEntity.Orm.Select<TestConfig>().Limit(10).ToList(); var repo = BaseEntity.Orm.Select<TestConfig>().Limit(10).ToList();

View File

@ -9,7 +9,7 @@
当实体类属性为【对象】时以JSON形式映射存储 当实体类属性为【对象】时以JSON形式映射存储
</summary> </summary>
</member> </member>
<member name="M:FreeSql.Extensions.JsonMapCore.UseJsonMap(IFreeSql)"> <member name="M:FreeSqlJsonMapCoreExtensions.UseJsonMap(IFreeSql)">
<summary> <summary>
当实体类属性为【对象】时,并且标记特性 [JsonMap] 时该属性将以JSON形式映射存储 当实体类属性为【对象】时,并且标记特性 [JsonMap] 时该属性将以JSON形式映射存储
</summary> </summary>

View File

@ -6,16 +6,22 @@ using System.Linq;
using System.Linq.Expressions; using System.Linq.Expressions;
using System.Reflection; using System.Reflection;
using System.Text; using System.Text;
using System.Threading;
namespace FreeSql.Extensions public static class FreeSqlJsonMapCoreExtensions
{ {
public static class JsonMapCore static int _isAoped = 0;
{
static bool _isAoped = false;
static object _isAopedLock = new object();
static ConcurrentDictionary<Type, bool> _dicTypes = new ConcurrentDictionary<Type, bool>(); static ConcurrentDictionary<Type, bool> _dicTypes = new ConcurrentDictionary<Type, bool>();
static MethodInfo MethodJsonConvertDeserializeObject = typeof(JsonConvert).GetMethod("DeserializeObject", new[] { typeof(string), typeof(Type) }); static MethodInfo MethodJsonConvertDeserializeObject = typeof(JsonConvert).GetMethod("DeserializeObject", new[] { typeof(string), typeof(Type) });
static MethodInfo MethodJsonConvertSerializeObject = typeof(JsonConvert).GetMethod("SerializeObject", new[] { typeof(object), typeof(JsonSerializerSettings) }); static MethodInfo MethodJsonConvertSerializeObject = typeof(JsonConvert).GetMethod("SerializeObject", new[] { typeof(object), typeof(JsonSerializerSettings) });
static ConcurrentDictionary<Type, ConcurrentDictionary<string, bool>> _dicJsonMapFluentApi = new ConcurrentDictionary<Type, ConcurrentDictionary<string, bool>>();
public static ColumnFluent JsonMap(this ColumnFluent col)
{
_dicJsonMapFluentApi.GetOrAdd(col._entityType, et => new ConcurrentDictionary<string, bool>())
.GetOrAdd(col._property.Name, pn => true);
return col;
}
/// <summary> /// <summary>
/// 当实体类属性为【对象】时,并且标记特性 [JsonMap] 时该属性将以JSON形式映射存储 /// 当实体类属性为【对象】时,并且标记特性 [JsonMap] 时该属性将以JSON形式映射存储
@ -23,26 +29,24 @@ namespace FreeSql.Extensions
/// <returns></returns> /// <returns></returns>
public static void UseJsonMap(this IFreeSql that) public static void UseJsonMap(this IFreeSql that)
{ {
UseJsonMap(that, Newtonsoft.Json.JsonConvert.DefaultSettings?.Invoke() ?? new JsonSerializerSettings()); UseJsonMap(that, JsonConvert.DefaultSettings?.Invoke() ?? new JsonSerializerSettings());
} }
public static void UseJsonMap(this IFreeSql that, JsonSerializerSettings settings) public static void UseJsonMap(this IFreeSql that, JsonSerializerSettings settings)
{ {
if (_isAoped == false) if (Interlocked.CompareExchange(ref _isAoped, 1, 0) == 0)
lock (_isAopedLock)
if (_isAoped == false)
{ {
_isAoped = true;
FreeSql.Internal.Utils.GetDataReaderValueBlockExpressionSwitchTypeFullName.Add((LabelTarget returnTarget, Expression valueExp, Type type) => FreeSql.Internal.Utils.GetDataReaderValueBlockExpressionSwitchTypeFullName.Add((LabelTarget returnTarget, Expression valueExp, Type type) =>
{ {
if (_dicTypes.ContainsKey(type)) return Expression.Return(returnTarget, Expression.TypeAs(Expression.Call(MethodJsonConvertDeserializeObject, Expression.Convert(valueExp, typeof(string)), Expression.Constant(type)), type)); if (_dicTypes.ContainsKey(type)) return Expression.Return(returnTarget, Expression.TypeAs(Expression.Call(MethodJsonConvertDeserializeObject, Expression.Convert(valueExp, typeof(string)), Expression.Constant(type)), type));
return null; return null;
}); });
}
that.Aop.ConfigEntityProperty += new EventHandler<Aop.ConfigEntityPropertyEventArgs>((s, e) => that.Aop.ConfigEntityProperty += new EventHandler<FreeSql.Aop.ConfigEntityPropertyEventArgs>((s, e) =>
{ {
if (e.Property.GetCustomAttributes(typeof(JsonMapAttribute), false).Any()) var isJsonMap = e.Property.GetCustomAttributes(typeof(JsonMapAttribute), false).Any() || _dicJsonMapFluentApi.TryGetValue(e.EntityType, out var tryjmfu) && tryjmfu.ContainsKey(e.Property.Name);
if (isJsonMap)
{ {
e.ModifyResult.MapType = typeof(string); e.ModifyResult.MapType = typeof(string);
if (_dicTypes.TryAdd(e.Property.PropertyType, true)) if (_dicTypes.TryAdd(e.Property.PropertyType, true))
@ -60,5 +64,3 @@ namespace FreeSql.Extensions
} }
} }
}
}

View File

@ -120,13 +120,6 @@
清空状态数据 清空状态数据
</summary> </summary>
</member> </member>
<member name="M:FreeSql.DbSet`1.RemoveAsync(System.Linq.Expressions.Expression{System.Func{`0,System.Boolean}})">
<summary>
根据 lambda 条件删除数据
</summary>
<param name="predicate"></param>
<returns></returns>
</member>
<member name="M:FreeSql.DbSet`1.Add(`0)"> <member name="M:FreeSql.DbSet`1.Add(`0)">
<summary> <summary>
添加 添加
@ -395,14 +388,5 @@
<param name="that"></param> <param name="that"></param>
<returns></returns> <returns></returns>
</member> </member>
<member name="M:Microsoft.Extensions.DependencyInjection.FreeSqlRepositoryDependencyInjection.AddFreeRepository(Microsoft.Extensions.DependencyInjection.IServiceCollection,System.Action{FreeSql.FluentDataFilter},System.Reflection.Assembly[])">
<summary>
批量注入 Repository可以参考代码自行调整
</summary>
<param name="services"></param>
<param name="globalDataFilter"></param>
<param name="assemblies"></param>
<returns></returns>
</member>
</members> </members>
</doc> </doc>

View File

@ -1,16 +1,22 @@
using System; using System;
using System.Collections.Generic;
using System.Reflection;
namespace FreeSql.DataAnnotations namespace FreeSql.DataAnnotations
{ {
public class ColumnFluent public class ColumnFluent
{ {
public ColumnFluent(ColumnAttribute column) public ColumnFluent(ColumnAttribute column, PropertyInfo property, Type entityType)
{ {
_column = column; _column = column;
_property = property;
_entityType = entityType;
} }
ColumnAttribute _column; public ColumnAttribute _column;
public PropertyInfo _property;
public Type _entityType;
/// <summary> /// <summary>
/// 数据库列名 /// 数据库列名
/// </summary> /// </summary>

View File

@ -47,9 +47,9 @@ namespace FreeSql.DataAnnotations
public ColumnFluent Property(string proto) public ColumnFluent Property(string proto)
{ {
if (_properties.ContainsKey(proto) == false) throw new KeyNotFoundException($"找不到属性名 {proto}"); if (_properties.TryGetValue(proto, out var tryProto) == false) throw new KeyNotFoundException($"找不到属性名 {proto}");
var col = _table._columns.GetOrAdd(proto, name => new ColumnAttribute { Name = proto }); var col = _table._columns.GetOrAdd(tryProto.Name, name => new ColumnAttribute { Name = proto });
return new ColumnFluent(col); return new ColumnFluent(col, tryProto, _entityType);
} }
/// <summary> /// <summary>
@ -131,7 +131,7 @@ namespace FreeSql.DataAnnotations
{ {
if (_properties.TryGetValue(proto, out var tryProto) == false) throw new KeyNotFoundException($"找不到属性名 {proto}"); if (_properties.TryGetValue(proto, out var tryProto) == false) throw new KeyNotFoundException($"找不到属性名 {proto}");
var col = _table._columns.GetOrAdd(tryProto.Name, name => new ColumnAttribute { Name = proto }); var col = _table._columns.GetOrAdd(tryProto.Name, name => new ColumnAttribute { Name = proto });
return new ColumnFluent(col); return new ColumnFluent(col, tryProto, typeof(T));
} }
/// <summary> /// <summary>