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;
|
|
|
|
|
protected override object RootViewModel
|
|
|
|
|
{
|
|
|
|
|
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)
|
|
|
|
|
{
|
2015-09-25 07:19:23 -07:00
|
|
|
|
var viewManager = new ViewManager(this.GetInstance, new List<Assembly>() { this.GetType().Assembly });
|
|
|
|
|
builder.RegisterInstance<IViewManager>(viewManager);
|
|
|
|
|
|
2015-03-30 07:02:07 -07:00
|
|
|
|
builder.RegisterInstance<IWindowManagerConfig>(this);
|
2014-11-30 07:57:08 -08:00
|
|
|
|
builder.RegisterType<WindowManager>().As<IWindowManager>().SingleInstance();
|
|
|
|
|
builder.RegisterType<EventAggregator>().As<IEventAggregator>().SingleInstance();
|
|
|
|
|
builder.RegisterType<MessageBoxViewModel>().As<IMessageBoxViewModel>(); // Not singleton!
|
2015-09-24 09:48:40 -07:00
|
|
|
|
builder.RegisterAssemblyTypes(this.GetType().Assembly);
|
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
|
|
|
|
|
2015-02-09 04:39:21 -08:00
|
|
|
|
public override void Dispose()
|
2015-01-14 04:23:43 -08:00
|
|
|
|
{
|
|
|
|
|
this.container.Dispose();
|
|
|
|
|
}
|
2014-11-30 07:57:08 -08:00
|
|
|
|
}
|
|
|
|
|
}
|