Archive for June, 2009
(Portuguese) Controller, mas nem tanto
Sorry, this entry is only available in Portuguese.
(Portuguese) Scrum não menciona engenharia. E daí?
Sorry, this entry is only available in Portuguese.
(Portuguese) Novos objetivos
Sorry, this entry is only available in Portuguese.
Model validation specifications
Recently I designed an API concerned with model validation specifications.
The idea was to provide specifications for the design of my model objects.
The resulting code will show you how it works:
[TestInitialize]
public void setup()
{
ModelSpecifications.StartSpecificationsFor<User>();
}
[TestMethod]
public void should_have_an_unique_name_with_min_lenght_of_2_and_max_length_of_20()
{
"Name"
.NotNullOrEmpty()
.Length(2, 20)
.Unique();
}
[TestMethod]
public void should_have_an_unique_email_correctly_formatted()
{
"Email"
.NotNullOrEmpty()
.EmailFormatting()
.Unique();
}
The API is ensuring, under reflection, that the specified model type has the called validations correctly declared (the validation is performed upon attributes usage).
Note that performing validations on top of the attributes is not this API concern.
The underlying code is pretty simple.
ValidationEnsurer is the object who ensures the specified validations.
It asks the type under specification for validation attributes matching the specified validations.
The fluent interface is obtained with string extension methods.
These methods encapsulates callings to ValidationEnsurer and to my testing infrastructure:
public static string NotNullOrEmpty(this string propertyName)
{
var isValidating = new ValidationEnsurer(ModelSpecifications.TypeUnderSpec)
.IsValidating(new NotNullNotEmptyAttribute(), propertyName);
Assert.IsTrue(isValidating);
return propertyName;
}
Now I can ensure in my testing/specification fixtures the correct behavior for the validation of my model objects.
Maybe this could be helpful, so here is a demo project that you can use, customizing your own underlying code: source code.
(Portuguese) Building Domain Specific Languages in Boo – Review
Sorry, this entry is only available in Portuguese.