Stylet/Bootstrappers/AutofacBootstrapper.cs

57 lines
1.9 KiB
C#
Raw Normal View History

using Autofac;
using Stylet;
using System;
using System.Linq;
using System.Windows;
namespace Bootstrappers
{
public class AutofacBootstrapper<TRootViewModel> : BootstrapperBase where TRootViewModel : class
{
private IContainer container;
private object _rootViewModel;
protected override object RootViewModel
{
get { return this._rootViewModel ?? (this._rootViewModel = this.GetInstance(typeof(TRootViewModel))); }
}
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)
{
builder.RegisterInstance<IViewManagerConfig>(this);
builder.RegisterType<ViewManager>().As<IViewManager>().SingleInstance();
builder.RegisterInstance<IWindowManagerConfig>(this);
builder.RegisterType<WindowManager>().As<IWindowManager>().SingleInstance();
builder.RegisterType<EventAggregator>().As<IEventAggregator>().SingleInstance();
builder.RegisterType<MessageBoxViewModel>().As<IMessageBoxViewModel>(); // Not singleton!
builder.RegisterAssemblyTypes(this.Assemblies.ToArray());
}
/// <summary>
/// Override to add your own types to the IoC container.
/// </summary>
protected virtual void ConfigureIoC(ContainerBuilder builder) { }
public override object GetInstance(Type type)
{
return this.container.Resolve(type);
}
2015-02-09 04:39:21 -08:00
public override void Dispose()
{
this.container.Dispose();
}
}
}