Question

I am using Nhibernate and I have a problem when fetching a base class with multiple derived classes (each class mapping a different table). When I watch the request, Nhibernate joins on every derived tables which has a huge an impact on the performances...

Here is a simplified vision of my classes :

public class Animal{
    public virtual int ID { get; set;}
    public virtual string Name { get; set;}
}

public class Dog : Animal{
    //others properties
}

public class Cat: Animal{
    //others properties
}

public class Person{
    public virtual int ID { get; set;}
    public virtual IEnumerable<Animal> Animals { get; set;}
}

A person has a list of Animals and I just want their names. The example is not perfect and more it's more complicated (a banking program) but it reflect well my problematic. I KNOW it can be done differently etc, but it is a legacy so I don't have a choice...

Thanks in advance.

Was it helpful?

Solution 2

After all, I created a class AnimalBase which is inherited by Dog, Cat and so forth and a class Animal without any child (both having the interface IAnimal).

As in 95% of my request, I only need Animal, I reference this class in my other objects like Person.

Not perfect but I did not find anything better...

Thanks Firo for your help.

OTHER TIPS

IMO NHibernate will only joind tables which contain projected columns. define a query but do not return Person but project into a dto/anonymous class the properties you need

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top