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))); public override object GetInstance(Type type) { return Kernel.Get(type); } public override void Dispose() { base.Dispose(); ScreenExtensions.TryDispose(_rootViewModel); if (Kernel != null) Kernel.Dispose(); } 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) { ViewManagerConfig viewManagerConfig = new() { ViewFactory = GetInstance, ViewAssemblies = new List {GetType().Assembly} }; kernel.Bind().ToConstant(new ArtemisViewManager(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) { } protected override void Launch() { } } }