Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it ok for entities to access repositories?

I've just started working with DDD, so maybe this is a silly question...

Is it ok for an entity to access a repository (via some IRepository interface) to get a value at runtime? For example, I want to enforce a "default" selection for a property:

class Person {     private Company _employer;      public Company Employer {         get { return _employer; }         set {              if(value != null) {                 _employer = value;             } else {                 _employer = employerRepository.GetDefaultEmployer();             }         }     }      ... } 

My question is whehter doing something like this is a horrible violation of DDD principles. And if it isn't, my next question would be what it the best way to provide the repository to use? Should it be supplied when the Person object is created?

Thanks, P

like image 589
user101945 Avatar asked May 06 '09 01:05

user101945


People also ask

Can domain services access repository?

Yes, a domain service can access repositories.

What is entity repository?

Entity Repositories are thin layers on top of EntityManager . They act as an extension point, so we can add custom methods, or even alter the existing ones. The default, EntityRepository implementation just forwards the calls to underlying EntityManager instance.

Should repositories return domain objects?

Your repositories should return domain objects and the client of the repository can decide if it needs to do the mapping. By mapping the domain objects to view models (or something else) inside a repository, you prevent the client of your repositories from getting access to the underlying domain object.

What is repository in DDD?

In DDD, a repository is an objcect that participates in the domain but really abstracts away storage and infrastructure details. Most systems have a persistent storage like a database for its fully functioning. Applying repositories happens by integrating and synchronizing with existing aggregate objects in the system.


1 Answers

it's not a horrible violation of DDD it's a horrible violation of... well... it's just plain horrible (i say this tongue in cheek) :).

First off, your entity becomes dependent on having a repository... that's not ideal. Ideally you'd want to have your repository create the Person and then assign it everything it needs to be effective in the current domain context.

So when you need a Person, you'll go personRepository.GetPersonWithDefaultEmployer() and get back a person which has default employer populated. The personRepository will have a dependency on an employerRepository and use that to populate the person before returning it.

PersonReposotory : IPersonRepository {     private readonly IEmployerRepository employerRepository;      //use constructor injection to populate the EmployerRepository     public PersonRepository(IEmployerRepository employerRepository)     {         this.employerRepository = employerRepository;     }      public person GetPersonWithDefaultEmployer(int personId)     {         Person person = GetPerson(personId);         person.Employer = employerRepository.GetDefaultEmployer(personId);         return person;     } } 
like image 189
lomaxx Avatar answered Sep 28 '22 12:09

lomaxx