-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCacheManager.cs
357 lines (281 loc) · 14.5 KB
/
CacheManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
using DbSyncKit.DB.Attributes;
using DbSyncKit.DB.Enum;
using DbSyncKit.DB.Interface;
using System.Reflection;
namespace DbSyncKit.DB.Manager
{
/// <summary>
/// Provides a cache manager for storing and retrieving type properties.
/// </summary>
public static class CacheManager
{
#region Declerations
/// <summary>
/// Cache to store type properties.
/// </summary>
private static readonly Dictionary<Type, PropertyInfo[]> TypePropertiesCache = new Dictionary<Type, PropertyInfo[]>();
/// <summary>
/// Cache to store type properties in a collection, categorized by <see cref="CachePropertyType"/>.
/// </summary>
private static readonly Dictionary<Type, Dictionary<string,object>> TypePropertiesCacheCollection = new Dictionary<Type, Dictionary<string, object>>();
#endregion
#region Cache Methods
/// <summary>
/// Gets the properties of a specified type, using a cached version if available.
/// </summary>
/// <param name="type">The type for which to retrieve properties.</param>
/// <returns>An array of <see cref="PropertyInfo"/> objects representing the properties of the specified type.</returns>
public static PropertyInfo[] GetTypeProperties(Type type)
{
// Check if the type's properties are already in the cache
if (TypePropertiesCache.TryGetValue(type, out var properties))
{
return properties;
}
// Retrieve and cache the type's properties
properties = type.GetProperties();
TypePropertiesCache[type] = properties;
return properties;
}
/// <summary>
/// Retrieves a list of identity columns for a specified data contract type.
/// </summary>
/// <param name="type">The type for which to retrieve identity columns.</param>
/// <returns>A list containing the names of identity columns for the specified data contract type.</returns>
/// <remarks>
/// This method uses reflection to analyze the properties of the specified type and retrieves properties marked with a [Key] attribute, indicating identity columns.
/// </remarks>
public static List<string> GetIdentityColumns(Type type)
{
var typeCaches = GetOrCreateDictionary(type);
// Try to retrieve the identity columns from the cache
if (typeCaches.TryGetValue(nameof(CachePropertyType.Identity), out var identityColumns))
{
return (List<string>)identityColumns;
}
// If not found in the cache, retrieve and cache the identity columns
identityColumns = CacheManager.GetTypeProperties(type)
.Where(prop =>
Attribute.IsDefined(prop, typeof(KeyPropertyAttribute)) &&
((KeyPropertyAttribute)Attribute.GetCustomAttribute(prop, typeof(KeyPropertyAttribute))!).IsPrimaryKey
).Select(prop => prop.Name).ToList();
typeCaches.Add(nameof(CachePropertyType.Identity), identityColumns);
return (List<string>)identityColumns;
}
/// <summary>
/// Gets the names of properties marked as key columns for a specified type.
/// </summary>
/// <param name="type">The type for which to retrieve key columns.</param>
/// <returns>A list of key column names.</returns>
public static List<string> GetKeyColumns(Type type)
{
var typeCaches = GetOrCreateDictionary(type);
if (typeCaches.TryGetValue(nameof(CachePropertyType.Key), out var keyColumns))
{
return (List<string>)keyColumns;
}
keyColumns = GetTypeProperties(type)
.Where(prop =>
Attribute.IsDefined(prop, typeof(KeyPropertyAttribute)) &&
((KeyPropertyAttribute)Attribute.GetCustomAttribute(prop, typeof(KeyPropertyAttribute))!).KeyProperty
).Select(prop => prop.Name).ToList();
typeCaches.Add(nameof(CachePropertyType.Key), keyColumns);
return (List<string>)keyColumns;
}
/// <summary>
/// Gets the names of properties marked as excluded properties for a specified type.
/// </summary>
/// <param name="type">The type for which to retrieve excluded columns.</param>
/// <returns>A list of excluded column names.</returns>
public static List<string> GetExcludedColumns(Type type)
{
var typeCaches = GetOrCreateDictionary(type);
if (typeCaches.TryGetValue(nameof(CachePropertyType.Excluded), out var excludedProperties))
{
return (List<string>)excludedProperties;
}
excludedProperties = GetExcludedProperties(type).Select(prop => prop.Name).ToList();
typeCaches.Add(nameof(CachePropertyType.Excluded), excludedProperties);
return (List<string>)excludedProperties;
}
/// <summary>
/// Gets the names of all properties for a specified type.
/// </summary>
/// <param name="type">The type for which to retrieve all properties.</param>
/// <returns>A list of all property names.</returns>
public static List<string> GetAllColumns(Type type)
{
var typeCaches = GetOrCreateDictionary(type);
if (typeCaches.TryGetValue(nameof(CachePropertyType.All), out var allColumns))
{
return (List<string>)allColumns;
}
allColumns = GetTypeProperties(type).Select(prop => prop.Name).ToList();
typeCaches.Add(nameof(CachePropertyType.All), allColumns);
return (List<string>)allColumns;
}
/// <summary>
/// Gets the name of the database table associated with the specified type.
/// </summary>
/// <param name="type">The type for which to retrieve the table name.</param>
/// <returns>The name of the database table associated with the specified type.</returns>
public static string GetTableName(Type type)
{
var typeCaches = GetOrCreateDictionary(type);
if (typeCaches.TryGetValue(nameof(CachePropertyType.TableName), out var TableName))
{
return (string)TableName;
}
TableNameAttribute? tableNameAttribute = (TableNameAttribute?)Attribute.GetCustomAttribute(type, typeof(TableNameAttribute));
if (tableNameAttribute != null)
TableName = tableNameAttribute.TableName;
else
TableName = type.Name;
typeCaches.Add(nameof(CachePropertyType.TableName), TableName);
return (string)TableName;
}
/// <summary>
/// Gets the schema of the database table associated with the specified type.
/// </summary>
/// <param name="type">The type for which to retrieve the table schema.</param>
/// <returns>The schema of the database table associated with the specified type.</returns>
public static string? GetTableSchema(Type type)
{
var typeCaches = GetOrCreateDictionary(type);
if (typeCaches.TryGetValue(nameof(CachePropertyType.TableSchema), out var TableSchema))
{
return (string)TableSchema;
}
TableSchemaAttribute? tableSchemaAttribute = (TableSchemaAttribute?)Attribute.GetCustomAttribute(type, typeof(TableSchemaAttribute));
if (tableSchemaAttribute != null)
TableSchema = tableSchemaAttribute.SchemaName;
else
TableSchema = null;
typeCaches.Add(nameof(CachePropertyType.TableSchema), TableSchema!);
return (string?)TableSchema;
}
/// <summary>
/// Gets a value indicating whether the INSERT query for the specified type should include the identity column.
/// </summary>
/// <param name="type">The type for which to determine whether to include the identity column in the INSERT query.</param>
/// <returns><c>true</c> if the INSERT query should include the identity column; otherwise, <c>false</c>.</returns>
public static bool GetInsertWithID(Type type)
{
var typeCaches = GetOrCreateDictionary(type);
if (typeCaches.TryGetValue(nameof(CachePropertyType.GenerateWithID), out var _GenerateQueryWithID))
{
return (bool)_GenerateQueryWithID;
}
GenerateInsertWithIDAttribute? tableSchemaAttribute = (GenerateInsertWithIDAttribute?)Attribute.GetCustomAttribute(type, typeof(GenerateInsertWithIDAttribute));
if (tableSchemaAttribute != null)
_GenerateQueryWithID = tableSchemaAttribute.GenerateWithID;
else
_GenerateQueryWithID = false;
typeCaches.Add(nameof(CachePropertyType.GenerateWithID), _GenerateQueryWithID);
return (bool)_GenerateQueryWithID;
}
/// <summary>
/// Gets a value indicating whether to include the identity insert in the INSERT query for the specified type.
/// </summary>
/// <param name="type">The type for which to determine whether to include the identity insert in the INSERT query.</param>
/// <returns><c>true</c> if identity insert should be included; otherwise, <c>false</c>.</returns>
public static bool GetIncludeIdentityInsert(Type type)
{
var typeCaches = GetOrCreateDictionary(type);
if (typeCaches.TryGetValue(nameof(CachePropertyType.IncludeIdentityInsert), out var _IncludeIdentityInsertInQuery))
{
return (bool)_IncludeIdentityInsertInQuery;
}
GenerateInsertWithIDAttribute? attribute = (GenerateInsertWithIDAttribute?)Attribute.GetCustomAttribute(type, typeof(GenerateInsertWithIDAttribute));
if (attribute != null)
_IncludeIdentityInsertInQuery = attribute.IncludeIdentityInsert;
else
_IncludeIdentityInsertInQuery = false;
typeCaches.Add(nameof(CachePropertyType.IncludeIdentityInsert), _IncludeIdentityInsertInQuery);
return (bool)_IncludeIdentityInsertInQuery;
}
/// <summary>
/// Gets the properties that are comparable (not key or excluded) for a specified type.
/// </summary>
/// <param name="type">The type for which to retrieve comparable properties.</param>
/// <returns>An array of <see cref="PropertyInfo"/> objects representing comparable properties of the specified type.</returns>
public static PropertyInfo[] GetComparableProperties(Type type)
{
var typeCaches = GetOrCreateDictionary(type);
if (typeCaches.TryGetValue(nameof(CachePropertyType.ComparableProperties), out var _properties))
{
return (PropertyInfo[])_properties;
}
var keyProps = GetKeyColumns(type);
var excludeProps = GetExcludedColumns(type);
_properties = GetTypeProperties(type).Where(prop => !keyProps.Contains(prop.Name) && !excludeProps.Contains(prop.Name)).ToArray();
return (PropertyInfo[])_properties;
}
/// <summary>
/// Gets the properties that are unique for a specified type.
/// </summary>
/// <param name="type">The type for which to retrieve unique properties.</param>
/// <returns>An array of <see cref="PropertyInfo"/> objects representing unique properties of the specified type.</returns>
public static PropertyInfo[] GetKeyProperties(Type type)
{
var typeCaches = GetOrCreateDictionary(type);
if (typeCaches.TryGetValue(nameof(CachePropertyType.KeyProperties), out var _properties))
{
return (PropertyInfo[])_properties;
}
var keyProps = GetKeyColumns(type);
var excludeProps = GetExcludedColumns(type);
_properties = GetTypeProperties(type)
.Where(prop =>
Attribute.IsDefined(prop, typeof(KeyPropertyAttribute)) &&
((KeyPropertyAttribute)Attribute.GetCustomAttribute(prop, typeof(KeyPropertyAttribute))!).KeyProperty
).ToArray();
return (PropertyInfo[])_properties;
}
/// <summary>
/// Gets the excluded properties for a specified type.
/// </summary>
/// <param name="type">The type for which to retrieve the excluded properties.</param>
/// <returns>An array of <see cref="PropertyInfo"/> representing the excluded properties for the specified type.</returns>
public static PropertyInfo[] GetExcludedProperties(Type type)
{
var typeCaches = GetOrCreateDictionary(type);
if (typeCaches.TryGetValue(nameof(CachePropertyType.ExcludedProperties), out var _properties))
{
return (PropertyInfo[])_properties;
}
var keyProps = GetKeyColumns(type);
_properties = GetTypeProperties(type)
.Where(prop =>
Attribute.IsDefined(prop, typeof(ExcludedPropertyAttribute)) &&
((ExcludedPropertyAttribute)Attribute.GetCustomAttribute(prop, typeof(ExcludedPropertyAttribute))!).Excluded
).ToArray();
return (PropertyInfo[])_properties;
}
/// <summary>
/// Disposes of cached data associated with the specified type.
/// </summary>
/// <param name="type">The type for which to dispose of cached data.</param>
public static void DisposeType(Type type)
{
TypePropertiesCacheCollection.Remove(type);
TypePropertiesCache.Remove(type);
}
#endregion
#region Private Methods
/// <summary>
/// Gets or creates a dictionary for caching type-specific properties.
/// </summary>
/// <param name="type">The type for which to retrieve or create a dictionary.</param>
/// <returns>A dictionary for caching type-specific properties.</returns>
private static Dictionary<string, object> GetOrCreateDictionary(Type type)
{
if (!TypePropertiesCacheCollection.TryGetValue(type, out var typeCaches))
{
typeCaches = TypePropertiesCacheCollection[type] = new Dictionary<string, object>();
}
return typeCaches;
}
#endregion
}
}