2

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

UpTheCreek
  • 31,444
  • 34
  • 152
  • 221
Jideo
  • 51
  • 5

1 Answers1

0

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.

Arnis Lapsa
  • 45,880
  • 29
  • 115
  • 195
  • If you expose an IList, how do you handle the consumer modifying the collection directly? My understanding is that it's best practice to create a helper method to do that so you can handle the bi-directional relationship. – Mike Cole Mar 13 '11 at 00:16
  • @Mike I don't handle that. That's what I meant with sacrifice. I'm just making up convention not to do that. Might not work if You don't have control over consumers (easy for me - I even don't have a team in my project, I'm alone). What did You mean with helper methods handling bi-directional relationships? – Arnis Lapsa Mar 13 '11 at 00:22
  • @Mike how would You handle messing around with reflection and modifying private collection? :) – Arnis Lapsa Mar 13 '11 at 00:24
  • LOL, good point. I'd probably put some sort of alert so I could smack somebody upside the head if they did it. – Mike Cole Mar 13 '11 at 19:56