开发者

How do I use Common Service Locator in Ninject 2

开发者 https://www.devze.com 2023-01-16 16:33 出处:网络
C开发者_C百科hanges in Ninject 2 say that Ninject support Common Service Locator, but how do I use it? I don\'t find any manual or sample.The Ninject2 download contains a CommonServiceLocator.NinjectA

C开发者_C百科hanges in Ninject 2 say that Ninject support Common Service Locator, but how do I use it? I don't find any manual or sample.


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.

0

精彩评论

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