Share via


Adding Pipeline Handlers to Existing Target Objects

Retired Content

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

The latest Enterprise Library information can be found at the Enterprise Library site.

When client code already contains an instance of an object that you want to policy-enable, the code should use the static Wrap method. The Policy Injection Application Block obtains a reference to the existing instance of the object, creates a suitable proxy instance, and then—based on configuration information—inserts the handler pipeline between all the policy-enabled members of the target object and the matching members of the proxy. The result is that the original object is not modified and the policies do not apply to the methods called through the original reference.

The Wrap method exposes overloads that allow developers to provide custom configuration information if required:

  • Wrap<TargetObjectType>(existing-target-object):
    • This wraps the existing instance of the specified class that inherits from MarshalByRefObject and creates a handler pipeline using the default configuration information for the application block.
  • Wrap<TargetObjectType>(IConfigurationSource, existing-target-object):
    • This wraps the existing instance of the specified class that inherits from MarshalByRefObject and creates a handler pipeline using the configuration information stored in the instance of a class that implements the IConfigurationSource interface.
  • Wrap<ITargetInterface>(existing-target-object):
    • This wraps the existing instance of the specified class that implements the interface ITargetInterface and creates a handler pipeline using the default configuration information for the application block.
  • Wrap<ITargetInterface>(IConfigurationSource, existing-target-object):
    • This wraps the existing instance of the specified class that implements the interface ITargetInterface and creates a handler pipeline using the configuration information stored in the instance of a class that implements the IConfigurationSource interface.

The next procedure describes how to wrap and policy-enable an existing instance of a class named OrderTransactions that inherits from MarshalByRefObject, using the default configuration information for the Policy Injection Application Block.

To policy-enable an existing instance of a class that inherits from MarshalByRefObject

  1. Call the static Wrap method that the Policy Injection Application Block exposes, specifying the target class type using the .NET Framework Generics syntax. Also, provide as a parameter to the Wrap method a reference to the existing object you want to wrap, as shown in the following code example.

    string storeName = "Contoso";
    OrderTransactions orders = PolicyInjection.Wrap<OrderTransactions>(myOrderObject);
    
    'Usage
    Dim storeName As String = "Contoso"
    Dim orders As OrderTransactions = PolicyInjection.Wrap(Of OrderTransactions)(myOrderObject)
    
  2. Call any policy-enabled methods of the target class. For example, this code assumes the class exposes a method named CountOrderItems that takes as a parameter the name of a customer and returns an integer value.

    int orderCount = orders.CountOrderItems("SMITH");
    
    'Usage
    Dim orderCount As Integer = orders.CountOrderItems("SMITH")
    
  3. Read and set policy-enabled properties of the target class. For example, this code assumes the class exposes a property named InvoiceGreeting of type System.String.

    string greeting = orders.InvoiceGreeting;
    orders.InvoiceGreeting = "Happy New Year";
    
    'Usage
    Dim greeting As String = orders.InvoiceGreeting
    orders.InvoiceGreeting = "Happy New Year"
    

The next procedure describes how to wrap and policy-enable an existing instance of a class named OrderTransactions that inherits from MarshalByRefObject, using the custom configuration information stored in an instance of a class that implements the IConfigurationSource interface.

To policy-enable an existing instance of a class that inherits from MarshalByRefObject with custom configuration information

  1. Create an instance of a class that implements the IConfigurationSource interface, and which contains the custom configuration information you want to use for the object, as shown in the following code. For more information about creating custom configuration information, see Creating Objects Using the Provider Factory Methods.

    IConfigurationSource configInfo = new FileConfigurationSource("myconfig.config");
    // Populate configuration information here.
    ...
    
    'Usage
    Dim configInfo As IConfigurationSource = New FileConfigurationSource("myconfig.config")
    ' Populate configuration information here.
    ...
    
  2. Call the static Wrap method that the Policy Injection Application Block exposes, specifying the target class type using the .NET Framework Generics syntax. Also, provide as parameters to the Wrap method a reference to the custom configuration instance and a reference to the existing object you want to wrap.

    string storeName = "Contoso";
    OrderTransactions orders = PolicyInjection.Wrap<OrderTransactions>(configInfo, myOrderObject);
    
    'Usage
    Dim storeName As String = "Contoso"
    Dim orders As OrderTransactions = PolicyInjection.Wrap(Of OrderTransactions)(configInfo, myOrderObject)
    
  3. Call any policy-enabled methods of the target class. For example, this code assumes the class exposes a method named CountOrderItems that takes as a parameter the name of a customer and returns an integer value.

    int orderCount = orders.CountOrderItems("SMITH");
    
    'Usage
    Dim orderCount As Integer = orders.CountOrderItems("SMITH")
    
  4. Read and set policy-enabled properties of the target class. For example, this code assumes the class exposes a property named InvoiceGreeting of type System.String.

    string greeting = orders.InvoiceGreeting;
    orders.InvoiceGreeting = "Happy New Year";
    
    'Usage
    Dim greeting As String = orders.InvoiceGreeting
    orders.InvoiceGreeting = "Happy New Year"
    

The next procedure describes how to wrap and policy-enable an existing instance of a class named OrderTransactions that implements the interface IStoreTransactions using the default configuration information for the Policy Injection Application Block.

To policy-enable an existing instance of a class that implements a known interface

  1. Call the static Wrap method that the Policy Injection Application Block exposes, specifying the name of the known interface that the class implements using the .NET Framework Generics syntax. Also, provide as a parameter to the Wrap method a reference to the existing object you want to wrap, as shown in the following code example. Note that the returned object is referenced as an instance of the target interface type.

    string storeName = "Contoso";
    IStoreTransactions orders = PolicyInjection.Wrap<IStoreTransactions>(myOrderObject);
    
    'Usage
    Dim storeName As String = "Contoso"
    Dim orders As IStoreTransactions = PolicyInjection.Wrap(Of IStoreTransactions)(myOrderObject)
    
  2. Call any policy-enabled methods of the target class. For example, this code assumes the class exposes a method named CountOrderItems that takes as a parameter the name of a customer and returns an integer value.

    int orderCount = orders.CountOrderItems("SMITH");
    
    'Usage
    Dim orderCount As Integer = orders.CountOrderItems("SMITH")
    
  3. Read and set policy-enabled properties of the target class. For example, this code assumes the class exposes a property named InvoiceGreeting of type System.String.

    string greeting = orders.InvoiceGreeting;
    orders.InvoiceGreeting = "Happy New Year";
    
    'Usage
    Dim greeting As String = orders.InvoiceGreeting
    orders.InvoiceGreeting = "Happy New Year"
    

The next procedure describes how to wrap and policy-enable an existing instance of a class named OrderTransactions that implements the interface IStoreTransactions using the custom configuration information stored in an instance of a class that implements the IConfigurationSource interface.

To policy-enable an existing instance of a class that implements a known interface with custom configuration information

  1. Create an instance of a class that implements the IConfigurationSource interface, and which contains the custom configuration information you want to use for the object, as shown in the following code example. For more information about creating custom configuration information, see Creating Objects Using the Provider Factory Methods.

    IConfigurationSource configInfo = new FileConfigurationSource("myconfig.config");
    // Populate configuration information here.
    ...
    
    'Usage
    Dim configInfo As IConfigurationSource = New FileConfigurationSource("myconfig.config")
    ' Populate configuration information here.
    ...
    
  2. Call the static Wrap method that the Policy Injection Application Block exposes, specifying the name of the known interface that the class implements using the .NET Framework Generics syntax. Also, provide as parameters to the Wrap method a reference to the custom configuration instance and a reference to the existing object you want to wrap. Note that the returned object is referenced as an instance of the target interface type.

    string storeName = "Contoso";
    IStoreTransactions orders = PolicyInjection.Wrap<IStoreTransactions>(configInfo, myOrderObject);
    
    'Usage
    Dim storeName As String = "Contoso"
    Dim orders As IStoreTransactions = PolicyInjection.Wrap(Of IStoreTransactions)(configInfo, myOrderObject)
    
  3. Call any policy-enabled methods of the target class. For example, this code assumes the class exposes a method named CountOrderItems that takes as a parameter the name of a customer and returns an integer value.

    int orderCount = orders.CountOrderItems("SMITH");
    
    'Usage
    Dim orderCount As Integer = orders.CountOrderItems("SMITH")
    
  4. Read and set policy-enabled properties of the target class. For example, this code assumes the class exposes a property named InvoiceGreeting of type System.String.

    string greeting = orders.InvoiceGreeting;
    orders.InvoiceGreeting = "Happy New Year";
    
    'Usage
    Dim greeting As String = orders.InvoiceGreeting
    orders.InvoiceGreeting = "Happy New Year"