Skip to content

Instantly share code, notes, and snippets.

View arleypadua's full-sized avatar
🤓
Learning

Arley Pádua arleypadua

🤓
Learning
View GitHub Profile
@arleypadua
arleypadua / anemic-domain-model-private-setters.cs
Last active August 22, 2018 04:09
anemic-domain-model-private-setters
// We also want to encapsulate the way this class is instantiated.
private Order() { }
public string OrderId { get; private set; }
public string CustomerId { get; private set; }
public decimal Discount { get; private set; }
public bool Paid { get; private set; }
public DateTime? PaymentTime { get; private set; }
public DateTime CreationTime { get; private set; }
public List<OrderItem> Items { get; private set; }
public class Order
{
public string OrderId { get; set; }
public string CustomerId { get; set; }
public decimal Discount { get; set; }
public bool Paid { get; set; }
public DateTime? PaymentTime { get; set; }
public DateTime CreationTime { get; set; }
public List<OrderItem> Items { get; set; }
@arleypadua
arleypadua / anemic-domain-model-logic.cs
Last active August 21, 2018 16:42
anemic-domain-model-logic
public class OrderService
{
private readonly IOrderRepository _repository;
public void AddItem(string orderId, OrderItem item)
{
var order = _repository.GetById(orderId);
// Validate
if (order.Paid) throw new InvalidOperationException("The order is paid already.");
if (order.Items.Count > 5) throw new InvalidOperationException("Cannot add more than 5 items in an order.");
@arleypadua
arleypadua / anemic-domain-model.cs
Last active August 22, 2018 04:07
anemic-domain-model
public class Order
{
public string OrderId { get; set; }
public string CustomerId { get; set; }
public decimal Discount { get; set; }
public bool Paid { get; set; }
public DateTime? PaymentTime { get; set; }
public DateTime CreationTime { get; set; }
public List<OrderItem> Items { get; set; }
var builder = new EmployeeTestBuilder();
var employee = builder
.WithName("Samuel")
.HasAge(35)
.HasGrossSalaryOf(1000)
.Build();
employee.CalculateNetSalary().Should().Be(850);
public class EmployeeTestBuilder
{
private Employee _employee;
public EmployeeTestBuilder()
{
_employee = new Employee();
}
public EmployeeTestBuilder Default()
{
public class Employee
{
public string Name { get; set; }
public DateTime BirthDate { get; set; }
public double GrossSalary { get; set; }
public double CalculateNetSalary()
{
var age = DateTime.Now.Year - BirthDate.Year;