Last active
September 12, 2017 08:52
-
-
Save M0ns1gn0r/72dc43187418118c68d413d0eedb881d to your computer and use it in GitHub Desktop.
MotherFactory pattern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class PostCode | |
{ | |
public PostCode() {} | |
public PostCode(string postcode) {} | |
} | |
public class Address | |
{ | |
public Address(string street, string city, PostCode postCode) {} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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