Update Specification and other packages to latest version (#582)
* Updating repositories and specification version Need to fix broken tests * removing test that would just be testing mocked result now * Refactored from IAsyncRepository and removed it. Tests pass. * Update packages
This commit is contained in:
@@ -10,79 +10,85 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace Microsoft.eShopWeb.Infrastructure.Data
|
||||
{
|
||||
public class EfRepository<T> : RepositoryBase<T>, IReadRepository<T>, IRepository<T> where T : class, IAggregateRoot
|
||||
{
|
||||
public EfRepository(CatalogContext dbContext) : base(dbContext)
|
||||
{
|
||||
}
|
||||
}
|
||||
/// <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 class EfRepository<T> : IAsyncRepository<T> where T : BaseEntity, IAggregateRoot
|
||||
// {
|
||||
// protected readonly CatalogContext _dbContext;
|
||||
|
||||
public EfRepository(CatalogContext dbContext)
|
||||
{
|
||||
_dbContext = 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 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>> 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<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<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, cancellationToken);
|
||||
await _dbContext.SaveChangesAsync(cancellationToken);
|
||||
// public async Task<T> AddAsync(T entity, CancellationToken cancellationToken = default)
|
||||
// {
|
||||
// await _dbContext.Set<T>().AddAsync(entity, cancellationToken);
|
||||
// await _dbContext.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return entity;
|
||||
}
|
||||
// return entity;
|
||||
// }
|
||||
|
||||
public async Task UpdateAsync(T entity, CancellationToken cancellationToken = default)
|
||||
{
|
||||
_dbContext.Entry(entity).State = EntityState.Modified;
|
||||
await _dbContext.SaveChangesAsync(cancellationToken);
|
||||
}
|
||||
// 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 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> 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);
|
||||
}
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
// private IQueryable<T> ApplySpecification(ISpecification<T> spec)
|
||||
// {
|
||||
// var evaluator = new SpecificationEvaluator<T>();
|
||||
// return evaluator.GetQuery(_dbContext.Set<T>().AsQueryable(), spec);
|
||||
// }
|
||||
// }
|
||||
}
|
||||
@@ -5,18 +5,18 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace Microsoft.eShopWeb.Infrastructure.Data
|
||||
{
|
||||
public class OrderRepository : EfRepository<Order>, IOrderRepository
|
||||
{
|
||||
public OrderRepository(CatalogContext dbContext) : base(dbContext)
|
||||
{
|
||||
}
|
||||
//public class OrderRepository : EfRepository<Order>, IOrderRepository
|
||||
//{
|
||||
// public OrderRepository(CatalogContext dbContext) : base(dbContext)
|
||||
// {
|
||||
// }
|
||||
|
||||
public Task<Order> GetByIdWithItemsAsync(int id)
|
||||
{
|
||||
return _dbContext.Orders
|
||||
.Include(o => o.OrderItems)
|
||||
.ThenInclude(i => i.ItemOrdered)
|
||||
.FirstOrDefaultAsync(x => x.Id == id);
|
||||
}
|
||||
}
|
||||
// public Task<Order> GetByIdWithItemsAsync(int id)
|
||||
// {
|
||||
// return _dbContext.Orders
|
||||
// .Include(o => o.OrderItems)
|
||||
// .ThenInclude(i => i.ItemOrdered)
|
||||
// .FirstOrDefaultAsync(x => x.Id == id);
|
||||
// }
|
||||
//}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Ardalis.Specification.EntityFrameworkCore" Version="4.1.0" />
|
||||
<PackageReference Include="Ardalis.Specification.EntityFrameworkCore" Version="5.2.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="5.0.10" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.11" />
|
||||
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.8.0" />
|
||||
|
||||
Reference in New Issue
Block a user