Skip to content

Commit a2b88c1

Browse files
Merge pull request #350 from johelvisguzman/context-base
Added a new base repository context class to support common linq operations
2 parents f353ef5 + 33f5fad commit a2b88c1

File tree

8 files changed

+803
-1129
lines changed

8 files changed

+803
-1129
lines changed

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

Lines changed: 49 additions & 397 deletions
Large diffs are not rendered by default.

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

Lines changed: 53 additions & 401 deletions
Large diffs are not rendered by default.

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

Lines changed: 71 additions & 250 deletions
Large diffs are not rendered by default.

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

Lines changed: 2 additions & 48 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>
@@ -163,8 +118,7 @@ public static ISpecificationQueryStrategy<TEntity> GetByPrimaryKeySpecification<
163118

164119
var propInfos = GetPrimaryKeyPropertyInfos<TEntity>().ToList();
165120

166-
if (keyValues.Length != propInfos.Count)
167-
throw new ArgumentException(Resources.EntityPrimaryKeyValuesLengthMismatch, nameof(keyValues));
121+
ThrowsIfEntityPrimaryKeyValuesLengthMismatch<TEntity>(keyValues);
168122

169123
var parameter = Expression.Parameter(typeof(TEntity), "x");
170124

@@ -230,7 +184,7 @@ public static void ThrowsIfInvalidPrimaryKeyDefinition<TEntity>(params Type[] ke
230184
/// <param name="keyValues"></param>
231185
public static void ThrowsIfEntityPrimaryKeyValuesLengthMismatch<TEntity>(object[] keyValues) where TEntity : class
232186
{
233-
if (keyValues.Length != PrimaryKeyConventionHelper.GetPrimaryKeyPropertyInfos<TEntity>().Count())
187+
if (keyValues.Length != GetPrimaryKeyPropertyInfos<TEntity>().Count())
234188
throw new ArgumentException(DotNetToolkit.Repository.Properties.Resources.EntityPrimaryKeyValuesLengthMismatch, nameof(keyValues));
235189
}
236190

src/DotNetToolkit.Repository/Configuration/LinqRepositoryContextBase.cs

Lines changed: 333 additions & 0 deletions
Large diffs are not rendered by default.

src/DotNetToolkit.Repository/Configuration/LinqRepositoryContextBaseAsync.cs

Lines changed: 275 additions & 0 deletions
Large diffs are not rendered by default.

src/DotNetToolkit.Repository/Extensions/QueryableExtensions.cs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,15 @@
88
using System.Linq;
99
using System.Reflection;
1010

11-
internal static class QueryableExtensions
11+
/// <summary>
12+
/// Contains various utility methods for applying options to the specified <see cref="IQueryable{T}" />.
13+
/// </summary>
14+
public static class QueryableExtensions
1215
{
16+
/// <summary>
17+
/// Apply a specification strategy options to the specified entity's query.
18+
/// </summary>
19+
/// <returns>The entity's query with the applied options.</returns>
1320
public static IQueryable<T> ApplySpecificationOptions<T>(this IQueryable<T> query, IQueryOptions<T> options) where T : class
1421
{
1522
if (query == null)
@@ -21,6 +28,10 @@ public static IQueryable<T> ApplySpecificationOptions<T>(this IQueryable<T> quer
2128
return query;
2229
}
2330

31+
/// <summary>
32+
/// Apply a sorting options to the specified entity's query.
33+
/// </summary>
34+
/// <returns>The entity's query with the applied options.</returns>
2435
public static IOrderedQueryable<T> ApplySortingOptions<T>(this IQueryable<T> query, IQueryOptions<T> options) where T : class
2536
{
2637
if (query == null)
@@ -62,6 +73,10 @@ public static IOrderedQueryable<T> ApplySortingOptions<T>(this IQueryable<T> que
6273
return sortedQuery;
6374
}
6475

76+
/// <summary>
77+
/// Apply a paging options to the specified entity's query.
78+
/// </summary>
79+
/// <returns>The entity's query with the applied options.</returns>
6580
public static IQueryable<T> ApplyPagingOptions<T>(this IQueryable<T> query, IQueryOptions<T> options)
6681
{
6782
if (query == null)
@@ -95,22 +110,22 @@ private static IOrderedQueryable<T> ApplyOrder<T>(IQueryable<T> source, string p
95110
return (IOrderedQueryable<T>)result;
96111
}
97112

98-
public static IOrderedQueryable<T> OrderBy<T>(this IQueryable<T> source, string propertyName)
113+
private static IOrderedQueryable<T> OrderBy<T>(this IQueryable<T> source, string propertyName)
99114
{
100115
return ApplyOrder<T>(source, propertyName, "OrderBy");
101116
}
102117

103-
public static IOrderedQueryable<T> OrderByDescending<T>(this IQueryable<T> source, string propertyName)
118+
private static IOrderedQueryable<T> OrderByDescending<T>(this IQueryable<T> source, string propertyName)
104119
{
105120
return ApplyOrder<T>(source, propertyName, "OrderByDescending");
106121
}
107122

108-
public static IOrderedQueryable<T> ThenBy<T>(this IOrderedQueryable<T> source, string propertyName)
123+
private static IOrderedQueryable<T> ThenBy<T>(this IOrderedQueryable<T> source, string propertyName)
109124
{
110125
return ApplyOrder<T>(source, propertyName, "ThenBy");
111126
}
112127

113-
public static IOrderedQueryable<T> ThenByDescending<T>(this IOrderedQueryable<T> source, string propertyName)
128+
private static IOrderedQueryable<T> ThenByDescending<T>(this IOrderedQueryable<T> source, string propertyName)
114129
{
115130
return ApplyOrder<T>(source, propertyName, "ThenByDescending");
116131
}

test/DotNetToolkit.Repository.Test/CachingProviderManagerTests.cs

Lines changed: 0 additions & 28 deletions
This file was deleted.

0 commit comments

Comments
 (0)