The default ASP.NET Core multiple implementations (in many ways) how to inject injected under way - sky carrot stars - CSDN blog

Original: ASP.NET Core default injection method how to inject multiple implementations (in many ways) - sky carrot stars - CSDN blog

Disclaimer: This article is starfd original article, reproduced, please indicate the source. https://blog.csdn.net/starfd/article/details/81282651

In our development process, the service, the general definitions we interfacerealize are there is only a concrete realization of (some people say this case is the tenth generation single pass mode ), this time the Core default injection method has been to support our needs, but to one these abstract definitions realize is concerned, we may have a variety of specific implementation in the project, we may choose different implementation in accordance with specific needs.

First, under the first concrete example of our business scenario, suppose we have the following interfaceand its two concrete realization

    public interface ILogicService
    {
        int GetNo();
    }

    public class LogicSericeImpt1 : ILogicService
    {
        public int GetNo()
        {
            return 1;
        }
    }
    public class LogicSericeImpt2 : ILogicService
    {
        public int GetNo()
        {
            return 2;
        }
    }
    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

Example is very simple, it is different implementations return a different number, the following terms of how to obtain different implementations in the case of injection.

Direct injection IServiceProviderof way


Because Core Inside, IServiceProviderare allowed to be injected directly into specific applications in, so there is a direct by IServiceProvideracquiring specific ways to achieve.

1. By GetServiceway

First inject concrete implementation

services.AddTransient<LogicSericeImpt1>();
services.AddTransient<LogicSericeImpt2>();
    
    
  • 1
  • 2

Then acquires constructor embodied in the following manner

public TodoController(IServiceProvider serviceProvider)
{
    var s1FromProvider = serviceProvider.GetService<LogicSericeImpt1>();
    var s2FromProvider = serviceProvider.GetService<LogicSericeImpt2>();
}
    
    
  • 1
  • 2
  • 3
  • 4
  • 5

2. By GetServicesway

First injection interfaceand realization

services.AddTransient<ILogicService, LogicSericeImpt1>();
services.AddTransient<ILogicService, LogicSericeImpt2>();
    
    
  • 1
  • 2

Then acquires constructor embodied in the following manner

public TodoController(IServiceProvider serviceProvider)
{
    var logicAllRegs = serviceProvider.GetServices<ILogicService>().ToList();
    var s1FromAllRegs = logicAllRegs[0];
    var s2FromAllRegs = logicAllRegs[1];
}
    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

Direct injection mode set


In this way is actually eliminating the injection IServiceProviderprocess, directly GetServicesresults obtained are implanted. First injection interfaceand realization

services.AddTransient<ILogicService, LogicSericeImpt1>();
services.AddTransient<ILogicService, LogicSericeImpt2>();
    
    
  • 1
  • 2

Then acquires constructor embodied in the following manner

public TodoController(IEnumerable<ILogicService> services)
{
    var s1FromServices = services.First();
    var s2FromServices = services.Skip(1).First();
}
    
    
  • 1
  • 2
  • 3
  • 4
  • 5

Injected directly into Functhe factory mode


In this way, we are not injected directly into the concrete realization, but injected Funcsuch a factory implementation. First we need to inject concrete implementation

services.AddTransient<LogicSericeImpt1>();
services.AddTransient<LogicSericeImpt2>();
    
    
  • 1
  • 2

Then we continue to inject Func this plant, where we press intreturn different implementations, of course, you can also use other means such asstring

services.AddSingleton(provider =>
{
    Func<int, ILogicService> func = n =>
    {
        switch (n)
        {
            case 1:
                return provider.GetService<LogicSericeImpt1>();
            case 2:
                return provider.GetService<LogicSericeImpt2>();
            default:
                throw new NotSupportedException();
        }
    };
    return func;
});
    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

Then acquires constructor embodied in the following manner

public TodoController(Func<int, ILogicService> funcFactory)
{
    var s1FromFunc = funcFactory(1);
    var s2FromFunc = funcFactory(2);
}
    
    
  • 1
  • 2
  • 3
  • 4
  • 5

Guess you like

Origin www.cnblogs.com/lonelyxmas/p/10936998.html
Recommended