{"id":753,"date":"2013-10-23T10:41:19","date_gmt":"2013-10-23T10:41:19","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=753"},"modified":"2013-10-23T10:44:50","modified_gmt":"2013-10-23T10:44:50","slug":"ninject-childkernel","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/ninject-childkernel\/","title":{"rendered":"NInject ChildKernel"},"content":{"rendered":"<p>I&#8217;ve been working on an application which is basically a glorified scheduler. Within it I have the concept of projects which are made up triggers, tasks etc. So we have a parent child relationship going on between the project and the child elements, the triggers etc. <\/p>\n<p>One problem I came across was that the application holds a StandardKernel (as one might expect) and injects dependencies etc. into the projects, triggers and tasks but I wanted to use the same mechanism to allow me to inject the current instance of a project into the children, i.e. the triggers and tasks could (if they wanted) get access to the parent project instance. <\/p>\n<p>Obviously if I add the project to the application wide kernel, all triggers and tasks would get a project object, but not necessarily the project which acts as their parent (actually it&#8217;d probably fail to allow me to resolve to multiple instances of a project anyway). Of course I could pass the instance of the project to each trigger or task at construction or explicitly calling the relevant method\/property, but I wanted to reuse the NInject mechanisms so as to keep a standard way of injecting objects into this application.<\/p>\n<p>Enter the <strong>ChildKernel<\/strong>. A Ninject extension class available via <a href=\"https:\/\/github.com\/ninject\/ninject.extensions.childkernel\" title=\"ninject.extensions.childkernel\" target=\"_blank\">GitHub<\/a>.<\/p>\n<p>Using the ChildKernel we can create a new kernel, which (if you like) inherits the bindings from a parent kernel and then we add further bindings specific to it&#8217;s use, as per<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nChildKernel child = new ChildKernel(kernel);\r\nchild.Bind&lt;IProject&gt;().ToMethod(_ =&gt; currentProject);\r\n<\/pre>\n<p><em>kernel<\/em> would be our StandardKernel acting as the parent kernel.<\/p>\n<p>Now we simply <em>Get<\/em> or <em>Inject<\/em> using this child kernel and if a dependency is not resolved using the child, it is passed to the parent kernel to resolve.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been working on an application which is basically a glorified scheduler. Within it I have the concept of projects which are made up triggers, tasks etc. So we have a parent child relationship going on between the project and the child elements, the triggers etc. One problem I came across was that the application [&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":[18],"tags":[],"class_list":["post-753","post","type-post","status-publish","format-standard","hentry","category-ninject"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/753","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=753"}],"version-history":[{"count":3,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/753\/revisions"}],"predecessor-version":[{"id":755,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/753\/revisions\/755"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=753"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=753"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=753"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}