0

Nhibernate has a nice feature, which I have discovered coincidentally:

public interface IInterface {}

public class Impl1 : IInterface {}

public class Impl2 : IInterface {}

ISession session = sf.OpenSession();
session.QueryOver<IInterface>().List();

This will fetch me all Impl1 ans Impl2 objects (in case those classes are mapped). They need not be mapped as SubClassMaps, which leads me to the conclusion that NHibernate resolves the implementing classes all by itself.

Can anyone send me the link to documentation on this one? I know neither the name nor the technical background of this feature...

Thanks in advance!

Sebastian Edelmeier
  • 4,095
  • 3
  • 39
  • 60

1 Answers1

0

Actually, this is just the way NHibernate does inheritance mapping.

In addition to the usage you described, you also have the ability to i.e. define a child collection on an object, using the base type and put any object of inherited type to the collection. For instance, you could have another entity containing a collection of your IInterface objects:

public class MyEntity
{
    public IList<IInterface> MyCollection { get; set; }
}

Now you could put any object implementing IInterface into MyCollection, and NHibernate will persist them (if mapping is correct):

Impl1 i1 = new Impl1();
Impl2 i2 = new Impl2();
MyEntity entity = new MyEntity();
entity.MyCollection.Add(i1);
entity.MyCollection.Add(i2);

session.Save(entity);

However, the actual database usage (generated SQL) depends on the inheritance mapping strategy that you have defined, so get familiar with them first. You can read more in official documentation.

Miroslav Popovic
  • 12,100
  • 2
  • 35
  • 47