Skip to content

Instantly share code, notes, and snippets.

@eulerfx
Created May 20, 2012 01:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eulerfx/2733065 to your computer and use it in GitHub Desktop.
Save eulerfx/2733065 to your computer and use it in GitHub Desktop.
Always valid user profile
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);
@bes-slim
Copy link

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 ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment