Hello, I've been reading Pro ASP.NET MVC Framework, Steven Sanderson, and in chapter 11 it discusses data validation.
On page 390 we see the section Moving Validation Logic into Your Model Layer. In this section we see, in page 392, some code showing how to implement validation.
The code implements a GetRuleViolations()
method and the Save()
method uses it to throw a RuleException
if something was not alright.
It looks to me, however, that there's no distinction between the Domain Layer and a Data Access Layer, here's the code:
public void Save() {
var errors = GetRuleViolations();
if (errors.Count > 0)
throw new RuleException(errors);
// Todo: Now actually save to the database or whatever
}
private NameValueCollection GetRuleViolations() {
// validations...
}
In a project I'm working, I have a Domain layer, as persistence-ignorant as possible, and a Data Access layer, implementing data access through NHibernate, and implementing the repositories which interfaces were defined in the Domain layer.
If I implement the validation rules as the author proposes here, on the "Save()
" method, they would go on my Data Access layer, although, at least I think, they should reside on the domain model!
So, my question is: when creating a layered application, with a Domain layer implementing the domain entities and exposing interfaces to repositories (persistence ignorant), a Data Access layer implementing the repositories from the domain layer and implementing all the data access code, where should the validation rules reside?
My primary (or at least first) interface will be an ASP.NET MVC application, if that might change anything.
Thanks.