Skip to content

Commit 9b600eb

Browse files
Merge pull request #24 from johelvisguzman/hotfix/issue22
Add check to throw an exception if trying to get an entity with an invalid key value type
2 parents 9cb1683 + 0bd9b2e commit 9b600eb

File tree

7 files changed

+35
-19
lines changed

7 files changed

+35
-19
lines changed

src/DotNetToolkit.Repository.InMemory/InMemoryRepositoryBase.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -229,10 +229,6 @@ protected override IQueryable<TEntity> GetQuery(IFetchStrategy<TEntity> fetchStr
229229
/// </summary>
230230
protected override TEntity GetEntity(TKey key, IFetchStrategy<TEntity> fetchStrategy)
231231
{
232-
var propertyInfo = GetPrimaryKeyPropertyInfo();
233-
if (propertyInfo.PropertyType != key.GetType())
234-
throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Resources.EntityKeyValueTypeMismatch, key.GetType(), propertyInfo.PropertyType));
235-
236232
InMemoryCache<TEntity, TKey>.Instance
237233
.GetContext(_name)
238234
.TryGetValue(key, out EntitySet<TEntity, TKey> entitySet);

src/DotNetToolkit.Repository.InMemory/Properties/Resources.Designer.cs

Lines changed: 2 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/DotNetToolkit.Repository.InMemory/Properties/Resources.resx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,6 @@
127127
<data name="EntityKeyValueTypeInvalid" xml:space="preserve">
128128
<value>The instance of entity type '{0}' cannot be added to the in-memory store because the key type '{1}' is an invalid primary key property type.</value>
129129
</data>
130-
<data name="EntityKeyValueTypeMismatch" xml:space="preserve">
131-
<value>The key value was of type '{0}', which does not match the property type of '{1}'.</value>
132-
</data>
133130
<data name="EntityNotFoundInStore" xml:space="preserve">
134131
<value>Attempted to update or delete an entity that does not exist in the in-memory store.</value>
135132
</data>

src/DotNetToolkit.Repository/Properties/Resources.Designer.cs

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/DotNetToolkit.Repository/Properties/Resources.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@
123123
<data name="EntityKeyValueTypeInvalid" xml:space="preserve">
124124
<value>The instance of entity type '{0}' cannot be added to the in-memory store because the key type '{1}' is an invalid primary key property type.</value>
125125
</data>
126+
<data name="EntityKeyValueTypeMismatch" xml:space="preserve">
127+
<value>The key value was of type '{0}', which does not match the property type of '{1}'.</value>
128+
</data>
126129
<data name="EntityRequiresPrimaryKey" xml:space="preserve">
127130
<value>The instance of entity type '{0}' requires a primary key to be defined.</value>
128131
</data>

src/DotNetToolkit.Repository/RepositoryAsyncBase.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
namespace DotNetToolkit.Repository
22
{
33
using FetchStrategies;
4+
using Properties;
45
using Queries;
56
using Specifications;
67
using System;
78
using System.Collections.Generic;
9+
using System.Globalization;
810
using System.Linq.Expressions;
911
using System.Threading;
1012
using System.Threading.Tasks;
@@ -214,7 +216,7 @@ public abstract class RepositoryAsyncBase<TEntity, TKey> : RepositoryBase<TEntit
214216

215217
var entity = await GetAsync(key, cancellationToken);
216218
if (entity == null)
217-
throw new InvalidOperationException($"No entity found in the repository with the '{key}' key.");
219+
throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resources.EntityKeyNotFound, key));
218220

219221
DeleteItem(entity);
220222
await SaveChangesAsync(cancellationToken);

src/DotNetToolkit.Repository/RepositoryBase.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,16 @@ protected virtual TKey GeneratePrimaryKey()
246246
throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resources.EntityKeyValueTypeInvalid, typeof(TEntity), propertyType));
247247
}
248248

249+
/// <summary>
250+
/// Throws if the entity key value type does not match the type of the property defined.
251+
/// </summary>
252+
protected void ThrowIfEntityKeyValueTypeMismatch()
253+
{
254+
var propertyInfo = GetPrimaryKeyPropertyInfo();
255+
if (propertyInfo.PropertyType != typeof(TKey))
256+
throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Resources.EntityKeyValueTypeMismatch, typeof(TKey), propertyInfo.PropertyType));
257+
}
258+
249259
#endregion
250260

251261
#region Implementation of ICanAggregate<TEntity>
@@ -424,6 +434,8 @@ public TEntity Get(TKey key)
424434
if (key == null)
425435
throw new ArgumentNullException(nameof(key));
426436

437+
ThrowIfEntityKeyValueTypeMismatch();
438+
427439
return GetEntity(key);
428440
}
429441

@@ -438,6 +450,8 @@ public TEntity Get(TKey key, IFetchStrategy<TEntity> fetchStrategy)
438450
if (key == null)
439451
throw new ArgumentNullException(nameof(key));
440452

453+
ThrowIfEntityKeyValueTypeMismatch();
454+
441455
return GetEntity(key, fetchStrategy);
442456
}
443457

@@ -455,6 +469,8 @@ public TResult Get<TResult>(TKey key, Expression<Func<TEntity, TResult>> selecto
455469
if (selector == null)
456470
throw new ArgumentNullException(nameof(selector));
457471

472+
ThrowIfEntityKeyValueTypeMismatch();
473+
458474
return Get(key, selector, (IFetchStrategy<TEntity>)null);
459475
}
460476

@@ -473,6 +489,8 @@ public TResult Get<TResult>(TKey key, Expression<Func<TEntity, TResult>> selecto
473489
if (selector == null)
474490
throw new ArgumentNullException(nameof(selector));
475491

492+
ThrowIfEntityKeyValueTypeMismatch();
493+
476494
var result = GetEntity(key, fetchStrategy);
477495
var selectFunc = selector.Compile();
478496
var selectedResult = result == null

0 commit comments

Comments
 (0)