How to Install, Configure and Instantiate the Data Access Application Block

The Enterprise Library is one of my favorite third-party reusable software component.  One of the most important library is the Data Access Application Block.  The Data Access Application block simplify how you connect to a database. With this block you can connect to a Sql Server, Oracle, SqlCE databases.

Today I will talk about how to configure and create instances of a database, especially Sql Server database.  Since the enterprise Library is now on version 6.0, I will be using this version for my examples.

Installation

To install the Data Access Application Block into your project or solution you have two options: download it from the Patterns & Practices page or using NuGet.  With NuGet, you can search for “enterpriselibrary” on the NuGet Manager. Then, you select the Data Access Application Block from the list of files to be added, and all files needed are referenced in your project.

When downloading, either way, you will be adding two dll’s to your project: Microsft.Practices.EnterpriseLibrary.Common.dll and Microsoft.Practices.EnterpriseLibrary.Data.dll; the first one takes care of the configuration of this block and the others, and the second one is the basic functionality of the Data Access Application Block.

Configuration

After downloading and referencing the Data Access block dll’s, you will need to configure your configuration files (app.config or web.config) to use it.  You will be entering the following three new nodes:

<configSection>
    
<section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.enterpriseLibrary.Data" requiredPermission="true" />;          
</configSection>

<dataConfiguration defaultDatabase="MyDefaultDatabase" />

<connectionStrings>
    <add name="MyDefaultDatabase" connectionString="my_connection_string" providerName="System.Data.SqlClient" />
</connectionStrings>

As you can see, adding a new section called dataConfiguration on the node, we are telling our project that we will be using the Data Access Application block, and that it will be configured later in a node called .  On the node we only need to tell the application which will be our default database.  If we are not going to use any default database, we can simply remove the defaultDatabase attribute from the node.  On the node we are adding all the connections needed for our application to connect to a database.  it can be as many as needed.  The name attribute for each connection string is the key for the value of the defaultDatabase attribute in the node.

Instantiate

Every time we instantiate a new Database object on our code using the Data Access Application Block we will need to use the DatabaseProviderFactory class.  This class use the factory pattern to read the configuration that we put on our configuration file (app.cofig or web.config).

To Instantiate a new Database object in our code we can use several ways:

1. Create a Default Database

if you decided to use the defaultDatabase attribute on the node, you can easily create a database without telling the code to use an specific database.  You can create a database object using the defaultAttribute as follow:

var factory = new DatabaseProviderFactory();
var myDatabase = factory.CreateDefault();

2. Create a Named Database

Suppose that you have more than one connection string on your configuration file and you want to select only one of them for your code and it is not your default database, what would you do? You will use the Create method of the DatabaseProviderFactory class.

For example you have the given configuration file nodes:

<dataConfiguration defaultDatabase="MyDefaultDatabase" />

<connectionStrings>
&nbsp;&nbsp;&nbsp; <add name="MyDefaultDatabase" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; connectionString="my_connection_string" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; providerName="System.Data.SqlClient" />
&nbsp;&nbsp;&nbsp; <add name="MySecondDatabase" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; connectionString="my_second_connection_string" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; providerName="System.Data.SqlClient" />
</connectionStrings>

You can call “MySecondDatabase” database passing the name of the connection string to the Create method as follow:

var factory = new DatabaseProviderFactory();
var mySecondDatabase = factory.Create("MySecondDatabase");

Conclusion

There are others ways to create or instantiate a Database object using the Data Access Application Block, like using the DatabaseFactory Fa├žade. The two mentioned above are the most common ways.

As you can see installing, configuring and instantiating the Data Access Application Block is easy and will help you a lot in your coding day by day.

 

About the Author

Rodnney

I am a "multi hat" software developer with more than 18 years in experience. I had worked from government agencies, insurance companies, ticketing system and educational business using a lot of technologies like SQL Server, Oracle, ASP.NET, MVC, HTML, CSS, Javascript, Linq, etc.

Leave a Reply