85868

Which objects should contain the database access methods in domain driven design

Question:

I thought domain driven design is saying the Get-methods are in the Repositories and the Add/Del/Update are in the business objects?

Can you clarify this please?

When I look at the MVCMusicStore sample all CRUD methods to the database are in the entities ?!

<a href="http://mvcmusicstore.codeplex.com/SourceControl/changeset/view/d9f25c5263ed#MvcMusicStore%2fModels%2fShoppingCart.cs" rel="nofollow">http://mvcmusicstore.codeplex.com/SourceControl/changeset/view/d9f25c5263ed#MvcMusicStore%2fModels%2fShoppingCart.cs</a>

Answer1:

I've just answered a similar question <a href="https://stackoverflow.com/questions/11818160/could-domain-models-be-aware-of-repositories" rel="nofollow">could domain models be aware of repositories?</a> (and also <a href="https://stackoverflow.com/questions/11613716/does-save-method-belong-to-the-business-domain-entity/11630606#11630606" rel="nofollow">Does "Save" method belong to the Business Domain Entity?</a> ) - and it seems that many many DDD questions are about Active Record vs. Repository choice.

I'd follow Eric Evans advice

<blockquote>

In general, don't fight your frameworks. Seek ways to keep the fundamentals of DDD and let go the specifics when the framework is antagonistic. Look for affinities between the concepts of DDD and the concepts of the framework. This is assuming that you have no choice but to use the framework.

</blockquote>

So, even if the creator of DDD is aware of some possible inconveniences, for me the choice between Active Record and Repository depends on the current stack of technologies.

Answer2:

The link you posted isn't an example of domain-driven design, it simply illustrates the MVC pattern. As you mentioned, the entities contain all persistence logic while the business logic lies primarily in the controllers.

In a domain-driven design of the same system, entities would <em>not</em> contain any persistence logic but they <em>would</em> contain most of the business logic. All persistence functions would reside in repositories, and the controllers (a.k.a. services) would be extremely thin and delegate most of the work to entities.

Answer3:

Here's what a typical repository looks like:

public class OrderRepository { public MyClass Load(Guid id); // throws an exception if not found public void Save(Order order); public void Delete(Guid id); // does not throw an exception if not found (idempotent) }

Recommend

  • How to filter results with Ransack
  • select query and count based on condition
  • Rails Query to return users belongs to any cities & not belong to any cities
  • Android multuple camera preview
  • curl - How to escape < in parameter value
  • string Multiplication in C++
  • LINQ GroupBy Count
  • OneToOne bidirectional mapping foreign key auto fill
  • MSBuild: How to make several builds with different versions from same sources?
  • Is this hack a defined behavior for T4
  • Android gradle : Error No resource found that matches the given name: attr when refer to an android
  • How do you create a simple Automation Extender for Visual Studio with UITypeEditor
  • Combine two small queries (that group by different values) into one query
  • Objective-C – access extern const with a string containing its name? [duplicate]
  • Direct insert data in mysql data in android
  • Hatch area using pcolormesh in Basemap
  • PXAction seemingly does nothing
  • java.net.SocketTimeoutException on embedded tomcat with jersey
  • Peer to peer/adaptive payments with paypal [closed]
  • Why isn't \\\\ interpreted as a blackslash in this regex?
  • How to force Composer to download a local package?
  • View Paypal shopping cart contents on my site
  • Accessing local repository in offline mode
  • NRefactory: How do I access unresolved Named Arguments on a Property Attribute?
  • C#: Import/Export Settings into/from a File
  • OSX - always hide certain files
  • jwtBearer bearer token with rc-1 update to ASP.Net 5
  • Tamper-proof configuration files in .NET?
  • How can I set a binding to a Combox in a UserControl?
  • Should I or shouldn't I use the CachingConnectionFactory with hornetq 2.4.1
  • AJAX Html Editor Extender upload image appearing blank
  • Email verification using google app script and google forms
  • WinForms: two way TextBox problem
  • Trying to switch camera back to front but getting exception
  • In LanguageTool, how do you create a dictionary and use it for spell checking?
  • Free memory of cv::Mat loaded using FileStorage API
  • How do you join a server to an Active Directory (domain)?
  • Angular 2 constructor injection vs direct access
  • how does django model after text[] in postgresql [duplicate]
  • Programmatically clearing map cache