Maksim Kondratyuk
Maksim Kondratyuk

Reputation: 1399

Place for validation

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 ?

Upvotes: 0

Views: 172

Answers (2)

Chad Moran
Chad Moran

Reputation: 12854

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.

Upvotes: 1

Martin Peck
Martin Peck

Reputation: 11564

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.

Upvotes: 1

Related Questions