New Snippet New Snippet Recent Snippets Recent Snippets My Snippets My Snippets Web Code Search Snippets Search
Sign inor Register
Language: C#

CSL Export Provider and Unity Extension

414 Views
Copy Code Show/Hide Line Numbers
 
 
public class CSLExportProvider : ExportProvider
{
    private IServiceLocator serviceLocator;
    private IDictionary<string, Type> contractMapping;
    
    public CSLExportProvider(IServiceLocator serviceLocator)
    {
        this.contractMapping = new Dictionary<string, Type>();
        this.serviceLocator = serviceLocator;
    }
 
    //The IoC container needs to call this in order to tell MEF how to call it back.
    public void RegisterType(Type type)
    {
       contractMapping.Add(AttributedModelServices.GetContractName(type), type);
    }
 
    //Returns a lazy reference to a service coming out of the IoC
    protected override System.Collections.Generic.IEnumerable<System.ComponentModel.Composition.Primitives.Export> GetExportsCore(System.ComponentModel.Composition.Primitives.ImportDefinition definition, AtomicComposition atomicComposition)
    {
        if (definition.ContractName != null)
        {
            Type contractType;
            if(contractMapping.TryGetValue(definition.ContractName, out contractType))
            {
                if (definition.Cardinality == ImportCardinality.ExactlyOne || definition.Cardinality == ImportCardinality.ExactlyOne)
                {
                    var export = new Export(definition.ContractName, () => serviceLocator.GetInstance(contractType));
                    return new List<Export> { export };
                }
                
            }
        }
        return Enumerable.Empty<Export>();
    }
}
 
public class CSLExportProviderExtension : UnityContainerExtension
{
    private CSLExportProvider exportProvider;
 
    public CSLExportProviderExtension(CSLExportProvider exportProvider)
    {
        this.exportProvider = exportProvider;
    }
 
    protected override void Initialize()
    {
        this.Context.Registering += new EventHandler<RegisterEventArgs>(Context_Registering);
        this.Context.RegisteringInstance += new EventHandler<RegisterInstanceEventArgs>(Context_RegisteringInstance);
    }
 
    void Context_RegisteringInstance(object sender, RegisterInstanceEventArgs e)
    {
        exportProvider.RegisterType(e.RegisteredType);
    }
 
 
    void Context_Registering(object sender, RegisterEventArgs e)
    {
        exportProvider.RegisterType(e.TypeFrom);
    }
}
 
 
 
 
 
by Glenn Block
  September 26, 2009 @ 12:25am
Tags:
Comment:
Provides a bridge between MEF and a CSL instance. NOT bound to any IoC. Requires the container to call RegisterType for each type available within as it builds a contract mapping table.

To use, create the EP passing in your Service Locator instance, then pass that EP to the constructor of the CompositionContainer. Next call RegisterType passing in each interface that MEF should know about. In the sample you will find a UnityContainerExtension that you can pass to Unity in order to do this automatically.

Example:

var unityContainer = new UnityContainer();
var locator = new UnityServiceLocatorAdapter(unityContainer);
var ep = new CSLExportProvider(locator);
var catalog = new AggregateCatalog();
var container = new CompositionContainer(catalog, ep);
unityContext.AddExtension(new CSLExportproviderExtension(ep));
var foo = container.GetExportedValue<IFoo>(); //will resolve from Unity if not present in MEF, this includes dependencies

by Matthias Schippling    February 22, 2010 @ 10:17am

Is there a typo in line 28? I don't see the reason for ORing the same comparison.

Add a comment


Report Abuse
brought to you by:
West Wind Techologies


If you find this site useful and use it frequently please consider making a donation to support this free service.
Donate