Unity IOC框架使用實例
阿新 • • 發佈:2018-08-15
clas file 一個 ase depend 函數 csdn ner jsb
1.IOC簡介
IOC(Inversion of Control), 控制反轉
DI (Dependency Injection),依賴註入
IOC的基本概念是:不創建對象,但是描述創建它們的方式。在代碼中不直接與對象和服務連接,但在配置文件中描述哪一個組件需要哪一項服務。容器負責將這些聯系在一起。
2.Unity引入
3.創建單例模式容器類
using Microsoft.Practices.Unity; using Microsoft.Practices.Unity.Configuration; using System; using System.Collections.Generic; using System.Configuration; using System.Linq; using System.Text; using System.Threading.Tasks; namespace UnityIocTest { /// <summary> /// Unity IOC單例模式 /// </summary> public class UnitySingleton { //單例 private static UnitySingleton instance; //ioc容器 public IUnityContainer container; //獲取單例 public static UnitySingleton getInstance() { if (instance == null || instance.container == null) { string configFile = "Unity.config"; var fileMap = new ExeConfigurationFileMap { ExeConfigFilename = configFile }; //從config文件中讀取配置信息 Configuration configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None); //獲取指定名稱的配置節 UnityConfigurationSection section = (UnityConfigurationSection)configuration.GetSection("unity"); instance = new UnitySingleton() { //container = new UnityContainer().LoadConfiguration((UnityConfigurationSection)ConfigurationManager.GetSection("unity"), "MyContainer") container = new UnityContainer().LoadConfiguration(section, "MyContainer") //container = new UnityContainer() }; //instance.container.RegisterType<IExampleClass, ExampleClass>(); } return instance; } //IOC註入實體 public static T GetInstanceDAL<T>() { return getInstance().container.Resolve<T>(); } } }
此處是將Unity config配置單獨成一個文件,方便管理。
4.添加IOC相關類
接口類
public interface IExampleClass { void DoHelloWord(); }
具體實現類
public class ExampleClass : IExampleClass { public void DoHelloWord() { Console.WriteLine("Hello Word!"); } }
實現類擴展
public class DIExampleClass { //屬性註入 [Dependency] public IExampleClass example { get; set; } private IExampleClass testInject; public void DoWork() { example.DoHelloWord(); testInject.DoHelloWord(); } //方法註入 [InjectionMethod] public void Initialize(IExampleClass instance) { testInject = instance; } }
5.添加配置文件Unity.config
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,Microsoft.Practices.Unity.Configuration" /> </configSections> <unity> <!--定義類型別名--> <aliases> <add alias="IExampleClass" type="UnityIocTest.IExampleClass,UnityIocTest" /> <add alias="ExampleClass" type="UnityIocTest.ExampleClass,UnityIocTest" /> </aliases> <!--容器--> <container name="MyContainer"> <!--映射關系--> <!--<register type="IExampleClass" mapTo="ExampleClass"></register>--> <register type="UnityIocTest.IExampleClass,UnityIocTest" mapTo="UnityIocTest.ExampleClass,UnityIocTest"></register> </container> </unity> </configuration>
6.函數調用
class Program { static void Main(string[] args) { //var dao = UnitySingleton.GetInstanceDAL<IExampleClass>(); var dao = UnitySingleton.GetInstanceDAL<DIExampleClass>(); dao.DoWork(); Console.ReadLine(); } }
7.結果顯示
Hello Word!
Hello Word!
Unity IOC框架使用實例