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?
特别是,将方法移入Phrase实体类是否有意义?在那种情况下,怎么称呼?
EDIT:
The example above has been modified after the answer from moffdub and the comment from Adeel Ansari. The changes are highlighted.
上面的例子在moffdub的回答和Adeel Ansari的评论之后进行了修改。更改突出显示。
I would like to ask about the added IPhraseRepository.GetPhrase(phraseId) and how you would include that?
我想问一下添加的IPhraseRepository.GetPhrase(phraseId)以及如何包含它?
2 个解决方案
#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.
存储库应该使用短语,而不是字符串。我也不确定为什么SavePhrase方法会返回一个短语。我倾向于使这些方法无效。
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.
此外,要小心使您的域模型中的每个属性都具有公共getter和setter。这可能会导致您陷入贫血的领域模型。
#2
Just some thoughts:
只是一些想法:
SetId(int phraseId) should not be public
SetId(int phraseId)不应该是公共的
Phrase could implement IPhrase (or IPhraseAggregate) which would not expose SetId(..)
短语可以实现IPhrase(或IPhraseAggregate),它不会暴露SetId(..)
SavePhrase(Phrase phrase) could (should?) return void if the reference to the phrase entity stays "valid" after saving:
如果保存后对短语实体的引用保持“有效”,则SavePhrase(短语短语)可以(应该?)返回void:
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
}
#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.
存储库应该使用短语,而不是字符串。我也不确定为什么SavePhrase方法会返回一个短语。我倾向于使这些方法无效。
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.
此外,要小心使您的域模型中的每个属性都具有公共getter和setter。这可能会导致您陷入贫血的领域模型。
#2
Just some thoughts:
只是一些想法:
SetId(int phraseId) should not be public
SetId(int phraseId)不应该是公共的
Phrase could implement IPhrase (or IPhraseAggregate) which would not expose SetId(..)
短语可以实现IPhrase(或IPhraseAggregate),它不会暴露SetId(..)
SavePhrase(Phrase phrase) could (should?) return void if the reference to the phrase entity stays "valid" after saving:
如果保存后对短语实体的引用保持“有效”,则SavePhrase(短语短语)可以(应该?)返回void:
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
}