简体   繁体   中英

Caliburn Micro IoC.Get

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM