Stylet/Bootstrappers/NinjectBootstrapper.cs

61 lines
2.1 KiB
C#
Raw Normal View History

using Ninject;
using Stylet;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Windows;
namespace Bootstrappers
{
public class NinjectBootstrapper<TRootViewModel> : BootstrapperBase where TRootViewModel : class
{
private IKernel kernel;
private object _rootViewModel;
protected override object RootViewModel
{
get { return this._rootViewModel ?? (this._rootViewModel = this.GetInstance(typeof(TRootViewModel))); }
}
protected override void ConfigureBootstrapper()
{
this.kernel = new StandardKernel();
this.DefaultConfigureIoC(this.kernel);
this.ConfigureIoC(this.kernel);
}
/// <summary>
/// Carries out default configuration of the IoC container. Override if you don't want to do this
/// </summary>
protected virtual void DefaultConfigureIoC(IKernel kernel)
{
var viewManagerConfig = new ViewManagerConfig()
{
ViewAssemblies = new List<Assembly>() { this.GetType().Assembly },
ViewFactory = this.GetInstance,
};
kernel.Bind<ViewManagerConfig>().ToConstant(viewManagerConfig);
kernel.Bind<IViewManager>().To<ViewManager>().InSingletonScope();
kernel.Bind<IWindowManagerConfig>().ToConstant(this);
kernel.Bind<IWindowManager>().ToMethod(c => new WindowManager(c.Kernel.Get<IViewManager>(), () => c.Kernel.Get<IMessageBoxViewModel>(), c.Kernel.Get<IWindowManagerConfig>())).InSingletonScope();
kernel.Bind<IEventAggregator>().To<EventAggregator>().InSingletonScope();
kernel.Bind<IMessageBoxViewModel>().To<MessageBoxViewModel>(); // Not singleton!
}
/// <summary>
/// Override to add your own types to the IoC container.
/// </summary>
protected virtual void ConfigureIoC(IKernel kernel) { }
public override object GetInstance(Type type)
{
return this.kernel.Get(type);
}
2015-02-09 04:39:21 -08:00
public override void Dispose()
{
this.kernel.Dispose();
}
}
}