ASP.NET MVC 中的 DDD、视图模型和验证
我正在开发我的第一个 DDD 应用程序,并尝试遵循我在过去几个月中研究的一些基本规则。
我已经使用 Nhibernate 实现了存储库模式。
我以为我可以将我的实体从控制器“移动”到视图,但很快我就意识到这几乎是不可能的。
大多数人似乎更喜欢为每个视图定义特定的视图模型。
我并不特别喜欢重新定义我已经为实体创建的字段的想法,但似乎这是唯一的方法。
现在我面临的情况是我想附加一些验证规则。
我认为我可以将验证规则(使用 DataAnnotations)附加到实体,但如果我使用视图模型,它就无法工作。
以下是问题:
- 验证不应该是领域模型的一部分吗?
- 创建模型然后花时间在视图模型上重新映射相同的字段(属性)不是很耗时吗?
- 如果它至少没有验证规则,这不是一个贫血模型吗? 我开始怀疑 DDD 是否真的适合中小型应用程序。
我很感激任何帮助/建议。
I am developing my first DDD application and trying to follow some basic rules I've studied in these last few months.
I've implemented the repository pattern with Nhibernate.
I thought I could have "moved" my entities from the controller to the view, but soon I've realized it's almost impossible.
Most people seem to prefer to define a viewmodel specific for each view.
I don't particularly fancy the idea to redefine the fields I've already create for my entities but it seems that this is the only way.
Now I am facing the situation where I want to attach some validation rules.
I thought I could have attached the validation rules (with DataAnnotations) to the entities but it can't work if I am using a viewmodel.
Here are the questions:
- Shouldn't the validation be part of the domain model?
- Isn't it time consuming to create the model and then spend time to remap the same fields (properties) on the viewmodel?
- Isn't this an anemic model, if it doesn't have, at least, validation rules?
I am starting to wonder if DDD is really suitable for small/medium size application.
I appreciate any help/suggestion.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这个问题在这里已经被问了数百次,我也回答了数百次(所以这使你成为第一百个也是第一个问这个问题的人:-)):将用户验证逻辑放在你的视图模型上(诸如必填字段、日期时间格式之类的东西) ,...)并将业务验证逻辑放在您的实体上(例如用户名已被占用,用户无法再在您的网站上购买产品,因为他已达到最大配额,...)。
This has been asked hundreds of times here and I have answered it hundreds of times (so this makes you the hundredth and first person to ask this :-)): put user validation logic on your view models (things like required fields, datetime formats, ...) and put business validation logic on your entities (things like the username has already been taken, the user can no longer purchase products on your site because he has reached the maximum quota, ...).
我认为它应该在域模型和视图模型上。视图模型上的验证将检查类型 --datetime、decimal、int 等的有效输入,而域模型上的验证应检查特定于应用程序的规则。这样,即使您决定使用另一个 UI,业务验证仍然会存在,而 UI 将需要处理输入验证。
有一些工具可以帮助您完成此任务,例如 codeplex 上的 automapper。在我看来,这使得 BLL 和 UI 之间的分离更加清晰。
虽然这种方法总体上更耗时,但它也更具可扩展性。如果您的应用程序将来需要增长,那么这是设计架构的合理方法。
I think it should be on both the domain model and the viewmodels. The validation on the viewmodels will check for valid input as to type --datetime, decimal,int, etc whereas the validation on the domain model should check for rules specific to the application. In this way even if you decide to use another UI the business validation will still be in place, while the UI will need to take care of the input validation.
There are tools that can help you with that, for example, automapper on codeplex. In my opinion this results in cleaner separation between BLL and UI.
Whereas this approach is in overall more time consuming, it is also more scalable. If your application will need to grow in the future than this is a reasonable way to design the architecture.