From 7c092ba5ad584c0f066dacda2ded9c51fa02caa9 Mon Sep 17 00:00:00 2001 From: Eric Fleming Date: Thu, 11 Jul 2019 09:31:18 -0400 Subject: [PATCH] Enforcing IAggregateRoot (#273) - Enforcing it on the Async and EfRepositories - Making CatalogBrand, Item, and Type AggregateRoots because they are just lookup tables --- src/ApplicationCore/Entities/CatalogBrand.cs | 6 ++- src/ApplicationCore/Entities/CatalogItem.cs | 6 ++- src/ApplicationCore/Entities/CatalogType.cs | 6 ++- .../Interfaces/IAsyncRepository.cs | 2 +- src/Infrastructure/Data/EfRepository.cs | 2 +- .../DeleteAsync_Should.cs | 46 ------------------- 6 files changed, 14 insertions(+), 54 deletions(-) delete mode 100644 tests/IntegrationTests/Repositories/BasketItemRepositoryTests/DeleteAsync_Should.cs diff --git a/src/ApplicationCore/Entities/CatalogBrand.cs b/src/ApplicationCore/Entities/CatalogBrand.cs index 1c90dfa..873a74a 100644 --- a/src/ApplicationCore/Entities/CatalogBrand.cs +++ b/src/ApplicationCore/Entities/CatalogBrand.cs @@ -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; } } diff --git a/src/ApplicationCore/Entities/CatalogItem.cs b/src/ApplicationCore/Entities/CatalogItem.cs index afed5d6..815be26 100644 --- a/src/ApplicationCore/Entities/CatalogItem.cs +++ b/src/ApplicationCore/Entities/CatalogItem.cs @@ -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; } diff --git a/src/ApplicationCore/Entities/CatalogType.cs b/src/ApplicationCore/Entities/CatalogType.cs index d1982a6..869ce8c 100644 --- a/src/ApplicationCore/Entities/CatalogType.cs +++ b/src/ApplicationCore/Entities/CatalogType.cs @@ -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; } } diff --git a/src/ApplicationCore/Interfaces/IAsyncRepository.cs b/src/ApplicationCore/Interfaces/IAsyncRepository.cs index 459f607..77a67ab 100644 --- a/src/ApplicationCore/Interfaces/IAsyncRepository.cs +++ b/src/ApplicationCore/Interfaces/IAsyncRepository.cs @@ -4,7 +4,7 @@ using System.Threading.Tasks; namespace Microsoft.eShopWeb.ApplicationCore.Interfaces { - public interface IAsyncRepository where T : BaseEntity + public interface IAsyncRepository where T : BaseEntity, IAggregateRoot { Task GetByIdAsync(int id); Task> ListAllAsync(); diff --git a/src/Infrastructure/Data/EfRepository.cs b/src/Infrastructure/Data/EfRepository.cs index 8688e14..db05a27 100644 --- a/src/Infrastructure/Data/EfRepository.cs +++ b/src/Infrastructure/Data/EfRepository.cs @@ -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/ /// /// - public class EfRepository : IAsyncRepository where T : BaseEntity + public class EfRepository : IAsyncRepository where T : BaseEntity, IAggregateRoot { protected readonly CatalogContext _dbContext; diff --git a/tests/IntegrationTests/Repositories/BasketItemRepositoryTests/DeleteAsync_Should.cs b/tests/IntegrationTests/Repositories/BasketItemRepositoryTests/DeleteAsync_Should.cs deleted file mode 100644 index 73774c2..0000000 --- a/tests/IntegrationTests/Repositories/BasketItemRepositoryTests/DeleteAsync_Should.cs +++ /dev/null @@ -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 _basketRepository; - private readonly EfRepository _basketItemRepository; - private BasketBuilder BasketBuilder { get; } = new BasketBuilder(); - private readonly ITestOutputHelper _output; - - public DeleteAsync_Should(ITestOutputHelper output) - { - _output = output; - var dbOptions = new DbContextOptionsBuilder() - .UseInMemoryDatabase(databaseName: "TestCatalog") - .Options; - _catalogContext = new CatalogContext(dbOptions); - _basketRepository = new EfRepository(_catalogContext); - _basketItemRepository = new EfRepository(_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); - } - } -}