Search code examples
c#inheritancecastle-activerecord

Calling FindAll on derived classes using Castle ActiveRecord class table inheritance


I'm implementing a type hierarchy using class table inheritance. However, I'm having trouble with the static methods returning the base type instead of the child type. I've found a way around this but it's not too pretty. Take the following classes

public class Entity : ActiveRecordBase<Entity> { }
public class Person : Entity {}

calling

Person.FindAll();

actually returns an Entity[] instead of a Person[]. I can get around this by implementing FindAll in all derived classes, but who wants to do that? I was also able to create a base class that all classes derive from and implement

public R[] FindAll<R>() {}

but I just don't like the look of

Person.FindAll<Person>()

Is there any way to be able to call FindAll() from the derived classes and actually get the derived classes instead of the base class?


Solution

  • That's how .net works: there's no polymorphism for static methods. You already found a couple of workarounds, another one is not to rely on these static methods inherited from ActiveRecordBase<T>, but instead use ActiveRecordMediator<T> directly.