4 Using the Microsoft Enterprise Library : Data Access Application Block Overview

Data Access Application Block Overview
The Data Access Application Block (DAAB) is designed to allow developers to replace ADO.NET boiler-plate code with standardized code for everyday database tasks. The overloaded methods in the Database class can:
If your application needs to address specific DBMS functionality, you can use a DataDirect Connect for ADO.NET data provider.
When Should You Use DAABs?
DAABs include a small number of methods that simplify the most common methods of accessing a database. Each method encapsulates the logic required to retrieve the data and manage the connection to the database. You should consider using the application block if your application uses standard data access techniques.
The DAAB is used with ADO.NET, increasing efficiency and productivity when creating applications for ADO.NET. The abstract Database class provides a number of methods, such as ExecuteNonQuery, ExecuteReader, and ExecuteScalar, that are the same as the methods that are used by the DbCommand class, or, if you are using database-specific code, a data provider-specific class such as OracleCommand.
Although using the default DAAB during development is convenient, the resulting application lacks portability. When you use the provider-specific DataDirect Connect for ADO.NET DAAB implementation, the application includes the DataDirect Connect data providers’ SQL leveling capabilities. You have more flexibility, whether your application needs to access multiple databases, or whether you anticipate a change in your target data source.
Should You Use Generic or Database-specific Classes?
The application block supplements the code in ADO.NET that allows you to use the same code with different database types. You have two choices when using the DAAB with DataDirect Connect for ADO.NET:
The GenericDatabase class option is less suited to applications that need specific control of database behaviors. For portability, the GenericDatabase solution is the optimal approach.
If your application needs to retrieve data in specialized way, or if your code needs customization to take advantage of features specific to a DBMS, using the DataDirect Connect for ADO.NET data provider for that DBMS might be better suited to your needs.
Configuring the DAAB
Before you can configure the DAAB for use with your application, you must set up the environment:
1
2
Open the DataDirect Enterprise Library project for your data provider, located in install_dir\Enterprise Libraries\Src\CS\. The default configurations were created with Microsoft Enterprise Library 5.0.
3
Configuring the Data Access Application Block consists of two procedures:
Adding a New DAAB Entry
Now, use the Enterprise Library Configuration Tool to add a new DAAB entry. This procedure uses the configuration options for the .NET Framework 3.5 and the DB2 Enterprise Library sample solution.
To configure the Data Application Block on any supported platform:
1
Right-click on your project in Solution Explorer. Select Add / New Item. The Add New Item window appears.
2
Select the Application Configuration File template. Then, click Add. The App.config file is added to the the project.
3
Select Start / Programs / Microsoft patterns and practices / Enterprise Library 5.0 / Enterprise Library Configuration / EntLib Config .NET 3.5. The Enterprise Library Configuration window appears.
Enterprise Library Configuration window
4
Select File / Open. Then, select the App.config file you created in Step 3 and click OK. The App.config file is displayed.
Enterprise Library Configuration window with Application Settings and Database Settings titles.
5
6
Click the plus sign button (Plus sign button) in the Database Instances column and select Add Database Connection String. This adds a new connection string item to the configuration.
Enterprise Library Configuration window with the Database Connection String fields visible.
7
In the Name field, enter a name for the DAAB’s connection string, for example, MyDB2conn.
8
In the Connection String field, click the ellipsis button (Ellipsis button) to display the Edit Text Value dialog box. Type or paste a connection string in the text box and click OK.
For example, type:
Database Name=DEV1DB9A;Host=dev1;Port=6070;User ID=TEST01;Encryption Method=SSL;AuthenticationMethod=Kerberos;
9
In the Database Provider drop-down list, select the data provider. For example, select DDTek.DB2.4.0 for the DB2 data provider.
10
Enterprise Library Configuration window with the DB2 data provider selected and a default database instance defined
11
Select File / Save.
Adding the Data Access Application Block to Your Application
To add the DAAB to a new or existing application, perform these steps:
1
2
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using System.Data;
3
4
5
6
7
Using File Explorer, copy the DDTek.EnterpriseLibrary.Data.XXX.dll into your application’s working directory.
Using the Data Access Application Block in Application Code
Now that you have configured the DAAB, you can build applications on top of this DAAB.
In the following example, we use the DAAB MyDB2 and the DatabaseFactory to generate an instance of a Database object backed by a DB2 data source.
using System;
using System.Collections.Generic;
Microsoft.Practices.ServiceLocation;
using Microsoft.Practices.ServiceLocation;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Common;
using System.Data;
 
namespace DAAB_Test_App_1
{
   class Program
   {
      static void Main(string[] args)
      {
         Database database = DatabaseFactory.CreateDatabase("MyDB2");
         DataSet ds = database.ExecuteDataSet(CommandType.TableDirect, "SQLCOMMANDTEST_NCSRVR_1");
       
       }
   }
}
The Microsoft Enterprise Library DAAB coding patterns are now at your disposal.
Using the DAAB Classes with Enterprise Library Version 4.1
If you need to target Enterprise Library 4.1, open the DataDirect Enterprise Library project for your data provider, located in install_dir\Enterprise Libraries\Src\CS\ and use the Debug41 and Release41 configurations.