Skip to content

Commit

Permalink
Minor cleanup (dotnet-architecture#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
ardalis authored Oct 23, 2017
1 parent 16a6f83 commit 749a37d
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 38 deletions.
34 changes: 17 additions & 17 deletions src/ApplicationCore/Entities/Basket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,26 @@

namespace Microsoft.eShopWeb.ApplicationCore.Entities
{
public class Basket : BaseEntity
{
public string BuyerId { get; set; }
private readonly List<BasketItem> _items = new List<BasketItem>();
public IEnumerable<BasketItem> Items => _items.ToList();
public class Basket : BaseEntity
{
public string BuyerId { get; set; }
private readonly List<BasketItem> _items = new List<BasketItem>();
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,
UnitPrice = unitPrice
});
return;
}
var existingItem = Items.FirstOrDefault(i => i.CatalogItemId == catalogItemId);
existingItem.Quantity += quantity;
CatalogItemId = catalogItemId,
Quantity = quantity,
UnitPrice = unitPrice
});
return;
}
var existingItem = Items.FirstOrDefault(i => i.CatalogItemId == catalogItemId);
existingItem.Quantity += quantity;
}
}
}
10 changes: 0 additions & 10 deletions src/ApplicationCore/Entities/OrderAggregate/Address.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,5 @@ public Address(string street, string city, string state, string country, string
Country = country;
ZipCode = zipcode;
}

//protected override IEnumerable<object> GetAtomicValues()
//{
// yield return Street;
// yield return City;
// yield return State;
// yield return Country;
// yield return ZipCode;
//}

}
}
2 changes: 1 addition & 1 deletion src/ApplicationCore/Entities/OrderAggregate/Order.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public Order(string buyerId, Address shipToAddress, List<OrderItem> items)
// but only through the method Order.AddOrderItem() which includes behavior.
private readonly List<OrderItem> _orderItems = new List<OrderItem>();

public IReadOnlyCollection<OrderItem> OrderItems => _orderItems;
public IReadOnlyCollection<OrderItem> OrderItems => _orderItems.AsReadOnly();
// Using List<>.AsReadOnly()
// 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)
Expand Down
11 changes: 5 additions & 6 deletions src/Infrastructure/Data/CatalogContext.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Microsoft.EntityFrameworkCore;
using ApplicationCore.Entities.OrderAggregate;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.eShopWeb.ApplicationCore.Entities;
using ApplicationCore.Entities.OrderAggregate;

namespace Infrastructure.Data
{
Expand All @@ -11,10 +11,7 @@ public class CatalogContext : DbContext
public CatalogContext(DbContextOptions<CatalogContext> options) : base(options)
{
}
//public CatalogContext()
//{
// // required by migrations
//}

public DbSet<Basket> Baskets { get; set; }
public DbSet<CatalogItem> CatalogItems { get; set; }
public DbSet<CatalogBrand> CatalogBrands { get; set; }
Expand Down Expand Up @@ -95,10 +92,12 @@ private void ConfigureCatalogType(EntityTypeBuilder<CatalogType> builder)
.IsRequired()
.HasMaxLength(100);
}

private void ConfigureOrder(EntityTypeBuilder<Order> builder)
{
builder.OwnsOne(o => o.ShipToAddress);
}

private void ConfigureOrderItem(EntityTypeBuilder<OrderItem> builder)
{
builder.OwnsOne(i => i.ItemOrdered);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
using ApplicationCore.Entities.OrderAggregate;
using Infrastructure.Data;
using Infrastructure.Data;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using UnitTests.Builders;
using Xunit;
Expand Down

0 comments on commit 749a37d

Please sign in to comment.