服务层设计。理由把东西放到一个服务层理由、东西

2023-09-03 22:03:17 作者:倔强的蜘蛛侠也会悲伤

我有几个设计相关的问题:

服务层接口驻留在域层?例如用户服务? 什么是主要的原因为code部分转移到一个单独的层? 应服务层驻留在相同的组件,应用层

谢谢!

修改

我用 RavenDB 并有相当瘦的控制器动作,但两项行动是present为 [无为] 操作:

  [无为]
公开的IEnumerable<物品> GetAllArticles(){
    返回this.session.Query<物品>()
        .Customize((X => x.WaitForNonStaleResults()))
        .AsQueryable()
        .OrderBy(D => d.CreatedOn);
}

[无为]
公开的IEnumerable<字符串> GetAllCategories(){
    返回this.session.Query<物品>()
        。选择(X => x.Category)
        .Distinct()了ToList()
        .OrderBy(X =&X的催化剂);
}
 

。由于我不使用存储库模式。 难道是合理的,把它里面有服务?

解决方案   

应该服务层接口所在的领域层?例如   用户服务?

域名服务和应用程序服务(不提基础设施服务):微服务发展过程

在DDD必须2类型的服务区分开来。

域名服务位于领域层和含有不属于任何实体域逻辑或横跨几个实体。

的应用服务是在应用程序层,并定义坐标调用域层,并可能返回一个结果应用特定的操作。它们可以对应于使用的情况下,或用例的一个子部分。应用服务直接调用,而域名服务不能是客户端。

应用服务接口并不需要在域层,因为它们不是特定领域的,但是应用程序特定,并且该域不会使用它们。应用层引用领域层而不是周围的其他方式。

  

什么是主要的原因为code部分转移到一个单独的层?

这是我想到的第一个好处是可重用性(重用域名在其他地方不执行所有的应用程序特定的东西,与它一起)和可维护性(组在一起的东西,改变起来)。

请参阅关注分离中的单一职责原则,凝聚力。

  

应该服务层位于同一组件的应用程序   层?

在DDD,没有服务层。应用层通常相当于什么其他方法调用的服务层的,虽然。

  

由于我不使用存储库模式。它是合理的,把它里面的   服务?

经书DDD需要一个存储库对于这种操作。即使你不遵循DDD紧密,我不是命名的一切服务时,有万吨的更准确的名字给他们的忠实粉丝:数据访问对象,数据网关等关键要意识到这里是含有这些方法的目的应该被放置在一个低级别的数据的特定层(DAL,基础结构的层),因为它涉及与一个持久性数据存储(乌鸦DB)明确地

I have a few design-related questions:

should service layer interfaces reside in a domain layer? For example user service? what are the primary reasons to move a code part to a separate layer? should service layer reside at the same assembly as the application layer?

Thanks!

EDIT

I use a RavenDB and have quite skinny controller actions but two action are present as [NonAction] actions:

[NonAction]
public IEnumerable<Article> GetAllArticles() { 
    return this.session.Query<Article>()
        .Customize((x => x.WaitForNonStaleResults()))
        .AsQueryable()
        .OrderBy(d => d.CreatedOn);
}

[NonAction]
public IEnumerable<String> GetAllCategories() {
    return this.session.Query<Article>()
        .Select(x => x.Category)
        .Distinct().ToList()
        .OrderBy(x => x);
}

..As I don't use a repository pattern. Is it reasonable to put it inside a service?

解决方案

should service layer interfaces reside in a domain layer? For example user service?

In DDD you must distinguish between 2 types of services : Domain services and Application services (not mentioning Infrastructure services).

Domain services are located in the Domain layer and contain domain logic that doesn't belong in any entity or that spans across several entities.

Application services are in the Application layer and define application-specific operations that coordinate calls to the Domain layer and possibly return a result. They may correspond to a use case or a subpart of a use case. Application services are directly called by the client while Domain services can't be.

Application service interfaces don't need to be in the Domain layer since they are not domain-specific but application-specific, and the domain doesn't use them. The Application layer references the Domain layer but not the other way around.

what are the primary reasons to move a code part to a separate layer?

The first benefits that come to mind are reusability (reuse your Domain elsewhere without carrying all your application-specific stuff along with it) and maintainability (group together things that change together).

See Separation of concerns, Single Responsibility Principle, Cohesion.

should service layer reside at the same assembly as the application layer?

In DDD, there is no Service Layer. The Application Layer is often equivalent to what other approaches call a Service Layer, though.

As I don't use a repository pattern. Is it reasonable to put it inside a service?

"By the book" DDD would require a Repository for this kind of operations. Even if you don't follow DDD that closely, I'm not a big fan of naming everything a "Service" when there are tons of more accurate names to give them : Data Access Objects, Data Gateways, etc. The key thing to realize here is that an object containing these methods should be placed in a low-level data specific layer (DAL, Infrastructure layer) because it deals with one persistent data store (Raven DB) explicitely.