Using NHibernate Collection Filters with DDD Collections

I am trying to map a domain model to NHibernate. The domain model is implemented with what I consider to be DDD style. The mapping works mostly, but then when I try to use a collection filter on a collection, I get an exception that reads: no collection was found.

I know the problem has to do with how I implemented the collection. My question is, is it possible to use collection filters in nHibernate for collections implemented this way or should I just forget about it, i.e. NHibernate cannot work with this.

The code looks like this:

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 problem is that I don't want to publicly post the collection of internal addresses. All that works I am using access.camelcase-underscore, so nHibernate interacts directly with the field. I worked in the book Hibernate in Action, now I'm in chapter 7 where it deals with collection filters.

Is there any way to get around this. I need to work by opening an internal collection like this:

public ReadOnlyCollection<Address> Addresses 
{ 
   get { return _addresses; }
}

      

but I really don't want to do that.

Help would be really appreciated.

Jide

+2


source to share


1 answer


If I remember correctly - NHibernate filter works as an extra clause in sql queries to reduce the returned rows from db.

My question for you is why do you need this?
I mean, how many addresses can one person have? 1? five? ten?




About collection isolation ...

I myself accept this as a sacrifice for NHibernate (as well as no arguments, ctor and "virtuality"), and use nude ILists everywhere (with private setters) just to reduce technical complexity. Their content can of course be changed externally, but I just don't.

It is more important to make the code easy to understand than to make it super secure. Security will follow.

0


source







All Articles