How destroy objects in IoC - created with MEF

Jan 2, 2011 at 5:46 PM

Hi I use MEF and caliburn.micro in WPF app. I would like know how can I destroy instaces created with MEF.

For example simple shell:

 

 [Export(typeof(IShellViewModel))]
public class ShellViewModel : Conductor<IScreen>.Collection.OneActive, IShellViewModel
{

protected override void OnInitialize()
{
ShowLogOn();
base.OnInitialize();
}

//first screen
public void ShowLogOn()
{
var vm = IoC.Get<ILogOnViewModel>();
ActivateItem(vm);
}

//second screen
public void ShowMessenger(Account account)
{
ActiveItem.Deactivate(true);
var vm = IoC.Get<IMessengerViewModel>();
vm.Account = account;
ActivateItem(vm);
}

}

 First screen:

 

[Export(typeof(ILogOnViewModel))]
public class LogOnViewModel : Screen,ILogOnViewModel
{
User user=new User();
}

 Second screen:

 

  [Export(typeof(IMessengerViewModel))]
public class MessengerViewModel : Screen, IViewModelIdentity,
IMessengerViewModel, IHandle<Rp>, IHandle<string>
{..}

WPF app start with first screen associated with ILogOnViewModel interface. Then I deactivate this screen and activate second screen associated with IMessengerViewModel interface.

I check memory usage with ANTS Mememory profiler but instace of ILogOnViewModel is still alive also instace of class User is alive.

I am newbie in IoC, DI... all class which are exported with MEF must live during the whole WPF application lifetime cycle?

Other example, I create new wpf window with window manager.

 

[Export(typeof(IChatViewModel))]
[PartCreationPolicy(CreationPolicy.NonShared)]
public class ChatViewModel : Screen, IViewModelIdentity,
IChatViewModel, IHandle<Rp>, IHandle<DetailData>
{}

ChatViewModel is WPF Window.

Create instance of IChatViewModel:

private IWindowManager _windowManager;
var chatScreen = IoC.Get<IChatViewModel>();
_windowManager.Show(chatScreen);

Then I close (click on X (close) button on window) WPF Window, ChatViewModel is deactivated but instace of this class is still alive.

It exist way how kill/destroy this instaces?

Jan 3, 2011 at 2:26 PM

I've answered a similar question on stackoverflow.