[英]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.