I have ~40 classes, and they all implement an interface IEntity.
Of the ~40 classes, many use a “Many-To-One” mapping to an IEntity property. An example of this could be:
"IEntity.cs"
public interface IEntity
{
Guid ID
...Other properties (~7 of them)
}
"Item.cs"
public class Item : IEntity
{
Public Guid ID {get;set;}
...Other IEntity Properties
Public IEntity Owner {get;set;}
}
I believe to use NHibernate to map the Owner property on Item, I will need to use one of NHibernate’s inheritance mappings using IEntity as a base.
The issue is that configuration of the 2nd level cache is tied to a class definition, not to the subclasses. I would have to turn on caching on all ~40 classes in my model at one time, and I wouldn’t be able to just cache the classes I want, such as categories.
Does anyone know how I can map an entity that has a property that is the type of an abstract entity without losing the ability to make 2nd level cache choices?
firstly, I’m not sure I really get the idea of defining properties’ type as
IEntity– are you sure it’s okay forItemto be the owner of another item? is it okay for anEmployeeto be the owner of an item?If the answer is yes, and that’s the business logic of your system, then there’s nothing to do about this.
Otherwise- you may find it helpful to define an
IOwneror even aOwnerBasebase class to help with that distinction.This may also be helpful with ditinguishing which classes to cache and which not to.
And now to the caching question:
the definition of 2nd level cache needs to occur in two places:
1. The class’s mapping
2. The association mapping
in your case, I believe you would indeed have to turn on 2nd level cache for all the
IEntityclasses. But if you only turn on the caching for specific associations, then only those associations would be cached.(an exception to this is using
Get<T>()orLoad<T>()methods, which would also cause your objects to be cached. If I’m not mistaken, you can tell nHib not to use the cache for specific queries)