2010-11-03 72 views
1

我正在使用类表继承实现类型层次结构。但是,我在返回基类型而不是子类型的静态方法时遇到了问题。我找到了解决办法,但它不太漂亮。看看下面的类使用Castle ActiveRecord类表继承在派生类上调用FindAll

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

调用

Person.FindAll(); 

实际返回的人[]实体[]代替。我可以通过在所有派生类中实现FindAll来解决这个问题,但是谁愿意这样做呢?我还能够创建一个基类,所有的类都是从派生并实现

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

,但我只是不喜欢的

Person.FindAll<Person>() 

一下有什么办法能够调用的FindAll ()从派生类,实际得到派生类而不是基类?

回答

4

这就是.net的工作原理:静态方法没有多态性。您已经找到了几个解决方法,另一个是而不是依靠这些从ActiveRecordBase<T>继承的静态方法,但是直接使用ActiveRecordMediator<T>

0

也许你可以这样做:

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

这样FindAll()将返回Person[]

+0

这个问题是你不能映射实体''作为它是一个开放的泛型类。 – 2010-11-03 23:03:42

+0

我之前使用过这种模式,其中实体将用于保存一些常用的值(例如:id)和方法,并作为所有实体的共同祖先。我不明白你为什么想要映射它。 – gcores 2010-11-03 23:09:39

+0

@gcores:所以你可以把实体的属性放在一个单独的类中。 – 2010-11-03 23:13:40

0

即使是Castle.ActiveRecord使用您找到解决方法的文档。

看到这里一个完整的例子和一些其他的解决方案:http://docs.castleproject.org/Default.aspx?Page=Type%20hierarchy&NS=Active%20Record

我复制的情况下,该网站消失的代码。

基类 “实体”

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); 
    } 
} 

派生类 “人” 和 “公司”

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); 
    } 
}