using System; using System.Collections.Generic; using System.Reflection; using Ninject; using Stylet; namespace Artemis.UI.Stylet { public class NinjectBootstrapper : BootstrapperBase where TRootViewModel : class { private object _rootViewModel; protected IKernel Kernel; protected virtual object RootViewModel => _rootViewModel ?? (_rootViewModel = GetInstance(typeof(TRootViewModel))); protected override void ConfigureBootstrapper() { Kernel = new StandardKernel(); DefaultConfigureIoC(Kernel); ConfigureIoC(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 = GetInstance, ViewAssemblies = new List {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 Kernel.Get(type); } protected override void Launch() { DisplayRootView(RootViewModel); } public override void Dispose() { base.Dispose(); ScreenExtensions.TryDispose(_rootViewModel); if (Kernel != null) Kernel.Dispose(); } } }