Export (MEF) items in code.

Oct 19, 2012 at 6:13 PM

How would I export an item to the current container in code?  I have an IDataService with several implementations, but I want to determine which to use at runtime and register that one as shared.  How do I achieve this?

Coordinator
Oct 20, 2012 at 11:43 AM
Edited Oct 20, 2012 at 11:45 AM

Hi Adamaka,

My first thought was to use some of the MEF configuration code to do this on startup however there is an easier alternative. How about exporting an IDataServiceFactory with a GetDataService() method rather than exporting the IDataService itself? This could be exported as shared and could create the correct data service implementation when first called (or when first constructed if this makes more sense).

For example,

public interface IDataService
{
    ...
}

public interface IDataServiceFactory
{
    IDataService GetDataService();
}

[Export(typeof(IDataServiceFactory))]
[Shared]
public class DataServiceFactory : IDataServiceFactory
{
    Lazy<IDataService> currentDataService = new Lazy<IDataService>(CreateDataService);

    public IDataService GetDataService()
    {
        return currentDataService.Value;
    }

    private IDataService CreateDataService()
    {
        // Create the correct IDataService implementation

        ....
    }
}

In any calling code you could then import the IDataServiceFactory and call GetDataService(). You could even do this in the constructor and store the resulting IDataService as a field for future use.

Regards,

    Andy

Oct 23, 2012 at 4:18 PM

Works like a charm.

Thanks

Apr 26, 2013 at 5:05 PM
Hi Andy,
what's about debugging Okra and especially MEF in such a DataService environment? I did find nothing about MEF-Debugging which works for windows store apps.
regards
HOV
Coordinator
Apr 28, 2013 at 8:35 PM
HOV,

Where are you having issues with MEF?

In general MEF will throw an exception if there are any issues with composition and this is the same for Windows Store applications as with other MEF based applications. If you find your application does not start-up (i.e. just crashes on start-up because MEF can't compose the tree) then you may need to register with the Application.UnhandledException event to pick this up.

If you can provide more information on your problems then I might be able to be a bit more specific.

Regards,
Andy
May 6, 2013 at 3:06 PM
Hi Andy,
thank you.
I have already implemented a Application.UnhandledException Handler, but it didn't get nothing.
Because I am quite new to MEF, I think I should'nt start with a quite complex environment.
May be I use MEF in a second step.
Thanks again.
regards
HOV
Coordinator
May 6, 2013 at 8:10 PM
HOV,

No problem. Once you get your head around MEF it all makes sense, but sometimes it can be a bit confusing when getting started. The key thing to remember is that there has to be a chain of composition back to a root object. For example all Okra pages and viewmodels are composed by MEF by default, so anything they have that is marked with [Import] or in an [ImportingConstructor] will get composed, and any references of these items, etc. (e.g. view-model imports a repository that in turn imports a data service which in turn imports ...)

Keep me updated if you have any further issues though.

Andy