This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class EmployeeTestBuilder | |
{ | |
private Employee _employee; | |
public EmployeeTestBuilder() | |
{ | |
_employee = new Employee(); | |
} | |
public EmployeeTestBuilder Default() | |
{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var builder = new EmployeeTestBuilder(); | |
var employee = builder | |
.WithName("Samuel") | |
.HasAge(35) | |
.HasGrossSalaryOf(1000) | |
.Build(); | |
employee.CalculateNetSalary().Should().Be(850); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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."); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
OlderNewer