- 增加 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]
public T Config { get; set; }
public T Config2 { get; set; }
}
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();
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 = "testkey22", Config = new TestConfig { clicks = 22, title = "testtitle22" } }.Save();
new S_SysConfig<TestConfig> { Name = "testkey33", Config = new TestConfig { clicks = 33, title = "testtitle33" } }.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" }, Config2 = new TestConfig { clicks = 11, title = "testtitle11" } }.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 repo = BaseEntity.Orm.Select<TestConfig>().Limit(10).ToList();

View File

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

View File

@ -6,16 +6,22 @@ using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Threading;
namespace FreeSql.Extensions
public static class FreeSqlJsonMapCoreExtensions
{
public static class JsonMapCore
{
static bool _isAoped = false;
static object _isAopedLock = new object();
static int _isAoped = 0;
static ConcurrentDictionary<Type, bool> _dicTypes = new ConcurrentDictionary<Type, bool>();
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 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>
/// 当实体类属性为【对象】时,并且标记特性 [JsonMap] 时该属性将以JSON形式映射存储
@ -23,26 +29,24 @@ namespace FreeSql.Extensions
/// <returns></returns>
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)
{
if (_isAoped == false)
lock (_isAopedLock)
if (_isAoped == false)
if (Interlocked.CompareExchange(ref _isAoped, 1, 0) == 0)
{
_isAoped = true;
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));
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);
if (_dicTypes.TryAdd(e.Property.PropertyType, true))
@ -58,7 +62,5 @@ namespace FreeSql.Extensions
}
});
}
}
}
}

View File

@ -120,13 +120,6 @@
清空状态数据
</summary>
</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)">
<summary>
添加
@ -395,14 +388,5 @@
<param name="that"></param>
<returns></returns>
</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>
</doc>

View File

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

View File

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