Reputation: 1037
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?
Upvotes: 1
Views: 940
Reputation: 20361
Even the documentation of Castle.ActiveRecord uses the workaround that you found.
See here for a full example and some other solutions: http://docs.castleproject.org/Default.aspx?Page=Type%20hierarchy&NS=Active%20Record
I copied the code in case that site disappears.
Base class "Entity"
using Castle.ActiveRecord;
[ActiveRecord("entity"), JoinedBase]
public class Entity : ActiveRecordBase
{
private int id;
private string name;
private string type;
public Entity()
{
}
[PrimaryKey]
private int Id
{
get { return id; }
set { id = value; }
}
[Property]
public string Name
{
get { return name; }
set { name = value; }
}
[Property]
public string Type
{
get { return type; }
set { type = value; }
}
public static void DeleteAll()
{
DeleteAll(typeof(Entity));
}
public static Entity[] FindAll()
{
return (Entity[]) FindAll(typeof(Entity));
}
public static Entity Find(int id)
{
return (Entity) FindByPrimaryKey(typeof(Entity), id);
}
}
Derived classes "Person" and "Company"
using Castle.ActiveRecord;
[ActiveRecord("entitycompany")]
public class CompanyEntity : Entity
{
private byte company_type;
private int comp_id;
[JoinedKey("comp_id")]
public int CompId
{
get { return comp_id; }
set { comp_id = value; }
}
[Property("company_type")]
public byte CompanyType
{
get { return company_type; }
set { company_type = value; }
}
public new static void DeleteAll()
{
DeleteAll(typeof(CompanyEntity));
}
public new static CompanyEntity[] FindAll()
{
return (CompanyEntity[]) FindAll(typeof(CompanyEntity));
}
public new static CompanyEntity Find(int id)
{
return (CompanyEntity) FindByPrimaryKey(typeof(CompanyEntity), id);
}
}
[ActiveRecord("entityperson")]
public class PersonEntity : Entity
{
private int person_id;
[JoinedKey]
public int Person_Id
{
get { return person_id; }
set { person_id = value; }
}
public new static void DeleteAll()
{
DeleteAll(typeof(PersonEntity));
}
public new static PersonEntity[] FindAll()
{
return (PersonEntity[]) FindAll(typeof(PersonEntity));
}
public new static PersonEntity Find(int id)
{
return (PersonEntity) FindByPrimaryKey(typeof(PersonEntity), id);
}
}
Upvotes: 0
Reputation: 12656
Maybe you could do:
public class Entity<T> : ActiveRecordBase<T> { }
public class Person : Entity<Person> {}
That way FindAll()
would return Person[]
Upvotes: 0
Reputation: 99730
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.
Upvotes: 4