The Data Access Application Block provides an extensible framework for transparently supporting multiple types of relational databases. Applications that use the application block are portable across different database systems.
对于透明的支持多种类型的数据库数据访问应用块提供了一个可以扩展的框架。应用可以使用这个数据访问块方便地使用不同的数据库。
There are a number of general data access tools such as Open Database Connectivity (ODBC) or OLE DB what can provide access to a variety of data sources. One drawback to these tools is that how they are used depends on the target database. That means that programmers need to understand various programming models to access different database types; porting an application to a different database could require a significant amount of recoding.
有很多的数据访问工具,如ODBC,OLE DB可以访问各种数据源。这些工具的缺点是它们被使用依赖于特定的数据库。这就意味着程序员必须了解要访问的各种类型数据库的编程模型。
Another drawback to the ODBC or OLE DB approach is that performance may suffer. Generic data providers are slower than those optimized for a particular data source. The Data Access Application Block provides a factory-based implementation that features both transparent portability and optimized performance.
OLEDB OR ODBC的另一个缺点是性能受到损失。一般的数据提供者是慢的比专门的数据源的优化。数据访问块提供了一个基于工厂类的实现。这些特征是轻便和优化性能的。
Design Implications
These features imply several things about the application block's design:在应用块中这些特征应用了很多的东西。
· Abstraction of the database system 数据库系统的抽象。
· Isolation of features that are specific to the Database base class
These next sections describe these implications.
Abstraction of the Database System
The majority of the data access methods are available through the abstract Database class. Client code can refer to these methods in their code regardless of the actual Database-derived object used. For example, the following code snippet shows how to use the ExecuteDataSet method.
大多数的数据访问方法是有效的,在抽象的数据库类中。客户端代码参考它们的方法在代码中,而可以忽视本地的数据库继承类。例如,下面的代码片段显示了如何使用ExecuteDataSet方法。
Database db;
...
db.ExecuteDataSet(dbCommandWrapper);
A factory, DatabaseFactory, creates the specific Database-derived object. It returns an object of type Database, thus allowing the client code to remain generic regarding the actual database type returned. For example, the following code snippet creates the default database object.
一个工厂,DatabaseFactory,创建专门的数据库继承对象。它返回一个数据库类型的对象,这些允许客户端代码允许保留一般性,相对于实际的返回数据库类型。例如下面的代码片段建立一个默认的数据库对象。
Database db = DatabaseFactory.CreateDatabase();
The methods available on the Database class require information about the command to be executed as well as any associated parameters. Different database systems handle commands and parameters in different ways. The abstract base class DBCommandWrapper provides the means for the methods on the Database class to accept a common type, with the specific database systems providing their own derived implementations.
在数据类上的有效的方法需要这样的信息,这些信息关于执行的命令任何相关的参数。不同的数据库处理命令和参数有用不同的方法。抽象的基类DBCommandWrapper提供数据库方法可以接受的一般类型。对于专门的数据库系统提供他们自己继承的实现。
Isolation of Database-specific Features
Functionality specific to a particular database system is incorporated into the appropriate Database-derived class. For example, ExecuteXmlReader is only available in the SqlDatabase class. Because DatabaseFactory returns an object of type Database, the client code must downcast to the correct type to use any methods that are unique to a particular database. For example, the following code snippet creates a SQL Server database object, which is the only database object that can use the ExecuteXmlReader method. It then returns an XmlReader object.
对于特殊的数据库系统专门的功能被合并到合适的数据库继类中。例如ExecuteXmlReader仅仅在SqlDatabase中有效。因为DatabaseFactory返回了一个数据库类型的对象。客户端代码必须明确正确的使用任何方法的类型。明确特殊的数据库。便如,下面的代码片段建立了一个SQL Server数据库对象。仅仅它使用ExecuteXmlReader方法。然后返回XmlReader对象。
SqlDatabase dbSQL = (SqlDatabase)
DatabaseFactory.CreateDatabase("EntLibQuickStartsSql");
...
XmlReader xmlResults = dbSQL.ExecuteXmlReader(dbCommandWrapper);