Note that there are some explanatory texts on larger screens.

plurals
  1. POReducing Repositories to Aggregate Roots
    primarykey
    data
    text
    <p>I currently have a repository for just about every table in the database and would like to further align myself with DDD by reducing them to aggregate roots only.</p> <p>Let’s assume that I have the following tables, <code>User</code> and <code>Phone</code>. Each user might have one or more phones. Without the notion of aggregate root I might do something like this:</p> <pre><code>//assuming I have the userId in session for example and I want to update a phone number List&lt;Phone&gt; phones = PhoneRepository.GetPhoneNumberByUserId(userId); phones[0].Number = “911”; PhoneRepository.Update(phones[0]); </code></pre> <p>The concept of aggregate roots is easier to understand on paper than in practice. I will never have phone numbers that do not belong to a User, so would it make sense to do away with the PhoneRepository and incorporate phone related methods into the UserRepository? Assuming the answer is yes, I’m going to rewrite the prior code sample.</p> <p>Am I allowed to have a method on the UserRepository that returns phone numbers? Or should it always return a reference to a User, and then traverse the relationship through the User to get to the phone numbers:</p> <pre><code>List&lt;Phone&gt; phones = UserRepository.GetPhoneNumbers(userId); // Or User user = UserRepository.GetUserWithPhoneNumbers(userId); //this method will join to Phone </code></pre> <p>Regardless of which way I acquire the phones, assuming I modified one of them, how do I go about updating them? My limited understanding is that objects under the root should be updated through the root, which would steer me towards choice #1 below. Although this will work perfectly well with Entity Framework, this seems extremely un-descriptive, because reading the code I have no idea what I’m actually updating, even though Entity Framework is keeping tab on changed objects within the graph.</p> <pre><code>UserRepository.Update(user); // Or UserRepository.UpdatePhone(phone); </code></pre> <p>Lastly, assuming I have several lookup tables that are not really tied to anything, such as <code>CountryCodes</code>, <code>ColorsCodes</code>, <code>SomethingElseCodes</code>. I might use them to populate drop downs or for whatever other reason. Are these standalone repositories? Can they be combined into some sort of logical grouping/repository such as <code>CodesRepository</code>? Or is that against best practices.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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