forked from dotnet-architecture/eShopOnWeb
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactoring and Adding Tests (dotnet-architecture#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
- Loading branch information
Showing
41 changed files
with
449 additions
and
360 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
src/ApplicationCore/Specifications/BasketWithItemsSpecification.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/ApplicationCore/Specifications/CatalogFilterSpecification.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
using ApplicationCore.Interfaces; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.eShopWeb.ApplicationCore.Entities; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace Infrastructure.Data | ||
{ | ||
public class EfRepository<T> : IRepository<T> where T : BaseEntity | ||
{ | ||
private readonly CatalogContext _dbContext; | ||
|
||
public EfRepository(CatalogContext dbContext) | ||
{ | ||
_dbContext = dbContext; | ||
} | ||
|
||
public T GetById(int id) | ||
{ | ||
return _dbContext.Set<T>().SingleOrDefault(e => e.Id == id); | ||
} | ||
|
||
public List<T> List() | ||
{ | ||
return _dbContext.Set<T>().ToList(); | ||
} | ||
|
||
public List<T> List(ISpecification<T> spec) | ||
{ | ||
var queryableResultWithIncludes = spec.Includes | ||
.Aggregate(_dbContext.Set<T>().AsQueryable(), | ||
(current, include) => current.Include(include)); | ||
return queryableResultWithIncludes | ||
.Where(spec.Criteria) | ||
.ToList(); | ||
} | ||
|
||
public T Add(T entity) | ||
{ | ||
_dbContext.Set<T>().Add(entity); | ||
_dbContext.SaveChanges(); | ||
|
||
return entity; | ||
} | ||
|
||
public void Delete(T entity) | ||
{ | ||
_dbContext.Set<T>().Remove(entity); | ||
_dbContext.SaveChanges(); | ||
} | ||
|
||
public void Update(T entity) | ||
{ | ||
_dbContext.Entry(entity).State = EntityState.Modified; | ||
_dbContext.SaveChanges(); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.