Enforcing IAggregateRoot (#273)

- Enforcing it on the Async and EfRepositories
- Making CatalogBrand, Item, and Type AggregateRoots because they are
just lookup tables
This commit is contained in:
Eric Fleming
2019-07-11 09:31:18 -04:00
committed by Steve Smith
parent 4706682973
commit 7c092ba5ad
6 changed files with 14 additions and 54 deletions

View File

@@ -1,6 +1,8 @@
namespace Microsoft.eShopWeb.ApplicationCore.Entities
using Microsoft.eShopWeb.ApplicationCore.Interfaces;
namespace Microsoft.eShopWeb.ApplicationCore.Entities
{
public class CatalogBrand : BaseEntity
public class CatalogBrand : BaseEntity, IAggregateRoot
{
public string Brand { get; set; }
}

View File

@@ -1,6 +1,8 @@
namespace Microsoft.eShopWeb.ApplicationCore.Entities
using Microsoft.eShopWeb.ApplicationCore.Interfaces;
namespace Microsoft.eShopWeb.ApplicationCore.Entities
{
public class CatalogItem : BaseEntity
public class CatalogItem : BaseEntity, IAggregateRoot
{
public string Name { get; set; }
public string Description { get; set; }

View File

@@ -1,6 +1,8 @@
namespace Microsoft.eShopWeb.ApplicationCore.Entities
using Microsoft.eShopWeb.ApplicationCore.Interfaces;
namespace Microsoft.eShopWeb.ApplicationCore.Entities
{
public class CatalogType : BaseEntity
public class CatalogType : BaseEntity, IAggregateRoot
{
public string Type { get; set; }
}

View File

@@ -4,7 +4,7 @@ using System.Threading.Tasks;
namespace Microsoft.eShopWeb.ApplicationCore.Interfaces
{
public interface IAsyncRepository<T> where T : BaseEntity
public interface IAsyncRepository<T> where T : BaseEntity, IAggregateRoot
{
Task<T> GetByIdAsync(int id);
Task<IReadOnlyList<T>> ListAllAsync();

View File

@@ -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> : IAsyncRepository<T> where T : BaseEntity
public class EfRepository<T> : IAsyncRepository<T> where T : BaseEntity, IAggregateRoot
{
protected readonly CatalogContext _dbContext;

View File

@@ -1,46 +0,0 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.eShopWeb.ApplicationCore.Entities.BasketAggregate;
using Microsoft.eShopWeb.Infrastructure.Data;
using Microsoft.eShopWeb.UnitTests.Builders;
using System.Linq;
using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;
namespace Microsoft.eShopWeb.IntegrationTests.Repositories.BasketItemRepositoryTests
{
public class DeleteAsync_Should
{
private readonly CatalogContext _catalogContext;
private readonly EfRepository<Basket> _basketRepository;
private readonly EfRepository<BasketItem> _basketItemRepository;
private BasketBuilder BasketBuilder { get; } = new BasketBuilder();
private readonly ITestOutputHelper _output;
public DeleteAsync_Should(ITestOutputHelper output)
{
_output = output;
var dbOptions = new DbContextOptionsBuilder<CatalogContext>()
.UseInMemoryDatabase(databaseName: "TestCatalog")
.Options;
_catalogContext = new CatalogContext(dbOptions);
_basketRepository = new EfRepository<Basket>(_catalogContext);
_basketItemRepository = new EfRepository<BasketItem>(_catalogContext);
}
[Fact]
public async Task DeleteItemFromBasket()
{
var existingBasket = BasketBuilder.WithOneBasketItem();
_catalogContext.Add(existingBasket);
_catalogContext.SaveChanges();
await _basketItemRepository.DeleteAsync(existingBasket.Items.FirstOrDefault());
_catalogContext.SaveChanges();
var basketFromDB = await _basketRepository.GetByIdAsync(BasketBuilder.BasketId);
Assert.Equal(0, basketFromDB.Items.Count);
}
}
}