Refactoring and Adding Tests (#28)

* Introducing repository and refactoring services.
Changing entities to use int keys everywhere.

* Refactoring application services to live in web project and only reference repositories, not EF contexts.

* Cleaning up implementations

* Moving logic out of CatalogController
Moving entity knowledge out of viewmodels.

* Implementing specification includes better for catalogservice

* Cleaning up and adding specification unit tests
This commit is contained in:
Steve Smith
2017-08-07 13:25:11 -04:00
committed by GitHub
parent 084db74c77
commit d7eb59c097
41 changed files with 449 additions and 360 deletions

View File

@@ -1,7 +1,7 @@
namespace Microsoft.eShopWeb.ApplicationCore.Entities
{
public class BaseEntity<T>
public class BaseEntity
{
public T Id { get; set; }
public int Id { get; set; }
}
}

View File

@@ -3,25 +3,24 @@ using System.Linq;
namespace Microsoft.eShopWeb.ApplicationCore.Entities
{
public class Basket : BaseEntity<string>
public class Basket : BaseEntity
{
public string BuyerId { get; set; }
public List<BasketItem> Items { get; set; } = new List<BasketItem>();
public void AddItem(CatalogItem item, decimal unitPrice, int quantity = 1)
public void AddItem(int catalogItemId, decimal unitPrice, int quantity = 1)
{
if(!Items.Any(i => i.Item.Id == item.Id))
if (!Items.Any(i => i.CatalogItemId == catalogItemId))
{
Items.Add(new BasketItem()
{
Item = item,
//ProductId = productId,
CatalogItemId = catalogItemId,
Quantity = quantity,
UnitPrice = unitPrice
});
return;
}
var existingItem = Items.FirstOrDefault(i => i.Item.Id == item.Id);
var existingItem = Items.FirstOrDefault(i => i.CatalogItemId == catalogItemId);
existingItem.Quantity += quantity;
}
}

View File

@@ -1,10 +1,10 @@
namespace Microsoft.eShopWeb.ApplicationCore.Entities
{
public class BasketItem : BaseEntity<string>
public class BasketItem : BaseEntity
{
//public int ProductId { get; set; }
public decimal UnitPrice { get; set; }
public int Quantity { get; set; }
public CatalogItem Item { get; set; }
public int CatalogItemId { get; set; }
// public CatalogItem Item { get; set; }
}
}

View File

@@ -2,7 +2,7 @@
namespace Microsoft.eShopWeb.ApplicationCore.Entities
{
public class CatalogBrand : BaseEntity<int>
public class CatalogBrand : BaseEntity
{
public string Brand { get; set; }
}

View File

@@ -1,6 +1,6 @@
namespace Microsoft.eShopWeb.ApplicationCore.Entities
{
public class CatalogItem : BaseEntity<int>
public class CatalogItem : BaseEntity
{
public string Name { get; set; }
public string Description { get; set; }

View File

@@ -1,6 +1,6 @@
namespace Microsoft.eShopWeb.ApplicationCore.Entities
{
public class CatalogType : BaseEntity<int>
public class CatalogType : BaseEntity
{
public string Type { get; set; }
}

View File

@@ -2,6 +2,9 @@
namespace ApplicationCore.Exceptions
{
/// <summary>
/// Note: No longer required.
/// </summary>
public class CatalogImageMissingException : Exception
{
public CatalogImageMissingException(string message,
@@ -14,5 +17,13 @@ namespace ApplicationCore.Exceptions
innerException: innerException)
{
}
public CatalogImageMissingException() : base()
{
}
public CatalogImageMissingException(string message) : base(message)
{
}
}
}

View File

@@ -1,5 +1,9 @@
namespace ApplicationCore.Interfaces
{
/// <summary>
/// This type eliminates the need to depend directly on the ASP.NET Core logging types.
/// </summary>
/// <typeparam name="T"></typeparam>
public interface IAppLogger<T>
{
void LogWarning(string message, params object[] args);

View File

@@ -1,15 +0,0 @@
using Microsoft.eShopWeb.ApplicationCore.Entities;
using System.Threading.Tasks;
namespace ApplicationCore.Interfaces
{
public interface IBasketService
{
Task<Basket> GetBasket(string basketId);
Task<Basket> CreateBasket();
Task<Basket> CreateBasketForUser(string userId);
Task AddItemToBasket(Basket basket, int productId, int quantity);
//Task UpdateBasket(Basket basket);
}
}

View File

@@ -0,0 +1,18 @@
using Microsoft.eShopWeb.ApplicationCore.Entities;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
namespace ApplicationCore.Interfaces
{
public interface IRepository<T> where T : BaseEntity
{
T GetById(int id);
List<T> List();
List<T> List(ISpecification<T> spec);
T Add(T entity);
void Update(T entity);
void Delete(T entity);
}
}

View File

@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
namespace ApplicationCore.Interfaces
{
public interface ISpecification<T>
{
Expression<Func<T, bool>> Criteria { get; }
List<Expression<Func<T, object>>> Includes { get; }
void AddInclude(Expression<Func<T, object>> includeExpression);
}
}

View File

@@ -1,4 +1,7 @@
namespace ApplicationCore.Interfaces
using Microsoft.eShopWeb.ApplicationCore.Entities;
using System.Collections.Generic;
namespace ApplicationCore.Interfaces
{
public interface IUriComposer

View File

@@ -7,10 +7,8 @@ namespace ApplicationCore.Services
{
private readonly CatalogSettings _catalogSettings;
public UriComposer(CatalogSettings catalogSettings)
{
_catalogSettings = catalogSettings;
}
public UriComposer(CatalogSettings catalogSettings) => _catalogSettings = catalogSettings;
public string ComposePicUri(string uriTemplate)
{
return uriTemplate.Replace("http://catalogbaseurltobereplaced", _catalogSettings.CatalogBaseUrl);

View File

@@ -0,0 +1,28 @@
using ApplicationCore.Interfaces;
using Microsoft.eShopWeb.ApplicationCore.Entities;
using System;
using System.Linq.Expressions;
using System.Collections.Generic;
namespace ApplicationCore.Specifications
{
public class BasketWithItemsSpecification : ISpecification<Basket>
{
public BasketWithItemsSpecification(int basketId)
{
BasketId = basketId;
AddInclude(b => b.Items);
}
public int BasketId { get; }
public Expression<Func<Basket, bool>> Criteria => b => b.Id == BasketId;
public List<Expression<Func<Basket, object>>> Includes { get; } = new List<Expression<Func<Basket, object>>>();
public void AddInclude(Expression<Func<Basket, object>> includeExpression)
{
Includes.Add(includeExpression);
}
}
}

View File

@@ -0,0 +1,32 @@
using ApplicationCore.Interfaces;
using Microsoft.eShopWeb.ApplicationCore.Entities;
using System;
using System.Linq.Expressions;
using System.Collections.Generic;
namespace ApplicationCore.Specifications
{
public class CatalogFilterSpecification : ISpecification<CatalogItem>
{
public CatalogFilterSpecification(int? brandId, int? typeId)
{
BrandId = brandId;
TypeId = typeId;
}
public int? BrandId { get; }
public int? TypeId { get; }
public Expression<Func<CatalogItem, bool>> Criteria =>
i => (!BrandId.HasValue || i.CatalogBrandId == BrandId) &&
(!TypeId.HasValue || i.CatalogTypeId == TypeId);
public List<Expression<Func<CatalogItem, object>>> Includes { get; } = new List<Expression<Func<CatalogItem, object>>>();
public void AddInclude(Expression<Func<CatalogItem, object>> includeExpression)
{
Includes.Add(includeExpression);
}
}
}