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:
- 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)
- {
-
-
-
-
-
-
- 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);
- }
- }