Brief introduction
Unity : U nity is a Microsoft team to develop lightweight, extensible dependency injection container, provides a good solution for loosely coupled application that supports constructor injection, injection property, method injection.
Inversion of Control : (Inversion of Control, abbreviated as the IoC), is used to subtractive coupling problems between the application, the upper layer of the lower layer dependent program proceeds to a third party to assemble the container.
Dependency injection : (Dependency Injection, abbreviated DI): We'll issue a request to the vessel, the process to obtain the object instance is called dependency injection.
achieve
Use NuGet packages Add a reference:
Enter Unity.Configuration search and download automatically add the required package (Unity.Container, System.Runtime.CompilerServices.Unsafe, Unity.Abstractions)
Such as:
Configuration files are as follows:
<? XML Version = "1.0" encoding = "UTF-. 8" ?> < Configuration > < configSections > <-! Association between handlers and configuration section defines the configuration element. -> < sectionTop name = "Unity" type = "Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Unity.Configuration" /> </ configSections > < Unity > < Containers > < Container > <-! Type of object name, the name written MapTo object to inject the two parts are separated by commas, all one class, including the namespace, and second assembly name -> < mapTo="命名空间,程序集名称"> <lifetime type="singleton" /> <!--Api接口--> <property name="Url" value="*****" /> <!--用户名--> <property name="UserName" value="*****" /> <!--密码--> <property name="UserPassword" value="*****"/> </register> </container> </containers> </unity> </configuration>
Profile Initialization:
var configFileNames the Directory.GetFiles = ( @ " D: \ folder a " , " * .config " ); IF (configFileNames.Length =! . 1 ) { MessageBox.Show ($ @ " D: \ 'a folder 'which can be a profile, and now have a {configFileNames.Length} " ); return ; } var filemap = new new ExeConfigurationFileMap { ExeConfigFilename = configFileNames [ 0 ] }; // read configuration information from the config file var configuration =ConfigurationManager.OpenMappedExeConfiguration (filemap, ConfigurationUserLevel.None); // get the configuration section specifies the name var Section = (UnityConfigurationSection) configuration.GetSection ( " Unity " ); // create a container var _Uc = new new UnityContainer (); // load container section.Configure (_Uc);
Add a public member in VMMainWindow years, and with the [Dependency] mark at the top:
[Dependency] public IPms Pms { get; set; }
With RegisterSingleton to register:
_Uc.RegisterSingleton<VMMainWindow>();
Finally, using the Resolve to resolve:
var vmMain = UC.Resolve<VMMainWindow>();
This will get to the contents inside the container.
Note: The text reproduced from personal bloggers station old landscape Cen come to know? , Description link using Unity container implementation property injected .
Unity : U nity is a Microsoft team to develop lightweight, extensible dependency injection container, provides a good solution for loosely coupled application that supports constructor injection, injection property, method injection.
Inversion of Control : (Inversion of Control, abbreviated as the IoC), is used to subtractive coupling problems between the application, the upper layer of the lower layer dependent program proceeds to a third party to assemble the container.
Dependency injection : (Dependency Injection, abbreviated DI): We'll issue a request to the vessel, the process to obtain the object instance is called dependency injection.
achieve
Use NuGet packages Add a reference:
Enter Unity.Configuration search and download automatically add the required package (Unity.Container, System.Runtime.CompilerServices.Unsafe, Unity.Abstractions)
Such as:
Configuration files are as follows:
<?xml version="1.0" encoding="utf-8"?> <configuration> <configSections> <!--定义配置节处理程序与配置元素之间的关联。--> <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,Unity.Configuration" /> </configSections> <unity> <containers> <container> <!--type为对象的名称,mapTo为注入对象的名称 写法为用逗号隔开两部分,一是类的全部,包括命名空间,二是程序集名称--> <register type="命名空间,程序集名称" mapTo="命名空间,程序集名称"> <lifetime type="singleton" /> <!--Api接口--> <property name="Url" value="*****" /> <!--用户名--> <property name="UserName" value="*****" /> <!--密码--> <property name="UserPassword" value="*****"/> </register> </container> </containers> </unity> </configuration>
配置文件初始化:
var configFileNames = Directory.GetFiles(@"D:\某个文件夹", "*.config"); if (configFileNames.Length != 1) { MessageBox.Show($@"D:\‘某个文件夹’里面必须只能一个配置文件,现在有{configFileNames.Length}个"); return; } var fileMap = new ExeConfigurationFileMap { ExeConfigFilename = configFileNames[0] }; //从config文件中读取配置信息 var configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None); //获取指定名称的配置节 var section = (UnityConfigurationSection)configuration.GetSection("unity"); //创建容器 var _Uc = new UnityContainer(); //载入容器 section.Configure(_Uc);
在VMMainWindow里添加公共成员,并用 [Dependency]在上方标记:
[Dependency] public IPms Pms { get; set; }
用RegisterSingleton去注册:
_Uc.RegisterSingleton<VMMainWindow>();
最后使用Resolve去解析:
var vmMain = UC.Resolve<VMMainWindow>();
这样就获取到了容器里面的内容。
Note: The text reproduced from personal bloggers station old landscape Cen come to know? , Description link using Unity container implementation property injected .