每个实体一项服务? [英] One service for each entity?

查看:19
本文介绍了每个实体一项服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

再次 - 我对 DDD 的事情感到困惑:)

Again - i'm confused about DDD things :)

我有架构(我仍在研究它),简而言之就是这样:

I have architeture (I'm still working on it) that in short hand looks like that:

DataLayer:
 EntityDao -> Implementing domain layer interfaces (NHibernate)
DomainLayer:
 EntityRepository -> Repository for each entity with injected Dao
 DomainObjects/Entitys -> Some logic
UI
 ASP.Net MVC

而我现在正处于创建和使用一些服务类的地步.我对此有一些疑问:

And I'm right now in that point where I feel to create and use some Service class. I have some questions with that:

1.我是否应该为每个实体/域对象至少创建一个服务?

1.Should I create at least one service for each entity/domain object ?

2.a.服务是否应该有查询"方法,如 Find、FindAll、FindAll(LINQQuery)?

2.a.Should services have "query" method like Find, FIndAll, FindAll(LINQQuery)?

2.b.我是否应该停止在上层 (UI) 中使用存储库来获取实体的集合(查找"类方法)并开始仅使用服务?

2.b.Should I stop to use Repositorys in upper layers (UI) to get sets ("Find"-like methods) of entity's and start to use only services?

3.如果 2 个问题的答案是否定的 - 我是否应该以并行方式使用服务和存储库(在 UI 中我只需要获取所有实体时,我使用 Repository.FindAll,当我需要获取一些逻辑"列表时该实体的我使用 Service.FindXXX 方法)?

3.If answer on 2 question is No - Should I use Services and Repository in parallel fashion (When in UI I just need to get all entity's I use Repository.FindAll, when I need to get some "logical" list of that entity's i use Service.FindXXX method)?

4.不知何故,我觉得存储库不适合域层 - 我应该以某种方式将它们分开并在 DOMAIN 中只留下域特定的对象,如实体和服务?如果是 - 给我一些如何实现的结构示例.

4.Somehow I feel that Repositorys don't fit in Domain layer - should I separate them somehow and in DOMAIN leave only domain specific objects like Entity's and Services ? If Yes - give me some structure example how to achieve that.

一些对象的例子:

道:

public class NHibernateDao<T> : IDao<T>
{
    public NHibernateDao() { }

    public T Get(object id)
    {
        T entity = (T)NHibernateSession.Get(entityType, id);
        return entity;
    }
    public T Load(object id)
    {
        T entity = (T)NHibernateSession.Load(entityType, id);
        return entity;
    }
    public virtual T Update(T entity)
    {
        NHibernateSession.Update(entity);
        return entity;
    }
    ...

存储库:

public class BaseRepository<T>:IRepository<T>
{
    private DataInterfaces.IDao<T> mDao;

    public virtual T Get(object id)
    {
        return mDao.Get(id);
    }
    public virtual void Delete(T entity)
    {
        mDao.Delete(entity);
    }
    public virtual T Update(T entity)
    {
        return mDao.Update(entity);
    }
    public virtual IQueryable<T> FindAll()
    {
        return mDao.FindAll();
    }
    ...

域对象,目前主要是获取/设置容器 - 这个问题的背景是删除那个贫血模型.

Domain objects, at the moment , it's mainly get/set container - background of this question is to remove that anemic model.

推荐答案

1.每个实体一项服务?

没有.您不需要为一个实体创建一项服务.在 DDD 中,您将为不自然映射到单个实体(或值对象)的操作创建服务.优质的服务(来自 Evans):

No. You do not need to create one service for one entity. In DDD you would create services for operations that do not naturally map to a single entity (or value object). A good service (from Evans) :

  • 该操作与域概念相关,该域概念不是实体或值对象的自然部分.
  • 接口是根据域的元素定义的.
  • 操作是无状态的

因此,一个服务可以使用许多实体,而且可能有许多实体根本没有被单个服务使用.

So a service can consume many entities and there might be many entities that aren't consumed by a single service at all.

2a.服务是否应该具有查询"方法 (..)?

没有.一般来说,这些是存储库方法,而不是放在服务上.但是,可以对返回实体集合的服务进行操作.

No. Generally speaking those are repository methods and are not placed on services. However, there can be operations on a service that return a collection of entities.

2b.我是否应该停止在上层 (UI) 中使用存储库来获取实体的集合(查找"类方法)并开始仅使用服务?

这可能是个好主意.通常,当应用程序在 UI 层使用多个存储库时,UI 会对多个实体执行域操作.这些操作通常应该在领域层中实现;无论是在实体本身,还是在服务中.

That might be a good idea. Often, when an application uses many repositories in the UI layer, the UI performs domain operations on multiple entities. These operations should typically be implemented in the domain layer; either in the entities themselves, or in services.

3.我应该从 UI 并行使用服务和存储库吗?

最好不要,见上文;尽管在某些情况下,您可以通过这样做快速创建部分用户界面.

Better not, see above; although there might be situations where you can quickly create part of your UI by doing so.

4.不知何故,我觉得 Repositoriess 不适合领域层......

你是对的,你应该只将存储库接口放在域中.例如,请参阅 Kostassoid 的回答.

You're right, you should only put repository interfaces in the domain. See Kostassoid's answer for an example.

这篇关于每个实体一项服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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