简体   繁体   English

Dotnet核心方法注入控制器方法

[英]Dotnet core method injection with controller methods

Lets say i have the following controller in dotnet core: 假设我在dotnet核心中有以下控制器:

[Route("api/v1/[controller]")]
public class ValuesController : Controller
{
    private readonly IHandler<ValuesRequest, ValuesResponse> _valueHandler;
    private readonly IHandler<ValuesIdRequest, ValuesIdResponse> _valueIdHandler;

    public ValuesController(IHandler<ValuesRequest, ValuesResponse> valueHandler, 
                            IHandler<ValuesIdRequest, ValuesIdResponse> valueIdHandler)
    {
        _valueHandler = valueHandler;
        _valueIdHandler = valueIdHandler;
    }

    [HttpGet]
    public ValuesResponse Get(ValuesRequest request)
    {
        return _valueHandler.Handle(request);
    }

    [HttpGet("{id}")]
    public ValuesIdResponse Get(ValuesIdRequest request)
    {
        return _valueIdHandler.Handle(request);
    }
}

As you can see in the code above, I'm using dependency injection though the constructor. 正如您在上面的代码中看到的,我通过构造函数使用依赖注入。 However, I was thinking on how I could reduce the amount of code. 但是,我在考虑如何减少代码量。 So, I was thinking about using method injection, which should reduce the code to something like this: 所以,我正在考虑使用方法注入,这应该将代码减少到这样:

[Route("api/v1/[controller]")]
public class ValuesController : Controller
{
    [HttpGet]
    public ValuesResponse Get(ValuesRequest request, IHandler<ValuesRequest, ValuesResponse> handler)
    {
        return handler.Handle(request);
    }

    [HttpGet("{id}")]
    public ValuesIdResponse Get(ValuesIdRequest request, IHandler<ValuesIdRequest, ValuesIdResponse> handler)
    {
        return handler.Handle(request);
    }
}

I was wondering if it is possible to do something like this in combination with controller params. 我想知道是否可以结合控制器参数做这样的事情。 I tried finding an answer on the web, however I could not find similar problem/solution. 我试着在网上找到答案,但我找不到类似的问题/解决方案。

Reference Action Injection with FromServices 使用FromServices引用操作注入

Sometimes you don't need a service for more than one action within your controller. 有时,您不需要为控制器中的多个操作提供服务。 In this case, it may make sense to inject the service as a parameter to the action method. 在这种情况下,将服务作为参数注入动作方法可能是有意义的。 This is done by marking the parameter with the attribute [FromServices] as shown here: 这是通过使用属性[FromServices]标记参数来完成的,如下所示:

public ValuesResponse Get(ValuesRequest request, [FromServices]IHandler<ValuesRequest, ValuesResponse> handler)
{
    return handler.Handle(request);
}

While the answer would work using [FromService] within your actions, I have another suggestion. 虽然答案可以在你的行动中使用[FromService],但我有另一个建议。

From what I understand by reading the code you have provided is, that you use some kind of CQRS. 根据我的理解,通过阅读您提供的代码,您使用某种CQRS。 For that case I can suggest MediatR . 对于那种情况,我可以建议MediatR You will then only need to inject one interface into your controller and send your request using the IMediator . 然后,您只需要将一个接口注入控制器并使用IMediator发送请求。 This way you will keep your controller small and clean and you will not need to inject all the other handlers. 通过这种方式,您可以保持控制器的小巧和清洁,并且无需注入所有其他处理程序。

There is a nice and handy extension for Microsoft's IoC-Container to register all your handlers and all other necessary classes to use MediatR. 微软的IoC-Container有一个很好的方便的扩展 ,可以注册所有处理程序和所有其他必要的类来使用MediatR。

services.AddMediatR(typeof(Startup).Assembly);

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

相关问题 Dotnet Core API - 获取控制器方法的 URL - Dotnet Core API - Get the URL of a controller method 在调用 BuildServiceProvider 之前将 dotnet 核心依赖注入到扩展方法 - dotnet core dependency injection to extension method before BuildServiceProvider is called 在dotnet核心控制器中进行单元测试非公开方法 - Unit testing non-public methods in dotnet core controller 在 dotnet core 中使用 NLog 捕获控制器名称和方法名称 - Capture controller name and method name using NLog in dotnet core 使用依赖项注入dotnet内核在应用程序上运行Singleton实例方法启动 - Running a Singleton instance method on app start up using dependency injection dotnet core DotNet 核心依赖注入工厂类型 - DotNet Core Dependency Injection Factory Types GraphQL dotnet core 查询 class 依赖注入 - GraphQL dotnet core Query class dependency injection 依赖注入到 SignalR 集线器(dotnet core 3.1) - Dependency Injection into SignalR Hub (dotnet core 3.1) C#dotnet core 2将数据从中间件/过滤器传递给控制器​​方法 - C# dotnet core 2 pass data from middleware/filter to controller method Angular 6 HttpClient Post 未命中 DotNet Core 2.2 API 控制器中的 API Post 方法 - Angular 6 HttpClient Post not hitting API Post Method in DotNet Core 2.2 API Controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM