Created
March 9, 2012 05:35
-
-
Save ijoyce/2005193 to your computer and use it in GitHub Desktop.
Primitives Considered Evil
This file contains hidden or 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
| using System; | |
| namespace ThePast | |
| { | |
| public class User | |
| { | |
| public string FirstName { get; set; } | |
| public string LastName { get; set; } | |
| public string EmailAddress { get; set; } | |
| public User(string firstName, string lastName, string emailAddress) | |
| { | |
| FirstName = firstName; | |
| LastName = lastName; | |
| EmailAddress = EmailAddress; | |
| } | |
| } | |
| } | |
| namespace TheFuture | |
| { | |
| public class MainClass | |
| { | |
| public static void Main (string[] args) | |
| { | |
| var user = new User(new FirstName("Ian"), new LastName("Joyce"), new EmailAddress("ian.joyce@gmail.com")); | |
| Console.WriteLine(user); | |
| // Change my name and email. | |
| var user2 = user.With(new EmailAddress("ian.joyce2@gmail.com")).With(new LastName("Joyce 2")); | |
| Console.WriteLine(user2); | |
| Console.ReadKey(); | |
| } | |
| } | |
| // Implementing some ideas from http://jessitron.blogspot.com/2012/03/strong-typing-in-java-religious.html in C# | |
| // Favor types and immutability. | |
| public class User | |
| { | |
| private readonly FirstName firstName; | |
| public FirstName FirstName { get { return firstName; } } | |
| private readonly LastName lastName; | |
| public LastName LastName { get { return lastName; } } | |
| private readonly EmailAddress emailAddress; | |
| public EmailAddress EmailAddress { get { return emailAddress; } } | |
| public User(FirstName firstName, LastName lastName, EmailAddress emailAddress) | |
| { | |
| this.firstName = firstName; | |
| this.lastName = lastName; | |
| this.emailAddress = emailAddress; | |
| } | |
| public User With(FirstName firstName) | |
| { | |
| return new User(firstName, LastName, EmailAddress); | |
| } | |
| public User With(LastName lastName) | |
| { | |
| return new User(FirstName, lastName, EmailAddress); | |
| } | |
| public User With(EmailAddress emailAddress) | |
| { | |
| return new User(FirstName, LastName, emailAddress); | |
| } | |
| public override string ToString() | |
| { | |
| return string.Format("{0} {1} ({2})", FirstName, LastName, EmailAddress); | |
| } | |
| } | |
| // These should override Equals and HashCode. | |
| public class FirstName | |
| { | |
| private readonly string @value; | |
| public string Value { get { return value; } } | |
| public FirstName(string @value) | |
| { | |
| this.value = @value; | |
| } | |
| public override string ToString() | |
| { | |
| return Value; | |
| } | |
| } | |
| public class LastName | |
| { | |
| private readonly string @value; | |
| public string Value { get { return value; } } | |
| public LastName(string @value) | |
| { | |
| this.value = @value; | |
| } | |
| public override string ToString() | |
| { | |
| return Value; | |
| } | |
| } | |
| public class EmailAddress | |
| { | |
| private readonly string @value; | |
| public string Value { get { return value; } } | |
| public EmailAddress(string @value) | |
| { | |
| this.value = @value; | |
| } | |
| public override string ToString() | |
| { | |
| return Value; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment