Using the Create Method

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.

To create a new configured instance of an interceptable target class, the developer calls the static Create method of the Policy Injection Application Block factory class named PolicyInjection.

If the application block determines that the configuration specifies handlers for a method or property of the specified target class, it does the following:

  • It creates an instance of the target class.
  • It creates a suitable proxy for the class or for the specified interface that the class implements.
  • It wires up the specified handlers into a pipeline between the relevant methods and properties of the proxy and the concrete target class instance.
  • It returns a reference to the proxy.

If the application block determines that the configuration or target class attributes do not specify any matching handlers, the Create method just returns a new instance of the target class.


Ff648546.note(en-us,PandP.10).gifNote:
You must create instances of any objects that will potentially be policy-enabled using this factory approach instead of directly creating instances with the new operator, even when there is no policy to apply at the current time. This allows you to add policies in the future without requiring changes to the client code that creates the object instance. However, creating objects through the Policy Injection Application Block factory class does incur a small processing overhead. Where you know you will never want to apply a policy, especially in performance-critical situations, you should use the new operator instead.

Target Classes That Inherit from MarshalByRefObject

For a target class that inherits from MarshalByRefObject, client code uses the .NET Framework generic code syntax to specify the class name and any parameters that the target class constructor requires.

A class can have several constructors. Rules determine which constructor is chosen to create the object. For information about the rules for constructor selection, see Activator.CreateInstance Method (Type, Object[]) (System).

The Create method returns an instance of a policy-enabled proxy through which client code can access that class, as shown in the following code.

TargetClass theTarget = PolicyInjection.Create<TargetClass>(parameter1, parameter2);

Target Classes That Implement an Interface

For a target class that implements an interface, client code can use the .NET Framework generic code syntax to specify the implementing class name, the interface name, and any parameters that this target class constructor requires. The Create method returns an instance of a policy-enabled proxy through which client code can access the target class that implements the specified interface type, as shown in the following code.

ITargetInterface theTarget 
  = PolicyInjection.Create<TargetClass, ITargetInterface>(parameter1, parameter2);


Show: