Last active
May 20, 2022 16:27
-
-
Save maca134/ee0bc5ead3e41ea8600838efb7c5debd to your computer and use it in GitHub Desktop.
Caliburn.Micro/Ninject Bootstrapper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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