我有一个像这样的销售人员的 Repository 类:
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 需求越来越多,我需要首先解决这个问题.
最佳答案
创建一个专门的 ReportingRepository
返回一个专门形式的 SalesPerson,或者使用 CQRS .
关于design-patterns - 存储库类中的非 CRUD 操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48097687/