Created
May 20, 2012 01:11
-
-
Save eulerfx/2733065 to your computer and use it in GitHub Desktop.
Always valid user profile
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 UserProfile | |
{ | |
public UserProfile(string name) | |
{ | |
this.Name = name; | |
} | |
public string Name | |
{ | |
get { return _name; } | |
set | |
{ | |
if (string.IsNullOrEmpty(value)) | |
{ | |
throw new Exception("Name is required"); | |
} | |
_name = value; | |
} | |
} | |
} | |
// will throw exception | |
var profile = new UserProfile(null); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks! Good article.
I'm wondering if the customer comes with other business rules like : "the Name should start with 'A' " or "the name should contain letters" for instance. At that time i'll be obliged to come to this code and add another if statement. I'm wondering, if this this is not breaking the Open Close Principle?
If i try to add a kind of validator (i.e INameValidator) and discover all INameRule with the help of an IoC at runtime. Am I also deffering the validation of the entity? How can I solve the OCP in this case ?