当前位置:开发工具->.NET ->Unity IOC容器的构造函数使用笔记

原创版权标志Unity IOC容器的构造函数使用笔记

作者:阿郎  发表时间:2018/5/12 10:53:16  阅读:
[摘要] 部分Unity IOC容器在C#中的使用范例,基于构造函数的各个使用示例
使用支付宝扫码领红包,余额宝付款才可以使用红包哦!不要忘记哈。每天扫一次,天天赚红包!!可以将二维码保存到手机,每天直接扫码领红包啦!!

    示例一,正常使用:

    相关定义:

    

public interface ICar
{
 int Run();
}

public class BMW : ICar
{
 private int _miles = 0;

 public int Run()
 {
 return ++_miles;
 }
}

public class Ford : ICar
{
 private int _miles = 0;
 public int Run()
 {
 return ++_miles;
 }
}

public class Audi : ICar
{
 private int _miles = 0;

 public int Run()
 {
 return ++_miles;
 }

}
public class Driver
{
 private ICar _car = null;

 public Driver(ICar car)
 {
 _car = car;
 }

 public void RunCar()
 {
 Console.WriteLine("Running {0} - {1} mile ", _car.GetType().Name, _car.Run());
 }
}

    

    使用:

    

var container = new UnityContainer();
container.RegisterType<ICar, BMW>();

var driver = container.Resolve<Driver>();
driver.RunCar();

    

    我们使用Resolve来解析我们想要的调用对象,在这里是Driver。

    示例二,多个注入:

    相关定义的代码不变,将使用部分的代码改为:

    

var container = new UnityContainer();
container.RegisterType<ICar, BMW>();
container.RegisterType<ICar, Audi>("LuxuryCar");

// Register Driver type 
container.RegisterType<Driver>("LuxuryCarDriver", 
 new InjectionConstructor(container.Resolve<ICar>("LuxuryCar")));

Driver driver1 = container.Resolve<Driver>();// injects BMW
driver1.RunCar();

Driver driver2 = container.Resolve<Driver>("LuxuryCarDriver");// injects Audi
driver2.RunCar();

    

    示例三,多个构造函数参数:

    相关定义:

    

public interface ICarKey { 
 
}

public class BMWKey : ICarKey 
{

}

public class AudiKey : ICarKey 
{

}

public class FordKey : ICarKey 
{

}

public class Driver
{
 private ICar _car = null;
 private ICarKey _key = null;

 public Driver(ICar car, ICarKey key) 
 {
 _car = car;
 _key = key;
 }

 public void RunCar()
 {
 Console.WriteLine("Running {0} with {1} - {2} mile ", _car.GetType().Name , _key.GetType().Name, _car.Run());
 }
}

    

    相关使用

    

var container = new UnityContainer();
 
container.RegisterType<ICar, Audi>();
container.RegisterType<ICarKey, AudiKey>();

var driver = container.Resolve<Driver>();
driver.RunCar();

    

    示例四,多个构造函数:

    相关定义不变,将使用部分代码改为:

    

public class Driver
{
 private ICar _car = null;
 
 [InjectionConstructor]
 public Driver(ICar car)
 {
 _car = car;
 }
 
 public Driver(string name)
 {
 }
 
 public void RunCar()
 {
 Console.WriteLine("Running {0} - {1} mile ", _car.GetType().Name, _car.Run());
 }
}

    

    

container.RegisterType<Driver>(new InjectionConstructor(new Ford()));

//or 

container.RegisterType<ICar, Ford>();
container.RegisterType<Driver>(new InjectionConstructor(container.Resolve<ICar>()));

    

    示例五,基类型作为构造函数参数:

    相关定义:

    

public class Driver
{
 private ICar _car = null;
 private string _name = string.Empty;

 public Driver(ICar car, string driverName)
 {
 _car = car;
 _name = driverName;
 }

 public void RunCar()
 {
 Console.WriteLine("{0} is running {1} - {2} mile ", 
 _name, _car.GetType().Name, _car.Run());
 }
}

    

    相关使用:

    

var container = new UnityContainer();
 
container.RegisterType<Driver>(new InjectionConstructor(new object[] { new Audi(), "Steve" }));

var driver = container.Resolve<Driver>(); // Injects Audi and Steve
driver.RunCar();

    

    完整请参考:

    http://www.tutorialsteacher.com/ioc/constructor-injection-using-unity-container

微信扫码关注公众号CPP技术网,微信号cpp_coder,关注我们的公众号,阅读更多精彩内容!每天还可以领取大红包哦!!!每天还可以领取大红包哦!!!每天还可以领取大红包哦!!!
文章来源:C++技术网原创文章版权为网站和作者共同所有,会员文章禁止转载。非会员文章转载做好本文超链接即表示授权转载。通过文章下面的分享按钮可以自由分享所有文章。

返回顶部

在线提问
问题标题:
问题描述:(简陋的描述会导致问题被最后回答、没有针对性回答甚至无法解答。请确保问题描述的足够清楚。)