我有一个针对销售人员的存储库类,如下所示:
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的需求越来越大,而且我需要首先解决。
最佳答案
创建返回专业化形式的SalesPerson的专业化ReportingRepository
,或使用CQRS。