Using Microsoft® Enterprise Library Application Blocks With DataDirect Connect for ADO.NET

View PDF

Introduction

Using Microsoft® Enterprise Library Application Blocks can simplify application development by wrapping common tasks, such as data access, into portable code that makes it easier to move your application from one DBMS to another. The classes in the Data Access Application Blocks (DAABs) provide access to the most frequently used features of ADO.NET. Applications can use the DAABs for tasks such as passing data through application layers and return changed data back to the database. Using DAABs eliminates the need to keep writing the same data access tasks for each new or revised application, so you can spend your time in more productively.

The DataDirect Connect® for ADO.NET Data Providers can be used with Data Access Application Blocks. This article describes how to use the Data Access Application Blocks in your applications.

The article consists of the following sections:

Data Access Application Block Overview

Data Access Application Blocks (DAABs) are designed to allow developers to replace ADO.NET boiler-plate code with standardized code for everyday database tasks. The overloaded methods in Database class can:

  • Return scalar values or XmlReaders
  • Determine which parameters it needs and create them
  • Involve commands in a transaction.

If your application needs to address specific DBMS functionality, you can use the DataDirect Connect for ADO.NET data provider.

When Should You Use a DAAB?

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 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 data provider’s 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 a DAAB with DataDirect Connect for ADO.NET:

  • The GenericDatabase class
  • The provider-specific DataDirect Connect for ADO.NET DAAB implementation

The GenericDatabase class option is less suited to applications that need specific control of database behaviors. For portability, the provider-specific solution is the optimal approach.

If your application needs to retrieve data in a 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 Data Access Application Blocks

Before you can configure the DAAB for use with your application, you must set up the environment:

  1. Download the Microsoft Enterprise Library 3.1 (May 2007) from http://msdn2.microsoft.com/en-us/library/aa480453.aspx.
  2. Compile the project and note the output directory.
  3. Open the DataDirect DAAB project for your DataDirect data provider, located by default in
    install_dir\Enterprise Libraries\Src\CS\ProviderName.
  4. Compile the project and note the output directory.

Configuring the Data Access Application Block consists of two parts:

  • Adding a New DAAB Entry
  • Adding the Data Access Application Block to Your Application

Adding a New DAAB Entry

Now, use the Enterprise Library Configuration Tool to add a new DAAB entry:

  1. Right-click Enterprise Library Configuration, and select New Application.
  2. Right-click Application Configuration, then select New / Data Access Application Block. The Enterprise Library Configuration window appears.

    Enterprise Library Configuration window, with the Data Access Application Block node expanded
  3. In the Name field, enter a name for the DAAB, for example, MyOracle.
  4. In the ConnectionString field, enter a connection string.
  5. In the ProviderName field, identify the DataDirect data provider:
    1. For DB2, enter DDTek.DB2
    2. For Oracle, enter DDTek.Oracle
    3. For SQL Server, enter DDTek.SQLServer
    4. For Sybase, enter DDTek.Sybase
  6. Right-click Customer Provider Mappings and select New / Provider Mappings.

    Enterprise Library Configuration window, with Custom Provider Mappings node expanded
  7. In the Name field, type the DAAB name entered in Step 3 (MyOracle).
  8. In the TypeName field, choose the browse (...) button and navigate to the Debug output directory of the DataDirect DAAB that you want to build.
  9. Select the file name, for example, DDTek.EnterpriseLibrary.Data.Oracle.dll, and then click Open.
  10. Leave the Enterprise Library Configuration window open for now. Do not save this configuration until you complete the following section.

Adding the Data Access Application Block to Your Application

To add the DAAB to a new or existing application, perform these steps:

  1. Right-click the project and select Add Reference.
  2. In the Add Reference dialog, select Enterprise Library Shared Library, and click OK.
  3. Right-click the project and select Add Reference.
  4. In the Add Reference dialog, select Enterprise Library Data Access Application Block, and click OK.
  5. Add the following directive to your C# source code:
    using Microsoft.Practices.EnterpriseLibrary.Data;
    using System.Data;
  6. Rebuild the solution to ensure that the new dependencies are functional.
  7. Determine the output Debug or Release path location of your current solution, and switch back to the Enterprise Library Configuration window (see "Adding a New DAAB Entry" on page 3).
  8. Right-click the connection string under the Application Configuration node and select Save Application.

    Enterprise Library Configuration window, with the Connection Strings node expanded
  9. Navigate to the Debug or Release output directory of your current solution, and locate the .exe file of the current solution, for example, MyApp.exe.
  10. Click the file name once, and add .config to the name, for example, MyApp.exe.config.
  11. Ensure Save as type 'All Files' is selected and select Save.
  12. Using File Explorer, copy the DDTek.EnterpriseLibrary.Data.XXX.dll from the DataDirect DAAB directories (where XXX indicates the data source).
  13. Place a copy of this DLL into either the Debug or Release output directory of your current solution.

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 MyOracle and the DatabaseFactory to generate an instance of a Database object backed by an Oracle data source.

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data;

namespace DAAB_Test_App_1
{
    class Program
    {
        static void Main(string[] args)
        {

            Database database = DatabaseFactory.CreateDatabase("MyOracle");
            DataSet ds = database.ExecuteDataSet(CommandType.TableDirect, "SQLCOMMANDTEST_NC_2003SERVER_1");

       }
    }
}

The Microsoft Enterprise Library DAAB coding patterns are now at your disposal.

Additional Resources

The following articles provide additional information about using Application Blocks:

Conclusion

When you use the Microsoft® Enterprise Library Data Access Application Blocks for standard data access tasks, the intricacies of the underlying databases are abstracted away from the application.

DataDirect Connect for ADO.NET fills in the gaps in functionality that the Enterprise Library DAAB, by design, leaves open. With the DataDirect data providers, you can build a true heterogeneous data access layer with the SQL leveling capabilities that DataDirect is known for.