Skip to content

Instantly share code, notes, and snippets.

@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)
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 / 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 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 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 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 January 8, 2022 06:23
NHibernate Async
Customer customer = await session.GetAsync<Customer>(1);