解说还是围绕着QuickStart展开的
在第二章的基础上添加引用
Microsoft.Practices.Unity
Microsoft.Practices.Unity.Configuration
System.configuration
然后在配置文件中添加以下节
1
<
unity
>
2
<
containers
>
3
<
container
name
="dataContainer"
>
4
<
extensions
>
5
<
add
type
="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Unity.EnterpriseLibraryCoreExtension, Microsoft.Practices.EnterpriseLibrary.Common"
/>
6
<
add
type
="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.Unity.DataAccessBlockExtension, Microsoft.Practices.EnterpriseLibrary.Data"
/>
7
</
extensions
>
8
</
container
>
9
</
containers
>
10
</
unity
>
![None.gif](/Images/OutliningIndicators/None.gif)
2
![None.gif](/Images/OutliningIndicators/None.gif)
3
![None.gif](/Images/OutliningIndicators/None.gif)
4
![None.gif](/Images/OutliningIndicators/None.gif)
5
![None.gif](/Images/OutliningIndicators/None.gif)
6
![None.gif](/Images/OutliningIndicators/None.gif)
7
![None.gif](/Images/OutliningIndicators/None.gif)
8
![None.gif](/Images/OutliningIndicators/None.gif)
9
![None.gif](/Images/OutliningIndicators/None.gif)
10
![None.gif](/Images/OutliningIndicators/None.gif)
并且在configSections中注册该节点:
<
section
name
="unity"
type
="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration"
/>
在DAO中标记依赖注入
1
public
class
SalesData
2![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
3
private Database db;
4![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
5
[InjectionConstructor]
6
public SalesData([Dependency]Database db)
7![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
8
this.db = db;
9
}
10![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
11
//以下代码省略
![None.gif](/Images/OutliningIndicators/None.gif)
2
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](/Images/OutliningIndicators/ContractedBlock.gif)
![dot.gif](/Images/dot.gif)
3
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
4
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
5
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
6
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
7
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](/Images/dot.gif)
8
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
9
![ExpandedSubBlockEnd.gif](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
10
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
11
![ExpandedBlockEnd.gif](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
[InjectionConstructor]标签用来标记注入哪个构造函数。
只有一个构造函数的话,该标签可以省略。
有两个构造函数的时候,会采用有参数的构造函数。
有该标签的时候,采用被标记的构造函数。
[Dependency]用来标记依赖。
下面是Main的代码
1
private
static
void
Main()
2![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
3
IUnityContainer container = new UnityContainer();
4![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
5
//从配置文件中读取unity节点
6
UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
7
//用该节点的信息配置container
8
section.Containers["dataContainer"].Configure(container);
9![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
10
//用容器配置创建QuickStartForm
11
AppForm = container.Resolve<QuickStartForm>();
12![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
13
Application.Run(AppForm);
14
}
![None.gif](/Images/OutliningIndicators/None.gif)
2
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](/Images/OutliningIndicators/ContractedBlock.gif)
![dot.gif](/Images/dot.gif)
3
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
4
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
5
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
6
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
7
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
8
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
9
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
10
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
11
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
12
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
13
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
14
![ExpandedBlockEnd.gif](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
Unity会自动配置相关的依赖关系