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

拟墨画扇 提交于 2019-12-13 02:49:09

问题


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; private set; }
    public string PhraseText { get; private set; }

    public Phrase(string phraseText) { this.PhraseText = phraseText; }

    public void SetId(int phraseId) { this.PhraseId = phraseId; }
}

// Assembly Repository (references assembly Model):
public interface IPhraseRepository
{
    Phrase SavePhrase(Phrase phrase);
    Phrase GetPhrase(int phraseId);
}

// 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(new Phrase(phraseText));
        // 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 highlighted.

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


回答1:


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.




回答2:


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
}


来源:https://stackoverflow.com/questions/452517/does-this-entity-repository-service-example-fit-into-domain-driven-design

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!