Skip to content

Instantly share code, notes, and snippets.

@theunrepentantgeek
Created February 9, 2016 20:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theunrepentantgeek/9edfd036de4bf6b868f6 to your computer and use it in GitHub Desktop.
Save theunrepentantgeek/9edfd036de4bf6b868f6 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Immutable
{
public struct PartyName
{
public string Name { get; }
public PartyName(string name)
{
Name = name;
}
}
public struct PartyCode
{
public string Code { get; }
public PartyCode(string code)
{
Code = code;
}
}
public abstract class Party<T>
where T : Party<T>
{
public PartyName Name { get; }
public PartyCode Code { get; }
public abstract T WithName(PartyName name);
public abstract T WithCode(PartyCode code);
protected Party(PartyName name, PartyCode code)
{
Name = name;
Code = code;
}
protected Party(Party<T> original)
{
Name = original.Name;
Code = original.Code;
}
protected Party(Party<T> original, PartyName name)
{
Name = name;
Code = original.Code;
}
protected Party(Party<T> original, PartyCode code)
{
Name = original.Name;
Code = code;
}
}
public struct PersonAge
{
public int Age { get; }
public PersonAge(int age)
{
Age = age;
}
}
public class Person : Party<Person>
{
public PersonAge Age { get; }
public Person(PartyName name, PartyCode code, PersonAge age)
: base(name, code)
{
Age = age;
}
public Person WithAge(PersonAge age)
{
return new Person(this, age);
}
public override Person WithName(PartyName name)
{
return new Person(this, name);
}
public override Person WithCode(PartyCode code)
{
return new Person(this, code);
}
protected Person(Person original, PartyName name)
: base(original, name)
{
Age = original.Age;
}
protected Person(Person original, PartyCode code)
: base(original, code)
{
Age = original.Age;
}
protected Person(Person original, PersonAge age)
: base(original)
{
Age = age;
}
}
public struct CompanyNumber
{
public int Number { get; }
public CompanyNumber(int number)
{
Number = number;
}
}
public class Company : Party<Company>
{
public CompanyNumber CompanyNumber { get; }
public Company(PartyName name, PartyCode code, CompanyNumber number)
: base(name, code)
{
CompanyNumber = number;
}
public Company WithAge(CompanyNumber number)
{
return new Company(this, number);
}
public override Company WithName(PartyName name)
{
return new Company(this, name);
}
public override Company WithCode(PartyCode code)
{
return new Company(this, code);
}
protected Company(Company original, PartyName name)
: base(original, name)
{
CompanyNumber = original.CompanyNumber;
}
protected Company(Company original, PartyCode code)
: base(original, code)
{
CompanyNumber = original.CompanyNumber;
}
protected Company(Company original, CompanyNumber number)
: base(original)
{
CompanyNumber = number;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment