<p>After reading Evan's and Nilsson's books I am still not sure how to manage Data access in a domain driven project. Should the CRUD methods be part of the repositories, i.e. OrderRepository.GetOrdersByCustomer(customer) or should they be part of the entities: Customer.GetOrders(). The latter approach seems more OO, but it will distribute Data Access for a single entity type among multiple objects, i.e. Customer.GetOrders(), Invoice.GetOrders(), ShipmentBatch.GetOrders() ,etc. What about Inserting and updating?</p>

[{'answer_id': 77244, 'author': 'Vin', 'author_id': 1747, 'author_profile': 'https://Stackoverflow.com/users/1747', 'pm_score': 2, 'selected': False, 'text': '<p>Even in a DDD, I would keep Data Access classes and routines separate from Entities.</p>\n\n<p>Reasons are,</p>\n\n<ol>\n<li>Testability improves</li>\n<li>Separation of concerns and Modular design</li>\n<li>More maintainable in the long run, as you add entities, routines</li>\n</ol>\n\n<p>I am no expert, just my opinion.</p>\n'}, {'answer_id': 77275, 'author': 'Chris Bilson', 'author_id': 12934, 'author_profile': 'https://Stackoverflow.com/users/12934', 'pm_score': 5, 'selected': True, 'text': '<p>CRUD-ish methods should be part of the Repository...ish. But I think you should ask why you have a bunch of CRUD methods. What do they <em>really</em> do? What are they <em>really</em> for? If you actually call out the data access patterns your application uses I think it makes the repository a lot more useful and keeps you from having to do shotgun surgery when certain types of changes happen to your domain.</p>\n\n<pre><code>CustomerRepo.GetThoseWhoHaventPaidTheirBill()\n\n// or\n\nGetCustomer(new HaventPaidBillSpecification())\n\n// is better than\n\nforeach (var customer in GetCustomer()) {\n    /* logic leaking all over the floor */\n}\n</code></pre>\n\n<p>"Save" type methods should also be part of the repository.</p>\n\n<p>If you have aggregate roots, this keeps you from having a Repository explosion, or having logic spread out all over: You don\'t have 4 x # of entities data access patterns, just the ones you actually use on the aggregate roots.</p>\n\n<p>That\'s my $.02.</p>\n'}, {'answer_id': 77323, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': '<p>The annoying thing with Nilsson\'s Applying DDD&amp;P is that he always starts with "I wouldn\'t do that in a real-world-application but..." and then his example follows. Back to the topic: I think OrderRepository.GetOrdersByCustomer(customer) is the way to go, but there is also a discussion on the ALT.Net Mailing list (<a href="http://tech.groups.yahoo.com/group/altdotnet/" rel="nofollow noreferrer">http://tech.groups.yahoo.com/group/altdotnet/</a>) about DDD. </p>\n'}, {'answer_id': 77444, 'author': 'Shane Courtrille', 'author_id': 12503, 'author_profile': 'https://Stackoverflow.com/users/12503', 'pm_score': 2, 'selected': False, 'text': "<p>I've done it both ways you are talking about,  My preferred approach now is the persistent ignorant (or PONO -- Plain Ole' .Net Object) method where your domain classes are only worried about being domain classes.  They do not know anything about how they are persisted or even if they are persisted.  Of course you have to be pragmatic about this at times and allow for things such as an Id (but even then I just use a layer super type which has the Id so I can have a single point where things like default value live)</p>\n\n<p>The main reason for this is that I strive to follow the principle of Single Responsibility.  By following this principle I've found my code much more testable and maintainable.  It's also much easier to make changes when they are needed since I only have one thing to think about.</p>\n\n<p>One thing to be watchful of is the method bloat that repositories can suffer from.  GetOrderbyCustomer.. GetAllOrders.. GetOrders30DaysOld.. etc etc.  One good solution to this problem is to look at the Query Object pattern. And then your repositories can just take in a query object to execute.</p>\n\n<p>I'd also strongly recommend looking into something like NHibernate.  It includes a lot of the concepts that make Repositories so useful (Identity Map, Cache, Query objects..)</p>\n"}, {'answer_id': 77847, 'author': 'JasonTrue', 'author_id': 13433, 'author_profile': 'https://Stackoverflow.com/users/13433', 'pm_score': 3, 'selected': False, 'text': "<p>DDD usually prefers the repository pattern over the active record pattern you hint at with Customer.Save.</p>\n\n<p>One downside in the Active Record model is that it pretty much presumes a single persistence model, barring some particularly intrusive code (in most languages).</p>\n\n<p>The repository interface is defined in the domain layer, but doesn't know whether your data is stored in a database or not. With the repository pattern, I can create an InMemoryRepository so that I can test domain logic in isolation, and use dependency injection in the application to have the service layer instantiate a SqlRepository, for example.</p>\n\n<p>To many people, having a special repository just for testing sounds goofy, but if you use the repository model, you may find that you don't really need a database for your particular application; sometimes a simple FileRepository will do the trick. Wedding to yourself to a database before you know you need it is potentially limiting. Even if a database is necessary, it's a lot faster to run tests against an InMemoryRepository.</p>\n\n<p>If you don't have much in the way of domain logic, you probably don't need DDD. ActiveRecord is quite suitable for a lot of problems, especially if you have mostly data and just a little bit of logic.</p>\n"}, {'answer_id': 108610, 'author': 'Stefan Moser', 'author_id': 8739, 'author_profile': 'https://Stackoverflow.com/users/8739', 'pm_score': 2, 'selected': False, 'text': "<p>Let's step back for a second.  Evans recommends that repositories return aggregate roots and not just entities.  So assuming that your Customer is an aggregate root that includes Orders, then when you fetched the customer from its repository, the orders came along with it.  You would access the orders by navigating the relationship from Customer to Orders.</p>\n\n<pre><code>customer.Orders;\n</code></pre>\n\n<p>So to answer your question, CRUD operations are present on aggregate root repositories.</p>\n\n<pre><code>CustomerRepository.Add(customer);\nCustomerRepository.Get(customerID);\nCustomerRepository.Save(customer);\nCustomerRepository.Delete(customer);\n</code></pre>\n"}]
['https://Stackoverflow.com/questions/77171', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2133/']