简体   繁体   English

将NHibernate集合过滤器与DDD集合一起使用

[英]Using NHibernate Collection Filters with DDD collections

I am trying to map a domain model in NHibernate. 我正在尝试在NHibernate中映射域模型。 The domain model is implemented with what I think is DDD style. 我认为是DDD样式来实现域模型。 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, ie nHibernate cannot work with this. 我的问题:是否可以在以这种方式实现的集合上使用nHibernate中的集合过滤器,或者我应该忘记它,即nHibernate无法与此配合使用。

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. 其他所有东西都起作用,我使用field.camelcase-underscore访问,因此nHibernate直接与该字段进行交互。 I've been working through the Hibernate in Action book, an now I'm in chapter 7 where it deals with collection filters. 我一直在研究《行动中的休眠》一书,而现在我在第7章中讨论集合过滤器。

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 吉德

If I recall correctly - NHibernate filter works as additional clause in sql queries to reduce returned rows from db. 如果我没记错的话-NHibernate过滤器可以作为sql查询中的附加子句来减少db返回的行。

My question to You is - why do You need that? 我对您的问题是-您为什么需要它?
I mean - how much addresses one person might have? 我的意思是-一个人可能有多少个地址? 1? 1个 5? 5? 10? 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. 我本人只是接受它作为NHibernate的牺牲(就像无参数的ctor和“虚拟”性一样),并在各处(使用私有设置者)使用暴露的IList只是为了降低技术复杂性。 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. 安全将随之而来。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM