Skip to content

Instantly share code, notes, and snippets.

@vkhorikov
vkhorikov / 1.cs
Last active December 2, 2017 14:50
.NET Value Type (struct) as a DDD Value Object
public struct Email
{
public string Value { get; }
public bool IsConfirmed { get; }
public Email(string value, bool isConfirmed)
{
Value = value;
IsConfirmed = isConfirmed;
}
@vkhorikov
vkhorikov / 1.cs
Last active November 1, 2017 12:52
Unit testing private methods
public class Customer
{
private CustomerStatus _status = CustomerStatus.Regular;
public void Promote()
{
_status = CustomerStatus.Preferred;
}
public decimal GetDiscount()
@vkhorikov
vkhorikov / 1.cs
Last active October 3, 2017 02:00
Domain events
public class OrderSubmitted : IDomainEvent
{
public Order Order { 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.cs
Last active August 5, 2017 13:55
Always valid vs not always valid domain model
public class Company
{
public void AssignDelivery(Delivery delivery)
{
if (!delivery.IsValid())
throw new Exception();
_deliveries.Add(delivery);
}
@vkhorikov
vkhorikov / 1.cs
Created July 13, 2017 12:15
Game Dev
export class Vector {
private _x: number;
private _y: number;
public getX(): number {
return this._x;
}
public getY(): number {
return this._y;
@vkhorikov
vkhorikov / 1.cs
Last active June 15, 2017 12:57
Value Objects: when to create one?
public struct DecimalGreaterThanZero : IEquatable<DecimalGreaterThanZero>
{
private readonly decimal _value;
public DecimalGreaterThanZero(decimal value)
{
if (CanCreate(value) == false)
throw new ArgumentException("Value must be greater than zero.", nameof(value));
_value = value;
@vkhorikov
vkhorikov / 1.cs
Last active June 6, 2017 23:44
On naming
public class FurnitureEntity : Entity { /* ... */ }
@vkhorikov
vkhorikov / 1.cs
Last active June 1, 2017 11:08
Code review: Fabric class
public class Fabric : AggregateRootWithLinkedObjects
{
public bool PreOrder { get; protected set; }
private string _Due { get; set; }
public Maybe<string> Due
{
get => _Due;
protected set => _Due = value.Unwrap();
}
@vkhorikov
vkhorikov / Fabric.cs
Created May 30, 2017 16:36
Source code for the 2nd code review
public class Fabric : AggregateRootWithLinkedObjects
{
public bool PreOrder { get; protected set; }
private string _Due { get; set; }
public Maybe<string> Due
{
get => _Due;
protected set => _Due = value.Unwrap();
}