// -------------------------------------------------------------------------------------------------------------------- // // This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License. // // // The Castle Bootstrapper // // -------------------------------------------------------------------------------------------------------------------- namespace HandBrakeWPF.Startup { using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using Caliburn.Micro; using Castle.Core; using Castle.MicroKernel.Registration; using Castle.Windsor; using HandBrake.ApplicationServices; using HandBrakeWPF.ViewModels; using HandBrakeWPF.ViewModels.Interfaces; /// /// The Castle Bootstrapper /// public class CastleBootstrapper : Bootstrapper { /// /// The Windsor Container /// private IWindsorContainer windsorContainer; /// /// Configure Castle Windsor /// protected override void Configure() { this.windsorContainer = new WindsorContainer(); this.windsorContainer.Register(Component.For().ImplementedBy()); this.windsorContainer.Register(Component.For().ImplementedBy()); // Initialise the ApplicationServices IWindsorInstaller this.windsorContainer.Register(Component.For().ImplementedBy()); this.windsorContainer.Install(windsorContainer.ResolveAll()); // Shell this.windsorContainer.Register(Component.For().ImplementedBy().LifeStyle.Is(LifestyleType.Singleton)); } /// /// Select Assemblies /// /// /// A List of Assembly objects /// protected override IEnumerable SelectAssemblies() { return AppDomain.CurrentDomain.GetAssemblies(); } /// /// Get an Instance of a service /// /// /// The service. /// /// /// The key. /// /// /// The Service Requested /// protected override object GetInstance(Type service, string key) { return string.IsNullOrWhiteSpace(key) ? this.windsorContainer.Resolve(service) : this.windsorContainer.Resolve(key, new { }); } /// /// Get all instances of a service /// /// /// The service. /// /// /// A collection of instances of the requested service type. /// protected override IEnumerable GetAllInstances(Type service) { return this.windsorContainer.ResolveAll(service).Cast(); } /// /// Build Up /// /// /// The instance. /// protected override void BuildUp(object instance) { instance.GetType().GetProperties() .Where(property => property.CanWrite && property.PropertyType.IsPublic) .Where(property => this.windsorContainer.Kernel.HasComponent(property.PropertyType)) .ForEach(property => property.SetValue(instance, this.windsorContainer.Resolve(property.PropertyType), null)); } } }