验证地点 [英] Place for validation
问题描述
在我的应用程序中,我使用这种结构:
In my application i use this structure:
控制器 -> 服务 -> 存储库
我创建存储库 + 过滤器作为 dal 层.但是我将验证方法(例如对于必填字段)放在服务层中.这样对吗?或者更好的是,如果我替换存储库层中的验证?
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 ?
推荐答案
有几个库可以帮助您进行验证.
There are a couple of libraries out there that can help you with validation.
第一个(我个人使用)是 FluentValidation,它允许您为模型对象创建验证器.它可以在 http://www.codeplex.com/FluentValidation
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
还有另一个专门用于 ASP.NET MVC 应用程序的库,它提供服务器端和客户端验证,称为 xVal.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.
这篇关于验证地点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!