简体   繁体   English

如何将域和存储库层公开为服务?

[英]How to expose domain and repository layer as a service?

I have the following layers currently in my application: 我的应用程序当前具有以下几层:

  1. API API
  2. AppService Layer AppService层
  3. Domain Model 领域模型
  4. Repository 知识库

The lower (2) layers (domain and repository) offer almost total reuse across business needs so I thought of exposing at least my domain model as a service, possibly a WCF service. 较低的(2)层(域和存储库)提供了跨业务需求的几乎全部重用,因此我考虑至少将域模型作为服务公开,可能是WCF服务。 Basically the domain entity data, behavior, and persistance are reusable and I need a way to centrally expose this. 基本上,域实体数据,行为和持久性是可重用的,我需要一种集中公开此方法的方法。

I have read the following ( Sharing domain model with WCF service ) and ones similar that indicate for good reason why not to expose domain entities directly via WCF. 我已经阅读了以下内容( 与WCF服务共享域模型 )和类似内容,它们出于充分的原因表明了为什么直接通过WCF公开域实体。 Most of the examples indicate to create data contracts to expose domain data and then have mapping mechnisms to map between the domain and DTO (Data Contract) data. 大多数示例表明创建数据协定以公开域数据,然后使用映射机制在域和DTO(数据协定)数据之间进行映射。 So far so good. 到现在为止还挺好。

If I have a method in my domain layer like below, how do I expose it via the new WCF service? 如果我的域层中有一个如下所示的方法,如何通过新的WCF服务公开它? I thought I would only expose DTOs and therefore how do I also expose the shared/common domain behavior I want to reuse across processes? 我以为我只会公开DTO,因此如何公开我想跨进程重用的共享/公共域行为?

public int ProcessSomeRule(string param1, string param2)

If the answer is to create a method on the WCF service that acts as a proxy still not exposing the ProcessSomeRule method directly, I'm cool with that but that spawns another question. 如果答案是在WCF服务上创建一个充当代理的方法,而该方法仍然不直接公开ProcessSomeRule方法,那么我对此很满意,但这引发了另一个问题。 The purpose of this abstraction and creating a method on the WCF service named for example ProcessSomeRuleWCF(string,string) that just turns around internally and calls the domain method seems to still be coupled and not offer a pure abstraction. 这种抽象的目的以及在WCF服务上创建一个名为ProcessSomeRuleWCF(string,string)的方法的方法,该方法仅在内部转入并调用域方法,似乎仍处于耦合状态,而不提供纯抽象。 It will still be sensitive to signature changes. 它仍然对签名更改敏感。

I read that by not exposing the methods directly, we can make changes internally without having to modify the contract interface. 我读到,通过不直接公开方法,我们可以在内部进行更改,而无需修改合同接口。 OK, this sounds great by definition, but if my internal method needs an additional parameter and now has the signature `ProcessSomeRule(string, string, string), guess what? 好的,从定义上来说,这听起来不错,但是如果我的内部方法需要一个附加参数,并且现在具有签名`ProcessSomeRule(string,string,string),那您猜怎么着? My contract interface method will now break because it's missing the 3rd parameter, so my interface has to change. 我的合同界面方法现在会中断,因为它缺少第三个参数,因此我的界面必须更改。 Not sure how to do this and still make the abstraction worthwhile? 不确定如何执行此操作,仍然值得进行抽象吗?

My repository in the same manner is generic and can be used across processes and I want to reuse this layer too. 我的存储库以相同的方式通用,可以在各个流程中使用,我也想重用这一层。 Currently my repository (UoW) Interface is injected into my AppService layer, so I have no idea how this would work if Repository layer was a WCF service. 目前,我的存储库(UoW)接口已注入到我的AppService层中,所以我不知道如果存储库层是WCF服务,这将如何工作。

Question: How do I expose the shared behavior of the domain and repository layers and what would the names of these new layers be called? 问题:如何公开域和存储库层的共享行为,这些新层的名称将如何命名? Is the new layer exposing the domain named 'Domain Service' and sit between 'AppService' and 'DomainModel'? 新层是否公开了名为“域服务”的域并位于“ AppService”和“ DomainModel”之间?

Lastly, if I'm on the wrong path with any of this feel free to guide me back. 最后,如果我走错了路,请随意引导我。 Thanks! 谢谢!

How do I expose the shared behavior of the domain and repository layers and what would the names of these new layers be called? 如何公开域和存储库层的共享行为,以及这些新层的名称将被称为什么?

In the context of a hexagonal (ports & adapters) architecture , your domain layer would be encapsulated by application services thereby defining the core of your application. 六角形(端口和适配器)体系结构的上下文中,您的域层将被应用程序服务封装,从而定义了应用程序的核心。 Application services implement use cases by delegating to aggregates and domain services and orchestrating access to repositories and infrastructural services. 应用程序服务通过委派聚合和域服务以及协调对存储库和基础结构服务的访问来实现用例。 Next, you can "adapt" your application to infrastructure. 接下来,您可以将应用程序“调整”到基础架构中。 Having a repository implement an repository interface declared in the domain layer is an example of this. 让存储库实现在域层中声明的存储库接口就是一个例子。 The repository implementation serves as an adapter between the domain and the outside world. 存储库实现充当域与外界之间的适配器。 Similarly for WCF services, usually called open-host services in DDD, they adapt your application to a transport infrastructure. 类似地,对于WCF服务(在DDD中通常称为开放主机服务 ),它们使您的应用程序适应传输基础结构。 The WCF service, or ASP.NET WebAPI service, would be a relatively thin layer that delegates to application services. WCF服务或ASP.NET WebAPI服务将是委派给应用程序服务的相对较薄的层。 Again, the purpose of this layer is only to serve as an adapter. 同样,该层的目的仅是充当适配器。 This layer does own the contract that it exposes publicly. 该层确实拥有它公开公开的合同。 In DDD this is referred to as the published language . 在DDD中,这称为发布语言 In WCF, for example, it would be defined via DataContract and DataMember attributes. 例如,在WCF中,它将通过DataContractDataMember属性进行定义。 These contracts are how external systems will access your domain. 这些合同是外部系统访问您的域的方式。 Having a buffer between published contracts and the internal domain is important because they will likely change at different rates and have different consumer requirements. 在已发布的合同和内部域之间留有缓冲很重要,因为它们可能会以不同的速率变化并具有不同的消费者要求。

Is the new layer exposing the domain named 'Domain Service' and sit between 'AppService' and 'DomainModel'? 新层是否公开了名为“域服务”的域并位于“ AppService”和“ DomainModel”之间?

No, a domain service is part of the domain layer itself, sitting along with entities, aggregates and value objects. 不,域服务是域层本身的一部分,与实体,集合和值对象一起存在。

I think to expose domain object as service is not difficult. 我认为将域对象公开为服务并不困难。 but system is really combination service and UI. 但系统实际上是服务和UI的组合。 UI access service, and service can be implemented on different ways: DDD or database-central . UI访问服务和服务可以通过不同的方式实现:DDD或database-central。 so it maybe not a good idea to tell the service caller how we implement our business logic. 因此,告诉服务调用者我们如何实现业务逻辑可能不是一个好主意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM