示例一,正常使用: 相關定義: public interface ICar { int Run(); } public class BMW : ICar { private int _miles = 0; public int Run() { return ++_miles; } } public ...
示例一,正常使用:
相關定義:
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<ICar, BMW>(); var driver = container.Resolve<Driver>(); driver.RunCar();View Code
我們使用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();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<ICar, Audi>(); container.RegisterType<ICarKey, AudiKey>(); var driver = container.Resolve<Driver>(); 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<Driver>(new InjectionConstructor(new Ford())); //or container.RegisterType<ICar, Ford>(); container.RegisterType<Driver>(new InjectionConstructor(container.Resolve<ICar>()));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<Driver>(new InjectionConstructor(new object[] { new Audi(), "Steve" })); var driver = container.Resolve<Driver>(); // Injects Audi and Steve driver.RunCar();View Code
完整請參考:
http://www.tutorialsteacher.com/ioc/constructor-injection-using-unity-container