1

I hope someone can shine some light on best practise for my usecase.

I am using mvvm light and the simpleIoC container in a wpf usercontrol. I register my model and view models, dataservice and designtime service to it (very much according to mvvm light sample code)

The SimpleIoC container usage examples that I have seen seems to always treat the container as static / global for GalaSoft namespace.

But if I would create two instances of my WPF control in the same application I of course want each user control to have its own set of VMs and Model instance. So basically its own set of SimpleIoC registered instances. How would I best accomplish that when the default IoC container seems to be a static object?

Johan
  • 502
  • 4
  • 18

3 Answers3

2

When you getinstance you can optionally provide a key. Although you get a singleton per type by default this generates another cached version of that type per key. Meaning you could use a guid or something as a key per instance you require.

There's a potential problem though. If you getinstance 100 different versions then they're all in memory for the lifetime of your app.

You're probably ok if this is just going to be a few instances.

Any more and you're probably best using a more sophisticated di container. SimpleIoC is only intended for simple use cases.

You can, however, use a factory method when you getinstance. This is not to my taste, but if you really wanted to use simpleioc then it's something to consider.

You can read more from laurent bugnion here.

https://msdn.microsoft.com/en-us/magazine/jj991965.aspx

Andy
  • 11,864
  • 2
  • 17
  • 20
  • Thanks this explained it very well. So Mark Feldman's answer about using factory pattern is just to provide a Guid or similar and generate an instance for that Guid. So if I use the factory method, that method would by itself have to keep the cached instances? Seems a lot of code for something that "should" be easy. – Johan Apr 24 '19 at 07:00
  • I think he's suggesting using the factory instead of a di container. And yes. What you seem to want to do is way easier with a more sophisticated di container. Simple is only good if it's also sufficient. – Andy Apr 24 '19 at 07:42
  • The answer of the question was just to create a separate instance of the SimpleIoC. That solved my problem since the VMLocator is not a singleton either. But I upvoted this post since it gave so much more useful details. Thanks! – Johan Apr 24 '19 at 14:59
1

That's what the factory pattern is designed to solve, you create a class that creates your control view models and you inject that instead.

Better separation of concerns too.

Mark Feldman
  • 15,731
  • 3
  • 31
  • 58
  • thanks, can you provide some example or reference? I googled factory pattern but I dont see how I van use it such that two usercontrols get their own set of VM instances and that they get the correct instance on using GetInstance(vmKeyString) ? – Johan Apr 23 '19 at 05:20
0

How would I best accomplish that when the default IoC container seems to be a static object?

Don't use the default container but create your own instance of the SimpleIoc class:

User Control A:

SimpleIoc containerA = new SimpleIoc();
containerA.Register<ViewModel>();
...
ViewModel vm = containerA.GetInstance<ViewModel46>();

User Control B:

SimpleIoc containerB = new SimpleIoc();
...
mm8
  • 163,881
  • 10
  • 57
  • 88
  • Yes this works partly. Basically I can indeed create a new instance of the container in the viewmodellocator and register the VMs to it. But then how do I get constructor Dependency Injection to work? – Johan Apr 23 '19 at 15:43
  • @Johan. It depends. How does it work currently. And where does your UserControls fit into this? Please don't ask additional questions in the comments field once your original question has been answered. – mm8 Apr 24 '19 at 11:37
  • This may work, but from a "purist" IoC perspective it doesn't look very good. I mean, creating a new container for each user control? I do understand that a better approach (factories, lifetime scopes) may not be available in SimpleIoc though. – rsenna Jul 14 '20 at 11:56