Skip to content
This repository was archived by the owner on Apr 20, 2024. It is now read-only.

Commit 1bed0fa

Browse files
author
Morten Turn Pedersen
committed
Implemented select statement in base repository
1 parent fffc33c commit 1bed0fa

File tree

3 files changed

+134
-2
lines changed

3 files changed

+134
-2
lines changed

NetCoreEntityFramework.Tests/EntityRepositoryTests.cs

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System;
77
using System.Collections.Generic;
88
using System.Linq;
9+
using System.Linq.Expressions;
910
using System.Threading.Tasks;
1011
using TestContext = Nodes.NetCore.EntityFramework.Tests.Mocks.TestContext;
1112

@@ -93,7 +94,6 @@ public void AddThrowsExceptionIfEntityIsNull()
9394
#endregion
9495

9596
#region List
96-
9797
[Test]
9898
public async Task GetListReturnsAll()
9999
{
@@ -167,6 +167,80 @@ public async Task GetListPaginated()
167167
Assert.AreEqual(3, entitiesLastPage.Count());
168168
}
169169

170+
[Test]
171+
public async Task GetListWithSelectReturnsAll()
172+
{
173+
var entities = await _repository.GetListWithSelect<string>(e => e.Property);
174+
175+
Assert.AreEqual(_listEntities.Count() + 1, entities.Count());
176+
}
177+
178+
[Test]
179+
public async Task GetListWithSelectWhere()
180+
{
181+
const string propertyToLookFor = "b";
182+
IEnumerable<string> entities = await _repository.GetListWithSelect(x => x.Property, x => x.Property == propertyToLookFor);
183+
184+
Assert.AreEqual(1, entities.Count());
185+
Assert.AreEqual(propertyToLookFor, entities.ElementAt(0));
186+
}
187+
188+
[Test]
189+
public async Task GetListWithSelectOrderBy()
190+
{
191+
var entities = await _repository.GetListWithSelect(x => x.Property, x => x.Property.Length == 1, x => x.Property);
192+
193+
Assert.AreEqual(_listEntities.Count(), entities.Count());
194+
Assert.AreEqual("a", entities.ElementAt(0));
195+
Assert.AreEqual("b", entities.ElementAt(1));
196+
Assert.AreEqual("c", entities.ElementAt(2));
197+
Assert.AreEqual("d", entities.ElementAt(3));
198+
Assert.AreEqual("e", entities.ElementAt(4));
199+
Assert.AreEqual("f", entities.ElementAt(5));
200+
Assert.AreEqual("g", entities.ElementAt(6));
201+
Assert.AreEqual("h", entities.ElementAt(7));
202+
Assert.AreEqual("i", entities.ElementAt(8));
203+
Assert.AreEqual("j", entities.ElementAt(9));
204+
Assert.AreEqual("k", entities.ElementAt(10));
205+
Assert.AreEqual("l", entities.ElementAt(11));
206+
Assert.AreEqual("m", entities.ElementAt(12));
207+
Assert.AreEqual("n", entities.ElementAt(13));
208+
}
209+
210+
[Test]
211+
public async Task GetListWithSelectOrderByDescending()
212+
{
213+
var entities = await _repository.GetListWithSelect(x => x.Property, x => x.Property.Length == 1, x => x.Property, OrderBy.Descending);
214+
215+
Assert.AreEqual(_listEntities.Count(), entities.Count());
216+
Assert.AreEqual("n", entities.ElementAt(0));
217+
Assert.AreEqual("m", entities.ElementAt(1));
218+
Assert.AreEqual("l", entities.ElementAt(2));
219+
Assert.AreEqual("k", entities.ElementAt(3));
220+
Assert.AreEqual("j", entities.ElementAt(4));
221+
Assert.AreEqual("i", entities.ElementAt(5));
222+
Assert.AreEqual("h", entities.ElementAt(6));
223+
Assert.AreEqual("g", entities.ElementAt(7));
224+
Assert.AreEqual("f", entities.ElementAt(8));
225+
Assert.AreEqual("e", entities.ElementAt(9));
226+
Assert.AreEqual("d", entities.ElementAt(10));
227+
Assert.AreEqual("c", entities.ElementAt(11));
228+
Assert.AreEqual("b", entities.ElementAt(12));
229+
Assert.AreEqual("a", entities.ElementAt(13));
230+
}
231+
232+
[Test]
233+
public async Task GetListWithSelectPaginated()
234+
{
235+
const int pageSize = 6;
236+
237+
var entities = await _repository.GetListWithSelect(x => x.Property, 1, pageSize);
238+
var entitiesLastPage = await _repository.GetListWithSelect(x => x.Property, 3, pageSize);
239+
240+
Assert.AreEqual(pageSize, entities.Count());
241+
Assert.AreEqual(3, entitiesLastPage.Count());
242+
}
243+
170244
private IEnumerable<TestEntity> GetTestList()
171245
{
172246
return new List<TestEntity>

NetCoreEntityFramework/Repositories/EntityRepository.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,21 @@ public async virtual Task<IEnumerable<TEntity>> GetList(
3737
return await query.ToListAsync();
3838
}
3939

40+
public async virtual Task<IEnumerable<TResult>> GetListWithSelect<TResult>(
41+
Expression<Func<TEntity, TResult>> select,
42+
[Range(1, int.MaxValue)] int page,
43+
[Range(1, int.MaxValue)] int pageSize,
44+
Expression<Func<TEntity, bool>> where = null,
45+
Expression<Func<TEntity, object>> orderByExpression = null,
46+
OrderBy orderBy = OrderBy.Ascending)
47+
{
48+
IQueryable<TEntity> query = GetQueryable(where, orderByExpression, orderBy);
49+
50+
query = Paginate(query, page, pageSize);
51+
52+
return await query.Select(select).ToListAsync();
53+
}
54+
4055
public async virtual Task<IEnumerable<TEntity>> GetList(
4156
Expression<Func<TEntity, bool>> where = null,
4257
Expression<Func<TEntity, object>> orderByExpression = null,
@@ -47,6 +62,17 @@ public async virtual Task<IEnumerable<TEntity>> GetList(
4762
return await query.ToListAsync();
4863
}
4964

65+
public async virtual Task<IEnumerable<TResult>> GetListWithSelect<TResult>(
66+
Expression<Func<TEntity, TResult>> select,
67+
Expression<Func<TEntity, bool>> where = null,
68+
Expression<Func<TEntity, object>> orderByExpression = null,
69+
OrderBy orderBy = OrderBy.Ascending)
70+
{
71+
IQueryable<TEntity> query = GetQueryable(where, orderByExpression, orderBy);
72+
73+
return await query.Select(select).ToListAsync();
74+
}
75+
5076
public virtual Task Add(TEntity entity)
5177
{
5278
if (entity == null)

NetCoreEntityFramework/Repositories/IEntityRepository.cs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,44 @@ Task<IEnumerable<TEntity>> GetList(
3838
/// <param name="where">The filter expression.</param>
3939
/// <param name="orderByExpression">The expression to order by.</param>
4040
/// <param name="orderBy">To order by ascending or descending.</param>
41-
/// <exception cref="ArgumentException"></exception>
4241
Task<IEnumerable<TEntity>> GetList(
4342
Expression<Func<TEntity, bool>> where = null,
4443
Expression<Func<TEntity, object>> orderByExpression = null,
4544
OrderBy orderBy = OrderBy.Ascending);
4645

46+
/// <summary>
47+
/// Get multiple entities paginated and translated.
48+
/// </summary>
49+
/// <typeparam name="TResult">The type to return.</typeparam>
50+
/// <param name="select">The select statement to get <typeparamref name="TResult"/>.</param>
51+
/// <param name="page">Which page to fetch (1 and above).</param>
52+
/// <param name="pageSize">The size of each page (1 and above).</param>
53+
/// <param name="where">The filter expression.</param>
54+
/// <param name="orderByExpression">The expression to order by.</param>
55+
/// <param name="orderBy">To order by ascending or descending.</param>
56+
/// <exception cref="ArgumentException"></exception>
57+
Task<IEnumerable<TResult>> GetListWithSelect<TResult>(
58+
Expression<Func<TEntity, TResult>> select,
59+
[Range(1, int.MaxValue)] int page,
60+
[Range(1, int.MaxValue)] int pageSize,
61+
Expression<Func<TEntity, bool>> where = null,
62+
Expression<Func<TEntity, object>> orderByExpression = null,
63+
OrderBy orderBy = OrderBy.Ascending);
64+
65+
/// <summary>
66+
/// Get multiple entities translated.
67+
/// </summary>
68+
/// <typeparam name="TResult">The type to return.</typeparam>
69+
/// <param name="select">The select statement to get <typeparamref name="TResult"/>.</param>
70+
/// <param name="where">The filter expression.</param>
71+
/// <param name="orderByExpression">The expression to order by.</param>
72+
/// <param name="orderBy">To order by ascending or descending.</param>
73+
Task<IEnumerable<TResult>> GetListWithSelect<TResult>(
74+
Expression<Func<TEntity, TResult>> select,
75+
Expression<Func<TEntity, bool>> where = null,
76+
Expression<Func<TEntity, object>> orderByExpression = null,
77+
OrderBy orderBy = OrderBy.Ascending);
78+
4779
/// <summary>
4880
/// Add the given <paramref name="entity"/> to the database.
4981
/// An ID will be generated if not provided.

0 commit comments

Comments
 (0)