using Autofac; using Stylet; using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Windows; namespace Bootstrappers { public class AutofacBootstrapper : 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(); } /// /// Carries out default configuration of the IoC container. Override if you don't want to do this /// protected virtual void DefaultConfigureIoC(ContainerBuilder builder) { var viewManager = new ViewManager(this.GetInstance, new List() { this.GetType().Assembly }); builder.RegisterInstance(viewManager); builder.RegisterInstance(this); builder.RegisterType().As().SingleInstance(); builder.RegisterType().As().SingleInstance(); builder.RegisterType().As(); // Not singleton! builder.RegisterAssemblyTypes(this.GetType().Assembly); } /// /// Override to add your own types to the IoC container. /// protected virtual void ConfigureIoC(ContainerBuilder builder) { } public override object GetInstance(Type type) { return this.container.Resolve(type); } public override void Dispose() { this.container.Dispose(); } } }