Quantcast
Channel: patterns & practices: Project Silk
Viewing all articles
Browse latest Browse all 93

New Post: IServiceLocator inject at constructor VS ServiceLocator's static member?

$
0
0

Hi,

As far as I know, the main difference is that you are obtaining the ServiceLocator from two different places.

When you obtain an instance of the ServiceLocator through constructor injection, you are receiving the instance registered in your dependency injection container. On the other hand, when you obtain an instance of the ServiceLocator through the ServiceLocator.Current property, you are receiving the instance referenced by the ServiceLocator class. As far as I know, as out of the box, both should hold the same ServiceLocator instance; therefore, it would be no difference in using one approach or the other.

However, if in your application, one of those references is changed (for example, you register a new instance of the ServiceLocator in your dependency injection container) the ServiceLocator.Current property will return a different instance that the one obtained through construction injection. As long as you don't manually change the instance of the ServiceLocator, you should be able to use both approaches without any problems.

As a side note, even if your view model derives from another class, you should be able to use constructor injection too. As far as I know, the only thing you should take into account when doing so is passing the corresponding parameters to the constructor of the base class.

I hope you find this useful,

Damian Cherubini
http://blogs.southworks.net/dcherubini


Viewing all articles
Browse latest Browse all 93

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>