{"id":102,"date":"2013-03-28T09:10:15","date_gmt":"2013-03-28T09:10:15","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=102"},"modified":"2013-03-28T09:12:51","modified_gmt":"2013-03-28T09:12:51","slug":"investigating-autofac","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/investigating-autofac\/","title":{"rendered":"Investigating Autofac"},"content":{"rendered":"<p>Time to try some different IoC frameworks. Let&#8217;s take a look at the basics of <a title=\"Autofac\" href=\"http:\/\/code.google.com\/p\/autofac\/\" target=\"_blank\">Autofac<\/a>. Using the simple IService implementation and a client with a constructor that requires the IService injected into we get the following code.<\/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;LocalService&quot;);\r\n   }\r\n}\r\n\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&#x5B;] 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>We can set up bindings in code using the following<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nvar builder = new ContainerBuilder();\r\nbuilder.RegisterType().As();\r\nbuilder.RegisterType().As();\r\n\r\nvar container = builder.Build();\r\n\r\nvar client = container.Resolve();\r\nclient.Run();\r\n<\/pre>\n<p>If we have multiple IService implementations then changing the Client to<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\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 builder = new ContainerBuilder();\r\nbuilder.RegisterType().As();\r\nbuilder.RegisterType().As();\r\nbuilder.RegisterType().As();\r\n\r\nvar container = builder.Build();\r\n\r\nvar client = container.Resolve();\r\nclient.Run();\r\n<\/pre>\n<p>Now let&#8217;s remove the constructor and use property injection. The client changes to<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic class Client : IClient\r\n{\r\n   public IService&#x5B;] Service { get; set; }\r\n\r\n   \/\/ the run method as before\r\n}\r\n<\/pre>\n<p>There&#8217;s no attributes or the likes to suggest that a property should be injected into. The change is in the binding code, just changing the Client registration code to<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nbuilder.RegisterType&lt;Client&gt;().As&lt;IClient&gt;().PropertiesAutowired();\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Time to try some different IoC frameworks. Let&#8217;s take a look at the basics of Autofac. Using the simple IService implementation and a client with a constructor that requires the IService injected into we get the following code. public interface IService { void Run(); } public class LocalService : IService { public void Run() { [&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":[19,17,2],"tags":[],"class_list":["post-102","post","type-post","status-publish","format-standard","hentry","category-autofac","category-ioc","category-programming"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/102","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=102"}],"version-history":[{"count":10,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/102\/revisions"}],"predecessor-version":[{"id":115,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/102\/revisions\/115"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=102"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=102"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=102"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}