{"id":209,"date":"2009-11-05T12:44:24","date_gmt":"2009-11-05T10:44:24","guid":{"rendered":"http:\/\/www.limilabs.com\/blog\/?p=209"},"modified":"2009-11-05T12:44:24","modified_gmt":"2009-11-05T10:44:24","slug":"wrapper-configuration-with-structuremap","status":"publish","type":"post","link":"https:\/\/www.limilabs.com\/blog\/wrapper-configuration-with-structuremap","title":{"rendered":"Wrapper configuration with StructureMap"},"content":{"rendered":"<p>Recently we have some problems with StructureMap configuration.<br \/>\nWe decided to move away from xml files and use registry configuration.<\/p>\n<p>Imagine the following scenario, we have a service class (MyService), and decide<br \/>\nto create simple caching mechanism, by creating a wrapper class (MyServiceWithCaching)<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\npublic interface IService\n{\n\tList&lt;string&gt; MyMethod();\n}\n\n\/\/ Original service\npublic class MyService : IService\n{\n\tpublic List&lt;string&gt; MyMethod() { ... }\n}\n\n\/\/ Service that supports simple caching\npublic class MyServiceWithCaching : IService\n{\n\tIService _service;\n\tList&lt;string&gt; _cached;\n\n\t\/\/ NOTE: We want IService to be injected by IoC container\n  \tpublic MyServiceWithCaching(IService service)\n  \t{\n\t\t_service =service;\n  \t}\n\n\tpublic List&lt;string&gt; MyMethod()\n\t{\n\t\tif (_cached == null)\n\t\t{\n\t\t\t\/\/ call original when no cached data\n\t\t\t_cached = _service.MyMethod();\n\t\t}\n\t\treturn _cached;\n\t}\n}\n<\/pre>\n<p>The problem is how to tell StructureMap that when somebody asks for <strong>IMyService <\/strong>it should get <strong>MyServiceWithCaching <\/strong>which should be injected with<strong> original MyService<\/strong> class.<\/p>\n<p>It took us some time to figure this out and here&#8217;s the solution:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\npublic class IocRegistry : Registry\n{\n    protected override void configure()\n    {\n        this.BuildInstancesOf&lt;imyService&gt;()\n             .TheDefaultIs(\n                Instance&lt;imyService&gt;()\n                    .UsingConcreteType&lt;myServiceWithCaching&gt;()\n                        .Child&lt;imyService&gt;()\n                            .IsConcreteType&lt;myService&gt;()\n            );\n\n        base.configure();\n    }\n} ;\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Recently we have some problems with StructureMap configuration. We decided to move away from xml files and use registry configuration. Imagine the following scenario, we have a service class (MyService), and decide to create simple caching mechanism, by creating a wrapper class (MyServiceWithCaching) public interface IService { List&lt;string&gt; MyMethod(); } \/\/ Original service public class [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[15,54],"class_list":["post-209","post","type-post","status-publish","format-standard","hentry","category-programming","tag-c","tag-structuremap"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/209"}],"collection":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/comments?post=209"}],"version-history":[{"count":0,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/209\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/media?parent=209"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/categories?post=209"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/tags?post=209"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}