• <tfoot id='1fKJ5'></tfoot>

      <legend id='1fKJ5'><style id='1fKJ5'><dir id='1fKJ5'><q id='1fKJ5'></q></dir></style></legend>

        <i id='1fKJ5'><tr id='1fKJ5'><dt id='1fKJ5'><q id='1fKJ5'><span id='1fKJ5'><b id='1fKJ5'><form id='1fKJ5'><ins id='1fKJ5'></ins><ul id='1fKJ5'></ul><sub id='1fKJ5'></sub></form><legend id='1fKJ5'></legend><bdo id='1fKJ5'><pre id='1fKJ5'><center id='1fKJ5'></center></pre></bdo></b><th id='1fKJ5'></th></span></q></dt></tr></i><div id='1fKJ5'><tfoot id='1fKJ5'></tfoot><dl id='1fKJ5'><fieldset id='1fKJ5'></fieldset></dl></div>
      1. <small id='1fKJ5'></small><noframes id='1fKJ5'>

          <bdo id='1fKJ5'></bdo><ul id='1fKJ5'></ul>
      2. 依赖注入与控制器类以外的类

        时间:2023-06-08
      3. <small id='3tdVE'></small><noframes id='3tdVE'>

            <legend id='3tdVE'><style id='3tdVE'><dir id='3tdVE'><q id='3tdVE'></q></dir></style></legend>
                • <bdo id='3tdVE'></bdo><ul id='3tdVE'></ul>

                  <i id='3tdVE'><tr id='3tdVE'><dt id='3tdVE'><q id='3tdVE'><span id='3tdVE'><b id='3tdVE'><form id='3tdVE'><ins id='3tdVE'></ins><ul id='3tdVE'></ul><sub id='3tdVE'></sub></form><legend id='3tdVE'></legend><bdo id='3tdVE'><pre id='3tdVE'><center id='3tdVE'></center></pre></bdo></b><th id='3tdVE'></th></span></q></dt></tr></i><div id='3tdVE'><tfoot id='3tdVE'></tfoot><dl id='3tdVE'><fieldset id='3tdVE'></fieldset></dl></div>
                • <tfoot id='3tdVE'></tfoot>
                    <tbody id='3tdVE'></tbody>
                  本文介绍了依赖注入与控制器类以外的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  此时我可以轻松地将东西注入到我的控制器中,在某些情况下构建我自己的 ResolverServices 类.生活是美好的.

                  At this point I'm injecting things into my Controllers with ease, in some cases building my own ResolverServices class. Life is good.

                  我不知道该怎么做是让框架自动注入到非控制器类中.起作用的是让框架自动注入我的控制器 IOptions,这实际上是我项目的配置:

                  What I cannot figure out how to do is get the framework to automatically inject into non-controller classes. What does work is having the framework automatically inject into my controller IOptions, which is effectively the configuration for my project:

                  public class MessageCenterController : Controller
                  {
                      private readonly MyOptions _options;
                  
                      public MessageCenterController(IOptions<MyOptions> options)
                      {
                          _options = options.Value;
                      }
                  }
                  

                  我正在考虑是否可以为我自己的课程做同样的事情.当我模仿控制器时,我假设我已经接近了,如下所示:

                  I'm thinking whether I can do the same for for my own classes. I assume I'm close when I mimic the controller, like this:

                  public class MyHelper
                  {
                      private readonly ProfileOptions _options;
                  
                      public MyHelper(IOptions<ProfileOptions> options)
                      {
                          _options = options.Value;
                      }
                  
                      public bool CheckIt()
                      {
                          return _options.SomeBoolValue;
                      }
                  }
                  

                  我认为我失败的地方是当我这样称呼它时:

                  I think where I'm failing is when I call it like this:

                  public void DoSomething()
                  {
                      var helper = new MyHelper(??????);
                  
                      if (helper.CheckIt())
                      {
                          // Do Something
                      }
                  }
                  

                  我追查到的问题实际上是所有关于 DI 的讨论都是在控制器级别讨论的.我尝试在 Controller 对象源代码中寻找它发生的位置,但它在那里变得有点疯狂.

                  The problem I have tracking this down is practically everything that talks about DI is talking about it at the controller level. I tried hunting down where it happens in the Controller object source code, but it gets kinda crazy in there.

                  我知道我可以手动创建一个 IOptions 实例并将其传递给 MyHelper 构造函数,但似乎我应该能够让框架这样做,因为它适用于 控制器.

                  I do know I can manually create an instance of IOptions and pass it to the MyHelper constructor, but it seems like I should be able to get the framework do that since it works for Controllers.

                  推荐答案

                  下面是一个不涉及 MVC 控制器的使用 DI 的工作示例.这是我需要做的来理解这个过程,所以也许它会帮助其他人.

                  Below is a working example of using DI without anything that involves MVC Controllers. This is what I needed to do to understand the process, so maybe it will help somebody else.

                  ShoppingCart 对象通过 DI 获得一个 INotifier 实例(通知客户他们的订单.)

                  The ShoppingCart object gets, via DI, an instance of INotifier (which notifies the customer of their order.)

                  using Microsoft.Extensions.DependencyInjection;
                  using System;
                  
                  namespace DiSample
                  {
                      // STEP 1: Define an interface.
                      /// <summary>
                      /// Defines how a user is notified. 
                      /// </summary>
                      public interface INotifier
                      {
                          void Send(string from, string to, string subject, string body);
                      }
                  
                      // STEP 2: Implement the interface
                      /// <summary>
                      /// Implementation of INotifier that notifies users by email.
                      /// </summary>
                      public class EmailNotifier : INotifier
                      {
                          public void Send(string from, string to, string subject, string body)
                          {
                              // TODO: Connect to something that will send an email.
                          }
                      }
                  
                      // STEP 3: Create a class that requires an implementation of the interface.
                      public class ShoppingCart
                      {
                          INotifier _notifier;
                  
                          public ShoppingCart(INotifier notifier)
                          {
                              _notifier = notifier;
                          }
                  
                          public void PlaceOrder(string customerEmail, string orderInfo)
                          {
                              _notifier.Send("admin@store.com", customerEmail, $"Order Placed", $"Thank you for your order of {orderInfo}");
                          }
                  
                      }
                  
                      public class Program
                      {
                          // STEP 4: Create console app to setup DI
                          static void Main(string[] args)
                          {
                              // create service collection
                              var serviceCollection = new ServiceCollection();
                  
                              // ConfigureServices(serviceCollection)
                              serviceCollection.AddTransient<INotifier, EmailNotifier>();
                  
                              // create service provider
                              var serviceProvider = serviceCollection.BuildServiceProvider();
                  
                              // This is where DI magic happens:
                              var myCart = ActivatorUtilities.CreateInstance<ShoppingCart>(serviceProvider);
                  
                              myCart.PlaceOrder("customer@home.com", "2 Widgets");
                  
                              System.Console.Write("Press any key to end.");
                              System.Console.ReadLine();
                          }
                      }
                  }
                  

                  这篇关于依赖注入与控制器类以外的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:用于 Mongodb 数据存储的 asp.net 核心中基于简单令牌的身份验证/授权 下一篇:如何实现“纯"ASP.NET Core Web API 使用 AddMvcCore()

                  相关文章

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

                        <bdo id='EZHdM'></bdo><ul id='EZHdM'></ul>

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

                    1. <tfoot id='EZHdM'></tfoot>