Guest User

Untitled

a guest
Feb 18th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. public class MEFBootstrapper:BootstrapperBase
  2. {
  3. #region Private Variables
  4. private CompositionContainer _Container;
  5. #endregion
  6.  
  7. #region Constructor
  8. public MEFBootstrapper()
  9. {
  10. Initialize();
  11. }
  12. #endregion
  13.  
  14. #region Overrides
  15.  
  16. protected override void Configure()
  17. {
  18. _Container = new CompositionContainer(
  19. new AggregateCatalog(AssemblySource.Instance.Select(x => new AssemblyCatalog(x)).OfType<ComposablePartCatalog>())
  20. );
  21. var batch = new CompositionBatch();
  22. batch.AddExportedValue<IWindowManager>(new WindowManager());
  23. batch.AddExportedValue<IEventAggregator>(new EventAggregator());
  24. batch.AddExportedValue(_Container);
  25.  
  26. _Container.Compose(batch);
  27. }
  28.  
  29. protected override object GetInstance(Type service, string key)
  30. {
  31. string contract = string.IsNullOrEmpty(key) ? AttributedModelServices.GetContractName(service) : key;
  32. var exports = _Container.GetExportedValues<object>(contract);
  33. if (exports.Any())
  34. return exports.First();
  35. else
  36. throw new Exception("Could not find the key");
  37. }
  38.  
  39. protected override void BuildUp(object instance)
  40. {
  41. _Container.SatisfyImportsOnce(instance);
  42. }
  43.  
  44. protected override IEnumerable<object> GetAllInstances(Type service)
  45. {
  46. return _Container.GetExportedValues<object>(AttributedModelServices.GetContractName(service));
  47. }
  48.  
  49. protected override void OnStartup(object sender, StartupEventArgs e)
  50. {
  51. this.DisplayRootViewFor<IShell>();
  52. }
  53. #endregion
  54. }
Add Comment
Please, Sign In to add comment