2014-11-30 07:57:08 -08:00
|
|
|
|
using Autofac;
|
|
|
|
|
using Stylet;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace Bootstrappers
|
|
|
|
|
{
|
|
|
|
|
public class AutofacBootstrapper<TRootViewModel> : BootstrapperBase<TRootViewModel> where TRootViewModel : class
|
|
|
|
|
{
|
|
|
|
|
private IContainer container;
|
|
|
|
|
|
|
|
|
|
protected override void ConfigureBootstrapper()
|
|
|
|
|
{
|
|
|
|
|
this.Configure();
|
|
|
|
|
|
|
|
|
|
var builder = new ContainerBuilder();
|
|
|
|
|
this.DefaultConfigureIoC(builder);
|
|
|
|
|
this.ConfigureIoC(builder);
|
|
|
|
|
this.container = builder.Build();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Override to configure your IoC container, and anything else
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected virtual void Configure() { }
|
|
|
|
|
|
|
|
|
|
/// <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(new ViewManager(this.Assemblies, type => this.container.Resolve(type))).As<IViewManager>().SingleInstance();
|
|
|
|
|
builder.RegisterType<WindowManager>().As<IWindowManager>().SingleInstance();
|
|
|
|
|
builder.RegisterType<EventAggregator>().As<IEventAggregator>().SingleInstance();
|
|
|
|
|
builder.RegisterType<MessageBoxViewModel>().As<IMessageBoxViewModel>(); // Not singleton!
|
2014-11-30 12:22:03 -08:00
|
|
|
|
builder.RegisterAssemblyTypes(this.Assemblies.ToArray());
|
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) { }
|
|
|
|
|
|
|
|
|
|
protected override T GetInstance<T>()
|
|
|
|
|
{
|
|
|
|
|
return this.container.Resolve<T>();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|