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.
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:
- internal class AppBootstrapper: BootstrapperBase
- {
- public static readonly Container ContainerInstance = new Container();
- public AppBootstrapper()
- {
- LogManager.GetLog = type = > new DebugLogger(type);
- Initialize();
- }
- protected override void Configure()
- {
- ContainerInstance.Register < IWindowManager, WindowManager > ();
- ContainerInstance.RegisterSingleton < IEventAggregator, EventAggregator > ();
- ContainerInstance.Register < MainWindowViewModel, MainWindowViewModel > ();
- ContainerInstance.Verify();
- }
- protected override void OnStartup(object sender, System.Windows.StartupEventArgs e) {
- DisplayRootViewFor < MainWindowViewModel > ();
- }
- protected override IEnumerable < object > GetAllInstances(Type service)
- {
- // This line throwing is exception when running the application
- // Error:
- // ---> An exception of type 'SimpleInjector.ActivationException' occurred in SimpleInjector.dll
- // ---> Additional information: No registration for type IEnumerable<MainWindowView> could be found.
- // ---> No registration for type IEnumerable
- <MainWindowView> could be found.
- // return ContainerInstance.GetAllInstances(service);
- IServiceProvider provider = ContainerInstance;
- Type collectionType = typeof(IEnumerable < > ).MakeGenericType(service);
- var services = (IEnumerable < object > ) provider.GetService(collectionType);
- return services ? ? Enumerable.Empty < object > ();
- }
- protected override object GetInstance(System.Type service, string key)
- {
- return ContainerInstance.GetInstance(service);
- }
- protected override IEnumerable < Assembly > SelectAssemblies()
- {
- return new[]
- {
- Assembly.GetExecutingAssembly()
- };
- }
- protected override void BuildUp(object instance)
- {
- var registration = ContainerInstance.GetRegistration(instance.GetType(), true);
- registration.Registration.InitializeInstance(instance);
- }
- }