<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Service Locator Pattern in C# with Lazy Initialization</title>
	<atom:link href="http://stefanoricciardi.com/2009/10/13/service-locator-pattern-in-c-with-lazy-initialization/feed/" rel="self" type="application/rss+xml" />
	<link>http://stefanoricciardi.com/2009/10/13/service-locator-pattern-in-c-with-lazy-initialization/</link>
	<description>On Software Development and Thereabouts</description>
	<lastBuildDate>Thu, 03 May 2012 05:56:00 +0200</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: Anonymous</title>
		<link>http://stefanoricciardi.com/2009/10/13/service-locator-pattern-in-c-with-lazy-initialization/comment-page-1/#comment-615</link>
		<dc:creator>Anonymous</dc:creator>
		<pubDate>Sat, 17 Sep 2011 12:02:00 +0000</pubDate>
		<guid isPermaLink="false">http://stefanoricciardi.net/?p=423#comment-615</guid>
		<description>Yes, that should work as well. Remember the goal of this post is to show a &lt;i&gt;simple&lt;/i&gt; example, not to re-write a full fledged IOC container.</description>
		<content:encoded><![CDATA[<p>Yes, that should work as well. Remember the goal of this post is to show a <i>simple</i> example, not to re-write a full fledged IOC container.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Istace Emmanuel</title>
		<link>http://stefanoricciardi.com/2009/10/13/service-locator-pattern-in-c-with-lazy-initialization/comment-page-1/#comment-614</link>
		<dc:creator>Istace Emmanuel</dc:creator>
		<pubDate>Fri, 02 Sep 2011 23:59:00 +0000</pubDate>
		<guid isPermaLink="false">http://stefanoricciardi.net/?p=423#comment-614</guid>
		<description>I&#039;m using Activator.CreateInstance(T); on the GetService(T) and i store pair of Interface/ImplementationType into the dictionnary. Sound it as a good solution ?</description>
		<content:encoded><![CDATA[<p>I&#8217;m using Activator.CreateInstance(T); on the GetService(T) and i store pair of Interface/ImplementationType into the dictionnary. Sound it as a good solution ?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Anonymous</title>
		<link>http://stefanoricciardi.com/2009/10/13/service-locator-pattern-in-c-with-lazy-initialization/comment-page-1/#comment-608</link>
		<dc:creator>Anonymous</dc:creator>
		<pubDate>Thu, 11 Aug 2011 07:16:00 +0000</pubDate>
		<guid isPermaLink="false">http://stefanoricciardi.net/?p=423#comment-608</guid>
		<description>Lazy has been introduced with .NET 4, which had not been released yet at the time of writing this post.</description>
		<content:encoded><![CDATA[<p>Lazy has been introduced with .NET 4, which had not been released yet at the time of writing this post.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Foo</title>
		<link>http://stefanoricciardi.com/2009/10/13/service-locator-pattern-in-c-with-lazy-initialization/comment-page-1/#comment-607</link>
		<dc:creator>Foo</dc:creator>
		<pubDate>Thu, 11 Aug 2011 04:20:00 +0000</pubDate>
		<guid isPermaLink="false">http://stefanoricciardi.net/?p=423#comment-607</guid>
		<description>why not use Lazy ?</description>
		<content:encoded><![CDATA[<p>why not use Lazy ?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: LegoMyEgo</title>
		<link>http://stefanoricciardi.com/2009/10/13/service-locator-pattern-in-c-with-lazy-initialization/comment-page-1/#comment-606</link>
		<dc:creator>LegoMyEgo</dc:creator>
		<pubDate>Wed, 03 Aug 2011 19:31:00 +0000</pubDate>
		<guid isPermaLink="false">http://stefanoricciardi.net/?p=423#comment-606</guid>
		<description>I tried using this method of constraining the Generic type to class, new() and it doesnt work because when using the GetService the T your usually passing in an Interface.  So.. the interface is an abstract type with no constructor.. so this does not work.  The example he has given the the blog works fine.. but your implementation here fails.</description>
		<content:encoded><![CDATA[<p>I tried using this method of constraining the Generic type to class, new() and it doesnt work because when using the GetService the T your usually passing in an Interface.  So.. the interface is an abstract type with no constructor.. so this does not work.  The example he has given the the blog works fine.. but your implementation here fails.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Anonymous</title>
		<link>http://stefanoricciardi.com/2009/10/13/service-locator-pattern-in-c-with-lazy-initialization/comment-page-1/#comment-499</link>
		<dc:creator>Anonymous</dc:creator>
		<pubDate>Wed, 17 Nov 2010 07:34:00 +0000</pubDate>
		<guid isPermaLink="false">http://stefanoricciardi.net/?p=423#comment-499</guid>
		<description>Phill,

thank you for your comment. Your solution seems to be cleaner and more
efficient.</description>
		<content:encoded><![CDATA[<p>Phill,</p>
<p>thank you for your comment. Your solution seems to be cleaner and more<br />
efficient.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Phill</title>
		<link>http://stefanoricciardi.com/2009/10/13/service-locator-pattern-in-c-with-lazy-initialization/comment-page-1/#comment-498</link>
		<dc:creator>Phill</dc:creator>
		<pubDate>Tue, 16 Nov 2010 22:32:00 +0000</pubDate>
		<guid isPermaLink="false">http://stefanoricciardi.net/?p=423#comment-498</guid>
		<description>There is no need to go to that much trouble... by adding a new() constraint to your generic parameter then you can insure that they have a default constructor and call that directly:

public T GetService() : where T : class, new()
19	    {
20	        if (this.instantiatedServices.ContainsKey(typeof(T)))
21	        {
22	            return (T)this.instantiatedServices[typeof(T)];
23	        }
24	        else
25	        {
26	            
33	                T service = new T();
34	 
35	                // add the service to the ones that we have already instantiated
36	                instantiatedServices.Add(typeof(T), service);
37	 
38	                return service;
39	            }
40	            catch (KeyNotFoundException)
41	            {
42	                throw new ApplicationException&quot;The requested service is not registered&quot;);
43	            }
44	        }
45	    }

Note, there may be some errors in that but the concept is sound.</description>
		<content:encoded><![CDATA[<p>There is no need to go to that much trouble&#8230; by adding a new() constraint to your generic parameter then you can insure that they have a default constructor and call that directly:</p>
<p>public T GetService() : where T : class, new()<br />
19	    {<br />
20	        if (this.instantiatedServices.ContainsKey(typeof(T)))<br />
21	        {<br />
22	            return (T)this.instantiatedServices[typeof(T)];<br />
23	        }<br />
24	        else<br />
25	        {<br />
26<br />
33	                T service = new T();<br />
34<br />
35	                // add the service to the ones that we have already instantiated<br />
36	                instantiatedServices.Add(typeof(T), service);<br />
37<br />
38	                return service;<br />
39	            }<br />
40	            catch (KeyNotFoundException)<br />
41	            {<br />
42	                throw new ApplicationException&#8221;The requested service is not registered&#8221;);<br />
43	            }<br />
44	        }<br />
45	    }</p>
<p>Note, there may be some errors in that but the concept is sound.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Alex Goldman</title>
		<link>http://stefanoricciardi.com/2009/10/13/service-locator-pattern-in-c-with-lazy-initialization/comment-page-1/#comment-131</link>
		<dc:creator>Alex Goldman</dc:creator>
		<pubDate>Sun, 02 May 2010 07:26:52 +0000</pubDate>
		<guid isPermaLink="false">http://stefanoricciardi.net/?p=423#comment-131</guid>
		<description>Hi,

There is small problem using Dictionary, Some times custom class that you create new instance they inherit several interfaces.
 
I using IList : TheList.OfType(the Interface)</description>
		<content:encoded><![CDATA[<p>Hi,</p>
<p>There is small problem using Dictionary, Some times custom class that you create new instance they inherit several interfaces.</p>
<p>I using IList : TheList.OfType(the Interface)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Alex Goldman</title>
		<link>http://stefanoricciardi.com/2009/10/13/service-locator-pattern-in-c-with-lazy-initialization/comment-page-1/#comment-456</link>
		<dc:creator>Alex Goldman</dc:creator>
		<pubDate>Sun, 02 May 2010 07:26:00 +0000</pubDate>
		<guid isPermaLink="false">http://stefanoricciardi.net/?p=423#comment-456</guid>
		<description>Hi,

There is small problem using Dictionary, Some times custom class that you create new instance they inherit several interfaces.
 
I using IList : TheList.OfType(the Interface)</description>
		<content:encoded><![CDATA[<p>Hi,</p>
<p>There is small problem using Dictionary, Some times custom class that you create new instance they inherit several interfaces.</p>
<p>I using IList : TheList.OfType(the Interface)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Cameron</title>
		<link>http://stefanoricciardi.com/2009/10/13/service-locator-pattern-in-c-with-lazy-initialization/comment-page-1/#comment-29</link>
		<dc:creator>Cameron</dc:creator>
		<pubDate>Wed, 17 Mar 2010 20:50:22 +0000</pubDate>
		<guid isPermaLink="false">http://stefanoricciardi.net/?p=423#comment-29</guid>
		<description>&quot;It would be interesting to investigate whether mature service locator implementations (Castle Windsor, Spring.NET, StructureMap, etc…) have tackled this kind of problem and how they solved it.&quot;

I think they use weak references (http://msdn.microsoft.com/en-us/library/system.weakreference.aspx)
But I guess that&#039;s the benefit of having a custom service locator class - if you don&#039;t need the logic for instance lifetime management then you can leave it out.</description>
		<content:encoded><![CDATA[<p>&#8220;It would be interesting to investigate whether mature service locator implementations (Castle Windsor, Spring.NET, StructureMap, etc…) have tackled this kind of problem and how they solved it.&#8221;</p>
<p>I think they use weak references (<a href="http://msdn.microsoft.com/en-us/library/system.weakreference.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/system.weakreference.aspx</a>)<br />
But I guess that&#8217;s the benefit of having a custom service locator class &#8211; if you don&#8217;t need the logic for instance lifetime management then you can leave it out.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

