By placing the MEF composition code directly in the App.xaml.cs file SoapBox limits the accessibility of the composition container to that single class. As soon as the code exits the OnStartup method there is no way to access the composition container until the application shuts down, right? So why isn't the composition container disposed as soon as composition is complete? What's the use of the composition container after composition?
Currently, SoapBox uses the composition container exclusively for application composition on start-up and does not provide a mechanism for accessing the composition container after start-up. You are absolutely right that the composition container could be disposed of as soon as composition is complete -- in-fact, you can try it yourself and you'll see that more eagerly disposing the composition catalog in the OnStartup method will not cause any problems, so long as you do it AFTER composition is complete! (No, really, try it. It requires you to change 2 lines of code and it is a good way to show/remind yourself that after composition an application that uses MEF is in no way different from an application that doesn't use MEF at all.)
Though you certainly could dispose of the composition container once composition completes, I prefer - and would suggest - doing the opposite. Namely, keep the composition container throughout the entire application lifecycle and make it more accessible to the add-ins. One approach to doing exactly this is detailed here: What To Do With MEF Composition Container After Application Start-Up. This post explains some of the added benefits had by the add-ins when the composition code is housed in its own class that exposes limited access of the composition container to interested add-ins.
I have no idea when the next release of SoapBoxCore will be, but I know I would love to see something like the CompositionService and the CompositionServiceLocator included in it for exactly the type of situation described in the referenced blog post.
answered 01 Nov '11, 23:06