Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Using the Policy Injection Application Block in ASP.NET : Page 5

Learn how to use AOP injection techniques to add, remove, and modify logging, validation, caching, exception handling, authorization, and performance measurements in your ASP.NET applications—without having to recompile your code.


advertisement
Behavior of the Caching Handlers
The example application applies the Caching Handler in three different circumstances:

  • To the GetCustomerList method of the AttributedCustomerModel class through a directly applied attribute, as shown below:
  • [CachingCallHandler(0, 0, 20)] public DataTable GetCustomerList() { ... }

  • To the InterfaceCustomerModel's GetCustomerNameWithWildcard and GetCustomerList methods through the policy named InterfaceModelPolicy declared in the configuration.
  • To the InterfaceCustomerModel's CityCount property through a Tag Attribute Handler defined in the policy named CacheByTagPolicy declared in the configuration, and applied to the method though a Tag attribute, as shown below:
  •  
    Figure 5. Caching Handler Output: The figure shows the effect of the Caching Handler when calling a method in the target class.

    [Tag("ApplyTenSecondCaching")] public Int32 CityCount { ... }

When you access any of these members, the trace section of the page shows the calls made to the target member. However, if you repeat the call within the cache duration period, the trace section shows that the block only instantiated the target class (it has to do this to add the handler pipeline), and did not call the target method or property—as shown in Figure 5.

In this example, you can see that the call to InterfaceCustomerModel.GetCustomerNameWithWildcard calls the GetCustomerName method. However, on the subsequent request, the block calls neither of these methods and returns the value from the cache instead.

Author's Note: The Caching Handler uses the built-in ASP.NET caching mechanism, which is domain-wide. The handler creates a cache key based on the complete signature of the current method, allowing it to vary the cache (create separate cached items) for calls that provide different parameters values. However, it does not detect and include the thread identity in the cache key, and so calls made on threads with different security contexts will return the same value from the cache. This may present a security risk if methods return sensitive or user-specific information.

Behavior of the Exception Handling Handler
The example application applies the Exception Handling Handler to only two methods, the GetCustomerNameWithWildcard and the GetCustomerList methods of the InterfaceCustomerModel class through the InterfaceModelPolicy declared in the configuration:

  • The configuration specifies the exception handling policy named CustomerModelPolicy, declared in the Exception Handling Application Block section, which will wrap any ArgumentNullException in a new Exception with a the message "You must specify a value for the customer ID."
  • In the AttributedCustomerModel class, the GetCustomerName method throws an ArgumentNullException when there is no customerID:


public String GetCustomerName(String customerID) { // ensure there is a customer ID specified if (customerID == String.Empty) { throw new ArgumentNullException("This exception will not be replaced..."); } ...

Therefore, as the attributes and configuration settings do not apply the Exception Handling Handler to this method, the page shows this error message:

ERROR: Value cannot be null. Parameter name: This exception will not be replaced...

GetCustomerNameWithWildcard also throws an ArgumentNullException when there is no customerID:

public String GetCustomerNameWithWildcard(String customerID) { // ensure there is a customer ID specified if (customerID == String.Empty) { throw new ArgumentNullException("This exception should be replaced..."); } ...

This method does have the Exception Handling Handler attached, and so—when there is no customer ID—the page displays the following error message generated by the exception policy defined in the Exception Handling Application Block configuration:

ERROR: You must specify a value for the customer ID



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap