Skip to content

Instantly share code, notes, and snippets.

@vkhorikov
vkhorikov / Customer.cs
Last active October 16, 2019 23:57
With primitive obsession
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 August 11, 2018 10:52
Base entity class
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 August 22, 2019 12:27
You are naming your tests wrong
[MethodUnderTest]_[Scenario]_[ExpectedResult]
@vkhorikov
vkhorikov / 1.cs
Created July 2, 2019 02:47
Decorators for both commands and queries
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 June 6, 2019 18:28
Merging domain events
public sealed class Messages
{
private readonly IServiceProvider _provider;
public Messages(IServiceProvider provider)
{
_provider = provider;
}
public void Dispatch(IDomainEvent domainEvent)
@vkhorikov
vkhorikov / 1.cs
Last active April 15, 2019 21:35
CQRS and exception handling
public class EditPersonalInfoCommandHandler : ICommandHandler<EditPersonalInfoCommand>
{
public Result Handle(EditPersonalInfoCommand command)
{
for (int i = 0; ; i++)
{
try
{
var unitOfWork = new UnitOfWork(_sessionFactory);
Student student = unitOfWork.GetStudentById(command.Id);
@vkhorikov
vkhorikov / 1.cs
Last active February 20, 2019 16:12
When to validate commands in CQRS
public sealed class EnrollCommand : ICommand
{
public long StudentId { get; }
public string Course { get; }
public string Grade { get; }
public EnrollCommand(long studentId, string course, string grade)
{
if (course == null || grade == null) // Precondition checks
throw ArgumentException();
@vkhorikov
vkhorikov / 1.cs
Last active January 29, 2019 17:37
Are CQRS commands part of the domain model?
public IActionResult EditPersonalInfo([FromBody] EditPersonalInfoCommand command)
{
var handler = new EditPersonalInfoCommandHandler(_unitOfWork);
Result result = handler.Handle(command);
return result.IsSuccess ? Ok() : Error(result.Error);
}
@vkhorikov
vkhorikov / 1.cs
Last active December 24, 2018 16:05
Hierarchy of value objects
public class Person : Entity
{
public virtual string Name { get; set; }
public virtual Document Document { get; set; }
}
public class Document : Entity
{
}
public abstract class Error
{
public abstract ErrorType Type { get; }
}
public class SimpleError : Error
{
public override ErrorType Type { get { return ErrorType.Simple; } }
public string Message { get; private set; }