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

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

问题描述

再次-我对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

现在我可以创建并使用一些Service类了.我对此有一些疑问:

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)中使用存储库来获取实体的集合(类似于"Find"的方法)并开始仅使用服务?

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)中使用存储库来获取实体的集合(类似于"Find"的方法)并开始仅使用服务吗?

那可能是个好主意.通常,当应用程序在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并行使用服务和存储库吗?

最好不要,见上文;尽管在某些情况下,您可以这样做快速创建UI的一部分.

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

4.我以某种方式觉得存储库不适合域层...

是的,您应该只将存储库接口放在域中.有关示例,请参见Kostassoid的答案.

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

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

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