Skip to content

Instantly share code, notes, and snippets.

@maca134
Last active May 20, 2022 16:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maca134/ee0bc5ead3e41ea8600838efb7c5debd to your computer and use it in GitHub Desktop.
Save maca134/ee0bc5ead3e41ea8600838efb7c5debd to your computer and use it in GitHub Desktop.
Caliburn.Micro/Ninject Bootstrapper
using System;
using System.Collections.Generic;
using System.Windows;
using Caliburn.Micro;
using Ninject;
namespace Maca134.Stuff
{
internal abstract class NinjectBootstrapper<TViewModel> : BootstrapperBase
{
protected readonly IKernel Kernel = new StandardKernel();
protected NinjectBootstrapper()
{
Initialize();
}
protected override void Configure()
{
base.Configure();
Kernel.Bind<IWindowManager>().To<WindowManager>().InSingletonScope();
Kernel.Bind<IEventAggregator>().To<EventAggregator>().InSingletonScope();
Kernel.Bind<TViewModel>().ToSelf().InSingletonScope();
}
protected override void OnStartup(object sender, StartupEventArgs e)
{
base.OnStartup(sender, e);
DisplayRootViewFor<TViewModel>();
}
#region IOC
protected override object GetInstance(Type service, string key)
{
if (service == null)
throw new ArgumentNullException(nameof(service));
return Kernel.Get(service);
}
protected override IEnumerable<object> GetAllInstances(Type service)
{
return Kernel.GetAll(service);
}
protected override void BuildUp(object instance)
{
Kernel.Inject(instance);
}
protected override void OnExit(object sender, EventArgs e)
{
Kernel.Dispose();
base.OnExit(sender, e);
}
#endregion
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment