Unity IOC容器的构造函数使用笔记(不能错过的Unity示例)

示例一,正常使用:

相关定义:

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());
    }
}
View Code

使用:

var container = new UnityContainer();
container.RegisterType();

var driver = container.Resolve();
driver.RunCar();
View Code

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

示例二,多个注入:

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

var container = new UnityContainer();
container.RegisterType();
container.RegisterType("LuxuryCar");

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

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

Driver driver2 = container.Resolve("LuxuryCarDriver");// injects Audi
driver2.RunCar();
View Code

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

相关定义:

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());
    }
}
View Code

相关使用:

var container = new UnityContainer();
            
container.RegisterType();
container.RegisterType();

var driver = container.Resolve();
driver.RunCar();
View Code

示例四,多个构造函数:

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

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());
    }
}
View Code
container.RegisterType(new InjectionConstructor(new Ford()));

//or 

container.RegisterType();
container.RegisterType(new InjectionConstructor(container.Resolve()));
View Code

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

相关定义:

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());
    }
} 
View Code

相关使用:

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

var driver = container.Resolve(); // Injects Audi and Steve
driver.RunCar();
View Code

完整请参考:

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

转载于:https://www.cnblogs.com/zhiyong-ITNote/p/9026749.html

你可能感兴趣的:(Unity IOC容器的构造函数使用笔记(不能错过的Unity示例))