Lifetime/accessibility of Container

Dec 14, 2012 at 10:34 AM

Hi there,

apologies if this is a stupid question, but what is the lifetime a MetroContainer?  I follow the registration and resolving of interfaces and instances, but the unit tests only demonstrate this within a single method, and use the same instance of the MetroContainer

In the WinRT app I'm building, I've got multiple views, and I want them to resolve various interfaces consistently, and the implementations of the interfaces is going to change over time - an ideal job for IoC

I'm haven't much experience of Xaml etc, but what I was hoping to do the registration at startup - (App.xaml.cs?) and then be able to resolve the necessary implementations in each View/ViewModel.  Creating a new instance of a MetroContainer in each View doesn't give access to anything created in App.xaml.cs

Is it a ServiceLocator I'm really after, or should I make the App instance of MetroContainer into publically accessible property?  What's the different ways of approaching this?

cheers

 

Toby

Coordinator
Dec 15, 2012 at 1:15 AM

Not a stupid question at all, Toby, but not entirely covered within the scope of this project! Would honestly suggest you do a bit of research of standard usage patterns of IoC containers, and then apply that to your WinRT code with metroioc.

 

That said, you will almost definitely want to create a singleton instance of the container for the whole app to use - don't create a container per view! 

One way to achieve this is what you suggest - by using a service locator to resolve the IoC container, and you can spin this up in App.xaml.cs

Personally, most of the xaml apps I write use Caliburn Micro (http://caliburnmicro.codeplex.com/) so I wire up the IoC in the Bootstrapper there...

 

Remember the fight club rule, though! (you don't talk about the IoC container) - if you find multiple calls to IoC.Resolve() littered through your code, then there's almost certainly something wrong; or at least something that could be improved by some refactoring.

 

There is a long 'todo' list on this project that I'm, frankly, 6 months away from addressing. I may put 'add sample project' onto that list...

 

thanks ;)

Ian

Jan 3, 2013 at 3:28 PM

Hey Ian,

thanks for that - I've just watched the "Roll your own MVVM framework" video from Mix10, and I like what I see - Caliburn Micro basically. 

A sample app would be great, once you get time. I got a lot from the unit tests, but not enough to use in it in anger. In the end, I went for a factory interface, with "GetXService" methods, with different implementations for testing, RT and WP8, and served it up as a singleton from App.cs. It works, but I think I can get something better with Caliburn

cheers


T