Base load intensity for non-controllers

It seems crazy that something like this causes me such a headache. But here it is:

How do you use the built-in dependency injection for the network core for an uncontrolled class? Please provide an example with instantiation.

Thank.

+4
source share
2 answers

Just make the class a service.

In startup.cs

services.AddScoped<AccountBusinessLayer>();

Then in the controller, as for other services:

private readonly AccountBusinessLayer _ABL;

Include in the constructor, as for other services:

 public AccountController(
    UserManager<ApplicationUser> userManager,
    SignInManager<ApplicationUser> signInManager,IOptions<IdentityCookieOptions> identityCookieOptions,
    IEmailSender emailSender,
    ISmsSender smsSender,
    ILoggerFactory loggerFactory,
    RoleManager<IdentityRole> roleManager,
    AccountBusinessLayer ABL
  )
{
  _userManager = userManager;
  _signInManager = signInManager;
  _externalCookieScheme = identityCookieOptions.Value.ExternalCookieAuthenticationScheme;
  _emailSender = emailSender;
  _smsSender = smsSender;
  _logger = loggerFactory.CreateLogger<AccountController>();
  _roleManager = roleManager;
  _ABL = ABL;
}
+2
source

I am not sure if this is the best answer, but as I decided to do this, I need to do the following:

1) @BrunoLM ASP.NET Core DI, @SystemCrash, UnderstandingDependencyInjection .

: , , , , , (# 1). , , , , SO.

2) OtherService. DoSomething(), TestService.

3) OtherService IServiceProvider, ITestService, GenerateRandom().

4) HomeController.cs, IServiceProvider OtherService.

, , :

OtherService.cs

using System;
using Microsoft.Extensions.DependencyInjection;

namespace UnderstandingDependencyInjection.Services
{
    public class OtherService
    {
        private readonly ITestService _testService;

        public OtherService(IServiceProvider serviceProvider)
        {
            _testService = serviceProvider.GetService<ITestService>();
        }

        public int DoSomething()
        {
            var rnd = _testService.GenerateRandom();
            return rnd * 2;
        }
    }
}

HomeController.cs

using Microsoft.Extensions.DependencyInjection;
using UnderstandingDependencyInjection.Services;

namespace UnderstandingDependencyInjection.Controllers
{
    public class HomeController : Controller
    {
        private readonly ITestService _testService;
        private readonly IServiceProvider _serviceProvider;

        public HomeController(IServiceProvider serviceProvider)
        {
            _serviceProvider = serviceProvider;
            _testService = serviceProvider.GetService<ITestService>();
        }

        public IActionResult Index()
        {
            // This works!
            // var rnd = _testService.GenerateRandom();

            // What if I need to reference the TestService 
            // from another service? I.e., OtherService?
            var otherService = new OtherService(_serviceProvider);

            var rnd = otherService.DoSomething();

            ViewBag.RandomNumber = rnd;
            return View();
        }

, , IServiceProvider, ... , - , ASP.NET Core Core.


, TestService?

/ OtherService. , , ASP.NET Core MVC Dependency Injection. ?

, , ON THE METHOD CALL . , , ... .

5) (), StaticService.

6) DoSomething, IServiceProvider .

7) IServiceProvider ITestService. GenerateRandom().

8) StaticService.DoSomething(), IServiceProvider, .

StaticService.cs

using Microsoft.Extensions.DependencyInjection;

namespace UnderstandingDependencyInjection.Services
{
    public class StaticService
    {
        // No constructors

        public static int DoSomething(IServiceProvider serviceProvider)
        {

            var testService = serviceProvider.GetService<ITestService>();
            var rnd = testService.GenerateRandom();
            return rnd * 3;
        }
    }
}

HomeController.cs

    public IActionResult Index()
    {
        // This works!
        // var rnd = _testService.GenerateRandom();

        // What if I need to reference the TestService 
        // from another service? I.e., OtherService?
        //var otherService = new OtherService(_serviceProvider);
        //var rnd = otherService.DoSomething();

        // What if I need to reference the TestService
        // from another service with a STATIC method?
        // Best I can tell, you have to pass the 
        // ServiceProvider in on the method call.
        var rnd = StaticService.DoSomething(_serviceProvider);

        ViewBag.RandomNumber = rnd;
        return View();
    }

ServiceProvider -?

, . ServiceProvider . , - , ASP.NET Core DI. , .

? , , DI? , IOtherService, ITestService ?

, , - BOTH ITestService AND IOtherService. , , OtherService, ITestService . .

?


?

, , :

Injection Dependency Factory ASP.NET

@Steven :

, , DI ASP.NET Core , ILogger, .

+1

Source: https://habr.com/ru/post/1666607/


All Articles