Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The sort answer to you question is the standard <strong>It depends</strong> .</p> <p>As a rule of thumb don't ever do this. Keep your entities without references to repositories. [<em>putting practical hat on</em>] In some rare, extremely rare cases where you have a very, very, very good reason for doing this than add a big comment explaining why you do it and do it - add the reference or use <a href="http://en.wikipedia.org/wiki/Double_dispatch">Double Dispatch</a> to pass the repository[<em>hat off</em>] </p> <p>Also if you wish to follow DDD principles, it's highly recommended you have access to a domain expert and an iterative process of development ( see <a href="http://www.infoq.com/presentations/ddd-eric-evans">Eric Evans - what i've learned since the book</a> ).</p> <p>With your domain expert you should define bounding contexts and most importantly the aggregates and their aggregate roots and their entities and value objects. Going down the DDD road is not easy at first but the rewords are worthed. </p> <p>A few things regarding the code you posted:</p> <ol> <li><p>It's not recommended to have public setters on your entities. Use methods instead that express the intent better.</p></li> <li><p>If a person instance is created without initializing the _employer field the getter for the Employer property will return null. If you then set the value of the Employer property to null, the next call to the getter will return a non null value. This is probably unexpected by the users of your class.</p></li> <li><p>The caller setting the Employer of the Person ( either by public setter or public method ) should know the exact Company instance it whats to set even if it's the default one. Maybe the caller can have the reference to the repository. </p></li> <li><p>Depending on your concrete domain the Company might be a value object. In that case instead of initializing the _employer with null you could initialize it with the default value for the value object. This could be the case if you only have very few companies (1-2) and they are immutable and don't have specific behavior.</p></li> </ol>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload