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 TRootViewModel _rootViewModel; protected virtual TRootViewModel RootViewModel { get { return this._rootViewModel ?? (this._rootViewModel = (TRootViewModel)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 viewManagerConfig = new ViewManagerConfig() { ViewFactory = this.GetInstance, ViewAssemblies = new List() { this.GetType().Assembly } }; builder.RegisterInstance(new ViewManager(viewManagerConfig)); builder.RegisterType(); builder.RegisterInstance(this).ExternallyOwned(); builder.RegisterType().As().SingleInstance(); builder.RegisterType().As().SingleInstance(); builder.RegisterType().As().ExternallyOwned(); // Not singleton! // See https://github.com/canton7/Stylet/discussions/211 builder.RegisterAssemblyTypes(this.GetType().Assembly).Where(x => !x.Name.Contains("ProcessedByFody")).ExternallyOwned(); } /// /// 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); } protected override void Launch() { base.DisplayRootView(this.RootViewModel); } public override void Dispose() { ScreenExtensions.TryDispose(this._rootViewModel); if (this.container != null) this.container.Dispose(); base.Dispose(); } } }