我试图理解贫血领域模型以及为什么它们被认为是反模式。
以下是一个实际的例子。
我有一个员工类,其中包含大量属性 - 姓名、性别、用户名等。
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.");
}
我没有真正遇到这种方法的太多问题,但据说这是一种贫血领域模型,所以我应该尽快离开。
但对我来说,现在并不清楚主导轮换服务中的逻辑应该放在哪里。它应该放在潜在客户身上吗?还是放在雇员身上呢?
那么所有注入的存储库等旋转服务需要什么 - 如何将它们注入到员工中,考虑到大多数情况下,当处理员工时我们都不需要这些存储库?
ILead
看起来是什么样子的? - Simon Buchan