<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Lesnikowski Blog &#187; StructureMap</title>
	<atom:link href="http://www.lesnikowski.com/blog/tag/structuremap/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.lesnikowski.com/blog</link>
	<description>EMAIL IMAP POP3 SMTP FTP FTPS barcode components blog</description>
	<lastBuildDate>Sun, 05 Feb 2012 14:10:23 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Wrapper configuration with StructureMap</title>
		<link>http://www.lesnikowski.com/blog/wrapper-configuration-with-structuremap/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=wrapper-configuration-with-structuremap</link>
		<comments>http://www.lesnikowski.com/blog/wrapper-configuration-with-structuremap/#comments</comments>
		<pubDate>Thu, 05 Nov 2009 10:44:24 +0000</pubDate>
		<dc:creator>Pawel Lesnikowski</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[IoC]]></category>
		<category><![CDATA[StructureMap]]></category>

		<guid isPermaLink="false">http://www.lesnikowski.com/blog/?p=209</guid>
		<description><![CDATA[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&#60;string&#62; MyMethod(); } // Original service public class [...]]]></description>
			<content:encoded><![CDATA[<p>Recently we have some problems with StructureMap configuration.<br />
We decided to move away from xml files and use registry configuration.</p>
<p>Imagine the following scenario, we have a service class (MyService), and decide<br />
to create simple caching mechanism, by creating a wrapper class (MyServiceWithCaching)</p>
<pre class="brush: csharp;">
public interface IService
{
	List&lt;string&gt; MyMethod();
}

// Original service
public class MyService : IService
{
	public List&lt;string&gt; MyMethod() { ... }
}

// Service that supports simple caching
public class MyServiceWithCaching : IService
{
	IService _service;
	List&lt;string&gt; _cached;

	// NOTE: We want IService to be injected by IoC container
  	public MyServiceWithCaching(IService service)
  	{
		_service =service;
  	}

	public List&lt;string&gt; MyMethod()
	{
		if (_cached == null)
		{
			// call original when no cached data
			_cached = _service.MyMethod();
		}
		return _cached;
	}
}
</pre>
<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>
<p>It took us some time to figure this out and here&#8217;s the solution:</p>
<pre class="brush: csharp;">
public class IocRegistry : Registry
{
    protected override void configure()
    {
        this.BuildInstancesOf&lt;IMyService&gt;()
             .TheDefaultIs(
                Instance&lt;IMyService&gt;()
                    .UsingConcreteType&lt;MyServiceWithCaching&gt;()
                        .Child&lt;IMyService&gt;()
                            .IsConcreteType&lt;MyService&gt;()
            );

        base.configure();
    }
} ;
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.lesnikowski.com/blog/wrapper-configuration-with-structuremap/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

