Unity
1、nuget引入程序包
>Unity
>Unity.Abstractions
>Unity.Configuration
>Unity.Container
>Unity.Interception
>Unity.Interception.Configuration
2.放置单独的配置文件
3.读取配置文件
1 public class UnityFactory 2 { 3 private static UnityContainer _container; 4 static UnityFactory() 5 { 6 ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap { 7 ExeConfigFilename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory,@"config\unity.config") 8 }; 9 Configuration configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None); 10 //指定该节点的处理程序 11 UnityConfigurationSection section = (UnityConfigurationSection)configuration.GetSection(UnityConfigurationSection.SectionName); 12 _container = new UnityContainer(); 13 section.Configure(_container, "container1"); 14 } 15 public static UnityContainer GetUnityFacory() 16 { 17 return _container; 18 } 19 }
4.使用
需要注意的点:
>unity配置文件设置为始终复制
>接口类的实现项目dll需要复制到bin文件中