【发布时间】:2018-06-14 07:50:12
【问题描述】:
我有一个类似这样的销售人员的存储库类:
public class SalesPersonRepository : ISalesPersonRepository
{
public int Add(SalesPerson salesPerson)
{
// Add the salesperson to the data store and return the Id
}
public void Update(int id, SalesPerson salesPerson)
{
// Update the sales person's record in the data store
}
public SalesPerson Get (int id)
{
// Fetch and return the salesperson's record
}
// a couple of other methods like GetAll and Delete
}
如您所见,我正在使用代表销售人员的 SalesPerson 类,在我的存储库类中,我正在使用与上述类似的标准 CRUD 操作。
但是,当我遇到这样的要求时:“为销售人员获取过去 30、60 和 90 天的销售数量”、“为销售人员获取客户数量”等,我不是确定我是否应该在 SalesPerson 对象中返回此数据。
我可以添加 SalesPerson.ThirtyDaySaleCount、SalesPerson.SixtyDaySaleCount、SalesPerson.NinetyDaySaleCount 等定义属性,并在存储库方法中分配它们,但显然,这似乎不是一个好的设计。
此外,当我为销售人员获取 30、60、90 天的销售计数时,我对 SalesPerson 类中定义的其他内容不感兴趣,例如名字、姓氏和其他信息。
那么我该如何返回这些特殊场景的数据呢?我应该为这些创建专门的类,还是应该继续将所有内容都放在 SalesPerson 类中?
PS:我知道 SalesPerson 是一个贫血对象,我可能应该考虑让它不贫血,但我最关心的是非 CRUD 需求正在大量涌现,我需要先解决这个问题.
【问题讨论】:
-
你为什么需要这些销售?仅用于在 UI 中读取/报告/显示,或者您还需要更新它们?
-
只是我的看法,虽然 ORM 对于简单的 CRUD 操作(有时)工作得相当好,但在涉及更复杂的报告类型查询时它们基本上很糟糕。所以是的,我会使用自定义方法来获取您的销售信息,而这反过来只会使用 sql 查询。
-
@ConstantinGalbenu 这些销售额是在 UI 上显示所必需的。
标签: design-patterns architecture domain-driven-design repository-pattern