@@ -12,10 +12,58 @@ namespace Bing.Domain.Entities;
1212public static class EntityHelper
1313{
1414 /// <summary>
15- /// Guid 生成函数
15+ /// ID生成器字典
16+ /// </summary>
17+ private static readonly IDictionary < Type , Func < object > > _idGenerators = new Dictionary < Type , Func < object > >
18+ {
19+ { typeof ( Guid ) , ( ) => GuidGenerateFunc ( ) } ,
20+ { typeof ( string ) , ( ) => StringGenerateFunc ( ) } ,
21+ { typeof ( long ) , ( ) => LongGenerateFunc ( ) } ,
22+ { typeof ( int ) , ( ) => IntGenerateFunc ( ) }
23+ } ;
24+
25+ /// <summary>
26+ /// Guid 生成函数,允许外部自定义生成方式。
1627 /// </summary>
1728 public static Func < Guid > GuidGenerateFunc { get ; set ; } = Guid . NewGuid ;
1829
30+ /// <summary>
31+ /// String ID 生成函数(默认为 Guid 字符串)。
32+ /// </summary>
33+ public static Func < string > StringGenerateFunc { get ; set ; } = ( ) => GuidGenerateFunc ( ) . ToString ( ) ;
34+
35+ /// <summary>
36+ /// Long ID 生成函数(默认为雪花 ID)。
37+ /// </summary>
38+ public static Func < long > LongGenerateFunc { get ; set ; }
39+
40+ /// <summary>
41+ /// Int ID 生成函数(默认不支持)。
42+ /// </summary>
43+ public static Func < int > IntGenerateFunc { get ; set ; } = ( ) => throw new InvalidOperationException ( "不支持 Int 作为 ID,请使用 Guid, string 或 long。" ) ;
44+
45+ /// <summary>
46+ /// 生成唯一标识 ID,支持 Guid、string、long 类型。
47+ /// </summary>
48+ /// <typeparam name="TKey">ID 类型</typeparam>
49+ /// <returns>生成的 ID 值</returns>
50+ public static TKey CreateKey < TKey > ( )
51+ {
52+ if ( _idGenerators . TryGetValue ( typeof ( TKey ) , out var generator ) )
53+ return ( TKey ) generator ( ) ;
54+ throw new InvalidOperationException ( $ "不支持的 ID 类型: { typeof ( TKey ) } ,请使用 Guid, string, long。") ;
55+ }
56+
57+ /// <summary>
58+ /// 判断实体类型是否为多租户实体。
59+ /// </summary>
60+ public static Func < IEntity , IEntity , bool > IsMultiTenantEntity { get ; set ; } = ( _ , _ ) => false ;
61+
62+ /// <summary>
63+ /// 在不同租户下是否允许相同 ID 作为相等的规则(默认:不允许)。
64+ /// </summary>
65+ public static Func < IEntity , IEntity , bool > AllowSameIdAcrossTenants { get ; set ; } = ( _ , _ ) => false ;
66+
1967 /// <summary>
2068 /// 判断两个 <see cref="IEntity"/> 实例是否相等。
2169 /// </summary>
@@ -39,7 +87,9 @@ public static bool EntityEquals(IEntity entity1, IEntity entity2)
3987 if ( ! typeOfEntity1 . IsAssignableFrom ( typeOfEntity2 ) && ! typeOfEntity2 . IsAssignableFrom ( typeOfEntity1 ) )
4088 return false ;
4189
42- // 多租户委托检查
90+ // 多租户检查
91+ if ( IsMultiTenantEntity ( entity1 , entity2 ) )
92+ return AllowSameIdAcrossTenants ( entity1 , entity2 ) ;
4393
4494 // 瞬时对象不视为相等
4595 if ( HasDefaultKeys ( entity1 ) && HasDefaultKeys ( entity2 ) )
@@ -48,7 +98,7 @@ public static bool EntityEquals(IEntity entity1, IEntity entity2)
4898 // 如果键数量不匹配,则不相等
4999 var entity1Keys = entity1 . GetKeys ( ) ;
50100 var entity2Keys = entity2 . GetKeys ( ) ;
51- if ( entity1Keys . Length != entity2Keys . Length )
101+ if ( entity1Keys . Length != entity2Keys . Length )
52102 return false ;
53103
54104 // 逐个比较主键值
@@ -97,7 +147,7 @@ public static bool IsEntity(Type type)
97147 /// <remarks>
98148 /// 用于判断一个类型是否为值对象类型。默认实现为检查是否继承自<see cref="ValueObjectBase{T}"/>。
99149 /// </remarks>
100- public static Func < Type , bool > IsValueObjectPredicate = type => typeof ( ValueObjectBase < > ) . IsAssignableFrom ( type ) ;
150+ public static Func < Type , bool > IsValueObjectPredicate { get ; set ; } = type => typeof ( ValueObjectBase < > ) . IsAssignableFrom ( type ) ;
101151
102152 /// <summary>
103153 /// 是否值对象类型
0 commit comments