Skip to content

Commit

Permalink
Update Specification and other packages to latest version (dotnet-arc…
Browse files Browse the repository at this point in the history
…hitecture#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
  • Loading branch information
ardalis authored Oct 25, 2021
1 parent fee2bbc commit 8a45a2c
Show file tree
Hide file tree
Showing 39 changed files with 281 additions and 289 deletions.
2 changes: 1 addition & 1 deletion src/ApplicationCore/ApplicationCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<ItemGroup>
<PackageReference Include="Ardalis.GuardClauses" Version="3.0.1" />
<PackageReference Include="Ardalis.Specification" Version="4.1.0" />
<PackageReference Include="Ardalis.Specification" Version="5.2.0" />
<PackageReference Include="MediatR" Version="9.0.0" />
<PackageReference Include="System.Security.Claims" Version="4.3.0" />
<PackageReference Include="System.Text.Json" Version="5.0.0" />
Expand Down
21 changes: 0 additions & 21 deletions src/ApplicationCore/Interfaces/IAsyncRepository.cs

This file was deleted.

8 changes: 4 additions & 4 deletions src/ApplicationCore/Interfaces/IOrderRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
namespace Microsoft.eShopWeb.ApplicationCore.Interfaces
{

public interface IOrderRepository : IAsyncRepository<Order>
{
Task<Order> GetByIdWithItemsAsync(int id);
}
//public interface IOrderRepository : IAsyncRepository<Order>
//{
// Task<Order> GetByIdWithItemsAsync(int id);
//}
}
8 changes: 8 additions & 0 deletions src/ApplicationCore/Interfaces/IReadRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using Ardalis.Specification;

namespace Microsoft.eShopWeb.ApplicationCore.Interfaces
{
public interface IReadRepository<T> : IReadRepositoryBase<T> where T : class, IAggregateRoot
{
}
}
8 changes: 8 additions & 0 deletions src/ApplicationCore/Interfaces/IRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using Ardalis.Specification;

namespace Microsoft.eShopWeb.ApplicationCore.Interfaces
{
public interface IRepository<T> : IRepositoryBase<T> where T : class, IAggregateRoot
{
}
}
12 changes: 6 additions & 6 deletions src/ApplicationCore/Services/BasketService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ namespace Microsoft.eShopWeb.ApplicationCore.Services
{
public class BasketService : IBasketService
{
private readonly IAsyncRepository<Basket> _basketRepository;
private readonly IRepository<Basket> _basketRepository;
private readonly IAppLogger<BasketService> _logger;

public BasketService(IAsyncRepository<Basket> basketRepository,
public BasketService(IRepository<Basket> basketRepository,
IAppLogger<BasketService> logger)
{
_basketRepository = basketRepository;
Expand All @@ -22,7 +22,7 @@ public BasketService(IAsyncRepository<Basket> basketRepository,
public async Task AddItemToBasket(int basketId, int catalogItemId, decimal price, int quantity = 1)
{
var basketSpec = new BasketWithItemsSpecification(basketId);
var basket = await _basketRepository.FirstOrDefaultAsync(basketSpec);
var basket = await _basketRepository.GetBySpecAsync(basketSpec);
Guard.Against.NullBasket(basketId, basket);

basket.AddItem(catalogItemId, price, quantity);
Expand All @@ -40,7 +40,7 @@ public async Task SetQuantities(int basketId, Dictionary<string, int> quantities
{
Guard.Against.Null(quantities, nameof(quantities));
var basketSpec = new BasketWithItemsSpecification(basketId);
var basket = await _basketRepository.FirstOrDefaultAsync(basketSpec);
var basket = await _basketRepository.GetBySpecAsync(basketSpec);
Guard.Against.NullBasket(basketId, basket);

foreach (var item in basket.Items)
Expand All @@ -60,10 +60,10 @@ public async Task TransferBasketAsync(string anonymousId, string userName)
Guard.Against.NullOrEmpty(anonymousId, nameof(anonymousId));
Guard.Against.NullOrEmpty(userName, nameof(userName));
var anonymousBasketSpec = new BasketWithItemsSpecification(anonymousId);
var anonymousBasket = await _basketRepository.FirstOrDefaultAsync(anonymousBasketSpec);
var anonymousBasket = await _basketRepository.GetBySpecAsync(anonymousBasketSpec);
if (anonymousBasket == null) return;
var userBasketSpec = new BasketWithItemsSpecification(userName);
var userBasket = await _basketRepository.FirstOrDefaultAsync(userBasketSpec);
var userBasket = await _basketRepository.GetBySpecAsync(userBasketSpec);
if (userBasket == null)
{
userBasket = new Basket(userName);
Expand Down
14 changes: 7 additions & 7 deletions src/ApplicationCore/Services/OrderService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ namespace Microsoft.eShopWeb.ApplicationCore.Services
{
public class OrderService : IOrderService
{
private readonly IAsyncRepository<Order> _orderRepository;
private readonly IRepository<Order> _orderRepository;
private readonly IUriComposer _uriComposer;
private readonly IAsyncRepository<Basket> _basketRepository;
private readonly IAsyncRepository<CatalogItem> _itemRepository;
private readonly IRepository<Basket> _basketRepository;
private readonly IRepository<CatalogItem> _itemRepository;

public OrderService(IAsyncRepository<Basket> basketRepository,
IAsyncRepository<CatalogItem> itemRepository,
IAsyncRepository<Order> orderRepository,
public OrderService(IRepository<Basket> basketRepository,
IRepository<CatalogItem> itemRepository,
IRepository<Order> orderRepository,
IUriComposer uriComposer)
{
_orderRepository = orderRepository;
Expand All @@ -30,7 +30,7 @@ public OrderService(IAsyncRepository<Basket> basketRepository,
public async Task CreateOrderAsync(int basketId, Address shippingAddress)
{
var basketSpec = new BasketWithItemsSpecification(basketId);
var basket = await _basketRepository.FirstOrDefaultAsync(basketSpec);
var basket = await _basketRepository.GetBySpecAsync(basketSpec);

Guard.Against.NullBasket(basketId, basket);
Guard.Against.EmptyBasketOnCheckout(basket.Items);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Microsoft.eShopWeb.ApplicationCore.Specifications
{
public sealed class BasketWithItemsSpecification : Specification<Basket>
public sealed class BasketWithItemsSpecification : Specification<Basket>, ISingleResultSpecification
{
public BasketWithItemsSpecification(int basketId)
{
Expand Down
16 changes: 16 additions & 0 deletions src/ApplicationCore/Specifications/OrderWithItemsByIdSpec.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Ardalis.Specification;
using Microsoft.eShopWeb.ApplicationCore.Entities.OrderAggregate;

namespace Microsoft.eShopWeb.ApplicationCore.Specifications
{
public class OrderWithItemsByIdSpec : Specification<Order>, ISingleResultSpecification
{
public OrderWithItemsByIdSpec(int orderId)
{
Query
.Where(order => order.Id == orderId)
.Include(o => o.OrderItems)
.ThenInclude(i => i.ItemOrdered);
}
}
}
1 change: 0 additions & 1 deletion src/BlazorAdmin/Services/HttpService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ public class HttpService
private readonly HttpClient _httpClient;
private readonly string _apiUrl;


public HttpService(HttpClient httpClient, BaseUrlConfiguration baseUrlConfiguration)
{
_httpClient = httpClient;
Expand Down
122 changes: 64 additions & 58 deletions src/Infrastructure/Data/EfRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,79 +10,85 @@

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);
// }
// }
}
26 changes: 13 additions & 13 deletions src/Infrastructure/Data/OrderRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@

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);
// }
//}
}
2 changes: 1 addition & 1 deletion src/Infrastructure/Infrastructure.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
Expand Down
6 changes: 3 additions & 3 deletions src/PublicApi/CatalogBrandEndpoints/List.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ public class List : BaseAsyncEndpoint
.WithoutRequest
.WithResponse<ListCatalogBrandsResponse>
{
private readonly IAsyncRepository<CatalogBrand> _catalogBrandRepository;
private readonly IRepository<CatalogBrand> _catalogBrandRepository;
private readonly IMapper _mapper;

public List(IAsyncRepository<CatalogBrand> catalogBrandRepository,
public List(IRepository<CatalogBrand> catalogBrandRepository,
IMapper mapper)
{
_catalogBrandRepository = catalogBrandRepository;
Expand All @@ -35,7 +35,7 @@ public override async Task<ActionResult<ListCatalogBrandsResponse>> HandleAsync(
{
var response = new ListCatalogBrandsResponse();

var items = await _catalogBrandRepository.ListAllAsync(cancellationToken);
var items = await _catalogBrandRepository.ListAsync(cancellationToken);

response.CatalogBrands.AddRange(items.Select(_mapper.Map<CatalogBrandDto>));

Expand Down
5 changes: 3 additions & 2 deletions src/PublicApi/CatalogItemEndpoints/Create.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ public class Create : BaseAsyncEndpoint
.WithRequest<CreateCatalogItemRequest>
.WithResponse<CreateCatalogItemResponse>
{
private readonly IAsyncRepository<CatalogItem> _itemRepository;
private readonly IRepository<CatalogItem> _itemRepository;
private readonly IUriComposer _uriComposer;

public Create(IAsyncRepository<CatalogItem> itemRepository, IUriComposer uriComposer)
public Create(IRepository<CatalogItem> itemRepository,
IUriComposer uriComposer)
{
_itemRepository = itemRepository;
_uriComposer = uriComposer;
Expand Down
Loading

0 comments on commit 8a45a2c

Please sign in to comment.