Skip to content

Instantly share code, notes, and snippets.

View arleypadua's full-sized avatar
🤓
Learning

Arley Pádua arleypadua

🤓
Learning
View GitHub Profile
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;
public class EmployeeTestBuilder
{
private Employee _employee;
public EmployeeTestBuilder()
{
_employee = new Employee();
}
public EmployeeTestBuilder Default()
{
var builder = new EmployeeTestBuilder();
var employee = builder
.WithName("Samuel")
.HasAge(35)
.HasGrossSalaryOf(1000)
.Build();
employee.CalculateNetSalary().Should().Be(850);
@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; }
@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.");
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-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; }
@arleypadua
arleypadua / anemic-domain-model-encapsulated-constructor.cs
Last active August 21, 2018 17:54
anemic-domain-model-encapsulated-constructor
public static Order New(string orderId, string customerId, DateTime? creationTime = null)
{
if (string.IsNullOrWhiteSpace(orderId)) throw new ArgumentNullException(nameof(orderId));
if (string.IsNullOrWhiteSpace(customerId)) throw new ArgumentNullException(nameof(customerId));
return new Order
{
OrderId = orderId,
CustomerId = customerId,
CreationTime = creationTime ?? DateTime.UtcNow,
@arleypadua
arleypadua / order-item-domain-model.cs
Last active August 22, 2018 04:10
order-item-domain-model
public class OrderItem
{
private OrderItem() { }
public static OrderItem New(string productId, int quantity, decimal price, decimal discount = 0)
{
if (string.IsNullOrWhiteSpace(productId)) throw new ArgumentNullException(nameof(productId));
if (quantity <= 0) throw new ArgumentOutOfRangeException(nameof(quantity));
if (price <= 0) throw new ArgumentOutOfRangeException(nameof(price));
if (discount < 0) throw new ArgumentOutOfRangeException(nameof(discount));
@arleypadua
arleypadua / encapsulated-list.cs
Created September 1, 2018 11:46
encapsulating-list
private List<OrderItem> _items; // The original source is private and can only be modified by this class.
public IEnumerable<OrderItem> Items => _items.AsReadOnly(); // Exposes a readonly copy of order items