Object Factories

Wherever an import in MEF requests a non-lazy export, MEF needs to create an instance of the type the export exists on (unless it’s a static export). The provider model exposes the possibility to customize the creation of the instance, with the help of object factories.

An object factory puts you in charge of returning an instance of whatever is requested by the MEF, by forwarding the request to an implementation of the ObjectFactory class. The class is really simple with only one method, Create, to implement.

public abstract class ObjectFactory
    public abstract object Create(Type type, params object[] parameters);

For example if you were to create (this will be included as of Preview 2) an object factory which used the NAspect AOP framework to create AOP proxies, you could do the following.

public class NAspectObjectFactory : ObjectFactory
    public override object Create(Type type, params object[] parameters)
        IEngine engine = 

        object proxy =
            engine.CreateProxy(type, parameters);

        return proxy;

This object factory would serve AOP proxies, instead of the actual instances, and enable you to inject interceptors at runtime.

An object factory is assigned to a DefinitionProviderPartCatalog instance and you can have different factories in place for different instances of the catalog. To assign a factory to a DefinitionProviderPartCatalog instance you set the value of the Factory property. If no factory is set then the DefaultObjectFactory is used.

The following object factories are currently available

Name Description
DefaultObjectFactory Creates plain instances of the requested type and forwards any parameters which may be present.

Last edited Mar 1, 2009 at 7:43 PM by TheCodeJunkie, version 1


No comments yet.