Creating New Policy-Enabled 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 requires a new instance of a policy-enabled object, it should use the static Create method. The Policy Injection Application Block creates a new instance of the object, a suitable proxy instance, and then—based on configuration information or attributes applied to members—it installs the handler pipeline between all the policy-enabled members of the target object and the matching members of the proxy.

The Create method exposes four overloads to support the two different types of interceptable objects and to allow developers to provide custom configuration information:

Create<TargetClass>(target_constructor_parameters):

  • This creates a new instance of the class TargetClass that inherits from MarshalByRefObject and creates a handler pipeline using the default configuration information for the application block.

Create<TargetClass, ITargetInterface>(target_constructor_parameters):

  • This creates a new instance of the class TargetClass that implements the interface ITargetInterface and creates a handler pipeline using the default configuration information for the application block.

Create<TargetClass>(IConfigurationSource, target_constructor_parameters):

  • This creates a new instance of the class TargetClass that inherits from MarshalByRefObject and creates a handler pipeline using the configuration information stored in a class that implements the IConfigurationSource interface.

Create<TargetClass, ITargetInterface>(IConfigurationSource, target_constructor_parameters):

  • This creates a new instance of the class TargetClass that implements the interface ITargetInterface and creates a handler pipeline using the configuration information stored in a class that implements the IConfigurationSource interface.

The next procedure describes how to create a new policy-enabled instance of a class named OrderTransactions that inherits from MarshalByRefObject using the default configuration information for the Policy Injection Application Block.

To create a new policy-enabled instance of a class that inherits from MarshalByRefObject

  1. Call the static Create method that the Policy Injection Application Block exposes, specifying the class name of the target object using the .NET Framework Generics syntax and any parameters required by the constructor of the target class. The following code example assumes that the OrderTransactions class requires the name of the store for which it will manipulate orders.
  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.
  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.

The next procedure describes how to create a new policy-enabled instance of a class named OrderTransactions that implements the interface IStoreTransactions using the default configuration information for the Policy Injection Application Block.

To create a new policy-enabled instance of a class that implements a known interface

  1. Call the static Create method that the Policy Injection Application Block exposes, specifying the class name of the target object and the name of the known interface that the class implements using the .NET Framework Generics syntax. Also, specify any parameters required by the constructor of the target class. The following code example assumes that the OrderTransactions class requires the name of the store for which it will manipulate orders.
  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.
  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.

The next procedure describes how to create a new policy-enabled instance of a class named OrderTransactions that inherits from MarshalByRefObject using the configuration information stored in an instance of a class that implements the IConfigurationSource interface.

To create a new policy-enabled 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 example. For best performance, you should create the IConfigurationSource once and cache it for reuse in the future. For more information about creating custom configuration information, see Creating Objects Using the Provider Factory Methods.
  2. Call the static Create method that the Policy Injection Application Block exposes, specifying the class name of the target object using the .NET Framework Generics syntax. Also, specify the reference to the class instance that contains the custom configuration information and any parameters required by the constructor of the target class. This example assumes that the OrderTransactions class requires the name of the store for which it will manipulate orders.
  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.
  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.

The next procedure describes how to create a new policy-enabled instance of a class named OrderTransactions that implements the interface IStoreTransactions using the configuration information stored in an instance of a class that implements the IConfigurationSource interface.

To create a new policy-enabled 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 best performance, you should create the IConfigurationSource once and cache it for reuse in the future. For more information about creating custom configuration information, see Creating Objects Using the Provider Factory Methods.
  2. Call the static Create method that the Policy Injection Application Block exposes, specifying the class name of the target object and the name of the known interface that the class implements using the .NET Framework Generics syntax. Also, specify the reference to the class instance that contains the custom configuration information and any parameters required by the constructor of the target class. This example assumes that the OrderTransactions class requires the name of the store for which it will manipulate orders.
  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.
  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.