使用STI和ActiveRecordBase<>具有完整的 FindAll

发布于 2024-08-31 10:00:42 字数 1276 浏览 6 评论 0 原文

是否可以使用单表继承的通用支持,并且仍然能够 FindAll 基类?

作为一个额外的问题,我是否能够使用 ActiveRecordLinqBase<>还有?我确实喜欢这些问题。

更多细节: 假设我定义了以下类:

public interface ICompany
{
    int ID { get; set; }
    string Name { get; set; }
}

[ActiveRecord("companies", 
  DiscriminatorColumn="type", 
  DiscriminatorType="String", 
  DiscriminatorValue="NA")]
public abstract class Company<T> : ActiveRecordBase<T>, ICompany
{
    [PrimaryKey]
    private int Id { get; set; }

    [Property]
    public String Name { get; set; }
}

[ActiveRecord(DiscriminatorValue="firm")]
public class Firm : Company<Firm>
{
    [Property]
    public string Description { get; set; }
}

[ActiveRecord(DiscriminatorValue="client")]
public class Client : Company<Client>
{
    [Property]
    public int ChargeRate { get; set; } 
}

这对于大多数情况都适用。我可以做这样的事情:

var x = Client.FindAll();

但有时我想要所有的公司。如果我没有使用泛型,我可以这样做:

var x = (Company[]) FindAll(Company);
Client a = (Client)x[0];
Firm b = (Firm)x[1];

有没有办法编写一个 FindAll 来返回 ICompany 的数组,然后可以将其类型转换为各自的类型?
比如:

var x = (ICompany[]) FindAll(Company<ICompany>);
Client a = (Client)x[0];

或者也许我正在实施的通用支持全错了?

Is it possible to use generic support with single table inheritance, and still be able to FindAll of the base class?

As a bonus question, will I be able to use ActiveRecordLinqBase<> as well? I do love those queries.

More detail:
Say I have the following classes defined:

public interface ICompany
{
    int ID { get; set; }
    string Name { get; set; }
}

[ActiveRecord("companies", 
  DiscriminatorColumn="type", 
  DiscriminatorType="String", 
  DiscriminatorValue="NA")]
public abstract class Company<T> : ActiveRecordBase<T>, ICompany
{
    [PrimaryKey]
    private int Id { get; set; }

    [Property]
    public String Name { get; set; }
}

[ActiveRecord(DiscriminatorValue="firm")]
public class Firm : Company<Firm>
{
    [Property]
    public string Description { get; set; }
}

[ActiveRecord(DiscriminatorValue="client")]
public class Client : Company<Client>
{
    [Property]
    public int ChargeRate { get; set; } 
}

This works fine for most cases. I can do things like:

var x = Client.FindAll();

But sometimes I want all of the companies. If I was not using generics I could do:

var x = (Company[]) FindAll(Company);
Client a = (Client)x[0];
Firm b = (Firm)x[1];

Is there a way to write a FindAll that returns an array of ICompany's that can then be typecast into their respective types?
Something like:

var x = (ICompany[]) FindAll(Company<ICompany>);
Client a = (Client)x[0];

Or maybe I am going about implementing the generic support all wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陪我终i 2024-09-07 10:00:42

怎么样:

[ActiveRecord("companies", 
  DiscriminatorColumn="type", 
  DiscriminatorType="String", 
  DiscriminatorValue="NA")]
public abstract class Company : ActiveRecordBase<Company>, ICompany {
    [PrimaryKey]
    private virtual int Id { get; set; }

    [Property]
    public virtual String Name { get; set; }
}

[ActiveRecord(DiscriminatorValue="firm")]
public class Firm : Company {
    [Property]
    public virtual string Description { get; set; }
}

[ActiveRecord(DiscriminatorValue="client")]
public class Client : Company {
    [Property]
    public virtual int ChargeRate { get; set; } 
}

var allClients = ActiveRecordMediator<Client>.FindAll();
var allCompanies = ActiveRecordMediator<Company>.FindAll(); // Gets all Companies (Firms and Clients). Same as Company.FindAll();

请注意,您不能只是将您的公司贬低为客户或公司,您需要使用适当的多态性或访问者。有关说明,请参阅

How about this:

[ActiveRecord("companies", 
  DiscriminatorColumn="type", 
  DiscriminatorType="String", 
  DiscriminatorValue="NA")]
public abstract class Company : ActiveRecordBase<Company>, ICompany {
    [PrimaryKey]
    private virtual int Id { get; set; }

    [Property]
    public virtual String Name { get; set; }
}

[ActiveRecord(DiscriminatorValue="firm")]
public class Firm : Company {
    [Property]
    public virtual string Description { get; set; }
}

[ActiveRecord(DiscriminatorValue="client")]
public class Client : Company {
    [Property]
    public virtual int ChargeRate { get; set; } 
}

var allClients = ActiveRecordMediator<Client>.FindAll();
var allCompanies = ActiveRecordMediator<Company>.FindAll(); // Gets all Companies (Firms and Clients). Same as Company.FindAll();

Note that you can't just downcast your Companies as Clients or Firms, you need to use proper polymorphism or a visitor. See this for an explanation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文