using Ninject; using Stylet; using System; using System.Collections.Generic; using System.Reflection; using System.Windows; namespace Bootstrappers { public class NinjectBootstrapper : BootstrapperBase where TRootViewModel : class { private IKernel kernel; private TRootViewModel _rootViewModel; protected virtual TRootViewModel RootViewModel { get { return this._rootViewModel ?? (this._rootViewModel = (TRootViewModel)this.GetInstance(typeof(TRootViewModel))); } } protected override void ConfigureBootstrapper() { this.kernel = new StandardKernel(); this.DefaultConfigureIoC(this.kernel); this.ConfigureIoC(this.kernel); } /// /// Carries out default configuration of the IoC container. Override if you don't want to do this /// protected virtual void DefaultConfigureIoC(IKernel kernel) { var viewManagerConfig = new ViewManagerConfig() { ViewFactory = this.GetInstance, ViewAssemblies = new List() { this.GetType().Assembly } }; kernel.Bind().ToConstant(new ViewManager(viewManagerConfig)); kernel.Bind().ToConstant(this).InTransientScope(); kernel.Bind().ToMethod(c => new WindowManager(c.Kernel.Get(), () => c.Kernel.Get(), c.Kernel.Get())).InSingletonScope(); kernel.Bind().To().InSingletonScope(); kernel.Bind().To(); // Not singleton! } /// /// Override to add your own types to the IoC container. /// protected virtual void ConfigureIoC(IKernel kernel) { } public override object GetInstance(Type type) { return this.kernel.Get(type); } protected override void Launch() { base.DisplayRootView(this.RootViewModel); } public override void Dispose() { ScreenExtensions.TryDispose(this._rootViewModel); if (this.kernel != null) this.kernel.Dispose(); base.Dispose(); } } }