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 { 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(new WindowManager()); batch.AddExportedValue(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(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 GetAllInstances(Type serviceType) { return container.GetExportedValues(AttributedModelServices.GetContractName(serviceType)); } protected override void BuildUp(object instance) { container.SatisfyImportsOnce(instance); } } }