Skip to content

Commit e45b505

Browse files
Removed in-memory specific functionality from the primary key convention helper
1 parent 57b5ad3 commit e45b505

File tree

2 files changed

+39
-47
lines changed

2 files changed

+39
-47
lines changed

src/DotNetToolkit.Repository.InMemory/Internal/InMemoryRepositoryContext.cs

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,43 @@ private object GeneratePrimaryKey(Type entityType)
128128
throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resources.EntityKeyValueTypeInvalid, entityType.FullName, propertyType));
129129
}
130130

131+
private static object GetPrimaryKeyValue(object obj)
132+
{
133+
if (obj == null)
134+
throw new ArgumentNullException(nameof(obj));
135+
136+
return Combine(PrimaryKeyConventionHelper.GetPrimaryKeyValues(obj));
137+
}
138+
139+
private static object Combine(object[] keyValues)
140+
{
141+
if (keyValues == null)
142+
throw new ArgumentNullException(nameof(keyValues));
143+
144+
object key;
145+
146+
switch (keyValues.Length)
147+
{
148+
case 3:
149+
{
150+
key = Tuple.Create(keyValues[0], keyValues[1], keyValues[2]);
151+
break;
152+
}
153+
case 2:
154+
{
155+
key = Tuple.Create(keyValues[0], keyValues[1]);
156+
break;
157+
}
158+
default:
159+
{
160+
key = keyValues[0];
161+
break;
162+
}
163+
}
164+
165+
return key;
166+
}
167+
131168
#endregion
132169

133170
#region Implementation of IRepositoryContext
@@ -231,7 +268,7 @@ public override int SaveChanges()
231268
while (_items.TryTake(out var entitySet))
232269
{
233270
var entityType = entitySet.Entity.GetType();
234-
var key = PrimaryKeyConventionHelper.GetPrimaryKeyValue(entitySet.Entity);
271+
var key = GetPrimaryKeyValue(entitySet.Entity);
235272

236273
if (!store.ContainsKey(entityType))
237274
store[entityType] = new ConcurrentDictionary<object, object>();
@@ -304,7 +341,7 @@ public override QueryResult<TEntity> Find<TEntity>(IFetchQueryStrategy<TEntity>
304341
if (!store.ContainsKey(entityType))
305342
return new QueryResult<TEntity>(default(TEntity));
306343

307-
var key = PrimaryKeyConventionHelper.Combine(keyValues);
344+
var key = Combine(keyValues);
308345

309346
store[entityType].TryGetValue(key, out object entity);
310347

src/DotNetToolkit.Repository/Configuration/Conventions/PrimaryKeyConventionHelper.cs

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -107,51 +107,6 @@ public static object[] GetPrimaryKeyValues(object obj)
107107
return keyValues;
108108
}
109109

110-
/// <summary>
111-
/// Gets the primary key value for the specified object. If the primary key is defined as a composite key, then the result will be returned as a tuple.
112-
/// </summary>
113-
/// <returns>The primary key value.</returns>
114-
public static object GetPrimaryKeyValue(object obj)
115-
{
116-
if (obj == null)
117-
throw new ArgumentNullException(nameof(obj));
118-
119-
return Combine(GetPrimaryKeyValues(obj));
120-
}
121-
122-
/// <summary>
123-
/// Merges the specified collection of key values into a tuple if there are more than one key; otherwise, it will return the single key object.
124-
/// </summary>
125-
/// <returns>The merged primary key value.</returns>
126-
public static object Combine(object[] keyValues)
127-
{
128-
if (keyValues == null)
129-
throw new ArgumentNullException(nameof(keyValues));
130-
131-
object key;
132-
133-
switch (keyValues.Length)
134-
{
135-
case 3:
136-
{
137-
key = Tuple.Create(keyValues[0], keyValues[1], keyValues[2]);
138-
break;
139-
}
140-
case 2:
141-
{
142-
key = Tuple.Create(keyValues[0], keyValues[1]);
143-
break;
144-
}
145-
default:
146-
{
147-
key = keyValues[0];
148-
break;
149-
}
150-
}
151-
152-
return key;
153-
}
154-
155110
/// <summary>
156111
/// Returns a specification for getting an entity by it's primary key.
157112
/// </summary>

0 commit comments

Comments
 (0)