没有System.Web.Http的ASP.NET Web API控制器(可插入,解耦架构) [英] ASP.NET Web API Controllers without System.Web.Http (Pluggable, Decoupled Architecture)

查看:197
本文介绍了没有System.Web.Http的ASP.NET Web API控制器(可插入,解耦架构)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建使用的ASP.NET Web API可插入的项目框架。我有很多的胆量想通了,但我有与我不完全知道如何去耦架构的一部分问题。

I'm creating a pluggable project framework using ASP.NET Web API. I have a lot of the guts figured out but I'm having a problem with one part of the architecture that I'm not fully sure how to decouple.

作为一个简单的例子,我有以下几点:

As a simplified example, I have the following:


  • 服务/ presentation层(简单的Web API托管项目,这里面很少,而且它基本上只是负责处理在IIS托管壳或自托管)

  • 域层与核心,项目领域层片

  • 服务(N多种服务,每到域层的参考:觉得微服务)。目前,这些服务的的有来的ASP.NET Web API 2.1
  • 参考
  • Service/Presentation Layer (simple Web API hosted project, very little inside this, and it's basically just a shell which handles hosting in IIS or self-hosted)
  • Domain layer, with the core, domain-layer pieces of the project
  • Services (N number of services, each with a reference to the Domain layer: think microservices). Currently, each of these Services also has a reference to ASP.NET Web API 2.1

现在,该服务的所有引用领域层,因为它有接口,助手,等等。但是,领域层还引用这样的东西的ASP.NET Web API 2.1,等等。我的想法是在域一基地BaseController,每个服务的控制器可以继承,并反过来将所有必要继承获得更多的有线式的东西(),邮政()等问题是我不希望这些服务也必须引用的ASP.NET Web API 2.1和其他依赖。我希望他们能够通过抽象而无需实际直接引用他们使用它们。换句话说,我想松耦合和需要的服务是能够自主生活,自主System.Web.Http的。这将有助于我执行单元测试和SOLID原则作为项目的发展。

Now, the Services all reference the Domain layer because it has interfaces, helpers, etc. But, the Domain layer also references stuff like ASP.NET Web API 2.1, and more. My idea was to have a base "BaseController" in the Domain that each of the Services' controllers could inherit from, and in turn would all necessarily inherit the wired-up stuff like Get(), Post(), etc. The problem is that I don't want each of these services to also have to reference ASP.NET Web API 2.1 and the other dependencies. I want them to be able to use them through abstractions without actually referencing them directly. In other words, I want looser coupling and want the Services to be able to live autonomously and independent of System.Web.Http. It would help me enforce unit testing and SOLID principles as the project grows.

也许我在这里采取了错误的方法,但也许不会。我的主要目标是创建一个可插拔的,使用微服务架构的ASP.NET Web每个微服务生活作为一个独立的组件API。所以,如果有另一个推荐的方法,我所有的耳朵。

Maybe I'm taking the wrong approach here, but maybe not. My main goal is to create a pluggable, microservices framework using ASP.NET Web API with each microservices living as an independent assembly. So if there's another recommended approach I'm all ears.

希望这是有道理的。任何指针?对于它的价值,我很熟悉设计模式和SOLID原则的概念;只是不知道怎么这串在​​一起,同时保持一定的自动请求处理的控制器提供原生的。

Hopefully this makes sense. Any pointers? For what it's worth, I'm quite familiar with concepts of design patterns and SOLID principles; just not sure how to string this all together while maintaining some of the automatic request handling that controllers provide natively.

谢谢!

推荐答案

您的意图是伟大的,执行没有这么多。你实际上是所谓的应用服务,这些都是实现使用该域的应用程序用例的服务。如果说你不希望被耦合到System.Web.Http等我想你想说你不希望你的耦合到的WebAPI framwork应用程序(尤其是域)。

Your intention is great, implementation not so much. What you is actually called Application Services, those are the services that implement the application use cases using the Domain. When saying you don't want to be coupled to System.Web.Http etc I think you want to say you don't want your app (especially the Domain) coupled to the WebApi framwork.

我想你误会了意思脱钩。它不是真的不依赖于其他约一组装,大约是不是你的对象/层耦合到其他对象/层的实现细节。这就是为什么domain应的从不了解控制器的WebAPI,等的mvc他们不是域的一部分,他们的UI或公共API的一部分。

I think you've misunderstood what decoupling means. It's not really about one assembly not depending on another, is about not coupling your objects/layer to an implementation detail of other objects/layers. That's why the Domain should never know about controllers, WebApi, Mvc etc they aren't part of the Domain, they're part of the UI or public API.

就在API层(名字巧合)应该知道的控制器和你使用的(顺便提一句的WebAPI)任何框架,别的只知道他们的关注。因此,API(应用) - >应用层 - >域

Just the API layer (a name coincidence) should know about controllers and whatever framework you're using (incidentally WebApi),anything else only know about their concerns. So the API (uses) -> Application Layer -> Domain.

拥有自主的成分是好的,但你真的不想只是一个程序集来处理一切。保持干净的东西,并有类似:

Having autonomous components is ok, but you really don't want just one assembly to handle everything. Keep things clean and have something like:


  • MyFeature.ApplicationServices

  • MyFeature.Domain

  • MyFeature.Persistence

  • 甚至MyFeature.UIWidgets(网络/ WPF等)

这只是一个建议,最重要的事情是保持分开的关注。

This is just a suggestion, the important thing is to keep the concerns separated.

下面是一个位$ C $的C

Here's a bit of code

public class MyAppService 
{
       public void DoSmth(InputData data){}
}

public class MyApiController
{
     public MyApiController(MyAppService svc) { }

     public void Post(SomeModel model)
     {
        var svcInput=model.ToServiceInput();
        _svc.DoSmth(svcInput);
     }

}

您的应用程序服务获取来自控制器的输入模式,并使用它来完成其工作。服务永远不知道的WebAPI或任何东西,只有输入模型。事物是解耦,易于测试。

Your app service gets an input model from the controller and uses that to do its job. The Service never knows about webApi or anything, only about the input model. And things are decoupled and easy to test.

这篇关于没有System.Web.Http的ASP.NET Web API控制器(可插入,解耦架构)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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