|
|
|
@ -12,7 +12,7 @@ namespace Microsoft.eShopWeb.Infrastructure.Data |
|
|
|
/// 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> : IRepository<T>, IAsyncRepository<T> where T : BaseEntity |
|
|
|
public class EfRepository<T> : IAsyncRepository<T> where T : BaseEntity |
|
|
|
{ |
|
|
|
protected readonly CatalogContext _dbContext; |
|
|
|
|
|
|
|
@ -20,59 +20,27 @@ namespace Microsoft.eShopWeb.Infrastructure.Data |
|
|
|
{ |
|
|
|
_dbContext = dbContext; |
|
|
|
} |
|
|
|
|
|
|
|
public virtual T GetById(int id) |
|
|
|
{ |
|
|
|
return _dbContext.Set<T>().Find(id); |
|
|
|
} |
|
|
|
|
|
|
|
public T GetSingleBySpec(ISpecification<T> spec) |
|
|
|
{ |
|
|
|
return List(spec).FirstOrDefault(); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public virtual async Task<T> GetByIdAsync(int id) |
|
|
|
{ |
|
|
|
return await _dbContext.Set<T>().FindAsync(id); |
|
|
|
} |
|
|
|
|
|
|
|
public IEnumerable<T> ListAll() |
|
|
|
{ |
|
|
|
return _dbContext.Set<T>().AsEnumerable(); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public async Task<IReadOnlyList<T>> ListAllAsync() |
|
|
|
{ |
|
|
|
return await _dbContext.Set<T>().ToListAsync(); |
|
|
|
} |
|
|
|
|
|
|
|
public IEnumerable<T> List(ISpecification<T> spec) |
|
|
|
{ |
|
|
|
return ApplySpecification(spec).AsEnumerable(); |
|
|
|
} |
|
|
|
public async Task<IReadOnlyList<T>> ListAsync(ISpecification<T> spec) |
|
|
|
{ |
|
|
|
return await ApplySpecification(spec).ToListAsync(); |
|
|
|
} |
|
|
|
|
|
|
|
public int Count(ISpecification<T> spec) |
|
|
|
{ |
|
|
|
return ApplySpecification(spec).Count(); |
|
|
|
} |
|
|
|
|
|
|
|
public async Task<int> CountAsync(ISpecification<T> spec) |
|
|
|
{ |
|
|
|
return await ApplySpecification(spec).CountAsync(); |
|
|
|
} |
|
|
|
|
|
|
|
public T Add(T entity) |
|
|
|
{ |
|
|
|
_dbContext.Set<T>().Add(entity); |
|
|
|
_dbContext.SaveChanges(); |
|
|
|
|
|
|
|
return entity; |
|
|
|
} |
|
|
|
|
|
|
|
public async Task<T> AddAsync(T entity) |
|
|
|
{ |
|
|
|
_dbContext.Set<T>().Add(entity); |
|
|
|
@ -80,24 +48,12 @@ namespace Microsoft.eShopWeb.Infrastructure.Data |
|
|
|
|
|
|
|
return entity; |
|
|
|
} |
|
|
|
|
|
|
|
public void Update(T entity) |
|
|
|
{ |
|
|
|
_dbContext.Entry(entity).State = EntityState.Modified; |
|
|
|
_dbContext.SaveChanges(); |
|
|
|
} |
|
|
|
|
|
|
|
public async Task UpdateAsync(T entity) |
|
|
|
{ |
|
|
|
_dbContext.Entry(entity).State = EntityState.Modified; |
|
|
|
await _dbContext.SaveChangesAsync(); |
|
|
|
} |
|
|
|
|
|
|
|
public void Delete(T entity) |
|
|
|
{ |
|
|
|
_dbContext.Set<T>().Remove(entity); |
|
|
|
_dbContext.SaveChanges(); |
|
|
|
} |
|
|
|
|
|
|
|
public async Task DeleteAsync(T entity) |
|
|
|
{ |
|
|
|
|