2014-11-30 12:22:03 -08:00
|
|
|
|
using Castle.Facilities.TypedFactory;
|
|
|
|
|
using Castle.MicroKernel.Registration;
|
|
|
|
|
using Castle.Windsor;
|
|
|
|
|
using Stylet;
|
|
|
|
|
using System;
|
2015-09-24 09:48:40 -07:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Reflection;
|
2015-01-14 04:23:43 -08:00
|
|
|
|
using System.Windows;
|
2014-11-30 12:22:03 -08:00
|
|
|
|
|
|
|
|
|
namespace Bootstrappers
|
|
|
|
|
{
|
2015-01-15 02:06:00 -08:00
|
|
|
|
public class CastleWindsorBootstrapper<TRootViewModel> : BootstrapperBase where TRootViewModel : class
|
2014-11-30 12:22:03 -08:00
|
|
|
|
{
|
|
|
|
|
private IWindsorContainer container;
|
|
|
|
|
|
2015-01-15 02:06:00 -08:00
|
|
|
|
private object _rootViewModel;
|
2016-01-27 04:30:26 -08:00
|
|
|
|
protected virtual object RootViewModel
|
2015-01-15 02:06:00 -08:00
|
|
|
|
{
|
|
|
|
|
get { return this._rootViewModel ?? (this._rootViewModel = this.GetInstance(typeof(TRootViewModel))); }
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-30 12:22:03 -08:00
|
|
|
|
protected override void ConfigureBootstrapper()
|
|
|
|
|
{
|
|
|
|
|
this.container = new WindsorContainer();
|
|
|
|
|
this.DefaultConfigureIoC(this.container);
|
|
|
|
|
this.ConfigureIoC(this.container);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Carries out default configuration of the IoC container. Override if you don't want to do this
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected virtual void DefaultConfigureIoC(IWindsorContainer container)
|
|
|
|
|
{
|
2015-09-25 07:19:23 -07:00
|
|
|
|
var viewManager = new ViewManager(this.GetInstance, new List<Assembly>() { this.GetType().Assembly });
|
2014-11-30 12:22:03 -08:00
|
|
|
|
container.Register(
|
2015-09-25 07:19:23 -07:00
|
|
|
|
Component.For<IViewManager>().Instance(viewManager),
|
2015-09-24 09:48:40 -07:00
|
|
|
|
Component.For<IWindowManagerConfig>().Instance(this),
|
2015-09-25 07:19:23 -07:00
|
|
|
|
Component.For<IMessageBoxViewModel>().ImplementedBy<MessageBoxViewModel>().LifestyleTransient(),
|
|
|
|
|
// For some reason we need to register the delegate separately?
|
|
|
|
|
Component.For<Func<IMessageBoxViewModel>>().Instance(() => new MessageBoxViewModel()),
|
2014-11-30 12:22:03 -08:00
|
|
|
|
Component.For<IWindowManager>().ImplementedBy<WindowManager>().LifestyleSingleton(),
|
2015-09-25 07:19:23 -07:00
|
|
|
|
Component.For<IEventAggregator>().ImplementedBy<EventAggregator>().LifestyleSingleton()
|
2014-11-30 12:22:03 -08:00
|
|
|
|
);
|
2015-09-24 09:48:40 -07:00
|
|
|
|
container.Register(Classes.FromAssembly(this.GetType().Assembly).Pick().LifestyleTransient());
|
2014-11-30 12:22:03 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Override to add your own types to the IoC container.
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected virtual void ConfigureIoC(IWindsorContainer container) { }
|
|
|
|
|
|
2014-12-02 07:20:16 -08:00
|
|
|
|
public override object GetInstance(Type type)
|
2014-11-30 12:22:03 -08:00
|
|
|
|
{
|
2014-12-02 04:52:58 -08:00
|
|
|
|
return this.container.Resolve(type);
|
2014-11-30 12:22:03 -08:00
|
|
|
|
}
|
2015-01-14 04:23:43 -08:00
|
|
|
|
|
2016-01-27 04:30:26 -08:00
|
|
|
|
public override void Launch()
|
|
|
|
|
{
|
|
|
|
|
base.DisplayRootView(this.RootViewModel);
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-09 04:39:21 -08:00
|
|
|
|
public override void Dispose()
|
2015-01-14 04:23:43 -08:00
|
|
|
|
{
|
2016-01-27 04:30:26 -08:00
|
|
|
|
base.Dispose();
|
|
|
|
|
ScreenExtensions.TryDispose(this._rootViewModel);
|
|
|
|
|
if (this.container != null)
|
|
|
|
|
this.container.Dispose();
|
2015-01-14 04:23:43 -08:00
|
|
|
|
}
|
2014-11-30 12:22:03 -08:00
|
|
|
|
}
|
|
|
|
|
}
|