Skip to content

Commit 9d547c2

Browse files
Merge pull request #96 from johelvisguzman/issue95
Made some performance changes to the convention helper class and repository base
2 parents 292f204 + 7876115 commit 9d547c2

File tree

4 files changed

+68
-11
lines changed

4 files changed

+68
-11
lines changed

src/DotNetToolkit.Repository.InMemory/InMemoryRepositoryBase.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ protected virtual void Dispose(bool disposing)
9595
internal void EnsureDeleted()
9696
{
9797
_items.Clear();
98-
InMemoryCache<TEntity, TKey>.Instance.GetContext(DatabaseName).Clear();
98+
InMemoryCache.Instance.GetContext(DatabaseName).Clear();
9999
}
100100

101101
#endregion
@@ -231,7 +231,7 @@ protected override void SaveChanges()
231231
{
232232
lock (_syncRoot)
233233
{
234-
var context = InMemoryCache<TEntity, TKey>.Instance.GetContext(DatabaseName);
234+
var context = InMemoryCache.Instance.GetContext(DatabaseName);
235235

236236
foreach (var entitySet in _items.Select(y => y.Value))
237237
{
@@ -273,7 +273,7 @@ protected override void SaveChanges()
273273
/// </summary>
274274
protected override IQueryable<TEntity> GetQuery(IFetchStrategy<TEntity> fetchStrategy = null)
275275
{
276-
return InMemoryCache<TEntity, TKey>.Instance
276+
return InMemoryCache.Instance
277277
.GetContext(DatabaseName)
278278
.Select(y => y.Value)
279279
.AsQueryable();
@@ -284,7 +284,7 @@ protected override IQueryable<TEntity> GetQuery(IFetchStrategy<TEntity> fetchStr
284284
/// </summary>
285285
protected override TEntity GetEntity(TKey key, IFetchStrategy<TEntity> fetchStrategy)
286286
{
287-
InMemoryCache<TEntity, TKey>.Instance
287+
InMemoryCache.Instance
288288
.GetContext(DatabaseName)
289289
.TryGetValue(key, out TEntity entity);
290290

@@ -358,17 +358,17 @@ private enum EntityState
358358

359359
#endregion
360360

361-
#region Nested type: InMemoryCache<TEntity, TKey>
361+
#region Nested type: InMemoryCache
362362

363363
/// <summary>
364364
/// Represents an internal thread safe database storage which will store any information for the in-memory
365365
/// store that is needed through the life time of the application.
366366
/// </summary>
367-
private class InMemoryCache<TEntity, TKey> where TEntity : class
367+
private class InMemoryCache
368368
{
369369
#region Fields
370370

371-
private static volatile InMemoryCache<TEntity, TKey> _instance;
371+
private static volatile InMemoryCache _instance;
372372
private static readonly object _syncRoot = new object();
373373
private readonly ConcurrentDictionary<string, SortedDictionary<TKey, TEntity>> _storage;
374374

@@ -377,7 +377,7 @@ private class InMemoryCache<TEntity, TKey> where TEntity : class
377377
#region Constructors
378378

379379
/// <summary>
380-
/// Prevents a default instance of the <see cref="InMemoryCache{TEntity, TKey}"/> class from being created.
380+
/// Prevents a default instance of the <see cref="InMemoryCache"/> class from being created.
381381
/// </summary>
382382
private InMemoryCache()
383383
{
@@ -391,7 +391,7 @@ private InMemoryCache()
391391
/// <summary>
392392
/// Gets the instance.
393393
/// </summary>
394-
public static InMemoryCache<TEntity, TKey> Instance
394+
public static InMemoryCache Instance
395395
{
396396
get
397397
{
@@ -400,7 +400,7 @@ public static InMemoryCache<TEntity, TKey> Instance
400400
lock (_syncRoot)
401401
{
402402
if (_instance == null)
403-
_instance = new InMemoryCache<TEntity, TKey>();
403+
_instance = new InMemoryCache();
404404
}
405405
}
406406

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+
}

src/DotNetToolkit.Repository/RepositoryBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,8 +264,8 @@ protected virtual TKey GeneratePrimaryKey()
264264
if (propertyType == typeof(int))
265265
{
266266
var key = GetQuery()
267-
.OrderByDescending(x => x.GetPrimaryKeyPropertyValue<TKey>())
268267
.Select(x => x.GetPrimaryKeyPropertyValue<TKey>())
268+
.OrderByDescending(x => x)
269269
.FirstOrDefault();
270270

271271
return (TKey)Convert.ChangeType(Convert.ToInt32(key) + 1, typeof(TKey));

0 commit comments

Comments
 (0)