In our development process, the service, the general definitions we interface
realize 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 interface
and 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 IServiceProvider
of way
Because Core Inside, IServiceProvider
are allowed to be injected directly into specific applications in, so there is a direct by IServiceProvider
acquiring specific ways to achieve.
1. By GetService
way
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 GetServices
way
First injection interface
and 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 IServiceProvider
process, directly GetServices
results obtained are implanted. First injection interface
and 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 Func
the factory mode
In this way, we are not injected directly into the concrete realization, but injected Func
such 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 int
return 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