<small id='LLRb7'></small><noframes id='LLRb7'>

      <i id='LLRb7'><tr id='LLRb7'><dt id='LLRb7'><q id='LLRb7'><span id='LLRb7'><b id='LLRb7'><form id='LLRb7'><ins id='LLRb7'></ins><ul id='LLRb7'></ul><sub id='LLRb7'></sub></form><legend id='LLRb7'></legend><bdo id='LLRb7'><pre id='LLRb7'><center id='LLRb7'></center></pre></bdo></b><th id='LLRb7'></th></span></q></dt></tr></i><div id='LLRb7'><tfoot id='LLRb7'></tfoot><dl id='LLRb7'><fieldset id='LLRb7'></fieldset></dl></div>
      • <bdo id='LLRb7'></bdo><ul id='LLRb7'></ul>

      <tfoot id='LLRb7'></tfoot><legend id='LLRb7'><style id='LLRb7'><dir id='LLRb7'><q id='LLRb7'></q></dir></style></legend>

      1. .Net Core Dependency Injection 从构造函数中注入

        时间:2023-07-10
        <legend id='D0k0A'><style id='D0k0A'><dir id='D0k0A'><q id='D0k0A'></q></dir></style></legend>
          <i id='D0k0A'><tr id='D0k0A'><dt id='D0k0A'><q id='D0k0A'><span id='D0k0A'><b id='D0k0A'><form id='D0k0A'><ins id='D0k0A'></ins><ul id='D0k0A'></ul><sub id='D0k0A'></sub></form><legend id='D0k0A'></legend><bdo id='D0k0A'><pre id='D0k0A'><center id='D0k0A'></center></pre></bdo></b><th id='D0k0A'></th></span></q></dt></tr></i><div id='D0k0A'><tfoot id='D0k0A'></tfoot><dl id='D0k0A'><fieldset id='D0k0A'></fieldset></dl></div>

          <small id='D0k0A'></small><noframes id='D0k0A'>

          <tfoot id='D0k0A'></tfoot>

            • <bdo id='D0k0A'></bdo><ul id='D0k0A'></ul>

                <tbody id='D0k0A'></tbody>

                1. 本文介绍了.Net Core Dependency Injection 从构造函数中注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我需要从构造函数中注入我在 Setup 中声明的所有内容.

                  I need to inject out of constructor, everything I declared in Setup.

                  我能做到吗?如何从构造函数中注入服务?类似于 Angular 2 中的 Injector 服务.

                  Ho can I do it ? How can I inject services out of constructor ? Something like Injector service in Angular 2.

                  在控制器中没有构造函数的情况下注入服务

                  类似的东西

                      public class ControllerBase : Controller
                      {
                          protected IRepository<Test> _test;
                          protected IRepository<Test1> _test1;
                          protected IRepository<Test2> _test2;
                  
                          public ControllerBase(INJECTOR injector)
                          {
                              _test = injector.inject(IRepository<Test>);
                              _test1 = injector.inject(IRepository<Test1>);
                              _test2 = injector.inject(IRepository<Test2>);
                          }
                      }
                  
                      public class SomeController : ControllerBase
                      {
                          public SomeController(INJECTOR injector)
                              : base(injector)
                          {
                  
                          }
                      }
                  

                  感谢@Rick van den Bosch 的回答

                  对于那些仍然无法得到我想要的东西的人:

                  public class ControllerBase : Controller
                  {
                      protected IRepository<Test> _test;
                      protected IRepository<Test1> _test1;
                      protected IRepository<Test2> _test2;
                  
                      public ControllerBase(IServiceProvider injector)
                      {
                          _test = injector.GetService<IRepository<Test>>();
                          _test1 = injector.GetService<IRepository<Test1>>();
                          _test2 = injector.GetService<IRepository<Test2>>();
                      }
                  }
                  
                  public class SomeController : ControllerBase
                  {
                      public SomeController(IServiceProvider injector)
                          : base(injector)
                      {
                          //HERE I HAVE ALL 3 REPO NOW WITHOUT EXTRA LINES
                      }
                  }
                  public class SomeController1 : ControllerBase
                  {
                      public SomeController1(IServiceProvider injector)
                          : base(injector)
                      {
                          //HERE I HAVE ALL 3 REPO NOW WITHOUT EXTRA LINES
                      }
                  }
                  

                  推荐答案

                  您可以将服务作为参数注入到操作方法中.这是通过使用属性 [FromServices] 标记参数来完成的.

                  You can inject the service as a parameter to the action method. This is done by marking the parameter with the attribute [FromServices].

                  这看起来像这样:

                  public IActionResult About([FromServices] IDateProvider dateProvider)
                  {
                      ViewData["Message"] = $"Current date is {dateProvider.CurrentDate}";
                  
                      return View();
                  }
                  

                  如果您正在寻找 BaseController 中的默认服务:您可以通过以下几种方式进行:

                  If you're looking for default services in a BaseController: you could go about that several ways:

                  1.仍然使用构造函数
                  这看起来像这样:

                  1. Still use a constructor
                  This would look something like this:

                  public class HomeController : BaseController
                  {
                      public HomeController(IDateProvider dateProvider) : base(dateProvider)
                      {
                      }
                  }
                  

                  public class BaseController
                  {
                      protected IDateProvider _dateProvider;
                  
                      protected BaseController(IDateProvider dateProvider)
                      {
                          _dateProvider = dateProvider;
                      }
                  }
                  

                  这样 IDateProvider 对 BaseController 和所有继承的 Controller 都可用.

                  This way the IDateProvider is available to both the BaseController and all inheriting Controllers.

                  <强>2.手动解析服务
                  这样您就可以手动解析服务.这可能在 BaseController 中,并且仅在您需要它们时(懒惰).有关详细信息,请参阅这篇文章.

                  为了简单和可读性,我可能会选择构造函数.

                  For simplicity and readability I would probably choose the constructor one.

                  这篇关于.Net Core Dependency Injection 从构造函数中注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何将数据从 AuthorizationHandler 传递到 Asp.net 核心中的控制器 下一篇:共享服务器上 PersistKeysToFileSystem 的文件路径

                  相关文章

                  <legend id='lS6KP'><style id='lS6KP'><dir id='lS6KP'><q id='lS6KP'></q></dir></style></legend>
                  <i id='lS6KP'><tr id='lS6KP'><dt id='lS6KP'><q id='lS6KP'><span id='lS6KP'><b id='lS6KP'><form id='lS6KP'><ins id='lS6KP'></ins><ul id='lS6KP'></ul><sub id='lS6KP'></sub></form><legend id='lS6KP'></legend><bdo id='lS6KP'><pre id='lS6KP'><center id='lS6KP'></center></pre></bdo></b><th id='lS6KP'></th></span></q></dt></tr></i><div id='lS6KP'><tfoot id='lS6KP'></tfoot><dl id='lS6KP'><fieldset id='lS6KP'></fieldset></dl></div>

                  <small id='lS6KP'></small><noframes id='lS6KP'>

                  1. <tfoot id='lS6KP'></tfoot>
                        <bdo id='lS6KP'></bdo><ul id='lS6KP'></ul>