Designing for Simplified Data Access

Developers face many implementation choices and requirements when they build data access solutions. They must access the data in a variety of ways, and their solutions must work with different types of databases, each of which handles data access differently. As a result, developers may frequently find themselves duplicating code that performs common tasks, such as managing connections and assigning parameters to commands.

开发者面对很多的实现的选择和需求,当他们构建数据访问方案时。他们必须用不同的方法去访问数据库,并且他们的方案必须工作于不同类型的数据库上。每一个的处理用不同的方法,结果是,开发者重复的发现相同的数据访问方法去执行相同的任务,如管理连接和设置参数。

Another challenge is maintaining a consistent approach in how data access operations are implemented. It may be necessary to maintain this consistency across single projects, multiple projects, or enterprise-scale solutions. Uniform methods of data access make the code easier to understand, more predictable, and easier to maintain.

另一个挑战时,去维护一致的数据访问方法如何实现。这是很必要的去维护一致性在单个项目,多个项目,和在整个企业方案内。更容易维护,更容易预测和维护。

The Data Access Application Block simplifies data access by encapsulating the logic that performs common database operations. These methods also handle common housekeeping tasks such as opening and closing connections. Also they are transparent; this means they work without modification with SQL Server, Oracle, and DB2 databases. Applications written for one type of database use the same methods as those written for another type of database. This means that applications are consistent in the ways they access data.

数据访问应用块简化了数据访问,通过封装公共的数据操作逻辑。这些方法也处理一般的任务,如打开关闭连接。它们是透明的,意味着它们工作不用修改SQL Server, Oracle, and DB2 databases.对一种类型的数据库被写的应用,可以用于另外一种数据库使用同样的方法。这意味着应用是一致的到于数据访问方法。,

Design Implications

Ensuring that the application block simplified the task of accessing data resulted in the following design decisions: 确保应用简化访问数据的任务,导致以下的设计决定。

·                 It should expose only a small number of methods that a developer would need to understand. 它应该暴露小部分的方法给开发者需要理解。

·                 It should encapsulate common housekeeping tasks. 封装常用的操作。

·                 It should make it easy to handle parameters. 容易处理的参数

·                 It should provide good performance. 它应该提供很好的性能

The next sections describe these decisions. 下面的章节描述这些决定。

 

 

 

 

Limited Set of Interfaces限定的接口集。

The application block supports a small number of interfaces that simplify the most common data access tasks. It provides an abstract base class, Database, that defines the set of methods the block supports. These methods include the following:

应用块提供一小部接口来简化大部分公共数据访问任务

·                 ExecuteDataSet

·                 LoadDataSet

·                 ExecuteReader

·                 ExecuteScalar

·                 ExecuteNonQuery

·                 UpdateDataSet

Each of these methods has multiple overloads. The overloads allow varying degrees of control over the information each method passes and they accomodate different styles of programming. One class of overloads allows you to pass objects of type DBCommandWrapper. An abstract base class, DBCommandWrapper, encapsulates both command and parameter handling into a single object. This means that, to execute them, the Database class methods require only a single DBCommandWrapper as a parameter, as shown in the following example.

[C#]

DBCommandWrapper dbCommandWrapper = db.GetStoredProcCommandWrapper("GetProductsByCategory");

dbCommandWrapper.AddInParameter("@CategoryID", DbType.Int32, 2);

 

DataSet productsDataSet = db.ExecuteDataSet(dbCommandWrapper);

[Visual Basic]

Dim dbCommandWrapper As DBCommandWrapper = db.GetStoredProcCommandWrapper("GetProductsByCategory")

dbCommandWrapper.AddInParameter("@CategoryID", DbType.Int32, Category)

 

Dim productsDataSet As DataSet = db.ExecuteDataSet(dbCommandWrapper)

DBCommandWrapper objects allow developers to control the various attributes of the command parameters, including the type, direction, and size. For each of the methods available on the Database class there are two overloads for the versions that accept a DBCommandWrapper, one for execution outside a transaction and one for execution within a transaction. For example, the following are the two overloads for the ExecuteDataSet method, where the first is when there is no transaction and the second where there is a transaction.

[C#]

public virtual DataSet ExecuteDataSet(DBCommandWrapper command)

 

public virtual DataSet ExecuteDataSet(DBCommandWrapper command, IDbTransaction transaction)

[Visual Basic]

Public MustOverride Function ExecuteDataSet(ByRef command As DBCommandWrapper) As DataSet

 

Public MustOverride Function ExecuteDataSet(ByRef command As DBCommandWrapper, ByRef transaction As IDbTransaction) As DataSet

For developers who prefer the convenience of simply passing all required information to a Database class method, there are overloads of each of the methods that allow you to supply the required information in a single call. For example, the ExecuteDataSet method includes the following overload, which allows the developer to pass a stored procedure name and a collection of parameters to be used.

[C#]

public virtual DataSet ExecuteDataSet(string storedProcedureName, params object[] parameterValues)

[Visual Basic]

Public MustOverride Function ExecuteDataSet(ByRef storedProcedureName As String, ByRef parameterValues As Object()) As DataSet

Encapsulation of Connection Lifetime

One of the most common tasks developers must consider is how to manage connections to the database. Whenever possible, the application block handles connection management. The application block's method opens a connection and closes it prior to returning. This reduces both the amount of client code required and the possibility of leaving connections open.

In the case of the ExecuteDataReader method, the DataReader object is executed using the CommandBehavior.CloseConnection method, which automatically closes connections when the DataReader is closed.

Convenient Parameter Handling

The call to GetStoredProcCommandWrapper allows developers to specify values to be used as parameters when the specified stored procedure is called. The Database class uses dynamic discovery of the parameter information. Because of this, the client code doesn't need to specify each parameter's type, as showing in the following example.

[C#]

Database db = DatabaseFactory.CreateDatabase();

string sqlCommand = "GetProductsByCategory";

DBCommandWrapper dbCommandWrapper = db.GetStoredProcCommandWrapper(sqlCommand, 2);

productsDataSet = db.ExecuteDataSet(dbCommandWrapper);

[Visual Basic]

Dim db As Database = DatabaseFactory.CreateDatabase()

Dim sqlCommand As String = "GetProductsByCategory"

Dim dbCommandWrapper As DBCommandWrapper = db.GetStoredProcCommandWrapper(sqlCommand, 2)

productsDataSet = db.ExecuteDataSet(dbCommandWrapper)

Dynamic discovery is convenient for developers because they can simply pass values without having to look up information such as the values' names and types.

Performance Considerations

Developers must consider performance when writing code that accesses data, and the design of the Data Access Application Block reflects this in a number of ways:

·                 Specific classes exist for each database type, avoiding the need to run method calls through generic layers. Deriving methods from the common abstract base class, Database, ensures conformance to a common interface. A common implementation is kept in a single location, without sacrificing performance.

·                 专门的类存在于每一个数据库中。避免了运行的方法通过一般的层次。派生于抽象类的方法,确保了公共接口的一致性。公共的实现保留在同一位置。而不消弱性能。

·                 The ParameterCache class provides a cache that stores the parameter information for each procedure call. Because dynamic discovery of parameters requires a roundtrip to the database, using a cache means that subsequent calls to the same procedure do not incur additional trips after the parameters are obtained.

ParameterCache类提供了一个缓存,对于每个过程的调用它存了参数信息。因为动态的发参数需要访问数据的过程。在参数被获得后,反来的对于同一过程的调用就不需要经过这个过程了
原文地址:https://www.cnblogs.com/snowball/p/159754.html