NHibernate 和 NHibernate 验证

发布于 2024-12-06 01:43:07 字数 78 浏览 3 评论 0原文

是否可以在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

自此以后,行同陌路 2024-12-13 01:43:07

是否可以使用 Nhibernate 验证来验证域模型
框架...?

如果您确实有一个域模型,那么它不需要验证框架。换句话说,对象封装行为并保护其内部不变量,而不依赖于外部魔法验证框架。 中的域对象永远不会进入“无效”状态第一名。如果它们是长寿的,那么它们也应该是“永远持久的”。域对象的有效性不应依赖于数据访问库可能触发或不触发的事件。您还可以发现不考虑验证是有帮助的,因为它过于笼统且依赖于上下文,而是考虑业务对象 不变量。您不需要第三方框架来正确强制对象中的不变量。在不将域类耦合到验证框架的情况下实现它确实不难。

但如果你将你的问题改写为:

是否可以使用 Nhibernate 验证 贫血 域模型验证
框架...?

那么答案就是:是的,加油,太棒了!但请记住,随着复杂性的增加,您可能希望强制执行更复杂的域规则,涉及多个对象字段、单独的域服务等。您要么通过编写“自定义验证器”与验证框架越来越耦合,要么干脆放弃它最终得到框架实现的一些规则以及遍布代码库的其他规则。可能值得一看答案和一般的DDD。

Is it possible to validate domain model with Nhibernate validation
framework... ?

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:

Is it possible to validate anemic domain model with Nhibernate validation
framework... ?

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.

咿呀咿呀哟 2024-12-13 01:43:07

以下摘录自 http://nhforge.org/wikis /validator/nhibernate-validator-1-0-0-documentation.aspx

NHibernate 基于事件的验证

NHibernate Validator 有两个内置的 NHibernate 事件监听器。
每当 PreInsertEvent 或 PreUpdateEvent 发生时,侦听器将
验证实体实例的所有约束并抛出异常
如果其中任何一项被违反。基本上,对象会在之前被检查
任何插入以及由 NHibernate 触发的任何更新之前。这
包括级联更改!这是最方便、最简单的
激活验证过程的方法。如果违反约束,
该事件将引发运行时 InvalidStateException,其中包含
描述每个失败的 InvalidValues 数组。

Excerpt below taken from http://nhforge.org/wikis/validator/nhibernate-validator-1-0-0-documentation.aspx

NHibernate event-based validation

NHibernate Validator has two built-in NHibernate event listeners.
Whenever a PreInsertEvent or PreUpdateEvent occurs, the listeners will
verify all constraints of the entity instance and throw an exception
if any of them are violated. Basically, objects will be checked before
any insert and before any update triggered by NHibernate. This
includes cascading changes! This is the most convenient and easiest
way to activate the validation process. If a constraint is violated,
the event will raise a runtime InvalidStateException which contains an
array of InvalidValues describing each failure.

天气好吗我好吗 2024-12-13 01:43:07

这个怎么样?

using(transaction...)
{
    validationA();
    validationB();
    session.saveOrUpdate();(do some transaction)
}

how about this?

using(transaction...)
{
    validationA();
    validationB();
    session.saveOrUpdate();(do some transaction)
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文