2014-11-30 07:57:08 -08:00
|
|
|
|
using Autofac;
|
|
|
|
|
using Stylet;
|
|
|
|
|
using System;
|
2015-09-24 09:48:40 -07:00
|
|
|
|
using System.Collections.Generic;
|
2014-11-30 07:57:08 -08:00
|
|
|
|
using System.Linq;
|
2015-09-24 09:48:40 -07:00
|
|
|
|
using System.Reflection;
|
2015-01-14 04:23:43 -08:00
|
|
|
|
using System.Windows;
|
2014-11-30 07:57:08 -08:00
|
|
|
|
|
|
|
|
|
namespace Bootstrappers
|
|
|
|
|
{
|
2015-01-15 02:06:00 -08:00
|
|
|
|
public class AutofacBootstrapper<TRootViewModel> : BootstrapperBase where TRootViewModel : class
|
2014-11-30 07:57:08 -08:00
|
|
|
|
{
|
|
|
|
|
private IContainer 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 07:57:08 -08:00
|
|
|
|
protected override void ConfigureBootstrapper()
|
|
|
|
|
{
|
|
|
|
|
var builder = new ContainerBuilder();
|
|
|
|
|
this.DefaultConfigureIoC(builder);
|
|
|
|
|
this.ConfigureIoC(builder);
|
|
|
|
|
this.container = builder.Build();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Carries out default configuration of the IoC container. Override if you don't want to do this
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected virtual void DefaultConfigureIoC(ContainerBuilder builder)
|
|
|
|
|
{
|
2016-07-09 04:21:02 -07:00
|
|
|
|
var viewManagerConfig = new ViewManagerConfig()
|
|
|
|
|
{
|
|
|
|
|
ViewFactory = this.GetInstance,
|
|
|
|
|
ViewAssemblies = new List<Assembly>() { this.GetType().Assembly }
|
|
|
|
|
};
|
|
|
|
|
builder.RegisterInstance<IViewManager>(new ViewManager(viewManagerConfig));
|
2015-09-25 07:19:23 -07:00
|
|
|
|
|
2016-11-25 09:04:50 -08:00
|
|
|
|
builder.RegisterInstance<IWindowManagerConfig>(this).ExternallyOwned();
|
2014-11-30 07:57:08 -08:00
|
|
|
|
builder.RegisterType<WindowManager>().As<IWindowManager>().SingleInstance();
|
|
|
|
|
builder.RegisterType<EventAggregator>().As<IEventAggregator>().SingleInstance();
|
2016-11-25 09:04:50 -08:00
|
|
|
|
builder.RegisterType<MessageBoxViewModel>().As<IMessageBoxViewModel>().ExternallyOwned(); // Not singleton!
|
|
|
|
|
builder.RegisterAssemblyTypes(this.GetType().Assembly).ExternallyOwned();
|
2014-11-30 07:57:08 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Override to add your own types to the IoC container.
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected virtual void ConfigureIoC(ContainerBuilder builder) { }
|
|
|
|
|
|
2014-12-02 07:20:16 -08:00
|
|
|
|
public override object GetInstance(Type type)
|
2014-11-30 07:57:08 -08:00
|
|
|
|
{
|
2014-12-02 04:52:58 -08:00
|
|
|
|
return this.container.Resolve(type);
|
2014-11-30 07:57:08 -08:00
|
|
|
|
}
|
2015-01-14 04:23:43 -08:00
|
|
|
|
|
2016-07-09 04:21:02 -07:00
|
|
|
|
protected override void Launch()
|
2016-01-27 04:30:26 -08:00
|
|
|
|
{
|
|
|
|
|
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
|
|
|
|
ScreenExtensions.TryDispose(this._rootViewModel);
|
|
|
|
|
if (this.container != null)
|
|
|
|
|
this.container.Dispose();
|
2018-09-30 10:57:12 -07:00
|
|
|
|
|
|
|
|
|
base.Dispose();
|
2015-01-14 04:23:43 -08:00
|
|
|
|
}
|
2014-11-30 07:57:08 -08:00
|
|
|
|
}
|
|
|
|
|
}
|