将 NHibernate 集合过滤器与 DDD 集合结合使用
我正在尝试在 NHibernate 中映射域模型。领域模型是用我认为的 DDD 风格实现的。映射大部分工作正常,但是当我尝试在集合上使用集合过滤器时,我得到一个异常,其中显示:该集合未被引用。
我知道问题出在我如何实现该集合。我的问题:是否可以在 nHibernate 中对以这种方式实现的集合使用集合过滤器,或者我应该忘记它,即 nHibernate 无法使用它。
代码如下:
Person
{
IList<Address> _addresses = new List<Address>();
public string FirstName {get; set;}
...
public void addAddress(Address address)
{
// ... do some checks or validation
_addresses.Add(address);
}
public void removeAddress(Address address) {...}
public ReadOnlyCollection<Address> Addresses
{
get { return new ReadOnlyCollection<Address>(_addresses); }
}
}
主要问题是我不想公开公开内部地址集合。 其他一切都有效,我使用 field.camelcase-underscore 访问,以便 nHibernate 直接与该字段交互。我一直在阅读《Hibernate in Action》一书,现在正在读第 7 章,其中涉及集合过滤器。
有什么办法解决这个问题吗?我已经通过像这样公开内部集合来使其工作:
public ReadOnlyCollection<Address> Addresses
{
get { return _addresses; }
}
但我真的不想这样做。
非常感谢您的帮助。
吉德
I am trying to map a domain model in NHibernate. The domain model is implemented with what I think is DDD style. The mapping works mostly but then when I try to use a collection filter on an a collection I get an exception which says: The collection was unreferenced.
I know the problem comes from how I've implemented the collection. My question: Is it possible to use collection filters in nHibernate on collections implemented this way or should I just forget it, i.e. nHibernate cannot work with this.
The code is as follows:
Person
{
IList<Address> _addresses = new List<Address>();
public string FirstName {get; set;}
...
public void addAddress(Address address)
{
// ... do some checks or validation
_addresses.Add(address);
}
public void removeAddress(Address address) {...}
public ReadOnlyCollection<Address> Addresses
{
get { return new ReadOnlyCollection<Address>(_addresses); }
}
}
The main issue is that I don't want to expose the internal addresses collection publicly.
Every other thing works, I use the field.camelcase-underscore access so nHibernate interacts directly with the field. I've been working through the Hibernate in Action book, an now I'm in chapter 7 where it deals with collection filters.
Is there any way around this. I've got it to work by exposing the internal collection like this:
public ReadOnlyCollection<Address> Addresses
{
get { return _addresses; }
}
but I really dont want to do this.
Help would really be appreciated.
Jide
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我没记错的话 - NHibernate 过滤器作为 sql 查询中的附加子句来减少从数据库返回的行。
我问你的问题是 - 为什么你需要那个?
我的意思是 - 一个人可能有多少个地址? 1? 5? 10?
关于集合隔离...
我自己只是接受它作为 NHibernate 的牺牲(就像无参数 ctor 和“虚拟性”)并在任何地方使用公开的 IList(带有私有设置器)只是为了降低技术复杂性。它们的内容肯定可以从外部修改,但我只是不这样做。
保持代码易于理解比使其超级安全更重要。安全将随之而来。
If I recall correctly - NHibernate filter works as additional clause in sql queries to reduce returned rows from db.
My question to You is - why do You need that?
I mean - how much addresses one person might have? 1? 5? 10?
About collection isolation...
I myself just accept it as a sacrifice for NHibernate (just like argument-less ctor's and "virtual`ity") and use exposed IList everywhere (with private setters) just to reduce technical complexity. Their contents surely can be modified from outside, but I just don't do that.
It's more important to keep code easily understandable than making it super safe. Safety will follow.