Skip to content

Instantly share code, notes, and snippets.

Vladimir Khorikov vkhorikov

Block or report user

Report or block vkhorikov

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@vkhorikov
vkhorikov / CustomerController.cs
Last active Mar 28, 2020
Handling failures and input errors in a functional way
View CustomerController.cs
[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(
View 01.cs
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 Dec 10, 2019
Without primitive obsession
View Customer.cs
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)
View MocksVsStubs.cs
[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>();
@vkhorikov
vkhorikov / Customer.cs
Last active Oct 16, 2019
With primitive obsession
View Customer.cs
public class Customer
{
public string Name { get; private set; }
public string Email { get; private set; }
public Customer(string name, string email)
{
// Validate name
if (string.IsNullOrWhiteSpace(name) || name.Length > 50)
throw new ArgumentException("Name is invalid");
@vkhorikov
vkhorikov / 1.cs
Created Aug 11, 2018
Base entity class
View 1.cs
public abstract class Entity
{
public virtual long Id { get; protected set; }
protected virtual object Actual => this;
public override bool Equals(object obj)
{
var other = obj as Entity;
if (other is null)
@vkhorikov
vkhorikov / 1.cs
Last active Aug 22, 2019
You are naming your tests wrong
View 1.cs
[MethodUnderTest]_[Scenario]_[ExpectedResult]
@vkhorikov
vkhorikov / full.cs
Last active Jul 9, 2019
Hierarchy of value objects - full code
View full.cs
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 / 1.cs
Created Jul 2, 2019
Decorators for both commands and queries
View 1.cs
public sealed class DatabaseCommandRetryDecorator<TCommand> : DatabaseRetryDecorator, ICommandHandler<TCommand>
where TCommand : ICommand
{
private readonly ICommandHandler<TCommand> _handler;
public DatabaseCommandRetryDecorator(ICommandHandler<TCommand> handler, Config config)
: base(config)
{
_handler = handler;
}
@vkhorikov
vkhorikov / 1.cs
Last active Jun 6, 2019
Merging domain events
View 1.cs
public sealed class Messages
{
private readonly IServiceProvider _provider;
public Messages(IServiceProvider provider)
{
_provider = provider;
}
public void Dispatch(IDomainEvent domainEvent)
You can’t perform that action at this time.