67010

Does this Entity Repository Service example fit into Domain-Driven Design?

Question:

I would like to know if you find the following pattern meaningful in domain driven design.

The domain layer consists of model and repository. The application layer consists of services that handles queries from the user interface, or from controllers in the Model-View-Controller pattern.

Details of the structure:

// Assembly Model: public class Phrase { public int PhraseId { get; <b>private</b> set; } public string PhraseText { get; <b>private</b> set; } <b>public Phrase(string phraseText) { this.PhraseText = phraseText; } public void SetId(int phraseId) { this.PhraseId = phraseId; }</b> } // Assembly Repository (references assembly Model): public interface IPhraseRepository { Phrase SavePhrase(<b>Phrase phrase</b>); <b>Phrase GetPhrase(int phraseId);</b> } // Assembly Services (references assemblies Model and Repository): public class PhraseService { private IPhraseRepository _phraseRepository; public PhraseService(IPhraseRepository phraseRepository) { _phraseRepository = phraseRepository; } public Phrase SavePhrase(string phraseText) { Phrase phrase = _phraseRepository.SavePhrase(<b>new Phrase(phraseText)</b>); // doing other things like sending mail, logging, etc. // ... return Phrase; } }

Particularly, would it make sense to move the method into the Phrase entity class? In that case, how would that be called?

EDIT:

The example above has been modified after the answer from moffdub and the comment from Adeel Ansari. The changes are <b>highlighted</b>.

I would like to ask about the added IPhraseRepository.GetPhrase(phraseId) and how you would include that?

Answer1:

The repository should take in a Phrase, not a string. I'm also not sure why the SavePhrase method returns a Phrase. I tend to make such methods void methods.

Also, be wary of making every property in your domain model have public getters and setters. That can lead you to an anemic domain model.

Answer2:

Just some thoughts:

SetId(int phraseId) should not be public

Phrase could implement IPhrase (or IPhraseAggregate) which would not expose SetId(..)

SavePhrase(Phrase phrase) could (should?) return void if the reference to the phrase entity stays "valid" after saving:

public void SavePhrase(string phraseText) { Phrase phrase = new Phrase(phraseText); // NOTE: keep a reference to phrase this._phraseRepository.SavePhrase(phrase); // NOTE: returns void return phrase; // NOTE: assume the repository sets the phrase.PhraseId }

Recommend

  • WordPress: How search a post for “post_content” with “wp_query” class?
  • Is this an Extension or Helper Method in MVC3?
  • MS Access Passthrough Query Update
  • Itext7 - Place Text at specific position
  • State variable-Cucumber - Ruby -Regular expressions
  • Install Symfony on Windows behind proxy server with NTLM authentication
  • GHC Generics: How to write an implementation of (:+:) that converts sum types from/to integers?
  • how to use subexpressions
  • Changing font size of pdfptable
  • XSLT transform with inline if statements
  • strtotime() converts a non existing date to another date
  • Do I need to clean user input for DB::query calls in laravel?
  • Excel - Formula or Macro to fill a cell based on another cell that links to yet another cell
  • Prolog binary search tree test - unwanted parents' parent node comparison
  • XMLWorkerHelper performance slow
  • Is it possible to have space between cells in iTextPdf?
  • How to use i18t for texts in the source code of Angular project?
  • String encoding problem on PdoStatement->bindParam()?
  • Installing Python modules with Anaconda or Canopy
  • sending/ receiving email in Java
  • Javascript + PHP Encryption with pidCrypt
  • AT Commands to Send SMS not working in Windows 8.1
  • Cannot Parse HTML Data Using Android / JSOUP
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • How to set the response of a form post action to a iframe source?
  • JTable with a ScrollPane misbehaving
  • Getting Messege Twice Using IMvxMessenger
  • Change div Background jquery
  • Qt: Run a script BEFORE make
  • apache spark aggregate function using min value
  • unknown Exception android
  • EntityFramework adding new object to nested object collection
  • Checking variable from a different class in C#
  • Sorting a 2D array using the second column C++
  • reshape alternating columns in less time and using less memory
  • Observable and ngFor in Angular 2
  • How can i traverse a binary tree from right to left in java?
  • failed to connect to specific WiFi in android programmatically
  • Unable to use reactive element in my shiny app
  • How can I use threading to 'tick' a timer to be accessed by other threads?