A lot of great news this weekend coming out of Microsoft Patterns & Practices. We have both the release of Enterprise Library 4.0 and Unity 1.1:
I have been very eager to see how the Enterprise Library Team planned to integrate Unity with Enterprise Library 4.0, and it turns out they did it quite simply using Unity Extensions. Simple and boring, which is just like we like to keep our code. Nobody likes tricky or clever code as it just makes our jobs more difficult.
Configuring the Data Access Application Block
Nothing new in the Enterprise Library 4.0 DAAB other than a few extra performance counters and a few classes to work with Unity. Therefore, the configuration of the Data Access Application Block using the Visual Studio 2008 Enterprise Library Configuration Editor shouldn't surprise you. Here we have 2 connection strings, Northwind and Northwind2, created in our DAAB Configuration Section with the default database set as Northwind.

DataAccessBlockExtension
I am going to assume you know a little bit about Unity, which is the dependency injection container from Microsoft Patterns & Practices. If not, the Enterprise Library 4.0 Documentation has nice coverage of Unity. If you want more information, you can check out this Unity Screencast - Unity Dependency Injection IoC Screencast.
Unity is extensible just like most IoC Tools, and the Data Access Application Block takes advantage of that extensibility to register the database settings in your IConfigurationSource into Unity by introducing a class called the DataAccessBlockExtension. The DataAccessBlockExtension doesn't do anything more than essentially using the DatabaseConfigurationView Class to iterate through your database settings and create the appropriate type mappings in Unity. If interested, I can talk about the details later, but you can read this article on the DatabaseConfigurationView for some ideas:
What this means is that registering your database settings in Unity is as simple as configuring your Unity Container to use the DataAccessBlockExtension:
IUnityContainer container = new UnityContainer();
container.AddNewExtension<EnterpriseLibraryCoreExtension>();
container.AddNewExtension<DataAccessBlockExtension>();
Told you it was boring :) You will notice an extra UnityContainerExtension, called EnterpriseLibraryCoreExtension, registered before the DataAccessBlockExtension. EnterpriseLibraryCoreExtension always has to be registered before the other Enterprise Library Extensions because it essentially establishes the location of the IConfigurationSource which is used by the other extensions, like the DataAccessBlockExtension. Above we are telling EnterpriseLibraryCoreExtension to use SystemConfigurationSource ( App.config or Web.config ), which is what it will use by default.
Using DAAB and Unity
Given the App.config and sample code above on how to register the database types with Unity, you can immediately start using Unity to resolve the databases:
IUnityContainer container = new UnityContainer();
container.AddNewExtension<EnterpriseLibraryCoreExtension>();
container.AddNewExtension<DataAccessBlockExtension>();
// Default Database
Database db = container.Resolve<Database>();
// Named Database
Database db2 = container.Resolve<Database>("Northwind2");
Injecting Database Classes into Custom Classes
As you would expect, Unity will of course inject the Data Access Application Block Database Classes into your custom classes whether you are using Constructor Injection, Property Injection, or Method Injection. Therefore let's say we have a CustomerDAO class that takes our default database class as a constructor parameter:
public class CustomerDAO
{
public CustomerDAO(Database db)
{
// ...
}
}
At the very simplest, you can create an instance of CustomerDAO using Unity as such and it will automatically inject our default database into the CustomerDAO Instance:
CustomerDAO dao = container.Resolve<CustomerDAO>();
And that is all there is to it.
Conclusion
Using the Unity IoC and Dependency Injection Tool with Enterprise Library 4.0 is pretty simple using the UnityContainer Extensions that ship with Enterprise Library 4.0. In this tutorial I showed you how to use Unity with the Enterprise Library 4.0 Data Access Application Block using the DataAccessBlock Exension.
Hope this helps,
David Hayden