static void Main(string[] args) { // 這裡將會建立 DI 容器 IUnityContainer container = new UnityContainer(); // 進行抽象型別與具體實作類別的註冊 container.RegisterType <ICompany, Company>(); // 進行抽象型別與具體實作類別的註冊,且 // ** 指定建構式注入、屬性注入、方法注入的條件 container.RegisterType <IEmployee, Employee>( new InjectionConstructor(typeof(ICompany)), new InjectionProperty("PropertyInjectionCompany", typeof(ICompany)), new InjectionMethod("Initialize", typeof(ICompany)), new InjectionMethod("AontherInitialize")); // 進行抽象型別的具體實作物件的解析 IEmployee emp = container.Resolve <IEmployee>(); // 執行取得物件的方法 emp.DisplaySalary(); Console.WriteLine("Press any key for continuing..."); Console.ReadKey(); }
static void Main(string[] args) { var container = new UnityContainer().LoadConfiguration(); IEmployee emp = container.Resolve <IEmployee>(); emp.DisplaySalary(); Console.WriteLine("Press any key for continuing..."); Console.ReadKey(); }
static void Main(string[] args) { IUnityContainer unitycontainer = new UnityContainer(); unitycontainer.RegisterType <ICompany, Company>(); unitycontainer.RegisterType <IEmployee, Employee>(); IEmployee emp = unitycontainer.Resolve <IEmployee>(); emp.DisplaySalary(); Console.WriteLine("Press any key for continuing..."); Console.ReadKey(); }
static void Main(string[] args) { var container = new UnityContainer(); container.RegisterTypes( AllClasses.FromLoadedAssemblies(), WithMappings.FromMatchingInterface, WithName.Default, WithLifetime.ContainerControlled); IEmployee emp = container.Resolve <IEmployee>(); emp.DisplaySalary(); Console.WriteLine("Press any key for continuing..."); Console.ReadKey(); }
static void Main(string[] args) { // 這裡將會建立 DI 容器 IUnityContainer unitycontainer = new UnityContainer(); // 進行抽象型別與具體實作類別的註冊 unitycontainer.RegisterType <ICompany, Company>(); unitycontainer.RegisterType <IEmployee, Employee>(); // 進行抽象型別的具體實作物件的解析 IEmployee emp = unitycontainer.Resolve <IEmployee>(); // 執行取得物件的方法 emp.DisplaySalary(); Console.WriteLine("Press any key for continuing..."); Console.ReadKey(); }