Skip to content

Commit ac2a4e6

Browse files
Added some internal in-memory caching to the convention helper class to help with performance
1 parent 292f204 commit ac2a4e6

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

src/DotNetToolkit.Repository/Helpers/ConventionHelper.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ public static PropertyInfo GetPrimaryKeyPropertyInfo(this Type entityType)
6060
if (entityType == null)
6161
throw new ArgumentNullException(nameof(entityType));
6262

63+
if (InMemoryCache.Instance.PrimaryKeyMapping.ContainsKey(entityType))
64+
return InMemoryCache.Instance.PrimaryKeyMapping[entityType];
65+
6366
var propertyInfo = entityType
6467
.GetRuntimeProperties()
6568
.Where(x => x.IsMapped())
@@ -74,6 +77,8 @@ public static PropertyInfo GetPrimaryKeyPropertyInfo(this Type entityType)
7477

7578
if (propertyInfo != null && propertyInfo.IsMapped())
7679
{
80+
InMemoryCache.Instance.PrimaryKeyMapping[entityType] = propertyInfo;
81+
7782
return propertyInfo;
7883
}
7984
}
@@ -97,11 +102,16 @@ public static PropertyInfo GetPrimaryKeyPropertyInfo<T>()
97102
/// <returns>The name of the table.</returns>
98103
public static string GetTableName(this Type entityType)
99104
{
105+
if (InMemoryCache.Instance.TableNameMapping.ContainsKey(entityType))
106+
return InMemoryCache.Instance.TableNameMapping[entityType];
107+
100108
var tableName = entityType.GetTypeInfo().GetCustomAttribute<TableAttribute>()?.Name;
101109

102110
if (string.IsNullOrEmpty(tableName))
103111
tableName = PluralizationHelper.Pluralize(entityType.Name);
104112

113+
InMemoryCache.Instance.TableNameMapping[entityType] = tableName;
114+
105115
return tableName;
106116
}
107117

@@ -145,6 +155,11 @@ public static int GetColumnOrder(this PropertyInfo pi)
145155
/// <returns>The name of the foreign key.</returns>
146156
public static PropertyInfo GetForeignKeyPropertyInfo(this Type sourceType, Type foreignType)
147157
{
158+
var tupleKey = Tuple.Create(sourceType, foreignType);
159+
160+
if (InMemoryCache.Instance.ForeignKeyMapping.ContainsKey(tupleKey))
161+
return InMemoryCache.Instance.ForeignKeyMapping[tupleKey];
162+
148163
var properties = sourceType.GetRuntimeProperties().Where(x => x.IsMapped());
149164
var foreignPropertyInfo = properties.SingleOrDefault(x => x.PropertyType == foreignType);
150165

@@ -179,6 +194,8 @@ public static PropertyInfo GetForeignKeyPropertyInfo(this Type sourceType, Type
179194
}
180195
}
181196

197+
InMemoryCache.Instance.ForeignKeyMapping[tupleKey] = propertyInfo;
198+
182199
return propertyInfo;
183200
}
184201

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
namespace DotNetToolkit.Repository.Helpers
2+
{
3+
using System;
4+
using System.Collections.Concurrent;
5+
using System.Reflection;
6+
7+
internal class InMemoryCache
8+
{
9+
private static volatile InMemoryCache _instance;
10+
private static readonly object _syncRoot = new object();
11+
12+
public static InMemoryCache Instance
13+
{
14+
get
15+
{
16+
if (_instance == null)
17+
{
18+
lock (_syncRoot)
19+
{
20+
if (_instance == null)
21+
_instance = new InMemoryCache();
22+
}
23+
}
24+
25+
return _instance;
26+
}
27+
}
28+
29+
public ConcurrentDictionary<Type, PropertyInfo> PrimaryKeyMapping { get; }
30+
public ConcurrentDictionary<Tuple<Type, Type>, PropertyInfo> ForeignKeyMapping { get; }
31+
public ConcurrentDictionary<Type, string> TableNameMapping { get; }
32+
33+
private InMemoryCache()
34+
{
35+
PrimaryKeyMapping = new ConcurrentDictionary<Type, PropertyInfo>();
36+
ForeignKeyMapping = new ConcurrentDictionary<Tuple<Type, Type>, PropertyInfo>();
37+
TableNameMapping = new ConcurrentDictionary<Type, string>();
38+
}
39+
}
40+
}

0 commit comments

Comments
 (0)