Skip to content

Commit 94b721a

Browse files
committed
extracf ef repository
1 parent a3ee809 commit 94b721a

File tree

2 files changed

+42
-40
lines changed

2 files changed

+42
-40
lines changed
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
namespace BasicClean.Infrastructure.Repository
2+
{
3+
internal class EFQueryRepository<T, TKey> : IQueryRepository<T, TKey> where T : BaseEntity<TKey>
4+
{
5+
readonly TodoDbContext _context;
6+
public EFQueryRepository(TodoDbContext todoDbContext)
7+
{
8+
_context = todoDbContext;
9+
}
10+
public T Get(Expression<Func<T, bool>> match)
11+
{
12+
return _context.Set<T>().FirstOrDefault(match);
13+
}
14+
15+
public IEnumerable<T> GetAll()
16+
{
17+
return _context.Set<T>().ToList();
18+
}
19+
20+
public IEnumerable<T> GetAll(Expression<Func<T, bool>> predicate)
21+
{
22+
return _context.Set<T>()
23+
.Where(predicate).ToList();
24+
}
25+
26+
public async Task<IEnumerable<T>> GetAllAsync()
27+
{
28+
return await _context.Set<T>().ToListAsync();
29+
}
30+
31+
public async Task<IEnumerable<T>> GetAllAsync(Expression<Func<T, bool>> predicate)
32+
{
33+
return await _context.Set<T>()
34+
.Where(predicate).ToListAsync();
35+
}
36+
37+
public async Task<T> GetAsync(Expression<Func<T, bool>> match)
38+
{
39+
return await _context.Set<T>().Where(match).FirstOrDefaultAsync();
40+
}
41+
}
42+
}

Diff for: BasicClean.Infrastructure/Repository/EfRepository.cs

-40
Original file line numberDiff line numberDiff line change
@@ -58,44 +58,4 @@ public async Task UpdateAsync(T entity)
5858
await _context.SaveChangesAsync();
5959
}
6060
}
61-
62-
internal class EFQueryRepository<T, TKey> : IQueryRepository<T, TKey> where T : BaseEntity<TKey>
63-
{
64-
readonly TodoDbContext _context;
65-
public EFQueryRepository(TodoDbContext todoDbContext)
66-
{
67-
_context = todoDbContext;
68-
}
69-
public T Get(Expression<Func<T, bool>> match)
70-
{
71-
return _context.Set<T>().FirstOrDefault(match);
72-
}
73-
74-
public IEnumerable<T> GetAll()
75-
{
76-
return _context.Set<T>().ToList();
77-
}
78-
79-
public IEnumerable<T> GetAll(Expression<Func<T, bool>> predicate)
80-
{
81-
return _context.Set<T>()
82-
.Where(predicate).ToList();
83-
}
84-
85-
public async Task<IEnumerable<T>> GetAllAsync()
86-
{
87-
return await _context.Set<T>().ToListAsync();
88-
}
89-
90-
public async Task<IEnumerable<T>> GetAllAsync(Expression<Func<T, bool>> predicate)
91-
{
92-
return await _context.Set<T>()
93-
.Where(predicate).ToListAsync();
94-
}
95-
96-
public async Task<T> GetAsync(Expression<Func<T, bool>> match)
97-
{
98-
return await _context.Set<T>().Where(match).FirstOrDefaultAsync();
99-
}
100-
}
10161
}

0 commit comments

Comments
 (0)