Skip to content

Instantly share code, notes, and snippets.

@jmhdez
Created Jan 9, 2014
Embed
What would you like to do?
public static class Build
{
public static OrderBuilder Order(Cutomer customer)
{
return new OrderBuilder(customer);
}
public static CustomerBuilder Customer(string name)
{
return new CustomerBuilder(name);
}
// TODO: Builders para product
}
public static class CustomerBuilder
{
private readonly Customer customer;
public CustomerBuilder(string name)
{
var address = new Address("C/ Molino, 12", "Alcorcón", "Madrid", "28056");
customer = new Customer(Guid.New(), name, address);
}
public CustomerBuilder Preferred()
{
customer.MakePreferred();
}
public static implicit operator Customer(CustomerBuilder builder)
{
return builder.customer;
}
}
public static class OrderBuilder
{
private readonly Order order;
public OrderBuilder(Customer customer)
{
order = new Order(1000, customer, DateTime.Today);
}
public OrderBuilder AddOne(string productName, decimal price)
{
Product product = Build.Product(productName);
order.AddLine(product, 1, price);
return this;
}
public static implicit operator Order(OrderBuilder builder)
{
return builder.order;
}
}
public class Address
{
public Address(string street, string city, string region, string zipCode)
{
Check.Require(!string.IsNullOrWhiteSpace(street), "Street cannot be empty");
Check.Require(!string.IsNullOrWhiteSpace(city), "City cannot be empty");
Check.Require(!string.IsNullOrWhiteSpace(region), "Region cannot be empty");
Check.Require(!Regex.Matches(zipCode ?? "", @"\d{5}"), "ZipCode must be a valid Spanish ZipCode");
Street = street;
City = city;
Region = region;
ZipCode = zipCode;
}
public readonly string Street;
public readonly string City;
public readonly string Region;
public readonly string ZipCode;
// TODO: Override Equals & GetHashCode
}
public class Customer
{
private readonly Guid code;
private string name;
private Address address;
private bool isPreferred;
public Customer(Guid code, string name, Address address)
{
Check.Require(code != null, "Customer code cannot be null");
Check.Require(!string.IsNullOrWhiteSpace(name), "Customer name cannot be empty");
Check.Require(address != null, "Customer must have a valid address");
this.code = code;
this.name = name;
this.address = address;
}
public void MakePreferred()
{
isPreferred = true;
}
// Etc...
}
public class Product
{
private readonly string reference
private string name;
public Product(string reference, string name)
{
Check.Require(!string.IsNullOrWhiteSpace(referece), "Reference cannot be empty");
Check.Require(!string.IsNullOrWhiteSpace(name), "Name cannot be empty");
this.reference = referece;
this.name = name;
}
}
public class Order
{
private readonly int number;
private readonly DateTime date;
private readonly Customer customer;
private IList<OrderLine> lines;
public Order(int number, Customer customer, DateTime date)
{
Check.Require(number >= 0, "Order number must be greater or equal than zero");
Check.Require(customer != null, "Order must have a valid customer");
this.number = number;
this.customer = customer;
this.date = date;
this.lines = new List<OrderLine>();
}
public void AddLine(Product product, decimal quantity, decimal price)
{
Check.Require(product != null, "Cannot add order line without product");
var orderLine = new OrderLine(product.Reference, product.Name, quantity, price);
lines.Add(orderLine);
}
// Etc...
}
public class OrderLine
{
// TODO
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment