增加动态创建实体API

This commit is contained in:
d4ilys 2023-04-24 14:28:45 +08:00
parent 91da92b11f
commit 43a8e8bee9
3 changed files with 73 additions and 18 deletions

View File

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Text;
using FreeSql.DataAnnotations;
using FreeSql.Internal;
namespace FreeSql.Extensions
@ -14,9 +15,9 @@ namespace FreeSql.Extensions
/// 动态创建Class Type
/// </summary>
/// <returns></returns>
public static DynamicCompileBuilder DynamicBuilder(this ICodeFirst codeFirst)
public static DynamicCompileBuilder DynamicEntity(this ICodeFirst codeFirst, string className, TableAttribute tableAttribute)
{
return new DynamicCompileBuilder();
return new DynamicCompileBuilder().SetClass(className, tableAttribute);
}
/// <summary>
@ -25,7 +26,7 @@ namespace FreeSql.Extensions
/// <param name="type"></param>
/// <param name="porpertys"></param>
/// <returns></returns>
public static object CreateObjectByType(this ICodeFirst codeFirst, Type type,
public static object CreateDynamicEntityInstance(this Type type,
Dictionary<string, object> porpertys)
{
return DynamicCompileBuilder.CreateObjectByType(type, porpertys);

View File

@ -1073,6 +1073,20 @@
</summary>
<returns></returns>
</member>
<member name="M:FreeSql.Extensions.CodeFirstExtensions.DynamicEntity(FreeSql.ICodeFirst,System.String,FreeSql.DataAnnotations.TableAttribute)">
<summary>
动态创建Class Type
</summary>
<returns></returns>
</member>
<member name="M:FreeSql.Extensions.CodeFirstExtensions.CreateObjectByType(System.Type,System.Collections.Generic.Dictionary{System.String,System.Object})">
<summary>
根据动态构建的Class生成实例并进行属性赋值
</summary>
<param name="type"></param>
<param name="porpertys"></param>
<returns></returns>
</member>
<member name="M:FreeSql.Extensions.EntityUtil.EntityUtilExtensions.GetEntityKeyString(IFreeSql,System.Type,System.Object,System.Boolean,System.String)">
<summary>
获取实体的主键值,以 "*|_,[,_|*" 分割,当任意一个主键属性无值时,返回 null
@ -4248,6 +4262,43 @@
<param name="type"></param>
<returns>Dictkey=属性名value=注释</returns>
</member>
<member name="M:FreeSql.Internal.DynamicCompileBuilder.SetClass(System.String,FreeSql.DataAnnotations.TableAttribute)">
<summary>
配置Class
</summary>
<param name="className">类名</param>
<param name="tableAttribute">类标记的特性[Table(Name = "xxx")]</param>
<returns></returns>
</member>
<member name="M:FreeSql.Internal.DynamicCompileBuilder.Property(System.String,System.Type,System.Attribute[])">
<summary>
配置属性
</summary>
<param name="propertyName">属性名称</param>
<param name="propertyType">属性类型</param>
<param name="attributes">属性标记的特性[Column(IsPrimary = true)]</param>
<returns></returns>
</member>
<member name="M:FreeSql.Internal.DynamicCompileBuilder.Build">
<summary>
Emit动态创建出Class - Type
</summary>
<returns></returns>
</member>
<member name="M:FreeSql.Internal.DynamicCompileBuilder.FirstCharToLower(System.String)">
<summary>
首字母小写
</summary>
<param name="input"></param>
<returns></returns>
</member>
<member name="M:FreeSql.Internal.DynamicCompileBuilder.FirstCharToUpper(System.String)">
<summary>
首字母大写
</summary>
<param name="input"></param>
<returns></returns>
</member>
<member name="P:FreeSql.Internal.DbUpdateVersionException.Table">
<summary>
更新实体的元数据

View File

@ -37,15 +37,15 @@ namespace FreeSql.Internal
/// </summary>
/// <param name="propertyName">属性名称</param>
/// <param name="propertyType">属性类型</param>
/// <param name="columnAttribute">属性标记的特性[Column(IsPrimary = true)]</param>
/// <param name="attributes">属性标记的特性[Column(IsPrimary = true)]</param>
/// <returns></returns>
public DynamicCompileBuilder SetProperty(string propertyName, Type propertyType, ColumnAttribute columnAttribute)
public DynamicCompileBuilder Property(string propertyName, Type propertyType, params Attribute [] attributes)
{
_properties.Add(new DynamicPropertyInfo()
{
PropertyName = propertyName,
PropertyType = propertyType,
ColumnAttribute = columnAttribute
Attributes = attributes
});
return this;
}
@ -103,24 +103,27 @@ namespace FreeSql.Internal
propertyBuilder.SetGetMethod(methodGet);
propertyBuilder.SetSetMethod(methodSet);
//设置特性
SetColumnAttribute(ref propertyBuilder, pinfo?.ColumnAttribute);
foreach (var pinfoAttribute in pinfo.Attributes)
{
//设置特性
SetPropertyAttribute(ref propertyBuilder, pinfoAttribute);
}
}
}
private void SetColumnAttribute(ref PropertyBuilder propertyBuilder, ColumnAttribute columnAttribute = null)
private void SetPropertyAttribute<T>(ref PropertyBuilder propertyBuilder, T tAttribute)
{
if (columnAttribute == null)
if (tAttribute == null)
return;
var propertyValues = new ArrayList();
foreach (var propertyInfo in columnAttribute.GetType().GetProperties().Where(p => p.CanWrite == true))
foreach (var propertyInfo in tAttribute.GetType().GetProperties().Where(p => p.CanWrite == true))
{
propertyValues.Add(propertyInfo.GetValue(columnAttribute));
propertyValues.Add(propertyInfo.GetValue(tAttribute));
}
var propertyInfos = typeof(ColumnAttribute).GetProperties().Where(p => p.CanWrite == true).ToArray();
var constructor = typeof(ColumnAttribute).GetConstructor(new Type[] { });
var propertyInfos = tAttribute.GetType().GetProperties().Where(p => p.CanWrite == true).ToArray();
var constructor = tAttribute.GetType().GetConstructor(new Type[] { });
var customAttributeBuilder =
new CustomAttributeBuilder(constructor, new object[0], propertyInfos, propertyValues.ToArray());
propertyBuilder.SetCustomAttribute(customAttributeBuilder);
@ -226,8 +229,8 @@ namespace FreeSql.Internal
internal class DynamicPropertyInfo
{
public string PropertyName { get; set; } = string.Empty;
public Type PropertyType { get; set; } = null;
public ColumnAttribute ColumnAttribute { get; set; } = null;
public Type PropertyType { get; set; }
public Attribute [] Attributes { get; set; }
}
}