当类和 EOM 模型位于单独的项目中时,MVC2 数据注释伙伴类似乎不起作用
我是 MVC2 的新手,在通过伙伴类实现验证时遇到了一些问题。 我正在使用带有依赖注入的存储库模式。 我的实体对象模型位于数据层项目中,伙伴类位于业务层项目中,而 MVC 2 项目作为单独的表示层项目。
任何人都可以帮助我在此架构中实现好友类吗?
I am new to MVC2 and having a little problem with implementing validation via buddy classes.
I am using Repository pattern with dependency injection.
My Entity Object Model is in Data Layer Project and Buddy Classes are in Business Layer project and MVC 2 Project as a separate Presentation Layer Project.
Can any one help me out with implementing Buddy Classes in this Architecture?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
部分类仅在同一程序集中工作。因此,您有两种可能性:在与模型类相同的程序集中定义验证规则,或者不使用 DataAnnotations。作为替代方案,您可以查看 FluentValidation ,它允许您在单独的程序集中定义验证规则,并且它具有与 ASP.NET MVC 也有很好的集成。
就您的 MVC 项目中的视图而言,它们不应该与您的模型类一起使用。您应该定义专门针对每个视图的需求定制的视图模型类。然后控制器将执行模型和视图模型之间的映射。 AutoMapper 可用于简化此任务。因此,基本上所有用户输入(例如必填字段和日期时间格式)都应该在服务层的视图模型和业务规则中进行验证。
Partial classes work only within the same assembly. So you have two possibilities: define validation rules in the same assembly as your model classes or don't use DataAnnotations. As an alternative you may take a look at FluentValidation allowing you to define validation rules in a separate assembly and which has a great integration with ASP.NET MVC as well.
As far as the views in your MVC project are concerned they should never work with your model classes. You should define view model classes which are specifically tailored to the needs of each view. The controller will then perform the mapping between the models and the view models. AutoMapper could be used to ease this task. So basically all user input like required fields and datetime formats should be validated at the view model and business rules at the service layer.