using Castle.Facilities.TypedFactory; using Castle.MicroKernel.Registration; using Castle.Windsor; using Stylet; using System; using System.Collections.Generic; using System.Reflection; using System.Windows; namespace Bootstrappers { public class CastleWindsorBootstrapper : BootstrapperBase where TRootViewModel : class { private IWindsorContainer container; private object _rootViewModel; protected virtual object RootViewModel { get { return this._rootViewModel ?? (this._rootViewModel = this.GetInstance(typeof(TRootViewModel))); } } protected override void ConfigureBootstrapper() { this.container = new WindsorContainer(); this.DefaultConfigureIoC(this.container); this.ConfigureIoC(this.container); } /// /// Carries out default configuration of the IoC container. Override if you don't want to do this /// protected virtual void DefaultConfigureIoC(IWindsorContainer container) { var viewManager = new ViewManager(this.GetInstance, new List() { this.GetType().Assembly }); container.Register( Component.For().Instance(viewManager), Component.For().Instance(this), Component.For().ImplementedBy().LifestyleTransient(), // For some reason we need to register the delegate separately? Component.For>().Instance(() => new MessageBoxViewModel()), Component.For().ImplementedBy().LifestyleSingleton(), Component.For().ImplementedBy().LifestyleSingleton() ); container.Register(Classes.FromAssembly(this.GetType().Assembly).Pick().LifestyleTransient()); } /// /// Override to add your own types to the IoC container. /// protected virtual void ConfigureIoC(IWindsorContainer container) { } public override object GetInstance(Type type) { return this.container.Resolve(type); } public override void Launch() { base.DisplayRootView(this.RootViewModel); } public override void Dispose() { base.Dispose(); ScreenExtensions.TryDispose(this._rootViewModel); if (this.container != null) this.container.Dispose(); } } }