Minor cleanup (#63)

This commit is contained in:
Steve Smith
2017-10-23 12:11:55 -04:00
committed by GitHub
parent 16a6f83ef4
commit 749a37d6d9
5 changed files with 24 additions and 38 deletions

View File

@@ -3,26 +3,26 @@ using System.Linq;
namespace Microsoft.eShopWeb.ApplicationCore.Entities namespace Microsoft.eShopWeb.ApplicationCore.Entities
{ {
public class Basket : BaseEntity public class Basket : BaseEntity
{ {
public string BuyerId { get; set; } public string BuyerId { get; set; }
private readonly List<BasketItem> _items = new List<BasketItem>(); private readonly List<BasketItem> _items = new List<BasketItem>();
public IEnumerable<BasketItem> Items => _items.ToList(); public IReadOnlyCollection<BasketItem> Items => _items.AsReadOnly();
public void AddItem(int catalogItemId, decimal unitPrice, int quantity = 1) public void AddItem(int catalogItemId, decimal unitPrice, int quantity = 1)
{
if (!Items.Any(i => i.CatalogItemId == catalogItemId))
{ {
if (!Items.Any(i => i.CatalogItemId == catalogItemId)) _items.Add(new BasketItem()
{ {
_items.Add(new BasketItem() CatalogItemId = catalogItemId,
{ Quantity = quantity,
CatalogItemId = catalogItemId, UnitPrice = unitPrice
Quantity = quantity, });
UnitPrice = unitPrice return;
});
return;
}
var existingItem = Items.FirstOrDefault(i => i.CatalogItemId == catalogItemId);
existingItem.Quantity += quantity;
} }
var existingItem = Items.FirstOrDefault(i => i.CatalogItemId == catalogItemId);
existingItem.Quantity += quantity;
} }
} }
}

View File

@@ -25,15 +25,5 @@ namespace ApplicationCore.Entities.OrderAggregate
Country = country; Country = country;
ZipCode = zipcode; ZipCode = zipcode;
} }
//protected override IEnumerable<object> GetAtomicValues()
//{
// yield return Street;
// yield return City;
// yield return State;
// yield return Country;
// yield return ZipCode;
//}
} }
} }

View File

@@ -28,7 +28,7 @@ namespace ApplicationCore.Entities.OrderAggregate
// but only through the method Order.AddOrderItem() which includes behavior. // but only through the method Order.AddOrderItem() which includes behavior.
private readonly List<OrderItem> _orderItems = new List<OrderItem>(); private readonly List<OrderItem> _orderItems = new List<OrderItem>();
public IReadOnlyCollection<OrderItem> OrderItems => _orderItems; public IReadOnlyCollection<OrderItem> OrderItems => _orderItems.AsReadOnly();
// Using List<>.AsReadOnly() // Using List<>.AsReadOnly()
// This will create a read only wrapper around the private list so is protected against "external updates". // This will create a read only wrapper around the private list so is protected against "external updates".
// It's much cheaper than .ToList() because it will not have to copy all items in a new collection. (Just one heap alloc for the wrapper instance) // It's much cheaper than .ToList() because it will not have to copy all items in a new collection. (Just one heap alloc for the wrapper instance)

View File

@@ -1,7 +1,7 @@
using Microsoft.EntityFrameworkCore; using ApplicationCore.Entities.OrderAggregate;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders; using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.eShopWeb.ApplicationCore.Entities; using Microsoft.eShopWeb.ApplicationCore.Entities;
using ApplicationCore.Entities.OrderAggregate;
namespace Infrastructure.Data namespace Infrastructure.Data
{ {
@@ -11,10 +11,7 @@ namespace Infrastructure.Data
public CatalogContext(DbContextOptions<CatalogContext> options) : base(options) public CatalogContext(DbContextOptions<CatalogContext> options) : base(options)
{ {
} }
//public CatalogContext()
//{
// // required by migrations
//}
public DbSet<Basket> Baskets { get; set; } public DbSet<Basket> Baskets { get; set; }
public DbSet<CatalogItem> CatalogItems { get; set; } public DbSet<CatalogItem> CatalogItems { get; set; }
public DbSet<CatalogBrand> CatalogBrands { get; set; } public DbSet<CatalogBrand> CatalogBrands { get; set; }
@@ -95,10 +92,12 @@ namespace Infrastructure.Data
.IsRequired() .IsRequired()
.HasMaxLength(100); .HasMaxLength(100);
} }
private void ConfigureOrder(EntityTypeBuilder<Order> builder) private void ConfigureOrder(EntityTypeBuilder<Order> builder)
{ {
builder.OwnsOne(o => o.ShipToAddress); builder.OwnsOne(o => o.ShipToAddress);
} }
private void ConfigureOrderItem(EntityTypeBuilder<OrderItem> builder) private void ConfigureOrderItem(EntityTypeBuilder<OrderItem> builder)
{ {
builder.OwnsOne(i => i.ItemOrdered); builder.OwnsOne(i => i.ItemOrdered);

View File

@@ -1,8 +1,5 @@
using ApplicationCore.Entities.OrderAggregate; using Infrastructure.Data;
using Infrastructure.Data;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using UnitTests.Builders; using UnitTests.Builders;
using Xunit; using Xunit;