NHibernate 和 NHibernate 验证
是否可以在 PreTranctionCommint 事件中使用 Nhibernate 验证框架来验证域模型?如果可能的话我们如何编写这个事件?
Is it possible to validate domain model with Nhibernate validation framework in PreTranctionCommint event? If possible how we can write this event ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您确实有一个域模型,那么它不需要验证框架。换句话说,对象封装行为并保护其内部不变量,而不依赖于外部魔法验证框架。 中的域对象永远不会进入“无效”状态第一名。如果它们是长寿的,那么它们也应该是“永远持久的”。域对象的有效性不应依赖于数据访问库可能触发或不触发的事件。您还可以发现不考虑验证是有帮助的,因为它过于笼统且依赖于上下文,而是考虑业务对象 不变量。您不需要第三方框架来正确强制对象中的不变量。在不将域类耦合到验证框架的情况下实现它确实不难。
但如果你将你的问题改写为:
那么答案就是:是的,加油,太棒了!但请记住,随着复杂性的增加,您可能希望强制执行更复杂的域规则,涉及多个对象字段、单独的域服务等。您要么通过编写“自定义验证器”与验证框架越来越耦合,要么干脆放弃它最终得到框架实现的一些规则以及遍布代码库的其他规则。可能值得一看答案和一般的DDD。
If you really have a domain model then it does not need validation framework. In other words the objects encapsulate behavior and protect their internal invariants without relying on external magic-validation framework. Domain objects never get into 'invalid' state in the first place. If they are long-lived then they should also be 'always persistable'. The validity of your domain objects should not rely on the event that may or may not be fired by data access library. You can also find it helpful to not think about VALIDATION because it is overgeneralized and context dependent but instead think about business object INVARIANTS. You don't need thirdparty framework to properly enforce invariants in your objects. It is really not hard to implement it without coupling your domain classes to validation framework.
But if you rephrase your question to:
Then the answer would be: Yes, go for it, its awesome! But keep in mind that as complexity grows you would want to enforce more complex domain rules involving multiple object fields, separate domain services etc. You will either get more and more coupled to validation framework by writing 'custom validators' or just give up on it and end up with some rules implemented by framework and other spread all over the code base. It might be worth looking at this answer and DDD in general.
以下摘录自 http://nhforge.org/wikis /validator/nhibernate-validator-1-0-0-documentation.aspx
Excerpt below taken from http://nhforge.org/wikis/validator/nhibernate-validator-1-0-0-documentation.aspx
这个怎么样?
how about this?