Feliz año Nuevo
Feliz Navidad y Feliz Año Nuevo
Happy new year
I wish you a happy new year 2012.
S.
The TDD Compare to SOLID
In his article, Mark Seemann [1] deals with the TDD approach, and he wondered if the TDD is a good design Method. For attempting to answer this question, his compare TDD to S.O.L.I.D. design.
References :
[1] : Mark Seemann – The TDD apostate
Asynchronous Programming in C# by Eric Lippert
I just found a great series of articles about Asynchronous Programming in C# [1].
Summary :
References :
[1] Eric Lippert : Asynchronous Programming in C#, The Series
NotifyingObject for WPF & Silverlight
Today, I read an excellent article about NotifyingObject on WPF, Tomer Shamam [1] provides a very nice solution to use the NotifyingObject with lambda expressions.
He adds some nice features like callback, coercing, validation, and other goodies from WPF DependencyProperty.
References :
[1] : Tomer Shamam, NotifyingObject for WPF & Silverlight
Make non-threadsafe code threadsafe
In his weblog, Jeffrey Richter [1] explains that a customer wanted to increase the performance of his code and the best way to accomplish this was to have multiple threads processing their own data independently of each other. But, again, the class library code base would produce errant results if we did this.
In this excellent article, Jeffrey Richter demonstrated the problem of data isolation in multi-threaded environment. Two distinct sources (threads) want to change the same data. To simulate this, both threads use a static generic list.
To correct the problem, Jeffrey Richter uses proxies over AppDomain (see the Chris Brumme article [2]) for isolation. I think that it’s a great job to isolate data and it’s a good pattern to do this.
However in this case, we have another way to correct the data isolation problem by using the [TreadStatic] attribut. A static field marked with ThreadStaticAttribute is not shared between threads. Each executing thread has a separate instance of the field, and independently sets and gets values for that field. If the field is accessed on a different thread, it will contain a different value.
Do not specify initial values for fields marked with ThreadStaticAttribute, because such initialization occurs only once, when the class constructor executes, and therefore affects only one thread. If you do not specify an initial value, you can rely on the field being initialized to its default value if it is a value type, or to a null reference if it is a reference type.
I would like to illustrate this with the exemple below :
- class Program
- {
- static void Main(string[] args)
- {
- ThreadPool.QueueUserWorkItem( (object target) => { NonConcurrentAlgorithm.Add("Item1"); } );
- NonConcurrentAlgorithm.Add("Item2");
- Thread.Sleep(1000); // To demonstrate the problem has been fixed
- NonConcurrentAlgorithm.GetItems().ForEach( (String name) => { Console.WriteLine(name); } );
- Console.WriteLine("Appuyer sur une touche pour quitter.");
- Console.ReadLine();
-
- /* This code will produce output similar to the following:
- * Item2
- * Appuyer sur une touche pour quitter. */
- }
- }
-
- internal static class NonConcurrentAlgorithm
- {
- [ThreadStatic]
- private static List _items=null;
- private static List Items
- {
- get
- {
- return _items;
- }
- }
- public static void Add(String item)
- {
- Items.Add(item);
- }
- public static List GetItems()
- {
- return Items;
- }
- }
Notes :
Use with caution, this attribute is useful only if you control the lifecyle of your threads (see the Scott Hanselman’ article [3]).
References :
[1] : Using AppDomains to make Non-Threadsafe code Threadsafe
[2] : AppDomains (“application domains”)
[3] : A tale of two techniques: The [ThreadStatic] Attribute and System.Web.HttpContext.Current.Items
Mocks, stubs, fakes & dummies
This terms are often use in TDD, but many people mix up this terms. In his article (based upon Gerard Meszaros’s book [3]) Martin Fowler [1] explains the main difference between them.
- Dummy objects are passed around but never actually used. Usually they are just used to fill parameter lists.
- Fake objects actually have working implementations, but usually take some shortcut which makes them not suitable for production (an in memory database is a good example).
- Stubs provide canned answers to calls made during the test, usually not responding at all to anything outside what’s programmed in for the test. Stubs may also record information about calls, such as an email gateway stub that remembers the messages it ’sent’, or maybe only how many messages it ’sent’.
- Mocks are what we are talking about here: objects pre-programmed with expectations which form a specification of the calls they are expected to receive.
Jeremy D. Miller [4] capsulated this idea by “state_based testing vs interaction testing” :
“The real difference between a mock and a stub is in the style of unit testing, i.e. state-based testing versus interaction testing. A stub is a class that is hard-coded to return data from its methods and properties. You use stubs inside unit tests when you’re testing that a class or method derives the expected output for a known input.
A mock object is a tool for interaction based testing. You use a mock object to record and verify the interaction between two classes. Roy Osherove made my all time favorite explanation of mock objects by comparing a mock object to a spy that listens to the interaction between two classes.”
Roy Osherove [2] provided a good scheme of the difference in flow information (see below)

Framework .NET for Mocking, Stubing & Faking :
- FakeItEasy
- NMock
- EasyMock.NET
- TypeMock Isolator Commercial / Paid
- Rhino Mocks
- Moq
- NSubstitute
- NMock3 based upon NMock, add lambda expressions
References :
[1] : Martin Fowler, Mocks Aren’t Stubs
[2] : Roy Osherove, Mocks and Stubs – The difference is in the flow of information
[3] : Gerard Meszaros, XUnit Test Patterns
[4] : Jeremy D. Miller, Mock Objects and Stubs: The Bottle Brush of TDD








Microsoft patterns & practices
Recent Comments