问题
So I am aware that the CommonServiceLocator is an intermediary tie for allowing you to switch out different IoC containers, but looking at the MVVM Light's sample code, specifically the ViewModelLocator, it seems interesting in the sense that
You register the SimpleIoC as the default locator, but at the same time you call Register on the SimpleIoc default instance.
So if you were going to tear out SimpleIoC in the future, wouldn't it mean you'll need to update all the registration calls?
Another question is, having used Castle Windsor in the past, and it's support for loading types by configuring them via the .config file, how would it work in the Windows 8 scenario?
I'm still trying to find the scenario's where the use of IoC makes sense now?
回答1:
The Common Service Locator implementation guarantees that you do not need to replace the getter calls (GetInstance, GetAllInstances, etc) but they stopped there. Registration is what makes an IOC container unique; for example, as you mention, some support configuration with config files (what SimpleIoc does not). So yes, when you replace SimpleIoc with another IOC container of your choice, you will typically replace the registration part. Note that it is usually OK because these calls are typically grouped in a common place, so they are rather easy to replace.
For more info, check the http://commonservicelocator.codeplex.com/ page.
Note that my goal in adding SimpleIoc to MVVM Light was not to offer an all-powerful IOC container. In fact, I bow to the existing ones (Castle Windsor, StructureMap, Unity, etc). Instead I wanted to provide a simple implementation that will lure unexperienced users in the world of IOC containers. The idea is that, when they need more advanced features, they will move to another, more complex implementation.
Hope this makes sense! Laurent
来源:https://stackoverflow.com/questions/10042628/mvvm-light-simple-ioc-commonservicelocator-trying-to-understand-the-purpose