This project has moved and is read-only. For the latest updates, please go here.

MEF2: ExportFactory created object - how to create more objects with the same scope?

Dec 7, 2011 at 8:37 AM

;) Yes, i know.

Given is a specific object that is created (with a separate scope) from an ExportFactory. In the running code of this object, I need to create more objects via GetExportedValue. There is no way to determine the needed objects before runtime (so a property with Import is not possible) as they are data driven.


Given a DataItemContext that is generated via ExportFactory (there are various numbers of them, one for every thread handling data items and we use up to 100 of them at the same time).

The DataItemContext instance gets from a thread a list of items and or every item it creates a DataItemProcessor dynamically. DataItemProcessors are shared (within the same DataItemContect - they are recreated if they already exist). Hwo do I do that?

In MEF1 my own scoping implementation would have a CompositionContainer for DataItemContext with the "Context" scope and the processors would live in this context, so every DataItemContext has it's own cache of items.

My idea is to get away from multiple CompositionContainers and use the MEF2 scoping. The ExportFactory seems like the proper way to get the DataItemContext. I can see via the MEF2 scoping how I can set up the DataItemProcessor subclasses to be tied to the scope of a Context. I can see that if I would use an Import or ImportMany those owuld be destroyed with the DataItemContext from the ExportFactory. Nice.

Now, I can rewrite the mechanism to use this. Or stay with my current approach of dynamically asking for an export depending on the data I discover. I jsut do not see any way to ask for an export (via GetExportedValue or ExportFactory) to enter the proper scope into the request. How would a call to CompositionContainer's GetExportedValue know the instance of DataItemContext it would have to attach the DataItemProcessor to? Please advice - and yes, I know I am a little stretching MEF here.