This repository has been archived by the owner on May 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
/
EfRepository.cs
88 lines (75 loc) · 3.43 KB
/
EfRepository.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
using Ardalis.Specification;
using Ardalis.Specification.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Microsoft.eShopWeb.ApplicationCore.Entities;
using Microsoft.eShopWeb.ApplicationCore.Interfaces;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace Microsoft.eShopWeb.Infrastructure.Data
{
/// <summary>
/// "There's some repetition here - couldn't we have some the sync methods call the async?"
/// https://blogs.msdn.microsoft.com/pfxteam/2012/04/13/should-i-expose-synchronous-wrappers-for-asynchronous-methods/
/// </summary>
/// <typeparam name="T"></typeparam>
public class EfRepository<T> : IAsyncRepository<T> where T : BaseEntity, IAggregateRoot
{
protected readonly CatalogContext _dbContext;
public EfRepository(CatalogContext dbContext)
{
_dbContext = dbContext;
}
public virtual async Task<T> GetByIdAsync(int id, CancellationToken cancellationToken = default)
{
var keyValues = new object[] { id };
return await _dbContext.Set<T>().FindAsync(keyValues, cancellationToken);
}
public async Task<IReadOnlyList<T>> ListAllAsync(CancellationToken cancellationToken = default)
{
return await _dbContext.Set<T>().ToListAsync(cancellationToken);
}
public async Task<IReadOnlyList<T>> ListAsync(ISpecification<T> spec, CancellationToken cancellationToken = default)
{
var specificationResult = ApplySpecification(spec);
return await specificationResult.ToListAsync(cancellationToken);
}
public async Task<int> CountAsync(ISpecification<T> spec, CancellationToken cancellationToken = default)
{
var specificationResult = ApplySpecification(spec);
return await specificationResult.CountAsync(cancellationToken);
}
public async Task<T> AddAsync(T entity, CancellationToken cancellationToken = default)
{
await _dbContext.Set<T>().AddAsync(entity);
await _dbContext.SaveChangesAsync(cancellationToken);
return entity;
}
public async Task UpdateAsync(T entity, CancellationToken cancellationToken = default)
{
_dbContext.Entry(entity).State = EntityState.Modified;
await _dbContext.SaveChangesAsync(cancellationToken);
}
public async Task DeleteAsync(T entity, CancellationToken cancellationToken = default)
{
_dbContext.Set<T>().Remove(entity);
await _dbContext.SaveChangesAsync(cancellationToken);
}
public async Task<T> FirstAsync(ISpecification<T> spec, CancellationToken cancellationToken = default)
{
var specificationResult = ApplySpecification(spec);
return await specificationResult.FirstAsync(cancellationToken);
}
public async Task<T> FirstOrDefaultAsync(ISpecification<T> spec, CancellationToken cancellationToken = default)
{
var specificationResult = ApplySpecification(spec);
return await specificationResult.FirstOrDefaultAsync(cancellationToken);
}
private IQueryable<T> ApplySpecification(ISpecification<T> spec)
{
var evaluator = new SpecificationEvaluator<T>();
return evaluator.GetQuery(_dbContext.Set<T>().AsQueryable(), spec);
}
}
}