{"id":91,"date":"2013-03-27T21:28:47","date_gmt":"2013-03-27T21:28:47","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=91"},"modified":"2013-03-27T22:01:40","modified_gmt":"2013-03-27T22:01:40","slug":"investigating-ninject","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/investigating-ninject\/","title":{"rendered":"Investigating Ninject"},"content":{"rendered":"<p>Whilst I&#8217;ve used IoC frameworks such as Unity, MEF, Spring.net etc. in the past, I&#8217;ve not gotten around to playing with <a title=\"Ninject\" href=\"http:\/\/www.ninject.org\/\" target=\"_blank\">Ninject<\/a>. So time to have a play.<\/p>\n<p>First off I created a simple interface, IService and corresponding implementing LocalService<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic interface IService\r\n{\r\n   void Run();\r\n}\r\n\r\npublic class LocalService : IService\r\n{\r\n   public void Run()\r\n   {\r\n      Console.WriteLine(&quot;Local Service&quot;);\r\n   }\r\n}\r\n<\/pre>\n<p>Now I created a simple client object<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic interface IClient\r\n{\r\n   IService Service { get; }\r\n\r\n   void Run();\r\n}\r\n\r\npublic class Client : IClient\r\n{\r\n   public Client(IService service)\r\n   {\r\n      Service = service;\r\n   }\r\n\r\n   public IService Service { get; private set; }\r\n\r\n   public void Run()\r\n   {\r\n      Service.Run();\r\n   }\r\n}\r\n<\/pre>\n<p>So as you can see the idea is simple. A client requires an IService passed by the IoC into the constructor. At some point we&#8217;ll call the client&#8217;s Run method and expect a LocalService to run. Obviously error handling etc. omitted for brevity.<\/p>\n<p>Now let&#8217;s create the configuration\/bindings to allow Ninject to inject the required service into the client.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nvar kernel = new StandardKernel();\r\n\r\nkernel.Bind&lt;IService&gt;().To&lt;LocalService&gt;();\r\nkernel.Bind&lt;IClient&gt;().To&lt;Client&gt;();\r\n\r\nvar client = kernel.Get&lt;IClient&gt;();\r\nclient.Run();\r\n<\/pre>\n<p>What about if we have multiple IService implementations, see the changes below which include the client accepting an array of IService and the kernel code includes an additional binding.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic class RemoteService : IService\r\n{\r\n   public void Run()\r\n   {\r\n     Console.WriteLine(&quot;RemoteService&quot;);\r\n   }\r\n}\r\n\r\npublic interface IClient\r\n{\r\n   IService&#x5B;] Service { get; }\r\n\r\n   void Run();\r\n}\r\n\r\npublic class Client : IClient\r\n{\r\n   public Client(IService&#x5B;] service)\r\n   {\r\n      Service = service;\r\n   }\r\n\r\n   public IService&#x5B;] Service { get; private set; }\r\n\r\n   public void Run()\r\n   {\r\n      foreach(IService service in Service)\r\n         service.Run();\r\n   }\r\n}\r\n\r\nvar kernel = new StandardKernel();\r\n\r\nkernel.Bind&lt;IService&gt;().To&lt;LocalService&gt;();\r\nkernel.Bind&lt;IService&gt;().To&lt;RemoteService&gt;();\r\nkernel.Bind&lt;IClient&gt;().To&lt;Client&gt;();\r\n\r\nvar client = kernel.Get&lt;IClient&gt;();\r\nclient.Run();\r\n<\/pre>\n<p>So using Ninject to inject into a constructor is simple. Now let&#8217;s make a change to the client constructor by removing the parameter and instead use property injection.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic class Client : IClient\r\n{\r\n   &#x5B;Inject]\r\n   public IService&#x5B;] Service { get; set; }\r\n\r\n  \/\/ the Run method remains unchanged as does the binding code\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Whilst I&#8217;ve used IoC frameworks such as Unity, MEF, Spring.net etc. in the past, I&#8217;ve not gotten around to playing with Ninject. So time to have a play. First off I created a simple interface, IService and corresponding implementing LocalService public interface IService { void Run(); } public class LocalService : IService { public void [&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":[3,17,18,2],"tags":[],"class_list":["post-91","post","type-post","status-publish","format-standard","hentry","category-c","category-ioc","category-ninject","category-programming"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/91","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=91"}],"version-history":[{"count":13,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/91\/revisions"}],"predecessor-version":[{"id":108,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/91\/revisions\/108"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=91"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=91"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=91"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}