Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use IoC container for plugin architecture

Correct me if I'm wrong, but MEF only is good for managing a set of unknown things (plugins) that can be auto-discovered and auto wired up. For a future project we will need a true IoC container to explicitly configure the known parts of the application (which MEF is not good at) but additionally we also need to support auto-discovered plugins (preferably POCO without attributes, if that is possible).

Can an IoC container support this easily/by default? If so can you give a quick hint on how this is done in Unity and StructureMap? Those are the two we currently favor. We really would like to avoid a dependency on an IoC container and MEF.

like image 696
bitbonk Avatar asked Feb 10 '11 22:02

bitbonk


1 Answers

I think it's important to note that while MEF isn't an IoC container in a conventional sense, it is performing inversion of control. In fact, I would disagree with that and say MEF is an IoC container much like any other. The real difference between say Unity and MEF, is that MEF by default supports composition over explicit type resolution, and type discovery over configuration. But, as we've seen with the MEFContrib project, it is entirely possibly to have MEF behave more like a traditional IoC container. MEF provides you a great baseline for modular component behaviour, taking a lot of the hard grafting out, and the way it is designed, it allows you to add in more functionality it. Let's say for instance you have your existing codebase built around another IoC container or service locator, you could wire up an ExportProvider to do so, heck you could wire up a provider to a service locator such as the Common Service Locator project and then plug in a compatible CSL implementation and have MEF composing component parts using types derived from your other IoC container. MEF also performs dependency injection for you.

If you want to avoid having a dependency on a specific IoC container or MEF itself, you could always use something like the Common Service Locator which is an abstraction over common container operations. That way if you need/want to change how everything is plumbed together, it's relatively painless. There are compatible CSL implementations for most IoC containers, and MEF.

Hope that helps.

like image 173
Matthew Abbott Avatar answered Oct 04 '22 02:10

Matthew Abbott