views:

106

answers:

2

In my application i use this structure:

Controller -> Services -> Repositories

I create repositories + filters as dal layer. But i place validation methods (for required fields for example) in service layer. Is this correct? Or better will be if i replace validation in repository layer?

And second question. In this architecture services can operate with many repositories. Will be a good to allow services operate with other services or only i need to operate with repositories ?

+1  A: 

There's a very good tutorial on validation within MVC here...

http://stephenwalther.com/blog/archive/2009/03/04/new-asp.net-mvc-validation-tutorials-posted-at-www.asp.netmvc.aspx

You should take a look.

Martin Peck
Yeah, thanks i read this. I create Service layer validation, but now I have an idea to replace validation for business objects itself, and I don't know shoud i do that or not ? I don't know what is better 1) objects have to validate themselves or 2) Another layer have to validate objects.
Maksim Kondratyuk
In asp.net we have 1 variant - Page.Validate(), in this case easy to create cascading validation in heavy objects.
Maksim Kondratyuk
+1  A: 

There are a couple of libraries out there that can help you with validation.

The first (which I personally use) is FluentValidation that allows you to create validators for your model objects. It can be found at http://www.codeplex.com/FluentValidation

There's another library out there specifically for ASP.NET MVC applications that provides both server-side and client-side validation called xVal. http://www.codeplex.com/FluentValidation

I usually setup my projects the way you have mentioned and most of my logic and/or validation happens in the service layer and the repository layer is simply for queries.

Chad Moran