Getting Started

The first step in using the AdaptiveFactory class is to decide on a base class for your runtime types. It is convenient to use a custom base class as it gives a natural location for the code called when the interface methods on the produced runtime types are invoked.

Assuming that base class is MyBase, the first step is to create and configure your factory:

// Create and configure factory instance
AdaptiveFactory<MyBase> factory = new AdaptiveFactory<MyBase>();
factory.ImplementMethods().UsingSharedExecuter(Init, Exec);

This creates a factory that forwards calls to all interface methods to the same method Exec on MyBase class. Whenever a new instance of a runtime type is created, the Init method on MyBase is called.

Once factory configuration is completed (can also include properties and events), new runtime types can be created using the Implement method:

// Implement interface IMyInterface using runtime type derived from MyBase
Type myType = factory.Implement<IMyInterface>();

The produced type will have the same public constructors as its base class. So if MyBase has a single public constructor taking e.g. a string, instances of produces runtime types can be made like this:

// Instantiate runtime type
IMyInterface myInstance = (IMyInterface)Activator.CreateInstance(myType, "ctor argument");

Samples

The Adaptive.Mail project illustrates a simple usage of AdaptiveFactory to implement a custom access layer.

Limitations

The AdaptiveFactory cannot yet handle indexed properties and ref/out parameters.

Last edited May 25, 2014 at 6:56 PM by ulrikborn, version 2