Skip to content

Instantly share code, notes, and snippets.

@M0ns1gn0r
Last active September 12, 2017 08:52
Show Gist options
  • Save M0ns1gn0r/72dc43187418118c68d413d0eedb881d to your computer and use it in GitHub Desktop.
Save M0ns1gn0r/72dc43187418118c68d413d0eedb881d to your computer and use it in GitHub Desktop.
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