【问题标题】:Calling FindAll on derived classes using Castle ActiveRecord class table inheritance使用 Castle ActiveRecord 类表继承对派生类调用 FindAll
【发布时间】:2010-11-03 20:42:10
【问题描述】:

我正在使用类表继承来实现类型层次结构。但是,我遇到了返回基类型而不是子类型的静态方法的问题。我找到了解决这个问题的方法,但它不太漂亮。参加以下课程

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

打电话

Person.FindAll();

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

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

但我就是不喜欢这个样子

Person.FindAll<Person>()

有什么方法可以从派生类调用 FindAll() 并实际获取派生类而不是基类?

【问题讨论】:

    标签: c# inheritance castle-activerecord


    【解决方案1】:

    .net 就是这样工作的:静态方法没有多态性。您已经找到了几种解决方法,另一种方法是依赖从ActiveRecordBase&lt;T&gt; 继承的这些静态方法,而是直接使用ActiveRecordMediator&lt;T&gt;

    【讨论】:

      【解决方案2】:

      也许你可以这样做:

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

      这样FindAll() 将返回Person[]

      【讨论】:

      • 这个问题是你不能映射Entity&lt;T&gt;,因为它是一个开放的泛型类。
      • 我以前使用过这种模式,其中 Entity 用于保存一些常见的值(例如:id)和方法,并作为所有实体的共同祖先。我不明白你为什么要映射它。
      • @gcores: 所以你可以把Entity的属性放在一个单独的类中。
      • @Mauricio Scheffer 抱歉我没听懂,也许你可以扩展你的意思?
      • @gcores: 对不起,我的意思是:所以你可以把 Entity 的属性放在一个单独的 table
      【解决方案3】:

      即使是 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);
          }
      }
      

      派生类“Person”和“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);
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-10-20
        • 1970-01-01
        • 2023-04-09
        • 2016-07-03
        • 1970-01-01
        • 2011-01-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多