Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Ninject across assemblies

Tags:

c#

ninject

I can see that similar questions has been asked previously, but being totally new to DI and .Net I am not able to grasps the entire solution or may not have found the right source....

  • I have assemblies WebAPI,BL,DL.
  • WebAPI is dependent on BL,
  • BL is dependent on DL,
  • WebAPI DOES NOT reference DL and I would like to keep it the same. There are few more assemblies but this is sufficient to illustrates the issues.

WebAPI has application start section therefore I can use it to initialize the Ninject Kernel and register dependencies for WebAPI project.

How could I achieve the same for BL and other assemblies?

like image 301
user1767363 Avatar asked Oct 25 '12 22:10

user1767363


1 Answers

There are a couple of different ways, you can use the Ninject Conventions to automagically resolve every ISomething to an implementation that has the same name (e.g. IThing -> Thing) or you can create a Ninject Module in each assembly which registers the dependencies (the module in your BL could load the module in your DL).

The approach you take would depend on whether you need to define different scopes for different objects, for example if you wanted some things resolved as singletons that may affect which method you use.

like image 145
Trevor Pilley Avatar answered Oct 12 '22 23:10

Trevor Pilley