SimpleIoc - can it provide new instance each time required?

SimpleIOC is what it is ... a simple IOC container. It will have some shortcommings ... but you are not bound to it you could always use another ICO container (e.g. Unity, Autofac, Castle, ...).

As Laurent states he modelled his SimpleIOC on this container. He also mentions this container as a source of his inspiration.

However, remember you are not bound to use a secific container with MVVM. In several of my projects I used Unity, but every other IOC container will to equally well, it is a matter of requirements, customer preferences, and - if everything else fails - plain old personal gusto.


You can get a different instance each time by passing a different key to the GetInstance method. However the instances will be cached, so if you do not want to keep them in the cache, you will need to call Unregister with the corresponding key.

In the demo, I was creating the VM upfront because the MainVM was sending messages to the SecondaryVM. Since the registration to the Messenger is done in the SecondaryVm's constructor, it needs to be created before it can start receiving messages. The Messenger is great because it is very decoupled, but it is one of these cases where you need to do extra work to compensate the decoupling: The SecondaryVM is the target of messages even though the MainVM does not get any reference to it.

Hope it makes sense. Cheers, Laurent


After wrestling with SimpleIoC to provide new instances each time a specific type is requested and finding out this feature is not implemented (the key-based method accepted above doesn't fit scenarios where you want to say, perform a database operation and throw the connection away each time), I came up with a relatively decent solution combining IoC with the Factory pattern: Create a class that takes the responsibility of instantiating new instances of a certain type through a function:

class MyObjectFactory: IMyObjectFactory
{
    public MyObject CreateObject()
    {
        return new MyObject();
    }
}

Create the interface for MyObject factory class:

public interface IMyObjectFactory
{
    MyObject CreateObject();
}

Then configure the IoC container to provide the factory to any classes that use instances of MyObject:

SimpleIoc.Default.Register<IMyObjectFactory, MyObjectFactory>();

Now any class requiring a new instance of MyObject would declare its MyObjectFactory requirement (instead of MyObject requirement) in the constructor for constructor injection:

public class MyObjectUser
{
    MyObject _myObject;
    public MyObjectUser(IMyObjectFactory factory)
    {
        _myObject = factory.CreateObject();
    }
}

This way I think you're not bound by the limitations of the Factory pattern and have all the benefits of IoC containers and Constructor Injection, circumventing SimpleIoC's limitation as well.