此实体存储库服务示例是否适合域驱动设计? [英] Does this Entity Repository Service example fit into Domain-Driven Design?

查看:77
本文介绍了此实体存储库服务示例是否适合域驱动设计?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道您是否发现以下模式在域驱动设计中有意义.

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.

结构细节:


// 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;
    }
}

特别是,将方法移入Phrase实体类是否有意义?在这种情况下,该怎么称呼?

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

在moffdub的回答和Adeel Ansari的评论之后,对上面的示例进行了修改.所做的更改突出显示.

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

我想问一下添加的IPhraseRepository.GetPhrase(phraseId)以及如何添加?

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

推荐答案

存储库应采用短语而不是字符串.我也不确定为什么SavePhrase方法返回短语.我倾向于使这些方法成为无效方法.

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.

这篇关于此实体存储库服务示例是否适合域驱动设计?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆