Create a gist now

Instantly share code, notes, and snippets.

@M0ns1gn0r /Domain.cs
Last active Sep 12, 2017

What would you like to do?
MotherFactory pattern
public class PostCode
{
public PostCode() {}
public PostCode(string postcode) {}
}
public class Address
{
public Address(string street, string city, PostCode postCode) {}
}
// An anchor for extension methods.
public abstract class MotherFactory {}
public static class AddressMotherFactory
{
public static Address Address(
this MotherFactory a,
string street = "",
string city = "",
PostCode postCode = null)
{
postCode = postCode ?? a.PostCode();
return new Address(street, city, postCode);
}
}
public static class PostCodeMotherFactory
{
public static PostCode PostCode(this MotherFactory a, string postCode = "NL-1000")
{
return new PostCode(postCode);
}
public static PostCode EmptyPostCode(this MotherFactory a)
{
return new PostCode();
}
}
public class AddressTests
{
public static MotherFactory an;
[Fact]
public void Should_not_fail()
{
var address = an.Address(postCode: an.EmptyPostCode());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment