using Microsoft.Practices.Unity; using Stylet; using System; using System.Windows; namespace Bootstrappers { public class UnityBootstrapper : BootstrapperBase where TRootViewModel : class { private IUnityContainer container; private object _rootViewModel; protected override object RootViewModel { get { return this._rootViewModel ?? (this._rootViewModel = this.GetInstance(typeof(TRootViewModel))); } } protected override void ConfigureBootstrapper() { this.Configure(); this.container = new UnityContainer(); this.DefaultConfigureIoC(this.container); this.ConfigureIoC(this.container); } /// /// Override to configure anything that needs configuring /// protected virtual void Configure() { } /// /// Carries out default configuration of the IoC container. Override if you don't want to do this /// protected virtual void DefaultConfigureIoC(IUnityContainer container) { // For some reason using ContainerControlledLifetimeManager results in a transient registration.... // This is a workaround var viewManager = new ViewManager(this); container.RegisterInstance(viewManager); container.RegisterInstance(new WindowManager(viewManager, () => container.Resolve())); container.RegisterInstance(new EventAggregator()); container.RegisterType(new PerResolveLifetimeManager()); container.RegisterTypes(AllClasses.FromAssemblies(this.Assemblies), WithMappings.None, WithName.Default, WithLifetime.PerResolve); } /// /// Override to add your own types to the IoC container. /// protected virtual void ConfigureIoC(IUnityContainer container) { } public override object GetInstance(Type type) { return this.container.Resolve(type); } public override void Dispose() { this.container.Dispose(); } } }