We are using Ninject with Caliburn.Micro to create a MVVM WCF Silverlight app. The issue I am having is with the life cycle of my view models.
I have created a simple ninject module to bind my view model and wcf client.
public class IDCardModule : NinjectModule
{
public override void Load()
{
Bind<IIdCardManagerClient>().To<IdCardManagerClient>();
Bind<IDCard.SL.ViewModel.IIdCardViewModel>().To<IDCard.SL.ViewModel.IdCardViewModel>();
}
}
In my IIdCardViewModel I have required it inherit from IDisposable, because I want to register and de-register for wcf events and some local unmanaged references.
However, Dispose is never being called.
I looked into adding a deactivation to call dispose like this:
Bind<IDCardExclude.SL.ViewModel.IIdCardExclusionViewModel>().To<IDCardExclude.SL.ViewModel.IdCardExclusionViewModel>().OnDeactivation(
m => m.Dispose());
But that forced me to add two things, an Unload override in my IDCardModule that retrieved the object and released it:
var releaseMe = this.Kernel.Get<IIdCardViewModel>();
this.Kernel.Components.Get<Ninject.Activation.Caching.ICache>().Release(releaseMe);
and either .InThreadScope() or .InSingletonScope() to my Bind method in Load.
Is there an easier way to force a deactivation on a specific object? Or should I look into another IOC framework?
I looked into IStartable and had similar issues. As well, I read in-depth Nate's article where he has an activation block and wraps everything in a using statement. My issue there is that my view model can be long running and I don't believe his solution will work here. As well, having a special thread that sleeps and calls GC.Collect, doesn't smell right either.
InTransientScoped objects lifecycle is not managed by Ninject. This means theese objects are not disposed and deactivated. If your view model is injected to another object you can use InParentScope from the named scope extension. See my blogpost about additional scopes of Ninject: http://www.planetgeek.ch/2010/12/08/how-to-use-the-additional-ninject-scopes-of-namedscope/