Skip to content

Instantly share code, notes, and snippets.

@vkhorikov
vkhorikov / 1.cs
Last active September 8, 2018 23:13
In Defense of Lazy Loading
public class Student : Entity
{
public virtual string Name { get; set; }
public virtual decimal TotalDebt { get; set; }
public virtual IList<Enrollment> Enrollments { get; set; }
public virtual IList<SportsActivity> SportsActivities { get; set; }
}
public class Enrollment : Entity
@vkhorikov
vkhorikov / 1.cs
Last active May 10, 2018 03:29
Value Objects and Identity
public class PlanningTool
{
internal int Id { get; private set; }
// Rest of class
public ICollection<PlanningToolTab> Tabs { get; private set; }
}
public class PlanningToolTab
{
@vkhorikov
vkhorikov / 1.cs
Last active March 21, 2018 12:47
Non-determinism in tests
public class SomeService
{
public void FirePeriodicJob()
{
/* Runs asynchronously */
}
}
@vkhorikov
vkhorikov / 1.cs
Last active March 20, 2018 12:30
Overriding methods in classes-dependencies
public class StatisticsCalculator
{
public async Task<(double weightDelivered, double totalCost)> Calculate(int customerId)
{
List<DeliveryRecord> records = await GetDeliveries(customerId);
double weightDelivered = records.Sum(x => x.Weight);
double totalCost = records.Sum(x => x.Cost);
return (weightDelivered, totalCost);
@vkhorikov
vkhorikov / 1.cs
Last active March 19, 2018 13:14
Code pollution
public class OrderRepository
{
public void Save(Order order)
{
/* ... */
}
public Order GetById(long id)
{
/* ... */
@vkhorikov
vkhorikov / 1.cs
Last active February 16, 2018 15:53
Code Review: Value Objects and Error Messages
public class AllowanceAmount : ValueObject
{
public const int MaxLength = 50;
public string Value { get; }
private AllowanceAmount(string value)
{
Value = value;
}
@vkhorikov
vkhorikov / 1.cs
Last active February 15, 2018 02:25
Using Value Objects to represent technical concerns
public class Customer
{
public string Name { get; set; }
public string Email { get; set; }
}
Customer customer = GetCustomer();
customer.Name = customer.Email; // Compiles perfectly
@vkhorikov
vkhorikov / 1.cs
Last active January 30, 2018 15:53
Leaking domain knowledge to tests
public static class Calculator
{
public static int Add(int value1, int value2)
{
return value1 + value2;
}
}
@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()