diff options
author | sr55 <[email protected]> | 2011-09-17 20:49:08 +0000 |
---|---|---|
committer | sr55 <[email protected]> | 2011-09-17 20:49:08 +0000 |
commit | d145ee9d9ed7be65b83124afbf0cc71b60adddc6 (patch) | |
tree | 8a7a47aa0bbeab6b18d6859bc457a215c5cbe61d /win/CS/HandBrakeWPF/Startup | |
parent | ac48779d6481154c16419d3c758af39ddcba4d86 (diff) |
HandBrakeWPF: Switch from Caliburn to Caliburn Micro and Add a CastleWindsor Bootstrapper.
git-svn-id: svn://svn.handbrake.fr/HandBrake/trunk@4230 b64f7644-9d1e-0410-96f1-a4d463321fa5
Diffstat (limited to 'win/CS/HandBrakeWPF/Startup')
-rw-r--r-- | win/CS/HandBrakeWPF/Startup/CastleBootstrapper.cs | 63 | ||||
-rw-r--r-- | win/CS/HandBrakeWPF/Startup/MefBootstrapper.cs | 53 |
2 files changed, 116 insertions, 0 deletions
diff --git a/win/CS/HandBrakeWPF/Startup/CastleBootstrapper.cs b/win/CS/HandBrakeWPF/Startup/CastleBootstrapper.cs new file mode 100644 index 000000000..22811d348 --- /dev/null +++ b/win/CS/HandBrakeWPF/Startup/CastleBootstrapper.cs @@ -0,0 +1,63 @@ +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 HandBrakeWPF.ViewModels;
+ using HandBrakeWPF.ViewModels.Interfaces;
+
+ /// <summary>
+ /// The Castle Bootstrapper
+ /// </summary>
+ public class CastleBootstrapper : Bootstrapper<IMainViewModel>
+ {
+ /// <summary>
+ /// The Windsor Container
+ /// </summary>
+ private IWindsorContainer windsorContainer;
+
+ /// <summary>
+ /// Configure Castle Windsor
+ /// </summary>
+ protected override void Configure()
+ {
+ this.windsorContainer = new WindsorContainer();
+ this.windsorContainer.Register(Component.For<IWindowManager>().ImplementedBy<WindowManager>());
+ this.windsorContainer.Register(Component.For<IEventAggregator>().ImplementedBy<EventAggregator>());
+
+ // Shell
+ this.windsorContainer.Register(Component.For<IMainViewModel>().ImplementedBy<MainViewModel>().LifeStyle.Is(LifestyleType.Singleton));
+ }
+
+ protected override IEnumerable<Assembly> SelectAssemblies()
+ {
+ return AppDomain.CurrentDomain.GetAssemblies();
+ }
+
+ protected override object GetInstance(Type service, string key)
+ {
+ return string.IsNullOrWhiteSpace(key) ? this.windsorContainer.Resolve(service) : this.windsorContainer.Resolve(key, new { });
+ }
+
+ protected override IEnumerable<object> GetAllInstances(Type service)
+ {
+ return this.windsorContainer.ResolveAll(service).Cast<object>();
+ }
+
+ 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));
+ }
+ }
+}
diff --git a/win/CS/HandBrakeWPF/Startup/MefBootstrapper.cs b/win/CS/HandBrakeWPF/Startup/MefBootstrapper.cs new file mode 100644 index 000000000..c03d4baf9 --- /dev/null +++ b/win/CS/HandBrakeWPF/Startup/MefBootstrapper.cs @@ -0,0 +1,53 @@ +namespace HandBrakeWPF.Startup
+{
+ using System;
+ using System.Collections.Generic;
+ using System.ComponentModel.Composition;
+ using System.ComponentModel.Composition.Hosting;
+ using System.Linq;
+
+ using Caliburn.Micro;
+
+ using HandBrakeWPF.ViewModels.Interfaces;
+
+ public class MefBootstrapper : Bootstrapper<IMainViewModel>
+ {
+ private CompositionContainer container;
+
+ protected override void Configure()
+ {
+ container = new CompositionContainer(
+ new AggregateCatalog(AssemblySource.Instance.Select(x => new AssemblyCatalog(x)))
+ );
+
+ var batch = new CompositionBatch();
+
+ batch.AddExportedValue<IWindowManager>(new WindowManager());
+ batch.AddExportedValue<IEventAggregator>(new EventAggregator());
+ batch.AddExportedValue(container);
+
+ container.Compose(batch);
+ }
+
+ protected override object GetInstance(Type serviceType, string key)
+ {
+ string contract = string.IsNullOrEmpty(key) ? AttributedModelServices.GetContractName(serviceType) : key;
+ var exports = container.GetExportedValues<object>(contract);
+
+ if (exports.Count() > 0)
+ return exports.First();
+
+ throw new Exception(string.Format("Could not locate any instances of contract {0}.", contract));
+ }
+
+ protected override IEnumerable<object> GetAllInstances(Type serviceType)
+ {
+ return container.GetExportedValues<object>(AttributedModelServices.GetContractName(serviceType));
+ }
+
+ protected override void BuildUp(object instance)
+ {
+ container.SatisfyImportsOnce(instance);
+ }
+ }
+}
\ No newline at end of file |