我试图理解贫血领域模型以及为什么它们被认为是反模式。
这是一个现实世界的例子。
我有一个 Employee 类,它有大量的属性 - 姓名、性别、用户名等
public class Employee
{
public string Name { get; set; }
public string Gender { get; set; }
public string Username { get; set; }
// Etc.. mostly getters and setters
}
接下来,我们有一个系统,该系统涉及在销售人员之间均匀轮流来电和网站查询(称为“潜在客户”)。该系统相当复杂,因为它涉及循环查询、检查假期、员工偏好等。因此,该系统目前被分成一个服务:EmployeeLeadRotationService。
public class EmployeeLeadRotationService : IEmployeeLeadRotationService
{
private IEmployeeRepository _employeeRepository;
// ...plus lots of other injected repositories and services
public void SelectEmployee(ILead lead)
{
// Etc. lots of complex logic
}
}
然后在我们网站查询表格的背面有这样的代码:
public void SubmitForm()
{
var lead = CreateLeadFromFormInput();
var selectedEmployee = Kernel.Get<IEmployeeLeadRotationService>()
.SelectEmployee(lead);
Response.Write(employee.Name + " will handle your enquiry. Thanks.");
}
我并没有真正遇到这种方法的很多问题,但据说这是我应该尖叫的事情,因为它是一个贫血域模型.
但对我来说,不清楚领先轮换服务的逻辑应该去哪里。它应该领先吗?它应该放在员工身上吗?
轮换服务所需的所有注入存储库等怎么样?考虑到大多数时候我们在与员工打交道时不需要任何这些存储库,那么如何将它们注入到员工中?