Skip to content

Commit 4668624

Browse files
Added a new utility method to the PrimaryKeyConventionHelper to throw an exception if number of keys does not match the one specified in an entity type
1 parent 79e37a5 commit 4668624

File tree

5 files changed

+17
-44
lines changed

5 files changed

+17
-44
lines changed

src/DotNetToolkit.Repository.AdoNet/Internal/AdoNetRepositoryContext.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,12 +1158,6 @@ private static Func<object, TElement> ConvertType<TElement>()
11581158
return ConvertValue<TElement>;
11591159
}
11601160

1161-
private void ThrowsIfEntityPrimaryKeyValuesLengthMismatch<TEntity>(object[] keyValues) where TEntity : class
1162-
{
1163-
if (keyValues.Length != PrimaryKeyConventionHelper.GetPrimaryKeyPropertyInfos<TEntity>().Count())
1164-
throw new ArgumentException(DotNetToolkit.Repository.Properties.Resources.EntityPrimaryKeyValuesLengthMismatch, nameof(keyValues));
1165-
}
1166-
11671161
private async Task OnSchemaValidationAsync(Type entityType, CancellationToken cancellationToken)
11681162
{
11691163
// Performs some schema validation for this type (either creates the table if does not exist, or validates)
@@ -1423,7 +1417,7 @@ public QueryResult<TEntity> Find<TEntity>(IFetchQueryStrategy<TEntity> fetchStra
14231417
if (keyValues == null)
14241418
throw new ArgumentNullException(nameof(keyValues));
14251419

1426-
ThrowsIfEntityPrimaryKeyValuesLengthMismatch<TEntity>(keyValues);
1420+
PrimaryKeyConventionHelper.ThrowsIfEntityPrimaryKeyValuesLengthMismatch<TEntity>(keyValues);
14271421

14281422
var options = new QueryOptions<TEntity>().Include(PrimaryKeyConventionHelper.GetByPrimaryKeySpecification<TEntity>(keyValues));
14291423

@@ -1777,7 +1771,7 @@ public Task<QueryResult<TEntity>> FindAsync<TEntity>(CancellationToken cancellat
17771771
if (keyValues == null)
17781772
throw new ArgumentNullException(nameof(keyValues));
17791773

1780-
ThrowsIfEntityPrimaryKeyValuesLengthMismatch<TEntity>(keyValues);
1774+
PrimaryKeyConventionHelper.ThrowsIfEntityPrimaryKeyValuesLengthMismatch<TEntity>(keyValues);
17811775

17821776
var options = new QueryOptions<TEntity>().Include(PrimaryKeyConventionHelper.GetByPrimaryKeySpecification<TEntity>(keyValues));
17831777

src/DotNetToolkit.Repository.EntityFramework/Internal/EfRepositoryContext.cs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,6 @@ public EfRepositoryContext(DbContext context)
5454

5555
#endregion
5656

57-
#region Private Methods
58-
59-
private void ThrowsIfEntityPrimaryKeyValuesLengthMismatch<TEntity>(object[] keyValues) where TEntity : class
60-
{
61-
if (keyValues.Length != PrimaryKeyConventionHelper.GetPrimaryKeyPropertyInfos<TEntity>().Count())
62-
throw new ArgumentException(DotNetToolkit.Repository.Properties.Resources.EntityPrimaryKeyValuesLengthMismatch, nameof(keyValues));
63-
}
64-
65-
#endregion
66-
6757
#region Implementation of IRepositoryContext
6858

6959
/// <summary>
@@ -291,7 +281,7 @@ public QueryResult<TEntity> Find<TEntity>(IFetchQueryStrategy<TEntity> fetchStra
291281
if (keyValues == null)
292282
throw new ArgumentNullException(nameof(keyValues));
293283

294-
ThrowsIfEntityPrimaryKeyValuesLengthMismatch<TEntity>(keyValues);
284+
PrimaryKeyConventionHelper.ThrowsIfEntityPrimaryKeyValuesLengthMismatch<TEntity>(keyValues);
295285

296286
if (fetchStrategy == null)
297287
{
@@ -638,7 +628,7 @@ public async Task<QueryResult<TEntity>> FindAsync<TEntity>(CancellationToken can
638628
if (keyValues == null)
639629
throw new ArgumentNullException(nameof(keyValues));
640630

641-
ThrowsIfEntityPrimaryKeyValuesLengthMismatch<TEntity>(keyValues);
631+
PrimaryKeyConventionHelper.ThrowsIfEntityPrimaryKeyValuesLengthMismatch<TEntity>(keyValues);
642632

643633
if (fetchStrategy == null)
644634
{

src/DotNetToolkit.Repository.EntityFrameworkCore/Internal/EfCoreRepositoryContext.cs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,6 @@ public EfCoreRepositoryContext(DbContext context)
5454

5555
#endregion
5656

57-
#region Private Methods
58-
59-
private void ThrowsIfEntityPrimaryKeyValuesLengthMismatch<TEntity>(object[] keyValues) where TEntity : class
60-
{
61-
if (keyValues.Length != PrimaryKeyConventionHelper.GetPrimaryKeyPropertyInfos<TEntity>().Count())
62-
throw new ArgumentException(DotNetToolkit.Repository.Properties.Resources.EntityPrimaryKeyValuesLengthMismatch, nameof(keyValues));
63-
}
64-
65-
#endregion
66-
6757
#region Implementation of IRepositoryContext
6858

6959
/// <summary>
@@ -294,7 +284,7 @@ public QueryResult<TEntity> Find<TEntity>(IFetchQueryStrategy<TEntity> fetchStra
294284
if (keyValues == null)
295285
throw new ArgumentNullException(nameof(keyValues));
296286

297-
ThrowsIfEntityPrimaryKeyValuesLengthMismatch<TEntity>(keyValues);
287+
PrimaryKeyConventionHelper.ThrowsIfEntityPrimaryKeyValuesLengthMismatch<TEntity>(keyValues);
298288

299289
if (fetchStrategy == null)
300290
{
@@ -641,7 +631,7 @@ public async Task<QueryResult<TEntity>> FindAsync<TEntity>(CancellationToken can
641631
if (keyValues == null)
642632
throw new ArgumentNullException(nameof(keyValues));
643633

644-
ThrowsIfEntityPrimaryKeyValuesLengthMismatch<TEntity>(keyValues);
634+
PrimaryKeyConventionHelper.ThrowsIfEntityPrimaryKeyValuesLengthMismatch<TEntity>(keyValues);
645635

646636
if (fetchStrategy == null)
647637
{

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

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,6 @@ internal class InMemoryRepositoryContext : IRepositoryContext
4040
/// </summary>
4141
public string DatabaseName { get; internal set; }
4242

43-
/// <summary>
44-
/// Gets the item types in-memory that have not yet been saved. This collection is cleared after the context is saved.
45-
/// </summary>
46-
internal IEnumerable<Type> ItemTypes { get { return _items.Select(x => x.Entity.GetType()); } }
47-
4843
/// <summary>
4944
/// Gets the repository context logger.
5045
/// </summary>
@@ -97,12 +92,6 @@ public void EnsureDeleted()
9792

9893
#region Private Methods
9994

100-
private void ThrowsIfEntityPrimaryKeyValuesLengthMismatch<TEntity>(object[] keyValues) where TEntity : class
101-
{
102-
if (keyValues.Length != PrimaryKeyConventionHelper.GetPrimaryKeyPropertyInfos<TEntity>().Count())
103-
throw new ArgumentException(DotNetToolkit.Repository.Properties.Resources.EntityPrimaryKeyValuesLengthMismatch, nameof(keyValues));
104-
}
105-
10695
private IQueryable<TEntity> AsQueryable<TEntity>() where TEntity : class
10796
{
10897
var entityType = typeof(TEntity);
@@ -327,7 +316,7 @@ public virtual QueryResult<TEntity> Find<TEntity>(IFetchQueryStrategy<TEntity> f
327316
if (keyValues == null)
328317
throw new ArgumentNullException(nameof(keyValues));
329318

330-
ThrowsIfEntityPrimaryKeyValuesLengthMismatch<TEntity>(keyValues);
319+
PrimaryKeyConventionHelper.ThrowsIfEntityPrimaryKeyValuesLengthMismatch<TEntity>(keyValues);
331320

332321
var entityType = typeof(TEntity);
333322
var store = InMemoryCache.Instance.GetDatabaseStore(DatabaseName);

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,16 @@ public static void ThrowsIfInvalidPrimaryKeyDefinition<TEntity>(params Type[] ke
224224
throw new InvalidOperationException(Resources.EntityPrimaryKeyTypesMismatch);
225225
}
226226

227+
/// <summary>
228+
/// Throws an exception if the specified number of key values does not match the ones defined for the entity.
229+
/// </summary>
230+
/// <param name="keyValues"></param>
231+
public static void ThrowsIfEntityPrimaryKeyValuesLengthMismatch<TEntity>(object[] keyValues) where TEntity : class
232+
{
233+
if (keyValues.Length != PrimaryKeyConventionHelper.GetPrimaryKeyPropertyInfos<TEntity>().Count())
234+
throw new ArgumentException(DotNetToolkit.Repository.Properties.Resources.EntityPrimaryKeyValuesLengthMismatch, nameof(keyValues));
235+
}
236+
227237
/// <summary>
228238
/// Gets the primary key name checks.
229239
/// </summary>

0 commit comments

Comments
 (0)