<p>DDD states that you should only ever access entities through their aggregate root. So say for instance that you have an aggregate root X which potentially has a <em>lot</em> of child Y entities. Now, for some scenario, you only really care about a subset of these Y entities at a time (maybe you're displaying them in a paged list or whatever).</p>

<p>Is it OK to implement a repository then, so that in such scenarios it returns an <em>incomplete</em> aggregate? Ie. an X object who'se Ys collection only contains the Y instances we're interested in and not <em>all</em> of them? This could for instance cause methods on X which perform some calculation involving the Ys to not behave as expected.</p>

<p>Is this perhaps an indication that the Y entity in question should be considered promoted to an aggregate root?</p>

<p>My current idea (in C#) is to leverage the delayed execution of LINQ, so that my X object has an IQueryable to represent its relationship with Y. This way, I can have transparent lazy loading with filtering... But getting this to work with an ORM (Linq to Sql in my case) might be a bit tricky.</p>

<p>Any other clever ideas?</p>

[{'answer_id': 25113, 'author': 'Aidan Ryan', 'author_id': 1042, 'author_profile': 'https://Stackoverflow.com/users/1042', 'pm_score': 1, 'selected': False, 'text': '<p>You\'re really asking two overlapping questions.</p>\n\n<ol>\n<li><p>The title and first half of your question are philosophical/theoretical. I think the reason for accessing entities only through their "aggregate root" is to abstract away the kinds of implementation details you\'re describing. Access through the aggregate root is a way to reduce complexity by having a trusted point of access. You\'re eliminating friction/ambiguity/uncertainty by adhering to a convention. It doesn\'t matter how it\'s implemented within the root, you just know that when you ask for an entity it will be there. I don\'t think this perspective rules out a "filtered repository" as you describe. But to provide a <a href="http://blogs.msdn.com/brada/archive/2003/10/02/50420.aspx" rel="nofollow noreferrer">pit of success</a> for devs to fall into, it should be impossible instantiate the repository without being explicit about its "filteredness;" likewise, if shared access to a  repository instance is possible, the "filteredness" should be explicit when coding in the caller.</p></li>\n<li><p>The second half of your question is about implementation on a specific platform. Not sure why you mention delayed execution, I think that\'s really orthogonal to the filtering question. The filtering itself could be a bit tricky to implement with LINQ. Maybe rather than inlining the Where lambdas, you set up a collection of them and select one depending on the filter you need.</p></li>\n</ol>\n'}, {'answer_id': 108695, 'author': 'Stefan Moser', 'author_id': 8739, 'author_profile': 'https://Stackoverflow.com/users/8739', 'pm_score': 3, 'selected': False, 'text': '<p>I consider an aggregate root with a <em>lot</em> of child entities to be a code smell, or a DDD smell if you will. :-)  Generally I look at two options.</p>\n\n<ol>\n<li>Split your aggregate into many smaller aggregates.  This means that my original design was not optimal and I need to identify some new entities.</li>\n<li>Split your domain into multiple bounded contexts.  This means that there are specific sets of scenarios that use a common subset of the entities in the aggregate, while there are other sets of scenarios that use a different subset.</li>\n</ol>\n'}, {'answer_id': 174017, 'author': 'Carl Björknäs', 'author_id': 23708, 'author_profile': 'https://Stackoverflow.com/users/23708', 'pm_score': 2, 'selected': False, 'text': '<p>Jimmy Nilsson hints in his book that instead of reading a complete aggregate you can read a snapshot of parts of it. But you are not supposed to be able to save changes in the snapshot classes to the database.</p>\n\n<p><a href="https://rads.stackoverflow.com/amzn/click/com/0321268202" rel="nofollow noreferrer" rel="nofollow noreferrer">Jimmy Nilsson\'s book</a> Chapter 6: Preparing for infrastructure - Querying. Page 226.</p>\n\n<p><a href="http://martinfowler.com/ap2/snapshot.html" rel="nofollow noreferrer">Snapshot pattern</a></p>\n'}, {'answer_id': 335294, 'author': 'mbillard', 'author_id': 810, 'author_profile': 'https://Stackoverflow.com/users/810', 'pm_score': 0, 'selected': False, 'text': "<p>You are allowed since the code will compile anyway, but if you're going for a pure DDD design you should not have incomplete instances of objects.</p>\n\n<p>You should look into LazyLoading if you're afraid to load a huge object of which you will only use a small portion of its child entities.</p>\n\n<p>LazyLoading delays the loading of whatever you decide to lazy-load until the moment they are accessed. They make use of callbacks to call the loading method once the code calls for them.</p>\n"}, {'answer_id': 54515804, 'author': 'DmitriBodiu', 'author_id': 2826532, 'author_profile': 'https://Stackoverflow.com/users/2826532', 'pm_score': 0, 'selected': False, 'text': '<blockquote>\n  <p>Is it OK to implement a repository then, so that in such scenarios it\n  returns an incomplete aggregate?</p>\n</blockquote>\n\n<p>Not at all. Aggregate is a transnational boundary to change the state of your system. Never use aggregates for querying data. Split the system into Write and Read sides. (read about CQR &amp; CQRS). When we think "CRUD" based, we implement our system, based on some resource. Lets say you have "Appointment" aggregate. Thinking "Crudish" means we should implement usecases Create, Update, Delete, GetAll appointments. That means Appointment[] should be returned for GetAll. When you think usecase based, (HexagonalArchitecture) your usecases would be ScheduleAppointment, RescheduleAppointment, CancelAppointment. But for query side it can be: /myCalendar. We return back all appointments for a specific user in a ClientCalendar object. Create separate DTO\'s for Query sides. Never use aggregates for this purpose.</p>\n'}]
['https://Stackoverflow.com/questions/25042', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1710/']