简体   繁体   English

MVC 4 - 在数据层中消耗WCF

[英]MVC 4 - consuming a WCF in a data layer

In my test project, I created a WCF service and got it running. 在我的测试项目中,我创建了一个WCF服务并使其运行。 Next, I created an MVC 4 project. 接下来,我创建了一个MVC 4项目。 It is broken into layers under one solution. 它在一个解决方案下分成几层。

  • The model layer. 模型层。
  • The UI/View/Controller layer UI /视图/控制器层
  • The repository layer. 存储库层。

To do a quick test: In the UI layer, I added a web reference to my WCF service. 要进行快速测试:在UI层,我添加了一个Web引用到我的WCF服务。 In the controller, I contacted the WCF service by "using" to populate a dropdown in the view. 在控制器中,我通过“使用”联系WCF服务以填充视图中的下拉列表。

However, I'm driving for separation with Dependency Injection. 但是,我正在驾驶与依赖注入分离。

In the repository layer, I created an Interface with the populate dropdown and injected it. 在存储库层中,我创建了一个带有填充下拉列表的接口并将其注入。 Not a problem. 不是问题。

What I'm struggling with the concept is: 我正在努力解决的问题是:

  1. Do I consume the WCF service in the UI layer and reference it in the repository layer? 我是否在UI层中使用WCF服务并在存储库层中引用它? (doesn't seem right) (似乎不对)
  2. Do I create another project - a data layer - and add a web reference to the WCF service then from the repository, I create a reference to the data layer? 我是否创建了另一个项目 - 数据层 - 然后从存储库添加Web引用到WCF服务,我创建了对数据层的引用?

Which brings me to another question - if I create a web reference to a WCF service in a separate project (layer), the information pertaining to the WCF service is not present in the main config.sys file... 这让我想到另一个问题 - 如果我在单独的项目(层)中创建对WCF服务的Web引用,则主config.sys文件中不存在与WCF服务相关的信息...

So I'm struggling to grasp this portion...anything I should be reading up on some more? 所以我正在努力抓住这一部分...我还应该阅读更多内容吗?

Realizing that your cake can be cut in many ways, I present you here with my take on how to cut it: 意识到你的蛋糕可以在很多方面切割,我在这里介绍如何切割它:

Question #1: "Do I consume the WCF service in the UI layer and reference it in the repository layer?" 问题1:“我是否在UI层中使用WCF服务并在存储库层中引用它?” First off: As Steven points to in your comment, you should consider deeply if you want to inject a third server tier. 首先:正如史蒂文在你的评论中指出的那样,你应该深入考虑是否要注入第三个服务器层。 The UI layer can be renamed to the "layer that is hosted in IIS". UI层可以重命名为“IIS中托管的图层”。 Doing so makes it the host layer for both your MVC presentation layer and the host layer for the services. 这样做使其成为MVC表示层和服务主机层的主机层。 However this does not mean that your services are implemented in this layer, only that they are hosted here (a matter of referencing the interfaces and implementation namespaces in the web.config only). 但是,这并不意味着您的服务是在此层中实现的,只是它们在此处托管(仅在web.config中引用接口和实现名称空间的问题)。

Question #2: "Do I create another project - a data layer - and add a web reference to the WCF service then from the repository, I create a reference to the data layer?" 问题2:“我是否创建了另一个项目 - 数据层 - 然后从存储库添加Web引用到WCF服务,我创建了对数据层的引用?” As I read this you are asking multiple questions at once: Yes, you should create a separate layer to encapsulate the repository. 在我读到这篇文章时,你会同时提出多个问题:是的,你应该创建一个单独的层来封装存储库。 No you should not let this layer contain anything related to wcf. 不,你不应该让这个图层包含任何与wcf相关的内容。 If a server layer needs client access to a wcf service, you should separate this into a logical layer, either residing as a folder in your businesslogic layer, or as a separate physical "data adapter layer". 如果服务器层需要客户端访问wcf服务,则应将其分为逻辑层,或者作为businesslogic层中的文件夹,或者作为单独的物理“数据适配器层”。 Never ever in your repository layer. 永远不会在您的存储库层。 While it is true that conceptually the wcf data adapter layer is at the same level as your repository layer, you should not mix database access and service access in the same logical or physical layer. 虽然概念上wcf数据适配器层与存储库层处于同一级别,但您不应在同一逻辑层或物理层中混合使用数据库访问和服务访问。

The third question I can only answer as a question: What purpose is it that you want WCF to fullfil in your architecture? 第三个问题我只能回答一个问题:您希望WCF在您的架构中实现满足的目的是什么? Do you want to inject a separate service tier or have you confused layer separation with tier separation? 您是要注入单独的服务层还是让层分离与层分离混淆?

Layer separation can be coupled runtime in-process, while tier separation can only be coupled runtime through some sort of on-the-network remoting (such as wcf). 层分离可以在进程内运行时耦合,而层分离只能通过某种网络远程处理(例如wcf)耦合运行时。

As Steven points out in his comment, you havnt really documented the need to use wcf in your architecture at all. 正如史蒂文在评论中指出的那样,你真的记录了在你的架构中使用wcf的必要性。

Lets look at your architecture in terms of the logical layers and tiers (cutting the cake): 让我们根据逻辑层和层(切蛋糕)来看待您的架构:

  • Client-Access-Layer (#1, running in the context of the browser) 客户端访问层(#1,在浏览器的上下文中运行)

  • Web-Host-Layer (#2, representing the outer server tier, contains all MVC code. Perhaps Model exists in its own physical layer. Web-Host-Layer(#2,代表外部服务器层,包含所有MVC代码。也许Model存在于其自己的物理层中。

  • Service-Layer (Not existing yet in your architecture, as there have been no need demonstrated for this further abstraction of layers into tiers) 服务层(在您的架构中尚未存在,因为没有必要证明这种层进一步抽象为层)

  • Business-Layer (#2, representing the business logic that lies between presentation and database and data access layers) 业务层(#2,表示介绍与数据库和数据访问层之间的业务逻辑)

  • Repository layer (#2, encapsulating database access logic and mapping) 存储库层(#2,封装数据库访问逻辑和映射)

  • Service Access layer (#2, encapsulating access to services on the outside, can be implemented as a folder in BL layer or in a separate physical layer) 服务访问层(#2,封装对外部服务的访问,可以实现为BL层或单独的物理层中的文件夹)

  • Database tier (#3, the sql server) 数据库层(#3,sql server)

So are you asking how to implement DI in WCF using IIS as the host and C# as the implementation language. 那么您是否在使用IIS作为主机并使用C#作为实现语言来询问如何在WCF中实现DI。

Or. 要么。

Are you asking general guidance into architecturing a n-tier .net application. 您是否要求构建n层.net应用程序的一般指导。

The first is easy, as there a tons of examples out there, such as this 第一个很容易,因为那里有很多例子,比如这个

The second is more tricky as the answer will always be "it depends". 第二个更棘手,因为答案永远是“它取决于”。 It depends on what your needs are, what your level of expertise is, what the projected timespan of your project is and so on. 这取决于您的需求,您的专业水平,项目预计的时间长度等等。

Hope this helps you a bit. 希望这对你有所帮助。

If you want I can send you my implementation of the behavior wcf unity stuff. 如果你想我可以发送给我执行wcf统一的行为。

Concepts: 概念:

  • Logical layer - an architectural concept used to isolate responsibility. 逻辑层 - 用于隔离责任的架构概念。 Layers have downward coupling, never upwards. 层具有向下耦合,从不向上。

  • Physical layer - an implementation construct used to enforce the logical separation. 物理层 - 用于强制逻辑分离的实现构造。 A .net assembly can be used as the implementation of a physical layer. .net程序集可用作物理层的实现。

  • Tier - one or more layers that can exist on a machine that is not hosting other tiers. 层 - 可以存在于未托管其他层的计算机上的一个或多个层。

It sounds to me like you're treating the WCF call as a data source, which makes it a natural fit for the repository layer. 听起来像你将WCF调用视为数据源,这使得它非常适合存储库层。 The repository's job is to abstract knowledge of the data source's implementation from the other layers. 存储库的工作是从其他层抽象出数据源实现的知识。

I would recommend not using .NET projects to enforce layer boundaries but rather to use folders within the same project to enforce logical separation rather than physical separation. 我建议不要使用.NET项目来强制执行层边界,而是使用同一项目中的文件夹来强制逻辑分离而不是物理分离。 Most use cases don't require physical separation and it adds complexity, such as the more difficult configuration you noted. 大多数用例不需要物理隔离,这增加了复杂性,例如您注意到的配置更加困难。

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

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