I am using NHibernate with a NullObject pattern to make my views simpler. I use a solution found here by James Gregory
public Address GetAddressOrDefault()
{
return Address ?? new NullAddress();
}
And Person has an Address property and so on....
This is working very good until I try to save my Person object for a person without an address. Since I created a new instance of the NullPerson object, NHibernate will try to save it. And gives me this:
ids for this class must be manually assigned before calling save():
Is there any way I can get NHibernate to not try to save my NullObjects? Or is there any other way I should be attacking this?
I know you've probably considered this but simplest solution seems to be to the reverse of
ie, prior to your save call:
Or...You might consider using a dedicated class for your ViewModel if things get more complex and using AutoMapper to do the monkey work.