views:

174

answers:

2

I would like to make use of the Null Object pattern in my domain, but I don't want to have records in my database that relate to it - I would prefer it if NHibernate were able to map a SQL null value to my Null object, and vice versa.

Is this possible (using Fluent NHibernate for mappings)

P.S. This seems like it is a fairly common issue people are looking to resolve, but I wonder why I have struggled to find an answer.

Edit: Judging by this blog entry it doesn't look like it's going to be directly possible: NHibernate & Null Object Pattern: The Options

+1  A: 

In not fluent nhibernate you can use an Import mapping that will not persist to the database like this;

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
      namespace="MyProject.MiddleTier"    
      assembly="MyProject.MiddleTier">
   <import class="ThingNotToPersist"/>
</hibernate-mapping>

Don't know how this work in fluent but hopefully it gives you a starter.

Mark Dickinson
Thanks, I'm not quite sure how to apply this to the null object pattern though.
Hainesy
Hmm, sorry, I'm not really applying this pattern in the work I'm doing. I use this technique with an ICriteria to get me the latest object from a collection. In the query against a mapped persistent object I use a Projection to get me the latest object, then I use a Treansformer to convert my persistent object into a mapped non persistent equivalent. I can't really go into much more detail, but the help I got for doing this was on here, I'll post a link when I find it.
Mark Dickinson
Here it is http://stackoverflow.com/questions/747382/only-get-latest-results-using-nhibernate
Mark Dickinson
A: 

The answer is... you can't. Oren says "NHibernate's concept of null isn't something that you can easily change, and it doesn't go through an interceptor to do so. You could use null objects for value types (using UserType), but not for entities."

Hainesy