sTodorov
sTodorov

Reputation: 5461

Validation in n-tier asp.net mvc applications

I am looking for some practical/theoretical information regarding best practices for validation in asp.net mvc n-tier applications.

I am working on a .Net application divided into the following layers:

UI -> Mvc3

BLL layer -> all business rules. Decoupled from data access and UI layers through interfaces

DAL layer -> Data access with the repository pattern, EF4 and pocos

Now, I am looking for a nice, clean and transparent way to specify my validation rules. Here are some thoughts on the matter so far:

UI validation should only be responsible for user input and its validity. BLL validation should be handling the validity of the data regarding the application business rules.

My main concern is how to bind the BLL and UI validation in the most efficient way. One think I am would like to avoid is having the UI check in a collection of validation and adding manually errors to the ModelState. Furthermore, I do not want to pass the ModelState to the BLL to be populated in there.

I will appreciate any thoughts on the matter.

P.S. Should this question be marked as a discussion ?

Upvotes: 2

Views: 1162

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038930

Your BLL layer could return validation errors under some standard form like for example a Dictionary<string, string> or some other custom built type. The controller receives a view model from the view so validation on this view model is invoked by the default model binder. If this validation passes next come the business rules. The view model is mapped to a model and passed to the service layer. If there are business errors the service would return a collection of errors which could be inserted into the ModelState from the controller using an extension method on the controller.

Upvotes: 2

Related Questions