I want to use some of predefined lookups without roundrips to Database in NHibernate.
Basically I would like to have code like this:
public class Countries
{
static Countries() {
Australia = new Country
{
Id = 14,
Description = "Australia"
}
}
public static Country Austrlia { get; protected set }
}
Then write this code snippets:
address.Country = Countries.Australia;// Snippet1
if (address.Country == Countries.Australia) { // Snippet2
// Do something
}
So I do override Equals, GetHashCode and even overload operators == and != for Country class lookup.
The Snippet1 works ONLY if the Country with given Id has not been loaded into memory. Otherwise it throws NonUniqueObjectException saying the object with given Id is already in memory.
To workaraound this I have to evict the loaded country and then assign the lookup value. This feels wrong and I'm not sure what will happen when the query for Country lookup will be executed again.
So the question is: How to maintain static lookup classes in NHibernate?
Thanks, Dmitriy.