1- using System . Reflection ;
1+ using DbSyncKit . DB . Attributes ;
2+ using DbSyncKit . DB . Enum ;
3+ using DbSyncKit . DB . Interface ;
4+ using System . Reflection ;
25
36namespace DbSyncKit . DB . Manager
47{
@@ -7,11 +10,21 @@ namespace DbSyncKit.DB.Manager
710 /// </summary>
811 public static class TypePropertyCacheManager
912 {
13+ #region Declerations
1014 /// <summary>
1115 /// Cache to store type properties.
1216 /// </summary>
1317 private static readonly Dictionary < Type , PropertyInfo [ ] > TypePropertiesCache = new Dictionary < Type , PropertyInfo [ ] > ( ) ;
1418
19+ /// <summary>
20+ /// Cache to store type properties in list.
21+ /// </summary>
22+ private static readonly Dictionary < Type , Dictionary < string , List < string > > > TypePropertiesCacheCollection = new Dictionary < Type , Dictionary < string , List < string > > > ( ) ;
23+
24+ #endregion
25+
26+ #region Cache Methods
27+
1528 /// <summary>
1629 /// Gets the properties of a specified type, using a cached version if available.
1730 /// </summary>
@@ -31,5 +44,117 @@ public static PropertyInfo[] GetTypeProperties(Type type)
3144
3245 return properties ;
3346 }
47+
48+ /// <summary>
49+ /// Retrieves a list of identity columns for a specified data contract type.
50+ /// </summary>
51+ /// <returns>A list containing the names of identity columns for the specified data contract type.</returns>
52+ /// <remarks>
53+ /// This method uses reflection to analyze the properties of the specified type and retrieves properties marked with a [Key] attribute, indicating identity columns.
54+ /// </remarks>
55+ public static List < string > GetIdentityColumns ( Type type )
56+ {
57+ var typeCaches = GetOrCreateDictionary ( type ) ;
58+ // Try to retrieve the identity columns from the cache
59+ if ( typeCaches . TryGetValue ( nameof ( CachePropertyType . Identity ) , out var identityColumns ) )
60+ {
61+ return identityColumns ;
62+ }
63+
64+ // If not found in the cache, retrieve and cache the identity columns
65+ identityColumns = TypePropertyCacheManager . GetTypeProperties ( type )
66+ . Where ( prop =>
67+ Attribute . IsDefined ( prop , typeof ( KeyPropertyAttribute ) ) &&
68+ ( ( KeyPropertyAttribute ) Attribute . GetCustomAttribute ( prop , typeof ( KeyPropertyAttribute ) ) ) . IsPrimaryKey
69+ ) . Select ( prop => prop . Name ) . ToList ( ) ;
70+
71+
72+ typeCaches . Add ( nameof ( CachePropertyType . Identity ) , identityColumns ) ;
73+
74+ return identityColumns ;
75+ }
76+
77+ /// <summary>
78+ /// Gets the names of properties marked as key columns for a specified type.
79+ /// </summary>
80+ /// <returns>A list of key column names.</returns>
81+ public static List < string > GetKeyColumns ( Type type )
82+ {
83+ var typeCaches = GetOrCreateDictionary ( type ) ;
84+
85+ if ( typeCaches . TryGetValue ( nameof ( CachePropertyType . Key ) , out var keyColumns ) )
86+ {
87+ return keyColumns ;
88+ }
89+
90+ keyColumns = GetTypeProperties ( type )
91+ . Where ( prop =>
92+ Attribute . IsDefined ( prop , typeof ( KeyPropertyAttribute ) ) &&
93+ ( ( KeyPropertyAttribute ) Attribute . GetCustomAttribute ( prop , typeof ( KeyPropertyAttribute ) ) ) . KeyProperty
94+ ) . Select ( prop => prop . Name ) . ToList ( ) ;
95+
96+ typeCaches . Add ( nameof ( CachePropertyType . Key ) , keyColumns ) ;
97+
98+ return keyColumns ;
99+ }
100+
101+ /// <summary>
102+ /// Gets the names of properties marked as excluded properties for a specified type.
103+ /// </summary>
104+ /// <returns>A list of excluded property names.</returns>
105+ public static List < string > GetExcludedProperties ( Type type )
106+ {
107+ var typeCaches = GetOrCreateDictionary ( type ) ;
108+
109+ if ( typeCaches . TryGetValue ( nameof ( CachePropertyType . Excluded ) , out var excludedProperties ) )
110+ {
111+ return excludedProperties ;
112+ }
113+
114+ excludedProperties = GetTypeProperties ( type )
115+ . Where ( prop =>
116+ Attribute . IsDefined ( prop , typeof ( ExcludedPropertyAttribute ) ) &&
117+ ( ( ExcludedPropertyAttribute ) Attribute . GetCustomAttribute ( prop , typeof ( ExcludedPropertyAttribute ) ) ) . Excluded
118+ ) . Select ( prop => prop . Name ) . ToList ( ) ;
119+
120+ typeCaches . Add ( nameof ( CachePropertyType . Excluded ) , excludedProperties ) ;
121+
122+ return excludedProperties ;
123+ }
124+
125+ /// <summary>
126+ /// Gets the names of all properties for a specified type.
127+ /// </summary>
128+ /// <returns>A list of all property names.</returns>
129+ public static List < string > GetAllColumns ( Type type )
130+ {
131+ var typeCaches = GetOrCreateDictionary ( type ) ;
132+
133+ if ( typeCaches . TryGetValue ( nameof ( CachePropertyType . All ) , out var allColumns ) )
134+ {
135+ return allColumns ;
136+ }
137+
138+ allColumns = GetTypeProperties ( type ) . Select ( prop => prop . Name ) . ToList ( ) ;
139+
140+ typeCaches . Add ( nameof ( CachePropertyType . All ) , allColumns ) ;
141+
142+ return allColumns ;
143+ }
144+ #endregion
145+
146+ #region Private Methods
147+ private static Dictionary < string , List < string > > GetOrCreateDictionary ( Type type )
148+ {
149+ if ( ! TypePropertiesCacheCollection . TryGetValue ( type , out var typeCaches ) )
150+ {
151+ typeCaches = TypePropertiesCacheCollection [ type ] = new Dictionary < string , List < string > > ( ) ;
152+ }
153+
154+ return typeCaches ;
155+ }
156+
157+ #endregion
158+
34159 }
35160}
0 commit comments