A Relational Database Management System (RDBMS) is the most common and preferred storage mechanism for enterprise applications. ADO.NET is the cornerstone while working with databases on the .NET platform; it provides the framework and implementations for several databases. Developers leveraging ADO.NET often have to write boilerplate code over and over again. While performing database operations, this might lead to lower productivity, inefficient code, connection leakage, and so on.
The Data Access Application Block abstracts developers from the underlying database technology by providing a common interface to perform database operations. It simplifies common data access functionality by taking care of the mundane tasks like creating a connection object, opening and closing a connection, parameter caching, and so on. The Data Access block supports all the features supported by ADO.NET; it goes a step further by bringing consistency and simplifying the common database tasks.
The benefits of the Data Access block are as follows:
In this chapter, you will:
ExecuteReader
and ExecuteDataSet
ExecuteXmlReader
ExecuteNonQuery
ExecuteScalar
DataSet
It takes two to tango, and in this case it takes configuration and application code. We configure the database connection string and set the provider name attribute; this attribute is mandatory for the Data Access block to work. Optionally, we may also set the default database instance attribute part of the Database Settings. In the application code, there are several elements involved in making the Data Access block work, but it all begins with a class called Database
. The Database
class abstracts us from the underlying database technology and provides us with a simple model to perform various actions against the configured database. It internally leverages the ADO.NET provider factory model (DbProviderFactory
); an instance of Database
contains a reference to a concrete DbProviderFactory
object, which exposes common ADO.NET objects such as DbConnection, DbCommand, DbDataAdapter, DbDataReader
, and so on. The DbProviderFactory
class is an abstract class, part of the System.Data.Common
namespace.
The following is a partial list of concrete implementations of DbProviderFactory
.
Provider name |
Provider Factory Implementation |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
Ever noticed an attribute called providerName
in the connection string entry? This information is used to construct the appropriate provider factory object and that's the reason it is a required attribute as far the Data Access block is concerned. Data Access block configuration code contains default mappings for the data providers. System.Data.SqlClient
data provider maps with SqlDatabase, System.Data.OracleClient
data provider maps with OracleDatabase
, and GenericDatabase
is used for all other data providers.
There is an active community called "EntLib Contrib" developing a library of extensions for Enterprise Library. Many third-party providers are available such as SQLite, Oracle (ODP.NET), MySql, IBM DB2, and PostgreSQL databases. Visit http://entlibcontrib.codeplex.com for more information.
3.144.113.55