|
1 |
| -using System.Linq.Expressions; |
2 |
| -using Logistics.Domain.Core; |
| 1 | +using Logistics.Domain.Core; |
3 | 2 | using Logistics.Domain.Persistence;
|
4 |
| -using Logistics.Domain.Specifications; |
5 | 3 | using Logistics.Infrastructure.EF.Data;
|
6 |
| -using Microsoft.EntityFrameworkCore; |
7 | 4 |
|
8 | 5 | namespace Logistics.Infrastructure.EF.Persistence;
|
9 | 6 |
|
10 |
| -public class MasterRepository<TEntity> : IMasterRepository<TEntity> |
| 7 | +public class MasterRepository<TEntity> : Repository<MasterDbContext, TEntity, string>, IMasterRepository<TEntity> |
11 | 8 | where TEntity : class, IEntity<string>
|
12 | 9 | {
|
13 |
| - private readonly MasterDbContext _masterDbContext; |
14 |
| - |
15 |
| - public MasterRepository(MasterDbContext masterDbContext) |
| 10 | + public MasterRepository(MasterDbContext masterDbContext) : base(masterDbContext) |
16 | 11 | {
|
17 |
| - _masterDbContext = masterDbContext; |
18 |
| - } |
19 |
| - |
20 |
| - public IQueryable<TEntity> ApplySpecification(ISpecification<TEntity> specification) |
21 |
| - { |
22 |
| - return SpecificationEvaluator<TEntity>.GetQuery(_masterDbContext.Set<TEntity>(), specification); |
23 |
| - } |
24 |
| - |
25 |
| - public IQueryable<TEntity> Query() |
26 |
| - { |
27 |
| - return _masterDbContext.Set<TEntity>(); |
28 |
| - } |
29 |
| - |
30 |
| - public Task<int> CountAsync(Expression<Func<TEntity, bool>>? predicate = default) |
31 |
| - { |
32 |
| - if (predicate is null) |
33 |
| - { |
34 |
| - return _masterDbContext.Set<TEntity>().CountAsync(); |
35 |
| - } |
36 |
| - |
37 |
| - return _masterDbContext.Set<TEntity>().CountAsync(predicate); |
38 |
| - } |
39 |
| - |
40 |
| - public Task<TEntity?> GetByIdAsync(string id) |
41 |
| - { |
42 |
| - return _masterDbContext.Set<TEntity>().FindAsync(id).AsTask(); |
43 |
| - } |
44 |
| - |
45 |
| - public Task<TEntity?> GetAsync(Expression<Func<TEntity, bool>> predicate) |
46 |
| - { |
47 |
| - return _masterDbContext.Set<TEntity>().FirstOrDefaultAsync(predicate); |
48 |
| - } |
49 |
| - |
50 |
| - public Task<List<TEntity>> GetListAsync(Expression<Func<TEntity, bool>> predicate) |
51 |
| - { |
52 |
| - return _masterDbContext.Set<TEntity>().Where(predicate).ToListAsync(); |
53 |
| - } |
54 |
| - |
55 |
| - public Task<List<TEntity>> GetListAsync(ISpecification<TEntity>? specification = default) |
56 |
| - { |
57 |
| - if (specification is null) |
58 |
| - { |
59 |
| - return _masterDbContext.Set<TEntity>().ToListAsync(); |
60 |
| - } |
61 |
| - |
62 |
| - return SpecificationEvaluator<TEntity>.GetQuery(_masterDbContext.Set<TEntity>(), specification).ToListAsync(); |
63 |
| - } |
64 |
| - |
65 |
| - public Task AddAsync(TEntity entity) |
66 |
| - { |
67 |
| - return _masterDbContext.Set<TEntity>().AddAsync(entity).AsTask(); |
68 |
| - } |
69 |
| - |
70 |
| - public void Update(TEntity entity) |
71 |
| - { |
72 |
| - _masterDbContext.Set<TEntity>().Update(entity); |
73 |
| - } |
74 |
| - |
75 |
| - public void Delete(TEntity? entity) |
76 |
| - { |
77 |
| - if (entity is null) |
78 |
| - { |
79 |
| - return; |
80 |
| - } |
81 |
| - |
82 |
| - _masterDbContext.Set<TEntity>().Remove(entity); |
83 | 12 | }
|
84 | 13 | }
|
0 commit comments