I'd like to apply Domain Driven Design principles in my project but couldn't determine what I should do with the business logic of dependent models.
For example, suppose this scenario:
I have Person
and Car
domain models. Each Person is suitable to buy certain cars from db based on age/budget/preferences/etc. In my model, I'd like to have a List of Cars (SuitableCars
) which are appropriate for this Person.
public class Person
{
public List<Car> SuitableCars {get; set;}
}
But in order to do that right now, I have to call a service method (GetSuitableCarsForPerson
) to fetch data from db (DI with repository), run my (sometimes fairly complicated multi-model dependent) custom logic and get the cars.
public class PersonService : IPersonService
{
private IRepository _repo;
public PersonService(IPRepository repository)
{
_repo = repository;
}
public List<Car> GetSuitableCarsForPerson(Person person)
{
// business goes here right now.
}
}
So the declaration of SuitableCars
property will become:
private IPersonService _personService;
public List<Car> SuitableCars
{
get
{
// I have to inject a PersonService in my model. Bad practice?
return _personService.GetSuitableCarsForPerson(this);
}
}
AFAIK, Services should be kept thin(ref) and are used to let you put Not-DomainModel-Related business in them. But I believe logic like what I have described belong to the model itself.
So, how to handle these kinds of logics where I should access relevant models and do various custom validations/filters to get the appropriate data?
Thank you.