Let's say you've an anemic domain model (ADM):
public class Employee
{
public Employee()
{
_roles = new List<Role>();
}
private IList<Role> _roles;
public Guid Id { get; set; }
public string Name { get; set; }
public IList<Roles> Roles { get { return _roles; } }
}
public class EmployeeManager
{
public Employee GetByName(string name)
{
Contract.Requires(name != null);
return repositoryOfEmployeesInstance.GetByName(name);
}
public void AddEmployee(string name)
{
Contract.Requires(name != null);
// The unique identifier will be generated by the OR/M behind the scenes...
repositoryOfEmployeesInstance.Add(new Employee { Name = "Matias" });
}
}
Later, in some other place, you've this code:
Employee some = new EmployeeManager().GetByName("Matias");
some.Roles.Add("Principal");
Now, in addition to the domain model, there's an aspect called DomainValidationAspect
which validates the new employee just before it's going to be persisted in the Repository
implementation.
The whole ValidateEmployeeAspect
can validate an Employee
either when one it's added or updated, which is the case of adding a role. It is responsible of loading Employee
's domain rules and verifying them. Domain rules are implemented using the specification pattern.
Finally, everything happens in a domain transaction.
Right, it's seems to be an anemic domain model from the point of view of object-oriented programming, but what about aspect-oriented programming.
So the question is...
...may a domain-driven design be anemic just because:
- ...in this case,
Employee
roles are added using the collection interface? - ...absence of behaviors in
Employee
from the point of view of pure object-oriented programming?
Some words
My opinion is it's true that aspect-oriented programming has the drawback of hiding a lot of details in the main flow of the domain (or any other layer), but a bird-eye on the concept behind an object-oriented plus aspect-oriented approach would map to a rich domain model rather than an anemic domain model.