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