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 writing of Boilerplate code. These libraries are not just helpers but they’re much more efficient than legacy code.



Figure 1: LegacyCode

Why I’m writing this blog? Well, here’s the reason. I have been a great fan of SimpleInjector because of it’s performance and Caliburn.Micro for being lightweight and faster. I have been using them since the libraries are in their initial version and doing great job till date. Today I spent 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 training you gonna miss a lot of stuff.

Let’s look at the changes in simple boootstrapper for WPF application. Since caliburn micro gives a nice way to setup 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 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.   
  14.         ContainerInstance.Register < MainWindowViewModel, MainWindowViewModel > ();  
  15.   
  16.         ContainerInstance.Verify();  
  17.     }  
  18.     protected override void OnStartup(object sender, System.Windows.StartupEventArgs e) {  
  19.         DisplayRootViewFor < MainWindowViewModel > ();  
  20.     }  
  21.     protected override IEnumerable < object > GetAllInstances(Type service)  
  22.     {  
  23.         // This line throwing is exception when running the application  
  24.         // Error:   
  25.         // ---> An exception of type 'SimpleInjector.ActivationException' occurred in SimpleInjector.dll  
  26.         // ---> Additional information: No registration for type IEnumerable<MainWindowView> could be found.   
  27.         // ---> No registration for type IEnumerable<MainWindowView> could be found.   
  28.         // return ContainerInstance.GetAllInstances(service);  
  29.         IServiceProvider provider = ContainerInstance;  
  30.         Type collectionType = typeof(IEnumerable < > ).MakeGenericType(service);  
  31.         var services = (IEnumerable < object > ) provider.GetService(collectionType);  
  32.         return services ? ? Enumerable.Empty < object > ();  
  33.     }  
  34.     protected override object GetInstance(System.Type service, string key)   
  35.     {  
  36.         return ContainerInstance.GetInstance(service);  
  37.     }  
  38.     protected override IEnumerable < Assembly > SelectAssemblies()   
  39.     {  
  40.         return new[]   
  41.         {  
  42.             Assembly.GetExecutingAssembly()  
  43.         };  
  44.     }  
  45.     protected override void BuildUp(object instance)  
  46.     {  
  47.         var registration = ContainerInstance.GetRegistration(instance.GetType(), true);  
  48.         registration.Registration.InitializeInstance(instance);  
  49.     }  
  50. }
Ebook Download
View all
Learn
View all