Skip to content

Instantly share code, notes, and snippets.

@vkhorikov
vkhorikov / CustomerController.cs
Last active May 4, 2024 01:25
Handling failures and input errors in a functional way
[HttpPost]
public HttpResponseMessage CreateCustomer(string name, string billingInfo)
{
Result<BillingInfo> billingInfoResult = BillingInfo.Create(billingInfo);
Result<CustomerName> customerNameResult = CustomerName.Create(name);
return Result.Combine(billingInfoResult, customerNameResult)
.OnSuccess(() => _paymentGateway.ChargeCommission(billingInfoResult.Value))
.OnSuccess(() => new Customer(customerNameResult.Value))
.OnSuccess(
public class Student : Entity
{
public string FirstName { get; private set; }
public string LastName { get; private set; }
public int FavoriteCourseId { get; private set; } // Foreign key
}
public class Course : Entity
{
public string Title { get; private set; }
@vkhorikov
vkhorikov / Customer.cs
Last active January 29, 2022 14:02
Without primitive obsession
public class Customer
{
public CustomerName Name { get; private set; }
public Email Email { get; private set; }
public Customer(CustomerName name, Email email)
{
if (name == null)
throw new ArgumentNullException("name");
if (email == null)
public void Create(string name, string email, string billingInfo)
{
Result<CustomerName> nameResult = CustomerName.Create(name);
Result<Email> emailResult = Email.Create(email);
Result<BillingInfo> billingInfoResult = BillingInfo.Create(billingInfo);
return Result.Combine(nameResult, emailResult, billingInfoResult)
.OnSuccess(() => _paymentGateway.ChargeCommission(billingInfoResult.Value));
/* Other OnSuccess, OnFailure, OnBoth methods */
}
@vkhorikov
vkhorikov / 1.cs
Last active January 8, 2022 06:23
NHibernate Async
Customer customer = await session.GetAsync<Customer>(1);
@vkhorikov
vkhorikov / full.cs
Last active April 4, 2021 10:19
Hierarchy of value objects - full code
public class Person : Entity
{
public virtual string Name { get; set; }
private readonly DocumentContainer _document;
public virtual Document Document
{
get => _document.Document;
set => _document.Document = value;
}
@vkhorikov
vkhorikov / EnumerationSample.cs
Created December 22, 2020 13:07
A sample implementation of the Enumeration pattern
public abstract class PoolType : ValueObject
{
public static readonly PoolType Quant = new QuantPoolType();
public static readonly PoolType Verbal = new VerbalPoolType();
public static readonly PoolType IR = new IRPoolType();
public static readonly PoolType Awa = new AwaPoolType();
public static readonly PoolType[] AllTypes = { Quant, Verbal, IR, Awa };
public abstract int Id { get; }
public abstract int Size { get; }
@vkhorikov
vkhorikov / 1.cs
Last active October 7, 2020 17:36
Value Object
public abstract class ValueObject<T>
where T : ValueObject<T>
{
public override bool Equals(object obj)
{
var valueObject = obj as T;
if (ReferenceEquals(valueObject, null))
return false;
@vkhorikov
vkhorikov / 1_Before.cs
Last active April 20, 2020 17:08
Representing data as code
public class Industry : Entity
{
public const string CarsIndustry = "Cars";
public const string PharmacyIndustry = "Pharmacy";
public const string MediaIndustry = "Media";
public string Name { get; private set; }
}
public class Customer : Entity
[Fact]
public void Parser_parses_xml_in_correct_order()
{
// Arrange : input values
string xml = "<outer><inner /></outer>";
var parser = new Parser();
// Arrange : record expectations
var mocks = new MockRepository();
IHandler handler = mocks.CreateMock<IHandler>();