Skip to content

Commit b0ac912

Browse files
committed
Optimized Performance by caching few methods
1 parent 9146b40 commit b0ac912

3 files changed

Lines changed: 146 additions & 11 deletions

File tree

Enum/CachePropertyType.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace DbSyncKit.DB.Enum
8+
{
9+
public enum CachePropertyType
10+
{
11+
All,
12+
Excluded,
13+
Key,
14+
Identity
15+
}
16+
}

Helper/QueryHelper.cs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
using DbSyncKit.DB.Attributes;
22
using DbSyncKit.DB.Interface;
33
using DbSyncKit.DB.Manager;
4-
5-
using System.ComponentModel.DataAnnotations;
64
using System.Text;
75
using System.Text.RegularExpressions;
86

@@ -96,8 +94,7 @@ public bool GetIncludeIdentityInsert<T>() where T : IDataContractComparer
9694
/// <seealso cref="IDataContractComparer"/>
9795
public List<string> GetKeyColumns<T>() where T : IDataContractComparer
9896
{
99-
return TypePropertyCacheManager.GetTypeProperties(typeof(T))
100-
.Where(prop => Attribute.IsDefined(prop, typeof(KeyPropertyAttribute))).Select(prop => prop.Name).ToList();
97+
return TypePropertyCacheManager.GetKeyColumns(typeof(T));
10198
}
10299

103100
/// <summary>
@@ -108,8 +105,7 @@ public List<string> GetKeyColumns<T>() where T : IDataContractComparer
108105
/// <seealso cref="IDataContractComparer"/>
109106
public List<string> GetExcludedProperties<T>() where T : IDataContractComparer
110107
{
111-
return TypePropertyCacheManager.GetTypeProperties(typeof(T))
112-
.Where(prop => Attribute.IsDefined(prop, typeof(ExcludedPropertyAttribute))).Select(prop => prop.Name).ToList();
108+
return TypePropertyCacheManager.GetExcludedProperties(typeof(T));
113109
}
114110

115111
/// <summary>
@@ -120,8 +116,7 @@ public List<string> GetExcludedProperties<T>() where T : IDataContractComparer
120116
/// <seealso cref="IDataContractComparer"/>
121117
public List<string> GetAllColumns<T>() where T : IDataContractComparer
122118
{
123-
return TypePropertyCacheManager.GetTypeProperties(typeof(T))
124-
.Select(prop => prop.Name).ToList();
119+
return TypePropertyCacheManager.GetAllColumns(typeof(T));
125120
}
126121

127122
/// <summary>
@@ -135,8 +130,7 @@ public List<string> GetAllColumns<T>() where T : IDataContractComparer
135130
/// <seealso cref="IDataContractComparer"/>
136131
public List<string> GetIdentityColumns<T>() where T : IDataContractComparer
137132
{
138-
return TypePropertyCacheManager.GetTypeProperties(typeof(T))
139-
.Where(prop => Attribute.IsDefined(prop, typeof(KeyAttribute))).Select(prop => prop.Name).ToList();
133+
return TypePropertyCacheManager.GetIdentityColumns(typeof(T));
140134
}
141135

142136
}

Manager/TypePropertyCacheManager.cs

Lines changed: 126 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
using System.Reflection;
1+
using DbSyncKit.DB.Attributes;
2+
using DbSyncKit.DB.Enum;
3+
using DbSyncKit.DB.Interface;
4+
using System.Reflection;
25

36
namespace 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

Comments
 (0)