Migrating To Simple Injector 3.0 With Caliburn Micro Bootstrap Changes

Migrating To Simple Injector 3.0 With Caliburn Micro Bootstrap Changes 

 
A note before I write this blog, I want to thank all the contributors to the open-source libraries for changing the life of developers by preventing the writing of Boilerplate code. These libraries are not just helpers but they’re much more efficient than legacy code.
 
a
Figure 1: LegacyCode
 
Why I’m writing this blog? Well, here’s the reason. I have been a great fan of SimpleInjector because of its performance and Caliburn.Micro for being lightweight and faster. I have been using them since the libraries are in their initial version and doing a great job to date. Today I spent a few hours creating a small application WPF and I was surprised everything changed. It was a whole new world, of course, if you don’t do practice with the training you gonna miss a lot of stuff.
 
Let’s look at the changes in simple boootstrapper for the WPF application. Caliburn micro gives a nice way to set up a  dependency injection by providing virtual methods in base class BootStrapperBase. Since the changes in SimpleInjector v3.0 for returning the list of registered instances that now throws an exception if none found. Here’s the complete code snippet for working with bootstrap, if it would help someone out there struggling the same problem:
  1. internal class AppBootstrapper: BootstrapperBase     
  2. {    
  3.     public static readonly Container ContainerInstance = new Container();    
  4.     public AppBootstrapper()     
  5.     {    
  6.         LogManager.GetLog = type = > new DebugLogger(type);    
  7.         Initialize();    
  8.     }    
  9.     protected override void Configure()    
  10.     {    
  11.         ContainerInstance.Register < IWindowManager, WindowManager > ();    
  12.         ContainerInstance.RegisterSingleton < IEventAggregator, EventAggregator > ();    
  13.         ContainerInstance.Register < MainWindowViewModel, MainWindowViewModel > ();    
  14.         ContainerInstance.Verify();    
  15.     }    
  16.     protected override void OnStartup(object sender, System.Windows.StartupEventArgs e) {    
  17.         DisplayRootViewFor < MainWindowViewModel > ();    
  18.     }    
  19.     protected override IEnumerable < object > GetAllInstances(Type service)    
  20.     {    
  21.         // This line throwing is exception when running the application    
  22.         // Error:     
  23.         // ---> An exception of type 'SimpleInjector.ActivationException' occurred in SimpleInjector.dll    
  24.         // ---> Additional information: No registration for type IEnumerable<MainWindowView> could be found.     
  25.         // ---> No registration for type IEnumerable  
  26. <MainWindowView> could be found.     
  27.         // return ContainerInstance.GetAllInstances(service);    
  28.         IServiceProvider provider = ContainerInstance;    
  29.         Type collectionType = typeof(IEnumerable < > ).MakeGenericType(service);    
  30.         var services = (IEnumerable < object > ) provider.GetService(collectionType);    
  31.         return services ? ? Enumerable.Empty < object > ();    
  32.     }    
  33.     protected override object GetInstance(System.Type service, string key)     
  34.     {    
  35.         return ContainerInstance.GetInstance(service);    
  36.     }    
  37.     protected override IEnumerable < Assembly > SelectAssemblies()     
  38.     {    
  39.         return new[]     
  40.         {    
  41.             Assembly.GetExecutingAssembly()    
  42.         };    
  43.     }    
  44.     protected override void BuildUp(object instance)    
  45.     {    
  46.         var registration = ContainerInstance.GetRegistration(instance.GetType(), true);    
  47.         registration.Registration.InitializeInstance(instance);    
  48.     }    
  49. }