依赖注入简单例子
using System;
using Microsoft.Extensions.DependencyInjection;
class Program
{
interface ITanGuan
{
void hello();
}
class TanGuan:ITanGuan
{
public int A{get;set;}
public int B{get;set;}
public TanGuan(IPower hs)
{
A = hs.GetA();
B = hs.GetB();
}
void ITanGuan.hello()
{
System.Console.WriteLine("hello");
}
}
interface IPower
{
int GetA();
int GetB();
}
class Power:IPower
{
public int GetA()
{
return 30;
}
public int GetB()
{
return 56;
}
}
static void Main(string[] args)
{
IServiceCollection services = new ServiceCollection();
services.AddSingleton(typeof(ITanGuan), typeof(TanGuan));
services.AddSingleton(typeof(IPower), typeof(Power));
IServiceProvider serviceProvider = services.BuildServiceProvider();
ITanGuan mything = serviceProvider.GetService<ITanGuan>();
}
}
上面这段代码中,类TanGuan依赖接口IPower。从第15行可以看出,TanGuan类的构造器需要传入IPower接口。
Main()方法内,就是通过IServiceCollection依赖容器将TanGuan和Power这两个类注入到了容器内。
52行则是生成一个带有依赖容器的IServiceProvider 依赖提供者。
53行就是通过IServiceProvider 提供方的GetService()方法获取ITanGuan服务的实体。
我们可以通过调试发现mything这个实体可以成功获取到TanGuan类实体。说明依赖注入框架能自动识别TanGuan构造器传入的IPower为Power,这个过程看起来都是自动的。
有了这个依赖注入,客户端代码与服务供应商之间的代码可以做到非常松的耦合。
因为不同的接口服务的具体类的供应商可以自己实现自己的类,如果客户端想更换不同的接口服务,只需要修改50行和51行的代码。
同一接口多次注入
下面的例子演示了,GetService<IEnumerable<…>>()的方法:
using System;
using System.Collections.Generic;
using Microsoft.Extensions.DependencyInjection;
class Program
{
interface ITanGuan
{
void hello();
}
class TanGuan:ITanGuan
{
public int A{get;set;}
public int B{get;set;}
public TanGuan(IPower hs)
{
A = hs.GetA();
B = hs.GetB();
}
void ITanGuan.hello()
{
System.Console.WriteLine("hello");
}
}
class TanGuan2:ITanGuan
{
void ITanGuan.hello()
{
System.Console.WriteLine("Hello 2");
}
}
interface IPower
{
int GetA();
int GetB();
}
class Power:IPower
{
public int GetA()
{
return 30;
}
public int GetB()
{
return 56;
}
}
static void Main(string[] args)
{
IServiceCollection services = new ServiceCollection();
services.AddSingleton(typeof(ITanGuan), typeof(TanGuan));
services.AddSingleton(typeof(ITanGuan), typeof(TanGuan2));
services.AddSingleton(typeof(IPower), typeof(Power));
IServiceProvider serviceProvider = services.BuildServiceProvider();
IEnumerable<ITanGuan> mything = serviceProvider.GetService<IEnumerable<ITanGuan>>();
}
}
依赖注入泛型接口
举例子:
using System;
using System.Collections.Generic;
using Microsoft.Extensions.DependencyInjection;
class Program
{
public interface IHome<T>
{
List<T> GetList();
}
class Home<T> : IHome<T>
{
private List<T> _list;
public Home()
{
_list = new List<T>();
}
public List<T> GetList()
{
return _list;
}
}
static void Main(string[] args)
{
IServiceCollection services = new ServiceCollection();
services.AddSingleton(typeof(IHome<>), typeof(Home<>)); // 注入泛型
IServiceProvider serviceProvider = services.BuildServiceProvider();
var d1 = serviceProvider.GetService<IHome<int>>(); // 获取服务IHome<int>
List<int> list = d1.GetList();
var d2 = serviceProvider.GetService<IHome<double>>(); // 获取服务IHome<double>
List<double> lsit2 = d2.GetList();
}
}
代码中有的:
services.AddSingleton(typeof(IHome<>), typeof(Home<>));
就是泛型接口的依赖注入。
在GetService()阶段,可以赋予不同的类型参数而产生不同的类型实例,显得非常高级。Asp.net Core中的ILogger<TCategoryName>接口就是这个注入原理。