<div >
依赖注入是有“传染性”的,如果一个类的对象是通过DI创建的,那么这个类的构造函数中声明的所有服务类型的参数都会被DI赋值;
但是如果一个对象是程序员手动创建的,那么这个对象就和DI没有关系,它的构造函数中声明的服务类型参数就不会被自动赋值
<div/>
<div .net默认构造函数注入>
<div 除了构造函数注入, 还可以通过写函数时,对函数参数加[FromService] 标记依赖注入>
<div/>
依赖注入的步骤:
1 创立IOC容器
ServiceCollection services = new ServiceCollection();
2 向IOC注册
services.AddScoped<ITestService, TestServicelmp1>
3 建立服务定位器 Locator
ServiceProvider sp = services.BuildServiceProvider()
4 从Locator中拿出方法GetService
ITestService ts1 = sp.GetService<ITestService>();
using System;
using System.Reflection;
using Microsoft.Extensions.DependencyInjection;
namespace DependencyInjection
{
class Program
{
static void Main(string[] args)
{
//var driver = new Driver(new HeavyTank());
//driver.Drive();
接口隔离的好处就是, 有选择性的继承对象的一部分属性
在这里, driver并没有Fire方法, 只有Diver()
//var dv1 = driver as ITank;
//dv1.Fire();
此处的dv1 有了Fire() 因为传进来的HeavyTank 继承了 ITank, 而ITank继承了IWeapon
//var dv2 = (ITank)driver;
//dv2.Fire();
此处的dv2 也有了Fire()
//Console.ReadKey();
//反射的原理: 通过描述找到方法
//ITank tank = new HeavyTank();
//var t = tank.GetType();
//object o = Activator.CreateInstance(t);
//MethodInfo fireMi = t.GetMethod("Fire");
//MethodInfo runMi = t.GetMethod("Run");
//fireMi.Invoke(o, null);
//runMi.Invoke(o, null);
//Console.ReadKey();
var serviceContainer = new ServiceCollection();
serviceContainer.AddScoped(typeof(ITank), typeof(HeavyTank)); //Type serviceType, Type implementationType
serviceContainer.AddScoped(typeof(IVehicle), typeof(Car)); //方法类型
serviceContainer.AddScoped<Driver>(); //构造器<Driver> 将方法类型 注入到构造器
var serviceProvider = serviceContainer.BuildServiceProvider();
//--------------分割线----------------以上为注册过程
ITank tank = serviceProvider.GetService<ITank>();
tank.Fire();
tank.Run();
//Console.ReadKey();
var driver = serviceProvider.GetService<Driver>();
driver.Drive();
Console.ReadKey();
}
}
class Driver
{
private IVehicle _vehicle; //此处使用了接口隔离,
public Driver(IVehicle vehicle)
{
_vehicle = vehicle;
}
public void Drive()
{
_vehicle.Run();
}
}
interface IVehicle
{
public void Run();
}
interface IWeapon
{
public void Fire();
}
interface ITank:IVehicle,IWeapon
{
}
//interface ITank
//{
// void Fire();
// void Run();
//}
class Car : IVehicle
{
public void Run()
{
Console.WriteLine("Car is running");
}
}
class Truck : IVehicle
{
public void Run()
{
Console.WriteLine("Truck is running");
}
}
class LightTank : ITank
{
public void Fire()
{
Console.WriteLine("Boom!");
}
public void Run()
{
Console.WriteLine("Ka ka ka!");
}
}
class MediumTank : ITank
{
public void Fire()
{
Console.WriteLine("Boom!!");
}
public void Run()
{
Console.WriteLine("Ka ka ka!!");
}
}
class HeavyTank : ITank
{
public void Fire()
{
Console.WriteLine("Boom!!!");
}
public void Run()
{
Console.WriteLine("Ka ka ka!!!");
}
}
}
using Microsoft.Extensions.DependencyInjection;
using System;
namespace DependencyInjectionDemon
{
class Program
{
static void Main(string[] arges)
{
ServiceCollection services = new ServiceCollection();
services.AddScoped<ITestService, TestServicelmp1>();//服务的类型, 实现的类型
//services.AddScoped(typeof(ITestService), typeof(TestServicelmp1));//与上面一样
using (ServiceProvider sp = services.BuildServiceProvider())
{
//TestServicelmp1 ts1 = sp.GetService<ITestService>();
ITestService ts1 = sp.GetService<ITestService>();
ts1.Name = "宋江";
ts1.SayHi();
Console.WriteLine(ts1.GetType());
}
}
static void Main1(string[] args)
{
ServiceCollection services = new ServiceCollection(); //构造容器对象
services.AddTransient<TestServicelmp1>(); //添加瞬态服务
using (ServiceProvider sp = services.BuildServiceProvider()) //创建provider 相当于serviceLocator 服务定位器
{
//服务定位器
TestServicelmp1 t = sp.GetService<TestServicelmp1>(); //向Provider要对象
t.Name = "西门庆";
TestServicelmp1 t1 = sp.GetService<TestServicelmp1>();
t1.Name = "武大郎";
t.SayHi();
t1.SayHi();
Console.WriteLine("--------------------");
Console.WriteLine(object.ReferenceEquals(t,t1));
//I am 西门庆
//I am 武大郎
//--------------------
//False
}
services.AddSingleton<TestServicelmp1>(); //添加单例
using (ServiceProvider sp = services.BuildServiceProvider()) //创建provider 相当于serviceLocator 服务定位器
{
//服务定位器
TestServicelmp1 t = sp.GetService<TestServicelmp1>(); //向Provider要对象
t.Name = "西门庆";
TestServicelmp1 t1 = sp.GetService<TestServicelmp1>();
t1.Name = "武大郎";
t.SayHi();
t1.SayHi();
Console.WriteLine("--------------------");
Console.WriteLine(object.ReferenceEquals(t, t1));
//I am 武大郎
//I am 武大郎
//--------------------
//True
}
services.AddScoped<TestServicelmp1>(); //添加范围对象
using (ServiceProvider sp = services.BuildServiceProvider()) //创建provider 相当于serviceLocator 服务定位器
{
using (IServiceScope scope1 = sp.CreateScope())
{
//在scope中获取Scope相关的对象 scope1.ServiceProvider而不是sp
//scope1.ServiceProvider.GetService<TestServicelmp1>();
//服务定位器
TestServicelmp1 t = scope1.ServiceProvider.GetService<TestServicelmp1>(); //向Provider要对象
t.Name = "西门庆";
TestServicelmp1 t1 = scope1.ServiceProvider.GetService<TestServicelmp1>();
t1.Name = "武大郎";
t.SayHi();
t1.SayHi();
Console.WriteLine("--------------------");
Console.WriteLine(object.ReferenceEquals(t, t1));
//I am 武大郎
//I am 武大郎
//--------------------
//True
}
}
}
}
}
// See https://aka.ms/new-console-template for more information
using ConfigTestDemon;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
//ConfigurationBuilder cfgBuilder = new ConfigurationBuilder();
optional: true 可以检测报错 reloadOnChange: true 自动加载. json设置成更新时复制
//cfgBuilder.AddJsonFile("config.json", optional: true, reloadOnChange: true);
//IConfigurationRoot configRoot = cfgBuilder.Build();
//Console.WriteLine("--------------原始方法----------------------------------");
//string name = configRoot["name"];
//Console.WriteLine($"name={name}");
//string add = configRoot.GetSection("proxy:address").Value;
//Console.WriteLine($"addr={add}");
//Console.WriteLine("--------------使用微软的Configuration.Binder----------------------------------");
使用微软的Configuration.Binder
//Proxy proxy = configRoot.GetSection("proxy").Get<Proxy>();
//Console.WriteLine($"IP地址是:{ proxy.Address},端口号是{proxy.Port}");
//Console.WriteLine("--------------直接读取根节点----------------------------");
//Config config = configRoot.Get<Config>();
//Console.WriteLine($"姓名是{config.Name}, 年龄是{config.Age}, IP地址是:{ config.Proxy.Address},端口号是{config.Proxy.Port}");
///*
// 结果:
//--------------原始方法----------------------------------
//name=zg
//addr=aa
//--------------使用微软的Configuration.Binder----------------------------------
//IP地址是:aa,端口号是8080
//--------------直接读取根节点----------------------------
//姓名是zg, 年龄是18, IP地址是:aa,端口号是8080
// */
//Console.WriteLine("--------------使用DI依赖注入----------------------------");
ConfigurationBuilder cfgBuilder = new ConfigurationBuilder();
//使用CommandLine
//optional: true 可以检测报错 reloadOnChange: true 自动加载. json设置成更新时复制
cfgBuilder.AddJsonFile("config.json", optional: true, reloadOnChange: true);
IConfigurationRoot configRoot = cfgBuilder.Build();
ServiceCollection services = new ServiceCollection(); //构造容器对象
services.AddScoped<TestController>(); //注册
services.AddScoped<OnlyProxy>();
//向IOC中注册AddOptions
//将Config对象绑在根节点configRoot
services.AddOptions()
.Configure<Config>(e => configRoot.Bind(e))
.Configure<Proxy>(e => configRoot.GetSection("proxy").Bind(e));
using (ServiceProvider sp = services.BuildServiceProvider()) //创建provider对象
{
while (true)
{
await Task.Delay(1000);
using (var scope = sp.CreateScope())
{
//新建CreateScope 可以自动刷新文本json的改变
Console.WriteLine("-----------读取完整的TestController---Test方法实现----------------------------");
var c = sp.GetRequiredService<TestController>();
c.Test();
Console.WriteLine("-----------只读取Proxy的地址和端口号---Test方法实现----------------------------");
var c2 = sp.GetRequiredService<OnlyProxy>();
c2.Test();
}
}
/*
* 结果:
--------------使用DI依赖注入----------------------------
-----------读取完整的TestController---Test方法实现----------------------------
18
---------------
aa
-----------只读取Proxy的地址和端口号---Test方法实现----------------------------
用OnlyProxy实现IP为aa,端口号为:8080
*/
}
public class Proxy
{
public string? Address { get; set; }
public string? Port { get; set; }
}
public class Config
{
public string? Name { get; set; }
public string? Age { get; set; }
public Proxy? Proxy { get; set; }
}
版权声明:本文为helldoger原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。