
In the last few years we notice a lot of companies adopt the DDD on their application architecture
which raised a lot of discussion between teams about a lot of ideas and designs , one of these discussions is the differences between Anemic Domain Model and Rich Domain Model
In this article will try to highlight the main difference is short words and in simple example
Anemic Domain Model
The simple idea behind the Anemic Domain Model is to have a model with a set of getters and setters ,that only contains the data and does not contains any methods thats reflect business rules and calculation, then you have to create another class name it as (Helper , service , manager… ) which contians all the business rules and calculation by taking the object as argument to change the model state or do any other needed business rules
as an example :
void main()
{
Employee employee = new Employee()
{
Age = 35,
BaseSalery = 1000,
JobPosition = Position.RegulerEmployee,
Name = "Test Employee"
};
EmployeeService employeeService = new EmployeeService();
Console.WriteLine( employeeService.CalculateSalaryIncrease(employee));
}
public class Employee
{
public string Name { get; set; }
public int Age { get; set; }
public decimal Salery { get; set; }
public Position JobPosition { get; set; }
}
public class EmployeeService
{
public double CalculateSalaryIncrease(Employee employee)
{
if (employee == null)
{
throw new NullReferenceException("employee must not be null");
}
double totalIncrease= employee.BaseSalery;
switch(employee.JobPosition)
{
case Position.RegulerEmployee:
totalIncrease= employee.BaseSalery * baseIncrease;
break;
case Position.Senior:
totalIncrease=employee.BaseSalery * seniorbounce;
break ;
case Position.Manager:
totalIncrease= employee.BaseSalery* Managerbounce;
break;
}
return totalIncrease;
}
}
” This is one of those anti-patterns that’s been around for quite a long time ,The fundamental horror of this anti-pattern is that it’s so contrary to the basic idea of object-oriented design; which is to combine data and process together. The anemic domain model is really just a procedural style design”.”
Rich Domain Model
The idea here in the Rich Domain Model is to have the data and the behavior in the same place , expressing the behavior using public method of the object , by that we se the responsibilitiy for keeping the state of the object to the object it self
void main()
{
Employee employee = new Employee()
{
Age = 35,
BaseSalery = 1000,
JobPosition = Position.RegulerEmployee,
Name = "Test Employee"
};
Console.WriteLine( employee.CalculateSalaryIncrease());
}
public class Employee
{
public string Name { get; set; }
public int Age { get; set; }
public decimal Salery { get; set; }
public Position JobPosition { get; set; }
public double CalculateSalaryIncrease( )
{
double totalIncrease= employee.BaseSalery;
switch(JobPosition)
{
case Position.RegulerEmployee:
totalIncrease= employee.BaseSalery * baseIncrease;
break;
case Position.Senior:
totalIncrease=employee.BaseSalery * seniorbounce;
break ;
case Position.Manager:
totalIncrease= employee.BaseSalery* Managerbounce;
break;
}
return totalIncrease;
}
}
Conclusion
Martin Fowler in his article defined Anemic Model as an anti-pattern , i think its depend on the type of the application and its complexity , i hop this samll articl gives at least a small idea about the deference between these to approch of development for more information you refer to