Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit df1bcd9

Browse files
Query specification refactored
1 parent ee14fe6 commit df1bcd9

File tree

5 files changed

+94
-96
lines changed

5 files changed

+94
-96
lines changed
Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,8 @@
11
using AspnetRunAngular.Core.Entities.Base;
2-
using AspnetRunAngular.Core.Interfaces;
3-
using System;
4-
using System.Collections.Generic;
5-
using System.Linq;
6-
using System.Linq.Expressions;
7-
using System.Threading.Tasks;
82

93
namespace AspnetRunAngular.Core.Repositories.Base
104
{
115
public interface IRepository<T> : IRepositoryBase<T, int> where T : IEntityBase<int>
126
{
13-
Task<IReadOnlyList<T>> GetAsync(ISpecification<T> spec);
14-
Task<IReadOnlyList<T>> GetAsync(Expression<Func<T, bool>> predicate);
15-
Task<IReadOnlyList<T>> GetAsync(Expression<Func<T, bool>> predicate = null,
16-
Func<IQueryable<T>, IOrderedQueryable<T>> orderBy = null,
17-
string includeString = null,
18-
bool disableTracking = true);
19-
Task<IReadOnlyList<T>> GetAsync(Expression<Func<T, bool>> predicate = null,
20-
Func<IQueryable<T>, IOrderedQueryable<T>> orderBy = null,
21-
List<Expression<Func<T, object>>> includes = null,
22-
bool disableTracking = true);
23-
Task<int> CountAsync(ISpecification<T> spec);
247
}
258
}

‎src/AspnetRunAngular.Core/Repositories/Base/IRepositoryBase.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
using AspnetRunAngular.Core.Entities.Base;
2+
using AspnetRunAngular.Core.Interfaces;
3+
using System;
24
using System.Collections.Generic;
35
using System.Linq;
6+
using System.Linq.Expressions;
47
using System.Threading.Tasks;
58

69
namespace AspnetRunAngular.Core.Repositories.Base
@@ -20,5 +23,21 @@ public interface IRepositoryBase<T, TId> where T : IEntityBase<TId>
2023
Task<T> SaveAsync(T entity);
2124

2225
Task DeleteAsync(T entity);
26+
27+
Task<IReadOnlyList<T>> GetAsync(ISpecification<T> spec);
28+
29+
Task<IReadOnlyList<T>> GetAsync(Expression<Func<T, bool>> predicate);
30+
31+
Task<IReadOnlyList<T>> GetAsync(Expression<Func<T, bool>> predicate = null,
32+
Func<IQueryable<T>, IOrderedQueryable<T>> orderBy = null,
33+
string includeString = null,
34+
bool disableTracking = true);
35+
36+
Task<IReadOnlyList<T>> GetAsync(Expression<Func<T, bool>> predicate = null,
37+
Func<IQueryable<T>, IOrderedQueryable<T>> orderBy = null,
38+
List<Expression<Func<T, object>>> includes = null,
39+
bool disableTracking = true);
40+
41+
Task<int> CountAsync(ISpecification<T> spec);
2342
}
2443
}
Lines changed: 1 addition & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Linq.Expressions;
5-
using System.Threading.Tasks;
6-
using AspnetRunAngular.Core.Entities.Base;
7-
using AspnetRunAngular.Core.Interfaces;
1+
using AspnetRunAngular.Core.Entities.Base;
82
using AspnetRunAngular.Core.Repositories.Base;
93
using AspnetRunAngular.Infrastructure.Data;
10-
using Microsoft.EntityFrameworkCore;
114

125
namespace AspnetRunAngular.Infrastructure.Repository.Base
136
{
@@ -18,75 +11,5 @@ public Repository(AspnetRunContext context)
1811
: base(context)
1912
{
2013
}
21-
22-
public async Task<IReadOnlyList<T>> GetAsync(ISpecification<T> spec)
23-
{
24-
return await ApplySpecification(spec).ToListAsync();
25-
}
26-
27-
public async Task<IReadOnlyList<T>> GetAsync(Expression<Func<T, bool>> predicate)
28-
{
29-
return await Table.Where(predicate).ToListAsync();
30-
}
31-
32-
public async Task<IReadOnlyList<T>> GetAsync(Expression<Func<T, bool>> predicate = null,
33-
Func<IQueryable<T>, IOrderedQueryable<T>> orderBy = null,
34-
string includeString = null,
35-
bool disableTracking = true)
36-
{
37-
var query = disableTracking ? TableNoTracking : Table;
38-
39-
if (!string.IsNullOrWhiteSpace(includeString))
40-
{
41-
query = query.Include(includeString);
42-
}
43-
44-
if (predicate != null)
45-
{
46-
query = query.Where(predicate);
47-
}
48-
49-
if (orderBy != null)
50-
{
51-
return await orderBy(query).ToListAsync();
52-
}
53-
54-
return await query.ToListAsync();
55-
}
56-
57-
public async Task<IReadOnlyList<T>> GetAsync(Expression<Func<T, bool>> predicate = null,
58-
Func<IQueryable<T>, IOrderedQueryable<T>> orderBy = null,
59-
List<Expression<Func<T, object>>> includes = null,
60-
bool disableTracking = true)
61-
{
62-
var query = disableTracking ? TableNoTracking : Table;
63-
64-
if (includes != null)
65-
{
66-
query = includes.Aggregate(query, (current, include) => current.Include(include));
67-
}
68-
69-
if (predicate != null)
70-
{
71-
query = query.Where(predicate);
72-
}
73-
74-
if (orderBy != null)
75-
{
76-
return await orderBy(query).ToListAsync();
77-
}
78-
79-
return await query.ToListAsync();
80-
}
81-
82-
public async Task<int> CountAsync(ISpecification<T> spec)
83-
{
84-
return await ApplySpecification(spec).CountAsync();
85-
}
86-
87-
private IQueryable<T> ApplySpecification(ISpecification<T> spec)
88-
{
89-
return SpecificationEvaluator<T>.GetQuery(Table, spec);
90-
}
9114
}
9215
}

‎src/AspnetRunAngular.Infrastructure/Repository/Base/RepositoryBase.cs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
using AspnetRunAngular.Core.Entities.Base;
2+
using AspnetRunAngular.Core.Interfaces;
23
using AspnetRunAngular.Core.Repositories.Base;
34
using AspnetRunAngular.Infrastructure.Data;
45
using Microsoft.EntityFrameworkCore;
6+
using System;
57
using System.Collections.Generic;
68
using System.Linq;
9+
using System.Linq.Expressions;
710
using System.Threading.Tasks;
811

912
namespace AspnetRunAngular.Infrastructure.Repository.Base
@@ -73,5 +76,75 @@ public async virtual Task<IReadOnlyList<T>> ListAllAsync()
7376
public IQueryable<T> Table => Entities;
7477

7578
public IQueryable<T> TableNoTracking => Entities.AsNoTracking();
79+
80+
public async Task<IReadOnlyList<T>> GetAsync(ISpecification<T> spec)
81+
{
82+
return await ApplySpecification(spec).ToListAsync();
83+
}
84+
85+
public async Task<IReadOnlyList<T>> GetAsync(Expression<Func<T, bool>> predicate)
86+
{
87+
return await Table.Where(predicate).ToListAsync();
88+
}
89+
90+
public async Task<IReadOnlyList<T>> GetAsync(Expression<Func<T, bool>> predicate = null,
91+
Func<IQueryable<T>, IOrderedQueryable<T>> orderBy = null,
92+
string includeString = null,
93+
bool disableTracking = true)
94+
{
95+
var query = disableTracking ? TableNoTracking : Table;
96+
97+
if (!string.IsNullOrWhiteSpace(includeString))
98+
{
99+
query = query.Include(includeString);
100+
}
101+
102+
if (predicate != null)
103+
{
104+
query = query.Where(predicate);
105+
}
106+
107+
if (orderBy != null)
108+
{
109+
return await orderBy(query).ToListAsync();
110+
}
111+
112+
return await query.ToListAsync();
113+
}
114+
115+
public async Task<IReadOnlyList<T>> GetAsync(Expression<Func<T, bool>> predicate = null,
116+
Func<IQueryable<T>, IOrderedQueryable<T>> orderBy = null,
117+
List<Expression<Func<T, object>>> includes = null,
118+
bool disableTracking = true)
119+
{
120+
var query = disableTracking ? TableNoTracking : Table;
121+
122+
if (includes != null)
123+
{
124+
query = includes.Aggregate(query, (current, include) => current.Include(include));
125+
}
126+
127+
if (predicate != null)
128+
{
129+
query = query.Where(predicate);
130+
}
131+
132+
if (orderBy != null)
133+
{
134+
return await orderBy(query).ToListAsync();
135+
}
136+
137+
return await query.ToListAsync();
138+
}
139+
140+
public async Task<int> CountAsync(ISpecification<T> spec)
141+
{
142+
return await ApplySpecification(spec).CountAsync();
143+
}
144+
145+
private IQueryable<T> ApplySpecification(ISpecification<T> spec)
146+
{
147+
return SpecificationEvaluator<T, TId>.GetQuery(Table, spec);
148+
}
76149
}
77150
}

‎src/AspnetRunAngular.Infrastructure/Repository/Base/SpecificationEvaluator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace AspnetRunAngular.Infrastructure.Repository.Base
77
{
8-
public class SpecificationEvaluator<T> where T : class, IEntityBase<int>
8+
public class SpecificationEvaluator<T,TId> where T : class, IEntityBase<TId>
99
{
1010
public static IQueryable<T> GetQuery(IQueryable<T> inputQuery, ISpecification<T> specification)
1111
{

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /