Skip to content

Instantly share code, notes, and snippets.

@romipetrelis
Last active March 23, 2017 17:56
Show Gist options
  • Save romipetrelis/c131734f5863dc80afecace483b0f0a5 to your computer and use it in GitHub Desktop.
Save romipetrelis/c131734f5863dc80afecace483b0f0a5 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
namespace Fluent
{
public class OrderBuilder
{
private readonly Order _order;
private OrderLine _currentLine;
private int _currentLineNumber;
public OrderBuilder()
{
_currentLineNumber = 0;
_order = new Order
{
Lines = new List<OrderLine>(),
BillingContact = new OrderContact(),
};
}
public OrderBuilder AddLine(string description, int quantity, decimal unitPrice)
{
return WithLine(description, quantity, unitPrice);
}
public OrderBuilder AndLine(string description, int quantity, decimal unitPrice)
{
return WithLine(description, quantity, unitPrice);
}
private OrderBuilder WithLine(string description, int quantity, decimal unitPrice)
{
_currentLineNumber++;
var toAdd = new OrderLine
{
Number = _currentLineNumber,
Description = description,
ExtendedPrice = quantity*unitPrice,
Quantity = quantity,
UnitPrice = unitPrice,
AddOns = new List<OrderLineAddOn>()
};
_order.Lines.Add(toAdd);
_currentLine = toAdd;
return this;
}
public OrderBuilder WithAddOn(string description, int quantity, decimal unitPrice)
{
var toAdd = new OrderLineAddOn
{
Description = description,
Quantity = quantity,
UnitPrice = unitPrice,
ExtendedPrice = quantity*unitPrice
};
_currentLine.AddOns.Add(toAdd);
return this;
}
public OrderBuilder WithOrderCode(string value)
{
_order.Code = value;
return this;
}
public OrderBuilder WithBillingContact(Action<OrderContactBuilder> contact)
{
var builder = new OrderContactBuilder();
contact(builder);
_order.BillingContact = builder.Build();
return this;
}
public OrderBuilder WithShippingContact(Action<OrderContactBuilder> contact)
{
var builder = new OrderContactBuilder();
contact(builder);
_order.ShippingContact = builder.Build();
return this;
}
public Order Build()
{
return _order;
}
}
public class OrderContactBuilder
{
private readonly OrderContact _contact;
public OrderContactBuilder()
{
_contact = new OrderContact();
}
public OrderContactBuilder FirstName(string value)
{
_contact.FirstName = value;
return this;
}
public OrderContactBuilder LastName(string value)
{
_contact.LastName = value;
return this;
}
public OrderContactBuilder Address1(string value)
{
_contact.Address1 = value;
return this;
}
public OrderContactBuilder Address2(string value)
{
_contact.Address2 = value;
return this;
}
public OrderContactBuilder City(string value)
{
_contact.City = value;
return this;
}
public OrderContactBuilder State(string value)
{
_contact.State = value;
return this;
}
public OrderContactBuilder ZipCode(string value)
{
_contact.ZipCode = value;
return this;
}
public OrderContactBuilder PhoneNumber(string value)
{
_contact.PhoneNumber = value;
return this;
}
public OrderContact Build()
{
return _contact;
}
}
public class Order
{
public string Code { get; set; }
public OrderContact BillingContact { get; set; }
public OrderContact ShippingContact { get; set; }
public IList<OrderLine> Lines { get; set; }
}
public class OrderLine
{
public int Number { get; set; }
public string Description { get; set; }
public int Quantity { get; set; }
public decimal UnitPrice { get; set; }
public decimal ExtendedPrice { get; set; }
public IList<OrderLineAddOn> AddOns { get; set; }
}
public class OrderLineAddOn
{
public string Description { get; set; }
public int Quantity { get; set; }
public decimal UnitPrice { get; set; }
public decimal ExtendedPrice { get; set; }
}
public class OrderContact
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string City { get; set; }
public string State { get; set; }
public string ZipCode { get; set; }
public string PhoneNumber { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment