Skip to content

Instantly share code, notes, and snippets.

@M0ns1gn0r
Last active September 12, 2017 08:52
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
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