Design of Enterprise Library Exception Handling application blocks

Source: Internet
Author: User
This document is maintained at: http://wiki.entlib.net.cn/entlibhelp31exceptionhandlingapplicationblock.ashx.

Exception Handling ApplicationProgramBlocks are designed to achieve the following goals:

    • Encapsulate the minimum number of applications used to complete the most common exception handling tasksCode.
    • Developers who need to abbreviated names for repeated code and custom code for common exception handling tasks.
    • After deployment, you can change the exception handling policy and make sure that all modifications are consistent.
    • Add the best practices for exception handling, as described in the exception management architecture guide.

Design highlights

Figure 1 shows the internal relationship between key classes of the exception handling Application Block.

Figure 1 design of the exception handling Application Block

Client code uses static methodsHandleexceptionIt interacts with the exception handling Application Block. This method is available inExceptionpolicyClass.HandleexceptionMethod To createPredictionpolicyimplType object.PredictionpolicyimplThe object hasPredictionpolicyentryObject set. This is an object used to specify the exception type in the configuration file for the naming policy. For each exception type,PredictionpolicyentryThe object containsIexceptionhandlerInterface Object List. The list is ordered, and the queue used by the exception handling Application Block to execute the policy is provided. ImplementedIexceptionhandlerEach object of the interface is associated with the configuration information specified for each handler type.


5.1-Simplified Catch Block Design

Common Exception Handling behaviors, such as recording and replacing exceptions to hide sensitive information, usually require multiple lines of code. Changing the exception handling behavior to apply the exception handling policy changes often involve multiple files and code lines. This process is prone to errors, and it is difficult to ensure that the policy is updated consistently across all layers of the application.

Differences
The regular Processing Application Block simplifies the process of exception handling code and updating code. It associates the exception handling behaviors and Policy names such as "data access layer
Policy and trust Boundary
Policy. Actions are represented by externally controllable policy names in the application configuration file. This means that developers only need to use two elements to write
Code in the Catch Block:

    • Call the transfer policy name and the abnormal staticHandleexceptionMethod.
    • Check slaveHandleexceptionCode returned by the method. If yesTrue, The original exception will be thrown again.

Design Connotation

Encapsulate the exception handling logic to a single method call that relies on the two points of the design of the exception handling Application Block:

    • Must be an API that supports policy names.
    • Depends on the Enterprise Library kernel.

The following sections describe these meanings.

API support for policy names

HandleexceptionThe method accepts strings containing policy names and exception objects as input.ExceptionpolicyfactoryThe object uses this policy name to locate the appropriate exception policy information in the application configuration file, such as the exception handling program to be run.

The policy name used in the Code must match the Policy Name found in the configuration file. Instead, developers must first create different policies for different exception types, and then call the correct policies based on the exception types. developers can configure policies for different exception types to have different behaviors. To support this feature,HandleexceptionThe method also accepts the original exception object as the input.

Typically, an application must runThrowStatement propagation exception to the caller. If the exception handling Application Block is usedThrowStatement, stack trace will indicate that the exception occurs in the Application Block rather than in the application code. Stack trace information to be captured to reflect the point where propagation occurs in the application,HandleexceptionMethod returns a Boolean value. This indicates whether the policy used for the exception type determines whether it will be propagated. Checking the return value is the responsibility of the application code.True, Throw the original exception. The following code demonstrates how to do this.

C #
 
Bool rethrow = exceptionpolicy. handleexception (ex, "data access layer policy ");
If (rethrow)
{
Throw;
}

Visual Basic

 
Dim rethrow as Boolean = exceptionpolicy. handleexception (ex, "data access layer policy ")
If (rethrow) then
Throw
End if

Depends on the Enterprise Library Kernel

The policy is defined externally in the configuration file used for the application. Differences
The Application Block that is often processed depends on the factory design pattern used to create policy objects. The configuration data determines the object type and attributes. Factory class used in Enterprise Library
The configuration helper class in the kernel constructs an object instance based on the configuration data. For more information, see Enterprise Library kernel.


5.2-design for behavior encapsulation in reusable handlers

The exception handling Application Block allows developers to create an exception handling program that represents common exception handling tasks. These handlers and combinations of handlers can be used for different policies.

Design Connotation

Encapsulating Exception Handling behaviors to reusable handlers depends on two aspects of the Application Block Design:

    • It must be a common interface implemented by a processing program.
    • The policy must be a combination of sequential handlers.

The following sections describe these meanings.

Common interface

The application component uses the Policy Name and exception to identify the action to be executed by the exception handling Application Block. The Application Block provides this behavior by executing an exception handler configured for the policy. To make the application block correctly execute the processing program, all the processing programs must implementIexceptionhandlerInterface. All three handlers provided by the Application Block support this interface, but developers can easily write custom handlers that match the same interface.

Policy support for sequential exception handling program combinations

Exception handlers can be reused between policies because they generally solve a single task. They can be combined in an exception policy to solve more complex tasks. The policy executes the configured handler in sequence.

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.