How do I use Common Service Locator in Ninject 2
A

1

14

Changes in Ninject 2 say that Ninject support Common Service Locator, but how do I use it? I don't find any manual or sample.

Anabantid answered 20/9, 2010 at 10:43 Comment(0)
Q
27

The Ninject2 download contains a CommonServiceLocator.NinjectAdapter.dll (in the 'extensions' folder of the zip file). Add this dll and the Ninject.dll to your project.

In the startup path of your application (after configuring the Ninject kernel), you can use the following code:

IKernel ninject = ...; 

var locator = new NinjectServiceLocator(ninject);

ServiceLocator.SetLocatorProvider(() => locator);

Please note that the Common Service Locator (CSL) is a library that provides an abstraction over IoC containers and contains a static facade (implementing the Service Locator pattern). It is a common interface that developers can use to prevent depending on a specific IoC implementation. The CSL is especially useful for framework developers. Application developers are better of using the dependency injection (DI) pattern. When DI is used correctly, there is no need for the use of the CSL in an application. While the CSL is more suited for framework development, even there I find the use of the CSL questionable. In believe that most of the time the use of such common interface can be removed by having a proper design.

Quickman answered 20/9, 2010 at 11:24 Comment(1)
What does CommonServiceLocator do?Sendal

© 2022 - 2024 — McMap. All rights reserved.