Extend and modify the Data Access Application Block of the Enterprise Library

Source: Internet
Author: User
To maintain this document, visit http://wiki.entlib.net.cn/entlibhelp31dataaccessapplicationblock.ashx.

Data Access ApplicationProgramIn the initial status of a block, it can work well in typical data access scenarios. However, you may need to customize some behavior of application blocks to better adapt to your application needs. There are two ways to complete it: Expansion and modification.

Extended Data Access Application Block

You can expand application blocks by designing extension points. A typical custom class must be written. A custom class must implement a specific interface or be derived from an abstract class. Because these custom classes exist in the application space, there is no need to modify or re-build the Application Block. The extension is specified using configuration settings.

Now, you can extend the application block by adding a new database type. If the application needs to come from a specific feature different from the SQL server or Oracle database. Only when the Application BlockGenericdatabaseClass and the ADO. NET data provider for the database system do not provide the required features before you can expand the application block. For example,GenericdatabaseThe object does not support the special parameter prefix used for stored procedures or the call is notDbcommandClass orDbconnectionClass. In additionGenericdatabaseClass does not support parameter discovery.

Yes
To support the creation
Data Providers expose new database classes to expand application blocks. For example, your database class supports parameter discovery. You can also extend the application block to allow customersCodeThrough type conversion or management
SQL syntax conversion to ensure database transparency. Build a custom database class to allow the application to support the entire Data Access Application Block API
Set and better consistency with other database syntaxes. To learn more, see Add a new Application Block Database provider.

Modify application blocks

You can modify the application block by modifying the Application Block source code. This may or may not require configuration changes. Because changes applySource codeBefore the modification takes effect, you must re-build the Application Block. You must also prepare to handle support and version issues. For more information, see modify data access application blocks.

6.1-Add a new Application Block Database provider

If the relational database system used does not have an Application Block Database provider, you can create one by yourself. Only in the Application BlockGenericdatabaseClass is not required for applications. (This assumes that there is an existing ADO. NET 2.0 database system for use.DbproviderfactoryType .) To create a new database provider, you must createDatabaseClass. In addition, if you want to keep the client code transparent to the database, you may have to write additional code to complete tasks such as type conversion.

Create a new database class

Create a new database class

1. Create a derived from Database Class. 2. The implementation allows the application block to instantiate the database class building function by using the connection string and any other information such as the package ing information for the Oracle database.

C #

 
Public sqldatabase (string connectionstring): This (connectionstring, sqlclientfactory. instance)
{
}

Visual Basic

 
Private function sqldatabase (byval connectionstring as string) as public: Me (connectionstring, sqlclientfactory. instance)
End Function

Note::DatabaseThe base class provides the necessary use of the connection string andDbproviderfactoryInitialization class implementation.

3. If necessary, reload the method used for parameter processing. For example, reloadDeriveparametersMethod To Support Parameter discovery. You can also overload the following methods:

Userparametersstartindex. This method is reloaded when the start index of the stored procedure parameter in the command is not zero. For example, the stored procedure of each SQL Server database has a return value parameter (whether explicitly used or not), which is a parameter other than other stored procedure parameters. This parameter is in ADO. netDbcommandObjectParametersThe first parameter in the Set (the starting index is zero ). Therefore,SqldatabaseThe class is overloaded.UserparametersstartindexReturns 1 as the starting index. In this way, the index affects the location of the first Stored Procedure Parameter and effectively skips the return value parameter.

Buildparametername.
Reload this method to ensure that the appropriate parameter name is used for calling the stored procedure. Reload this parameter to allow the customer code to pass parameter names without specific database tags. For example, sqldatabase
Class to ensure that all parameters start with the "@" character. Therefore, the customer code can pass parameter names without the "@" character to allow the customer code to remain transparent to the database.

Samenumberofparametersandvalues. Reload this method to compareDbcommandThe number of parameters in the object and the number of parameters in the array. For example, the parameter Array Used for the SQL Server database contains the return value parameter. SQL generates this parameter for each stored procedure call, but it is not included inDbcommandThe number of objects.SqldatabaseClass overloadSamenumberofparametersandvaluesMethod To compare the number of command parameters and the number of parameter values.

4. Add functions dedicated to the provider. For example,SqldatabaseClass providesExecutexmlreaderMethod. This will have to be converted down to its own database provider class type to access any customized method. You can also reload other methods to adapt to their behavior to suit your own situation. For example, you can modify the syntax of an SQL command before passing it to the ADO. Net class.

Configure the application to use the new provider

After you complete the new provider, you must configure the application to use it. For more information about how to configure custom application providers, see enter configuration information.

Suggestions for creating database providers

Below are some guidelines for creating and testing database providers:

    • Only the ADO. netDbproviderfactoryCreate a new database type only when the subclass already exists.
    • Use an existing database provider as your own model. Use the SQL server or Oracle data provider that comes with the data access application block as the basis for your own provision.
    • Use unit tests that are included in the data access application block assembly to verify the provider.
    • Confirm that the data type conversion is tested, and document the unsupported or supported items that are different from the provider. This document will help developers who build portable database applications.

6.2-Modify the Data Access Application Block

The Data Access Application Block is designed for various applications. With its extension point, you can adapt to application blocks to meet the needs of specific applications. However, to add new features to the Application Block, you can modify the source code (the application block comes with the source code and binary code ). If you decide to modify the source code, follow the suggestions in this section.

Suggestions for modifying data access application blocks

Follow these best practices when modifying source code:

    • Read the design section in this document to determine how the Application Block works.
    • It is strongly recommended that you modify the data to access the namespace of the Application Block if a large amount of code is modified or if the custom version of the application block is used together. Note that modifying the namespace of an Application Block requires modifying the namespace of all other applications and applications that use data to access the Application Block.
    • Use a strong name. A strongly-named assembly can uniquely identify, version, and perform integrity checks. You will have to generate your own key pair to sign the modified version of the application block. For more information, see strong named assemblies and versioning tutorial. Another method is to customize the version without signature, which is called a weak name.
    • To make the modification take effect, you must re-compile the code. Open the Data Access Application Block Visual Studio solution file and recreate the entire solution.
    • Perform your own unit tests based on unit tests that work with data access application blocks. Test the code as soon as possible.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.