是否有可能使用NHibernate而不改变作为框架一部分的DDD模型 [英] Is it possible to use NHibernate without altering a DDD model that is part of a framework

查看:197
本文介绍了是否有可能使用NHibernate而不改变作为框架一部分的DDD模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我深入挖掘了很多有关 DDD方法(无处不在的语言,聚集,存储库等)的东西,我认为,与我读了很多东西相反,实体应该有行为,而不是不可知论者。我看到的所有示例都倾向于使用虚拟自动属性空构造函数(受保护或最差,公共)显示实体,就是这样。我认为这种类型的对象更像DTO,然后是实体。



我正在使用特定的API创建框架不希望被绑定到ORM 。所以我首先建立了域(不考虑持久性),现在我想使用NHibernate作为持久化工具,所以我添加了一个新的项目到我目前的解决方案,以帮助确保我的模型没有改变,以支持NHibernate。这个项目应该是我的域内的抽象存储库的一个实现。现在遇到了困难。
$ b 自从这是我第一次使用NHibernate (我也在试用Fluent Nhibernate,但它似乎更受限制)我想知道:


  1. 是否可以在不改变属于框架的DDD模型的情况下使用NHibernate

  2. NHibernate需要的东西(约束)如预期的那样高效地工作(虚拟属性,空的构造函数等等)我认为这个列表对于许多开始学习的人有帮助NHibernate。

请记住我正在构建一个框架,所以 Open / Closed Principle 对我来说非常重要。

英语不好,我来自蒙特利尔,我说法语。
$ b 编辑1:这里有一个问题,我现在与NHibernate -

解决方案

您的问题的简短答案是,这是不可能的,但如果不需要延迟加载所需的更改是微不足道的。
$ b

无论如何,您都将默认的构造函数添加到尚未拥有它们的类中。如果你愿意放弃延迟加载,这些默认的构造函数可以是私有的,而且你不必对你的域模型做任何其他的改变来使用NHibernate。

这非常接近持久性的无知。



话虽如此,如果你想懒加载,你需要做一些改变概述在这个问题的其他答案),以便NHibernate可以创建您的聚合实体的代理。我个人还在试图决定延迟加载是否是一种支持DDD的技术,或者是一个过早的优化,需要对我的POCO进行太多侵入性的修改。我倾向于前者,但我真的希望NHibernate可以配置使用特定的构造函数。

你也可以看看Davy Brion的博客(我特别喜欢使用NHibernate实现价值对象 ),如果你对域驱动设计感兴趣,并且避免贫血域模型,这真的很有启发性。

I dig a lot of things about the DDD approach (Ubiquitous language, Aggregates, Repositories, etc.) and I think that, contrary to what I read a lot, entities should have behavior rather then being agnostic. All examples I see tend to present entities with virtual automatic properties and an empty constructor (protected or worst, public) and that's it. I consider this kind of objects more like DTOs then entities.

I'm in the process of creating a framework with its specific API and I don't want to be tied to an ORM. So I built the domain first (without thinking of persistence) and now I would like to use NHibernate as persistence tool so I added a new project to my current solution to help ensure that my model isn't altered to support NHibernate. This project should be an implementation of the abstract repositories that live inside my domain. And now the difficulties arise.

Since it is my first time with NHibernate (I'm also trying Fluent Nhibernate but it seems even more restricting) I would like to know :

  1. Is it possible to use NHibernate without altering a DDD model that is part of a framework
  2. The things (constraints) that are necessary for NHibernate to work as expected and efficiently (virtual properties, empty constructors, etc.) I think this list would be helpful to a lot of people who are starting to learn NHibernate.

Please keep in mind that I'm building a framework so the Open/Closed Principle is very important for me.

P.S.: Sorry if my english is not good, I'm from Montreal and I speak french.

Edit 1: Here is one problem I have with NHibernate now - How to map Type with Nhibernate (and Fluent NHibernate)

解决方案

The short answer to your question is that it is not possible, but if don't need lazy loading the required alterations are trivial.

No matter what, you will have add default constructors to classes that do not already have them. If you are willing to forgo lazy-loading, those default constructors can be private, and you don't have to make any other changes to your domain model to use NHibernate.

That's awfully close to persistence ignorance.

Having said that, if you want lazy-loading, you'll need to make several changes (outlined in other answers to this question) so that NHibernate can create proxies of your aggregated entities. I'm personally still trying to decide whether lazy-loading is an enabling technology for DDD or if it's a premature optimization that requires too many intrusive changes to my POCOs. I'm leaning toward the former, though I really wish NHibernate could be configured to use a specific constructors.

You might also take a look at Davy Brion's blog (I particularly liked Implementing A Value Object With NHibernate), which is really illuminating if you're interested in domain-driven-design and avoiding anemic domain models.

这篇关于是否有可能使用NHibernate而不改变作为框架一部分的DDD模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆