I think there are a few misunderstandings about what MEF can and cannot do.
MEF was originally conceived as a pure extensibility architecture, but as the platform evolves to its first release, it can be fully supported as a DI container. MEF will handle the dependency injection for you and does this through this ExportProvider . It is also possible to use other DI frameworks with MEF . Thus, there are actually several ways to achieve this:
- Create a
NinjectExportProvider that you can connect to the MEF, so when the MEF is looking for an available export, it will be able to request your Ninject container. - Use the implementation of the shared services locator pattern to connect between MEF and Ninject, or vice versa.
Since you use MEF for extensibility, you probably want to use the first, as this provides your Ninject components for MEF, which in turn provides them to your plugins.
Another thing that is a little disappointing, in fact, there is not much room for automatically enabling the ala Wordpress features on ASP.NET. ASP.NET is a compiled and managed environment, and because of this, you either resort to late binding, loading assemblies manually at runtime, or reloading the application to pick up new plugins, which spoils the object, which is able to connect new extensions through the application.
My advice: plan your architecture to pick up all the extensibility points as a launch and assume that any kernel changes will require a restart of applications and applications.
In terms of direct questions:
CompositionProvider takes as an example the ContainerConfiguration , which is used internally to create the CompositionContainer used by the provider. That way, you can use this as a point with which you set up a way to instantiate your container. ContainerConfiguration supports the WithProvider method:
var configuration = new ContainerConfiguration().WithProvider(new NinjectExportDescriptorProvider(kernel)); CompositionProvider.SetConfiguration(configuration);
Where NinjectExportDescriptorProvider can be:
public class NinjectExportDescriptorProvider: ExportDescriptorProvider { private readonly IKernel _kernel; public NinjectExportDescriptorProvider(IKernel kernel) { if (kernel == null) throw new ArgumentNullException("kernel"); _kernel = kernel; } public override IEnumerable<ExportDescriptorPromise> GetExportDescriptors( CompositionContract contract, DependencyAccessor dependencyAccessor) { var type = contract.ContractType; if (!_kernel.GetBindings(type).Any()) return NoExportDescriptors; return new[] { new ExportDescriptorPromise( contract, "Ninject Kernel", true,
Note. I have not tested this, this is all theory and is based on the example of AppSettingsExportDescriptorProvider at: http://mef.codeplex.com/wikipage?title=ProgrammingModelExtensions
It differs from the standard ExportProvider because the use of CompostionProvider built around a lightweight composition. But essentially you are ending access to your Ninject core and making it available to your CompositionContainer .
As with adding a specific new provider (see above), you can use ContainerConfiguration to read available assemblies, perhaps something like:
var configuration = new ContainerConfiguration().WithAssemblies(AppDomain.GetAssemblies())
Again, I have not tested all of this, but hope this at least points you in the right direction.