{"id":852,"date":"2013-12-06T11:01:13","date_gmt":"2013-12-06T11:01:13","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=852"},"modified":"2013-12-06T11:28:47","modified_gmt":"2013-12-06T11:28:47","slug":"caliburn-micro-and-inversion-of-control-using-ninject","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/caliburn-micro-and-inversion-of-control-using-ninject\/","title":{"rendered":"Caliburn Micro and inversion of control using Ninject"},"content":{"rendered":"<p>Caliburn Micro comes with it&#8217;s own built in mechanism for creating objects as and when required. However it&#8217;s bootstrapper comes with methods which allow us to override the default behaviour. The methods such as GetInstance, GetAllInstances and BuildUp are used to resolve dependencies in a user supplied IoC container.<\/p>\n<p>I&#8217;m the built in mechanism is more than adequate for most peoples usage, but I tend to rather like Ninject. So here are the steps (which ofcourse can be used with your own preferred IoC framework).<\/p>\n<p>Create a bootstrapper as per the following (where ShellViewModel is replaced with your view model name)<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic class AppBootstrapper : Bootstrapper&lt;ShellViewModel&gt;\r\n{\r\n   protected override void Configure()\r\n   {\r\n   }\r\n\r\n   protected override void OnExit(object sender, EventArgs e)\r\n   {\r\n   }\r\n\r\n   protected override object GetInstance(Type service, string key)\r\n   {\r\n   }\r\n\r\n   protected override IEnumerable&lt;object&gt; GetAllInstances(Type service)\r\n   {\r\n   }\r\n\r\n   protected override void BuildUp(object instance)\r\n   {\r\n   }\r\n}\r\n<\/pre>\n<p>These are the methods we need to override and implement the code for, to allow Caliburn Micro to use our preferred Ioc framework.<\/p>\n<p><strong>Configure<\/strong><\/p>\n<p>The configure method is used to configure Caliburn Micro to use our IoC framework, so basically this is where we instantiate the StandardKernel in Ninject. Firstly add a class level variable as follows<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nprivate IKernel kernel;\r\n<\/pre>\n<p>Next override the Configure method to both create the kernel and set-up default bindings<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nprotected override void Configure()\r\n{\r\n   kernel = new StandardKernel();\r\n\r\n   kernel.Bind&lt;IWindowManager&gt;().To&lt;WindowManager&gt;().InSingletonScope();\r\n   kernel.Bind&lt;IEventAggregator&gt;().To&lt;EventAggregator&gt;().InSingletonScope();\r\n}\r\n<\/pre>\n<p>We&#8217;re going to want to have access to the WindowManager and EventAggregator within our code, so we&#8217;ll set up the bindings for them. If we&#8217;re passing a class to the generic argument of the Bootstrapper this this is enough code for the Configure method, but if, as I often prefer, we have something like<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic class AppBootstrapper : Bootstrapper&lt;IShellViewModel&gt;\r\n<\/pre>\n<p>i.e. an interface passed as the generic argument, then we need to also set up the bindings within Ninject to resolve this interface. Hence adding the line<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nkernel.Bind&lt;IShellViewModel&gt;().To&lt;ShellViewModel&gt;().InSingletonScope();\r\n<\/pre>\n<p>to the end of the Configure method.<\/p>\n<p><strong>OnExit<\/strong><\/p>\n<p>Now we&#8217;ve created the instance of the kernel, the OnExit method allows us to place cleanup code such as<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nprotected override void OnExit(object sender, EventArgs e)\r\n{\r\n   kernel.Dispose();\r\n   base.OnExit(sender, e);\r\n}\r\n<\/pre>\n<p>This is (as you can see from the arguments, a event handler that Caliburn Micro hooks up to the Application.Exit event.<\/p>\n<p><strong>GetInstance<\/strong><\/p>\n<p>This must be overridden when providing our own IoC container. The method is used get the service for a given service, so we can simply call the following<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nprotected override object GetInstance(Type service, string key)\r\n{\r\n   if (service == null)\r\n      throw new ArgumentNullException(&quot;service&quot;);\r\n\r\n   return kernel.Get(service);\r\n}\r\n<\/pre>\n<p><strong>GetAllInstances<\/strong><\/p>\n<p>This method must be overridden when supplying our own IoC container and is used to get all instances of a service. We can override it thus<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nprotected override IEnumerable&lt;object&gt; GetAllInstances(Type service)\r\n{\r\n   return kernel.GetAll(service);\r\n}\r\n<\/pre>\n<p><strong>BuildUp<\/strong><\/p>\n<p>Finally, and again required when supplying our own IoC container, we need to override the BuildUp method. This is used inject instances into the IoC container and can be written as<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nprotected override void BuildUp(object instance)\r\n{\r\n   kernel.Inject(instance);\r\n}\r\n<\/pre>\n<p><strong>Full Code<\/strong><\/p>\n<p>The full code for this is as follows<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic class AppBootstrapper : Bootstrapper&lt;ShellViewModel&gt;\r\n{\r\n   private IKernel kernel;\r\n\r\n   protected override void Configure()\r\n   {\r\n      kernel = new StandardKernel();\r\n\r\n      kernel.Bind&lt;IWindowManager&gt;().To&lt;MetroWindowManager&gt;().InSingletonScope();\r\n      kernel.Bind&lt;IEventAggregator&gt;().To&lt;EventAggregator&gt;().InSingletonScope();\r\n\r\n      kernel.Bind&lt;IShellViewModel&gt;().To&lt;ShellViewModel&gt;().InSingletonScope();\r\n   }\r\n\r\n   protected override void OnExit(object sender, EventArgs e)\r\n   {\r\n      kernel.Dispose();\r\n      base.OnExit(sender, e);\r\n   }\r\n\r\n   protected override object GetInstance(Type service, string key)\r\n   {\r\n      if (service == null)\r\n         throw new ArgumentNullException(&quot;service&quot;);\r\n\t\t\t\r\n      return kernel.Get(service);\r\n   }\r\n\r\n   protected override IEnumerable&lt;object&gt; GetAllInstances(Type service)\r\n   {\r\n      return kernel.GetAll(service);\r\n   }\r\n\r\n   protected override void BuildUp(object instance)\r\n   {\r\n      kernel.Inject(instance);\r\n   }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Caliburn Micro comes with it&#8217;s own built in mechanism for creating objects as and when required. However it&#8217;s bootstrapper comes with methods which allow us to override the default behaviour. The methods such as GetInstance, GetAllInstances and BuildUp are used to resolve dependencies in a user supplied IoC container. I&#8217;m the built in mechanism is [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[61,17,18,13],"tags":[],"class_list":["post-852","post","type-post","status-publish","format-standard","hentry","category-caliburn-micro","category-ioc","category-ninject","category-wpf"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/852","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=852"}],"version-history":[{"count":7,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/852\/revisions"}],"predecessor-version":[{"id":873,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/852\/revisions\/873"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=852"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=852"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=852"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}