Inx51
Inx51

Reputation: 2089

Multiple DI-frameworks in a single ASP.net application

Is anyone aware of if its possible to have multiple DI-frameworks (for instance Unity and Ninject) within the same application. Or would the framework "collide" with each other?

Upvotes: 0

Views: 632

Answers (1)

Yacoub Massad
Yacoub Massad

Reputation: 27861

Everything is possible. But whether something makes sense is another matter.

You can create part of the object graph using DI framework 1, and then another part of the object graph with DI framework 2, and then you can wire/connect the two object graphs manually.

Please note that you probably shouldn't use a DI framework inside a class library (your framework). DI frameworks (or DI containers) should only be used inside applications (in the Composition Root).

Quoting from the referenced article (Composition Root):

Only applications should have Composition Roots. Libraries and frameworks shouldn't.

So, if you are just building some kind of framework (say for data access or email message processing), then you shouldn't use any DI container (DI and DI containers are different things). In your framework, you should enable DI, by declaring a class's dependencies in the constructor and making sure that such dependencies are abstract (e.g. declared as interfaces).

Consumers of your framework would then decide which DI container to use (or even if they want to use a DI container in the first place).

In my opinion, not using a DI container is better (even in applications). See this article for a reason why.

Upvotes: 2

Related Questions