Avoiding anemic domain model - a real example

Posted by cbp on Stack Overflow See other posts from Stack Overflow or by cbp
Published on 2010-05-18T05:49:45Z Indexed on 2010/05/18 6:20 UTC
Read the original article Hit count: 332

Filed under:

I am trying to understand Anemic Domain Models and why they are supposedly an anti-pattern.

Here is a real world example.

I have an Employee class, which has a ton of properties - name, gender, username, etc

public class Employee
{
    public string Name { get; set; }
    public string Gender { get; set; }
    public string Username { get; set; }
    // Etc.. mostly getters and setters
}

Next we have a system that involves rotating incoming phone calls and website enquiries (known as 'leads') evenly amongst sales staff. This system is quite complex as it involves round-robining enquiries, checking for holidays, employee preferences etc. So this system is currently seperated out into a service: 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
     }
}

Then on the backside of our website enquiry form we have code like this:

public void SubmitForm()
{
    var lead = CreateLeadFromFormInput();

    var selectedEmployee = Kernel.Get<IEmployeeLeadRotationService>()
                                 .SelectEmployee(lead);

    Response.Write(employee.Name + " will handle your enquiry. Thanks.");
}

I don't really encounter many problems with this approach, but supposedly this is something that I should run screaming from because it is an Anemic Domain Model.

But for me its not clear where the logic in the lead rotation service should go. Should it go in the lead? Should it go in the employee?

What about all the injected repositories etc that the rotation service requires - how would they be injected into the employee, given that most of the time when dealing with an employee we don't need any of these repositories?

© Stack Overflow or respective owner

Related posts about anemic-domain-model