2
votes

Having an issue finding a straight answer on this, although that might well be a failure on my part to search for the correct terms.

What i want to know, is in caliburn micro using IoC.Get(); returns a reference to that existing object, and also ( I assume ) if there is no instance of that object the IoC will create one

Also, what if i was to create an instance manually via new, and later used ioc.get, would this return the same instance i created manually or a new one?

The reason i ask is this, i'm writing a WPF app, and im using IoC.Get to load my viewmodels initially, now due to a redesign i would like the ability to create a whole new set of those views, so my parent viewmodel looks similar to this:

     public ProjectContainerViewModel()
     {
            _containersListModel = IoC.Get<ContainersListViewModel>(); 
            _orderedItemListModel = IoC.Get<ItemsOrderedViewModel>(); 
            _packageListModel = IoC.Get<PackagesListViewModel>(); 
     }

Could i instead create new instances here using _containersListModel = new ContainersListModel(). and from then on get that same instance with IOC?

EDIT: I'm using the bootstrapper found here: https://caliburnmicro.codeplex.com/wikipage?title=Customizing%20The%20Bootstrapper

Which defines the container, i'm also marking my viewmodels with [Export(typeof(ContainersListViewModel))] at the start of the class.

1
If you "new up" a new instance, the IoC container won't have any knowledge of that unless you add (or change) one of your bindings. Also, depending on bindings, you might get a new instance for every call to IoC.Get(), or the same instance over and over (singleton), depending on the way the implementation was bound. 1) What are you using for your DI container (e.g. Caliburn.Micro's SimpleContainer or something else)? 2) How are you creating the container's bindings (or are you letting it resolve "automatically")? - Steve
Also, you say you want to "create a whole new set of those views" - this has nothing to do with viewmodels. You want to create a new set of viewmodels? Just make sure that the IoC container isn't returning singletons, and then just call IoC.Get() again to get a new one. - Steve
I'm using the CompositionContainer that is part of the CM getting started stuff, also the viewmodels are marked with [Export(typeof(ContainersListViewModel))]. So to answer your questions, I'm not sure what it currently does, and thats why i'm asking because i cannot find out :( - Ben
this is MEF that is being used. MEF is the Container in this case not SimpleContainer. IoC is only getting something that "might" be in the container. Giving you either the "instance" (i.e. IEventAggregator) or giving you a "new instance". If it is not exported then its not in the container and IoC won't find it.. [Export()] has other properties that you need to know about before proceeding. Reference mef.codeplex.com - mvermef

1 Answers

4
votes

The default implementation of IoC in Caliburn Micro is a simple "new", but the IoC is an extension point you can plug ( and probably want ) your own IoC container. In order to do so you have to override the following functions in your bootstrapper:

            protected virtual void BuildUp(object instance);

        protected virtual object GetInstance(Type service, string key);

And write them to build the object using your preferred IoC container. Even if you feel you don't need an IoC container in the first steps, calling IoC.Get to build up your object will make your code easier to port in the future.