Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Injecting your IoC container?

I've been researching all morning trying to find best practices for accessing your IoC. After adding constructor injection to your classes you still need to access the contianer from classes that might be deep in your applications object graph. In my case I'm doing MVVM in WPF and some of my View Models need to create other View Models and they would use the container to do so. But the question is where to do they get the container from. Does it make sense to inject it and pass it around? Is it ok to make it an injectable singleton? Is a factory that serves up a singleton more appropraite?

What's are the options and tradeoffs?

Update

I found this great talk by Matt Hinze that covers a lot of IoC ground: http://www.drowningintechnicaldebt.com/ShawnWeisfeld/archive/2010/04/08/inversion-of-control-in-action-by-matt-hinze-north.aspx

Looks like one answer is to use the scanning feature and store the IoC configurations in registries in each assembly and then during scaning those registry configurations will be added.

Are there other approaches to consider? Especially considering that Matt demonstrated using the ServiceLocator pattern while Mark Seeman calls this an anti-pattern. Note that Matt cautioned against over using the pattern and that Mark's definition of the Service Locator (http://blog.ploeh.dk/Trackback.aspx?guid=5f05c086-295b-41e5-a50a-ed0cd77ac4bd) seems different than what Matt demonstrated.

like image 944
Ryan Vice Avatar asked Dec 04 '11 14:12

Ryan Vice


1 Answers

Instead of injecting your actual ViewModel instances, which will be hard, as you say, you could inject factories to your top ViewModels. It's kind of like the service locator pattern, expect the factories (or services providers, or what have you) will be more specific in what they can deliver.

like image 197
lbergnehr Avatar answered Oct 06 '22 19:10

lbergnehr