mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-08-02 09:55:57 +08:00
initial commit
This commit is contained in:
@ -0,0 +1,11 @@
|
||||
using System;
|
||||
|
||||
// ReSharper disable once CheckNamespace
|
||||
namespace FreeSql.DataAnnotations
|
||||
{
|
||||
/// <summary>
|
||||
/// When the entity class property is <see cref="object"/>, map storage in JSON format. <br />
|
||||
/// 当实体类属性为【对象】时,以 JSON 形式映射存储
|
||||
/// </summary>
|
||||
public class JsonMapAttribute : Attribute { }
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFrameworks>netstandard2.0;net45;net40</TargetFrameworks>
|
||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||
<Authors>FreeSql;ncc;YeXiangQin</Authors>
|
||||
<Description>FreeSql 扩展包,可实现实体类属性为对象时,以JSON形式映射存储.</Description>
|
||||
<PackageProjectUrl>https://github.com/2881099/FreeSql</PackageProjectUrl>
|
||||
<RepositoryUrl>https://github.com/2881099/FreeSql</RepositoryUrl>
|
||||
<RepositoryType>git</RepositoryType>
|
||||
<PackageLicenseExpression>MIT</PackageLicenseExpression>
|
||||
<PackageTags>FreeSql;ORM</PackageTags>
|
||||
<PackageId>$(AssemblyName)</PackageId>
|
||||
<PackageIcon>logo.png</PackageIcon>
|
||||
<Title>$(AssemblyName)</Title>
|
||||
<IsPackable>true</IsPackable>
|
||||
<GenerateAssemblyInfo>true</GenerateAssemblyInfo>
|
||||
<SignAssembly>true</SignAssembly>
|
||||
<AssemblyOriginatorKeyFile>key.snk</AssemblyOriginatorKeyFile>
|
||||
<DelaySign>false</DelaySign>
|
||||
<Version>3.2.833</Version>
|
||||
<PackageReadmeFile>readme.md</PackageReadmeFile>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Include="../../readme.md" Pack="true" PackagePath="\"/>
|
||||
<None Include="../../logo.png" Pack="true" PackagePath="\" />
|
||||
</ItemGroup>
|
||||
|
||||
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|netstandard2.0|AnyCPU'">
|
||||
<DocumentationFile>FreeSql.Extensions.JsonMap.xml</DocumentationFile>
|
||||
<WarningLevel>3</WarningLevel>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\FreeSql\FreeSql.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
@ -0,0 +1,21 @@
|
||||
<?xml version="1.0"?>
|
||||
<doc>
|
||||
<assembly>
|
||||
<name>FreeSql.Extensions.JsonMap</name>
|
||||
</assembly>
|
||||
<members>
|
||||
<member name="T:FreeSql.DataAnnotations.JsonMapAttribute">
|
||||
<summary>
|
||||
When the entity class property is <see cref="T:System.Object"/>, map storage in JSON format. <br />
|
||||
当实体类属性为【对象】时,以 JSON 形式映射存储
|
||||
</summary>
|
||||
</member>
|
||||
<member name="M:FreeSqlJsonMapCoreExtensions.UseJsonMap(IFreeSql)">
|
||||
<summary>
|
||||
When the entity class property is <see cref="T:System.Object"/> and the attribute is marked as <see cref="T:FreeSql.DataAnnotations.JsonMapAttribute"/>, map storage in JSON format. <br />
|
||||
当实体类属性为【对象】时,并且标记特性 [JsonMap] 时,该属性将以JSON形式映射存储
|
||||
</summary>
|
||||
<returns></returns>
|
||||
</member>
|
||||
</members>
|
||||
</doc>
|
79
Extensions/FreeSql.Extensions.JsonMap/JsonMapCore.cs
Normal file
79
Extensions/FreeSql.Extensions.JsonMap/JsonMapCore.cs
Normal file
@ -0,0 +1,79 @@
|
||||
using FreeSql.DataAnnotations;
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Reflection;
|
||||
using System.Threading;
|
||||
|
||||
public static class FreeSqlJsonMapCoreExtensions
|
||||
{
|
||||
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>>();
|
||||
static object _concurrentObj = new object();
|
||||
|
||||
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>
|
||||
/// When the entity class property is <see cref="object"/> and the attribute is marked as <see cref="JsonMapAttribute"/>, map storage in JSON format. <br />
|
||||
/// 当实体类属性为【对象】时,并且标记特性 [JsonMap] 时,该属性将以JSON形式映射存储
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static void UseJsonMap(this IFreeSql that)
|
||||
{
|
||||
UseJsonMap(that, JsonConvert.DefaultSettings?.Invoke() ?? new JsonSerializerSettings());
|
||||
}
|
||||
|
||||
public static void UseJsonMap(this IFreeSql that, JsonSerializerSettings settings)
|
||||
{
|
||||
if (Interlocked.CompareExchange(ref _isAoped, 1, 0) == 0)
|
||||
{
|
||||
FreeSql.Internal.Utils.GetDataReaderValueBlockExpressionSwitchTypeFullName.Add((LabelTarget returnTarget, Expression valueExp, Type type) =>
|
||||
{
|
||||
if (_dicTypes.ContainsKey(type)) return Expression.IfThenElse(
|
||||
Expression.TypeIs(valueExp, type),
|
||||
Expression.Return(returnTarget, valueExp),
|
||||
Expression.Return(returnTarget, Expression.TypeAs(Expression.Call(MethodJsonConvertDeserializeObject, Expression.Convert(valueExp, typeof(string)), Expression.Constant(type)), type))
|
||||
);
|
||||
return null;
|
||||
});
|
||||
}
|
||||
|
||||
that.Aop.ConfigEntityProperty += (s, e) =>
|
||||
{
|
||||
var isJsonMap = e.Property.GetCustomAttributes(typeof(JsonMapAttribute), false).Any() || _dicJsonMapFluentApi.TryGetValue(e.EntityType, out var tryjmfu) && tryjmfu.ContainsKey(e.Property.Name);
|
||||
if (isJsonMap)
|
||||
{
|
||||
if (_dicTypes.ContainsKey(e.Property.PropertyType) == false &&
|
||||
FreeSql.Internal.Utils.dicExecuteArrayRowReadClassOrTuple.ContainsKey(e.Property.PropertyType))
|
||||
return; //基础类型使用 JsonMap 无效
|
||||
|
||||
e.ModifyResult.MapType = typeof(string);
|
||||
e.ModifyResult.StringLength = -2;
|
||||
if (_dicTypes.TryAdd(e.Property.PropertyType, true))
|
||||
{
|
||||
lock (_concurrentObj)
|
||||
{
|
||||
FreeSql.Internal.Utils.dicExecuteArrayRowReadClassOrTuple[e.Property.PropertyType] = true;
|
||||
FreeSql.Internal.Utils.GetDataReaderValueBlockExpressionObjectToStringIfThenElse.Add((LabelTarget returnTarget, Expression valueExp, Expression elseExp, Type type) =>
|
||||
{
|
||||
return Expression.IfThenElse(
|
||||
Expression.TypeIs(valueExp, e.Property.PropertyType),
|
||||
Expression.Return(returnTarget, Expression.Call(MethodJsonConvertSerializeObject, Expression.Convert(valueExp, typeof(object)), Expression.Constant(settings)), typeof(object)),
|
||||
elseExp);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
25
Extensions/FreeSql.Extensions.JsonMap/README.MD
Normal file
25
Extensions/FreeSql.Extensions.JsonMap/README.MD
Normal file
@ -0,0 +1,25 @@
|
||||
[中文](README.zh-CN.MD) | **English**
|
||||
|
||||
FreeSql extension package, map *ValueObject* to `typeof(string)`, install the extension package:
|
||||
|
||||
> dotnet add package FreeSql.Extensions.JsonMap
|
||||
|
||||
```csharp
|
||||
fsql.UseJsonMap(); //Turn on function
|
||||
|
||||
class TestConfig
|
||||
{
|
||||
public int clicks { get; set; }
|
||||
public string title { get; set; }
|
||||
}
|
||||
|
||||
[Table(Name = "sysconfig")]
|
||||
public class S_SysConfig<T>
|
||||
{
|
||||
[Column(IsPrimary = true)]
|
||||
public string Name { get; set; }
|
||||
|
||||
[JsonMap]
|
||||
public T Config { get; set; }
|
||||
}
|
||||
```
|
25
Extensions/FreeSql.Extensions.JsonMap/README.zh-CN.MD
Normal file
25
Extensions/FreeSql.Extensions.JsonMap/README.zh-CN.MD
Normal file
@ -0,0 +1,25 @@
|
||||
**中文** | [English](README.MD)
|
||||
|
||||
FreeSql 扩展包,将值对象映射成 `typeof(string)`,安装扩展包:
|
||||
|
||||
> dotnet add package FreeSql.Extensions.JsonMap
|
||||
|
||||
```csharp
|
||||
fsql.UseJsonMap(); //开启功能
|
||||
|
||||
class TestConfig
|
||||
{
|
||||
public int clicks { get; set; }
|
||||
public string title { get; set; }
|
||||
}
|
||||
|
||||
[Table(Name = "sysconfig")]
|
||||
public class S_SysConfig<T>
|
||||
{
|
||||
[Column(IsPrimary = true)]
|
||||
public string Name { get; set; }
|
||||
|
||||
[JsonMap]
|
||||
public T Config { get; set; }
|
||||
}
|
||||
```
|
BIN
Extensions/FreeSql.Extensions.JsonMap/key.snk
Normal file
BIN
Extensions/FreeSql.Extensions.JsonMap/key.snk
Normal file
Binary file not shown.
Reference in New Issue
Block a user