Search code examples
nhibernatefluent-nhibernatenhibernate-mappingnull-object-pattern

How can I make NHibernate aware of a first-class "Null Object", without database persistence?


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


Solution

  • 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."