{"id":1476,"date":"2014-02-20T22:43:15","date_gmt":"2014-02-20T22:43:15","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=1476"},"modified":"2014-02-20T22:43:15","modified_gmt":"2014-02-20T22:43:15","slug":"binding-ninject-to-an-instance-based-upon-the-dependency-type","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/binding-ninject-to-an-instance-based-upon-the-dependency-type\/","title":{"rendered":"Binding Ninject to an instance based upon the dependency type"},"content":{"rendered":"<p>I want to bind the Common.Logging.ILog to an implementation which can take a type argument as it&#8217;s parameter, i.e.  We can use the Common.Logging.LogManager.GetManager method by passing a type into it, as per the following code<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nLogManager.GetLogger(typeof(MyType));\r\n<\/pre>\n<p>So how can we simply declare a dependency on our type to ILog and have it automatically get the logger with the type parameter. <\/p>\n<p>The code speaks for itself, so here it is<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nIKernel kernel = new StandardKernel();\r\n\r\nkernel.Bind&lt;ILog&gt;().ToMethod(ctx =&gt;\r\n{\r\n   Type type = ctx.Request.ParentContext.Request.Service;\r\n   return LogManager.GetLogger(type);\r\n});\r\n<\/pre>\n<p>In the above code we bind to a method, thus we can dynamically handle the binding. From the IContext <em>ctx<\/em> we can find which type requested the binding and then use this in the call to GetLogger.<\/p>\n<p>So with this the dependency object simply includes the following<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic class MyClass\r\n{\r\n   \/\/ other methods, properties etc.\r\n\r\n   &#x5B;Inject]\r\n   public ILog Logger { get; set; }\r\n}\r\n<\/pre>\n<p>and everything &#8220;magically&#8221; binds together to get the logger with the type set to typeof(MyClass).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I want to bind the Common.Logging.ILog to an implementation which can take a type argument as it&#8217;s parameter, i.e. We can use the Common.Logging.LogManager.GetManager method by passing a type into it, as per the following code LogManager.GetLogger(typeof(MyType)); So how can we simply declare a dependency on our type to ILog and have it automatically get [&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-1476","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\/1476","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=1476"}],"version-history":[{"count":2,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/1476\/revisions"}],"predecessor-version":[{"id":1478,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/1476\/revisions\/1478"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=1476"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=1476"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=1476"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}