Creating Interception Handler Attributes

Microsoft Enterprise Library 5.0

DropDown image DropDownHover image Collapse image Expand image CollapseAll image ExpandAll image Copy image CopyHover image

Handler attributes allow developers to apply handlers to classes and class members directly, without configuring them in the application configuration file. Developers creating custom handlers may want to provide an attribute for their handlers. To build a custom handler attribute, you create a class that derives from the HandlerAttribute base class shown here.

C# Copy Code
public abstract class HandlerAttribute : Attribute
{
  /// Derived classes implement this method. When called, it creates a 
  /// new call handler as specified in the attribute configuration.
  /// The parameter "container" specifies the IUnityContainer 
  /// to use when creating handlers, if necessary.
  /// returns a new call handler object.
  public abstract ICallHandler CreateHandler(IUnityContainer container);

  private int executionorder;
  /// <summary>
  /// Gets or sets the order in which the handler will be executed.
  /// </summary>
  public int Order
  {
    get { return this.executionorder; }
    set { this.order = value; }
  }
}
Visual Basic Copy Code
Public MustInherit Class HandlerAttribute : Inherits Attribute

  ''' Derived classes implement this method. When called, it creates a 
  ''' new call handler as specified in the attribute configuration.
  ''' The parameter container specifies the IUnityContainer 
  ''' to use when creating handlers, if necessary.
  ''' Returns a new call handler object.
  Public MustOverride Function CreateHandler(container As IUnityContainer) As ICallHandler

  Private executionorder As Integer
  ''' <summary>
  ''' Gets or sets the order in which the handler will be executed.
  ''' </summary>
  Public Property Order As Integer
    Get 
      Return Me. executionorder 
    End Get
    Set 
      Me.order = value
    End Set
  End Property

End Class

In your custom attribute class, you must implement one or more constructors that accept values from the attribute, and/or implement named properties that the developer can use to set the properties of the class. Then you simply override the CreateHandler abstract method declared within the base class to create and return the required handler class as an ICallHandler instance.

Example Call Handler Attribute

As an example, you could create a call handler attribute for a call handler similar to that described in the topic Creating Interception Policy Injection Call Handlers that prevents invocation of business processes on weekend days. In this case, assume that the handler has a property named SaturdayOK that allows you to set it to allow calls to occur on a Saturday. The call handler has two constructors: one that takes a parameter that sets the value of the SaturdayOK property to the specified value (true or false), and one that takes no parameters and sets the default value (false) for the SaturdayOK property. The following code shows an implementation of the WeekdayOnlyCallHandlerAttribute.

C# Copy Code
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Method)]
public class WeekdayOnlyCallHandlerAttribute : HandlerAttribute
{
  private bool allowSaturday;

  public WeekdayOnlyCallHandlerAttribute()
  {
    allowSaturday = false;
  }

  public WeekdayOnlyCallHandlerAttribute(bool SaturdayOK)
  {
    allowSaturday = SaturdayOK;
  }

  public override ICallHandler CreateHandler(IUnityContainer ignored)
  {
    return new WeekdayOnlyCallHandler(allowSaturday, Order);
  }
}
Visual Basic Copy Code
<AttributeUsage(AttributeTargets.Class Or AttributeTargets.Property Or AttributeTargets.Method)> _
Public Class WeekdayOnlyCallHandlerAttribute : Inherits HandlerAttribute

    Private allowSaturday As Boolean

    Public Sub New()
        allowSaturday = False
    End Sub

    Public Sub New(SaturdayOK As Boolean)
        allowSaturday = SaturdayOK
    End Sub

    Public Overrides Function CreateHandler(ignored As IUnityContainer) As ICallHandler
        Return New WeekdayOnlyCallHandler(allowSaturday, Order)
    End Function

End Class

Notice the AttributeUsage attribute that specifies where developers can apply the new custom attribute (on a class, a property, or a method), and—in this case—the provision of two constructors. The first (default) constructor uses the default value (false), while the second accepts a value for the SaturdayOK property. The CreateHandler method override instantiates the WeekdayOnlyCallHandler class with the appropriate values and returns this as an ICallHandler reference.