﻿<?xml version="1.0" encoding="utf-8"?><rss version="2.0"><channel><title>Ayende @ Rahien</title><link>http://ayende.com</link><description>Ayende @ Rahien</description><copyright>Copyright (C) Ayende Rahien  2004 - 2021 (c) 2026</copyright><ttl>60</ttl><item><title>Dario Iacampo commented on Design patterns in the test of time: Singleton</title><description>A pattern so common and useful as singleton is should be part of the language, as they do in Scala</description><link>http://ayende.com/159426/design-patterns-in-the-test-of-time-singleton#comment14</link><guid>http://ayende.com/159426/design-patterns-in-the-test-of-time-singleton#comment14</guid><pubDate>Sat, 10 Nov 2012 13:56:09 GMT</pubDate></item><item><title>kijana.woodard commented on Design patterns in the test of time: Singleton</title><description>@James C-S Write a unit test that asserts on the behavior when the date is "yesterday".</description><link>http://ayende.com/159426/design-patterns-in-the-test-of-time-singleton#comment13</link><guid>http://ayende.com/159426/design-patterns-in-the-test-of-time-singleton#comment13</guid><pubDate>Fri, 02 Nov 2012 03:06:46 GMT</pubDate></item><item><title>James C-S commented on Design patterns in the test of time: Singleton</title><description>Can you provide some more detail as to why `DateTime.Now` has hidden dependencies?</description><link>http://ayende.com/159426/design-patterns-in-the-test-of-time-singleton#comment12</link><guid>http://ayende.com/159426/design-patterns-in-the-test-of-time-singleton#comment12</guid><pubDate>Fri, 02 Nov 2012 01:07:51 GMT</pubDate></item><item><title>AndersM commented on Design patterns in the test of time: Singleton</title><description>Rafal: That is a matter of definition. I see nothing wrong in defining it as a  singleton in the request-scope. A lot of other people do the same.</description><link>http://ayende.com/159426/design-patterns-in-the-test-of-time-singleton#comment11</link><guid>http://ayende.com/159426/design-patterns-in-the-test-of-time-singleton#comment11</guid><pubDate>Thu, 01 Nov 2012 14:13:20 GMT</pubDate></item><item><title>Tudor commented on Design patterns in the test of time: Singleton</title><description>HttpContext.Current and DateTime.Now were given as examples of "hidden dependencies", not singletons..</description><link>http://ayende.com/159426/design-patterns-in-the-test-of-time-singleton#comment10</link><guid>http://ayende.com/159426/design-patterns-in-the-test-of-time-singleton#comment10</guid><pubDate>Thu, 01 Nov 2012 13:19:43 GMT</pubDate></item><item><title>Rafal commented on Design patterns in the test of time: Singleton</title><description>@AndersM
One instance per request is not the same as single instance. For example, httprequest.current is not shared by multiple threads...
And what about DateTime.Now - where's the singleton?</description><link>http://ayende.com/159426/design-patterns-in-the-test-of-time-singleton#comment9</link><guid>http://ayende.com/159426/design-patterns-in-the-test-of-time-singleton#comment9</guid><pubDate>Wed, 31 Oct 2012 17:18:08 GMT</pubDate></item><item><title>Oded commented on Design patterns in the test of time: Singleton</title><description>DateTime.Now is not a singelton, 
I would consider it a static property/factory of some sort...
You are getting a different instance on every call</description><link>http://ayende.com/159426/design-patterns-in-the-test-of-time-singleton#comment8</link><guid>http://ayende.com/159426/design-patterns-in-the-test-of-time-singleton#comment8</guid><pubDate>Wed, 31 Oct 2012 15:41:45 GMT</pubDate></item><item><title>Starfish commented on Design patterns in the test of time: Singleton</title><description>// Haiku
class Singleton
{
	private Singleton(){}
	public static readonly Singleton Current = new Singleton();
}


</description><link>http://ayende.com/159426/design-patterns-in-the-test-of-time-singleton#comment7</link><guid>http://ayende.com/159426/design-patterns-in-the-test-of-time-singleton#comment7</guid><pubDate>Wed, 31 Oct 2012 15:41:40 GMT</pubDate></item><item><title>dada commented on Design patterns in the test of time: Singleton</title><description>"per request" -&gt; not "per appdomain" (at least) -&gt; no singleton</description><link>http://ayende.com/159426/design-patterns-in-the-test-of-time-singleton#comment6</link><guid>http://ayende.com/159426/design-patterns-in-the-test-of-time-singleton#comment6</guid><pubDate>Wed, 31 Oct 2012 15:01:20 GMT</pubDate></item><item><title>AndersM commented on Design patterns in the test of time: Singleton</title><description>Dada: Yes, you get the singleton through the static property, but the returned object is still a singleton. There is only one httpcontext per request.</description><link>http://ayende.com/159426/design-patterns-in-the-test-of-time-singleton#comment5</link><guid>http://ayende.com/159426/design-patterns-in-the-test-of-time-singleton#comment5</guid><pubDate>Wed, 31 Oct 2012 14:50:02 GMT</pubDate></item><item><title>Jason Smith commented on Design patterns in the test of time: Singleton</title><description>As Patrick alluded, the biggest problem with the Singleton pattern is in how it is misapplied as an access "pattern".   If we follow the classification that the GOF gave it, Singleton is a *creational* pattern.   Period.   Unfortunately, most folks see a public getInstance() or Current member and think they've found a geek-sanctioned way to implement global variables.   

Second, it is an *implementation* concern.   Just like any other  implementation concern, that concern shouldn't be exposed to client code.  Simply put, the only code that should care something is a singleton, is the singleton itself.   Every other piece of code should depend on receiving an actual reference to an instance.</description><link>http://ayende.com/159426/design-patterns-in-the-test-of-time-singleton#comment4</link><guid>http://ayende.com/159426/design-patterns-in-the-test-of-time-singleton#comment4</guid><pubDate>Wed, 31 Oct 2012 13:50:39 GMT</pubDate></item><item><title>dada commented on Design patterns in the test of time: Singleton</title><description>"Probably the most famous of them was HttpContext.Current"

HttpContext.Current is not an example of a singleton, just an example of a public static prop.</description><link>http://ayende.com/159426/design-patterns-in-the-test-of-time-singleton#comment3</link><guid>http://ayende.com/159426/design-patterns-in-the-test-of-time-singleton#comment3</guid><pubDate>Wed, 31 Oct 2012 13:28:35 GMT</pubDate></item><item><title>Patrick Smacchia commented on Design patterns in the test of time: Singleton</title><description>&gt;Common workaround to that is to break apart the notion of accessing the value and the single nature of the value. So you typically inject the value in, and something else, usually the container, is in charge of managing the lifetime of the object.

I agree 100% with you on that. But static access to singleton object is really part of the DP Singleton in the mind of all developers. So I'd suggest giving a different name like Single Instance Pattern or Manager Pattern to what you describe.

Singleton per-se makes the code highly un-testable. More generally the 'static' keyword should be use with great care, here are my recommendations:
http://codebetter.com/patricksmacchia/2009/11/16/back-to-basics-usage-of-static/</description><link>http://ayende.com/159426/design-patterns-in-the-test-of-time-singleton#comment2</link><guid>http://ayende.com/159426/design-patterns-in-the-test-of-time-singleton#comment2</guid><pubDate>Wed, 31 Oct 2012 12:04:34 GMT</pubDate></item><item><title>sw commented on Design patterns in the test of time: Singleton</title><description>Isn't there a difference between the Singleton Pattern and a singleton lifetime? The pattern is always connected with the global static accessor while the lifetime is something that has to be managed by an external instance (like a di container).</description><link>http://ayende.com/159426/design-patterns-in-the-test-of-time-singleton#comment1</link><guid>http://ayende.com/159426/design-patterns-in-the-test-of-time-singleton#comment1</guid><pubDate>Wed, 31 Oct 2012 10:15:26 GMT</pubDate></item></channel></rss>