开发者

Configuring IoC container from modules/plug-ins?

i am in big dilema.. I am working on highly modular web app in ASP.NET MVC 2 (in fact, core will be super lightweight, all work on modules/plugins). I found MEF pretty useful for modules discovery, but i dont want to us it as IoC container. There is pretty good chance that I will need advanced features of "true" IoC container, so I would like to use Unity.

And here is the problem : how to allow modules to configure container (programatically) = register their own types (mvc controllers, custom implementations of services...) at application start without making hard dependency on Unity in all modules ? I know about Common Service Locator project, and it seems pretty good, but this interface co container only allows resolving types, not registering them (afaik).

I开发者_如何学C really hope you can understand my point, I know my english is terrible (I am from non english speaking country :) Thanks a lot !


I can certainly sympathize with not wanting to use MEF as a DI Container, but I think you should still consider whether that might not be applicable for your add-ins.

You already require your add-ins to use MEF, so they will all have a hard dependency on it. While I personally don't like the hard-coded, attribute-based approach used by MEF, it sounds like you are asking how each add-in can register itself with the DI Container. That also sounds like hard-coding to me, so you might as well just use MEF all the way in.

Applying MEF attributes is component registration.

If you really don't want to use MEF you only have a few other options (none of them particularly more attractive):

  • Require all add-ins to also take a hard dependency on Unity. I completely understand why you don't want to do this, but I'm just including this option for the sake of completeness
  • Require all add-ins to also take a hard dependency on Common Service Locator. In my view, this only shifts the problem slightly.
  • Define your own add-in registration interface that all add-ins must implement. You can then write your own implementation that uses Unity, so that all add-ins register themselves against this interface, but then you would more or less just be duplicating the features of MEF.
  • Write all add-ins in a DI-friendly, but container-agnostic style. This leaves the problem with configuring the DI Container, and you will then have to resort to XML configuration for that. This is a very brittle approach and can quickly lead to maintainance hell, so once again I'm just including this option for the sake of completeness.

Using MEF for add-ins doesn't preclude you from using Unity in your core application, but I do understand that it's very lightweight, so that may not make a lot of sense.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜