我有一组 POCO 课程:
class ReportBase
{
public string Name { get; set; }
public int CustomerID { get; set; }
}
class PurchaseReport : ReportBase
{
public int NumberOfPurchases { get; set; }
public double TotalPurchases { get; set; }
public bool IsVip { get; set; }
}
class SaleReport : ReportBase
{
public int NumberOfSales { get; set; }
public double TotalSales { get; set; }
}
我有一个返回 ReportBase 的 web 方法。调用者使用返回值根据实际类型通过向下转换和检查类型(一个网格用于销售,一个网格用于购买)来更新 UI(WPF)。有人建议使用三个 web 方法,每个方法都返回特定的类型。
我知道通过引入 if/else,downcast 通常是违反设计原则的。相反,我们应该使用虚函数。但是在 POCO 类中,我们并没有真正的虚拟行为(只有额外的字段)。
在这种情况下,你是支持还是反对沮丧,为什么?