{"id":345,"date":"2009-04-23T22:15:56","date_gmt":"2009-04-23T21:15:56","guid":{"rendered":"http:\/\/www.gamlor.info\/wordpress\/?p=345"},"modified":"2021-03-11T09:44:25","modified_gmt":"2021-03-11T08:44:25","slug":"small-di-containers-and-autofac","status":"publish","type":"post","link":"https:\/\/www.gamlor.info\/wordpress\/2009\/04\/small-di-containers-and-autofac\/","title":{"rendered":"Small DI-Containers and Autofac"},"content":{"rendered":"<p>There are tons of <a href=\"http:\/\/www.martinfowler.com\/articles\/injection.html\">dependency injection<\/a> (DI) containers around in the Java and the .NET world. The are the well know, big ones like Spring (<a href=\"http:\/\/www.springsource.org\/\">Java<\/a>, <a href=\"http:\/\/www.springframework.net\/\">.NET<\/a>), <a href=\"http:\/\/www.castleproject.org\/container\/index.html\">Castle Windsor<\/a> etc. Most of them are quite powerful. However, they aren\u2019t small, are heavily based on XML-Configurations and they have their complexity.<\/p>\n<p>So, on small projects I prefer a small container which does not require any XML-Configuration. The small containers usually are configured with plain old code. Having no XML reduces the possible errors like typos, because its checked by the compiler.<\/p>\n<p>So on the Java-world I quite like <a href=\"http:\/\/code.google.com\/p\/google-guice\/\">Guice<\/a>. On the .Net side I\u2019ve found <a href=\"http:\/\/code.google.com\/p\/autofac\/\">Autofac<\/a>. Autofac is a dependency injection container at its core and around 150 KiByte in size. So don\u2019t expect a huge feature list. Nevertheless this small library is nicely designed, so its a pleasure to work with. It supports the usual things like auto wiring components, creation of automatic factories, collections of components, open generic types as components etc.<\/p>\n<p>One cool feature of Autofac is its support for the disposal of components (IDisposable). You create an inner-container instance, use components, then you dispose this sub-container. All used components will be disposed with it. You don\u2019t need to propagate the IDisposable.Dispose() through your whole dependency-graph.<\/p>\n<p>Also its easy to build extensions on top of Autofac. For example Autofac doesn\u2019t support interceptors. So I\u2019ve integrated a simple interceptor-facility in a few hour. Also there is already a integration library for <a href=\"http:\/\/www.castleproject.org\/dynamicproxy\/index.html\">DynamicProxy2<\/a>.<\/p>\n<p>For small a project, Autofac could be a good choice.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are tons of dependency injection (DI) containers around in the Java and the .NET world. The are the well know, big ones like Spring (Java, .NET), Castle Windsor etc.&hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","_links_to":"","_links_to_target":""},"categories":[126,17],"tags":[21,102,101,295],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/posts\/345"}],"collection":[{"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/comments?post=345"}],"version-history":[{"count":3,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/posts\/345\/revisions"}],"predecessor-version":[{"id":3798,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/posts\/345\/revisions\/3798"}],"wp:attachment":[{"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/media?parent=345"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/categories?post=345"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/tags?post=345"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}