<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>ploeh blog</title><link>http://blogs.msdn.com/b/ploeh/</link><description>Mark Seemann&amp;#39;s discontinued .NET blog.</description><dc:language>en-US</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>ploeh blog Is Moving</title><link>http://blogs.msdn.com/b/ploeh/archive/2009/02/11/ploeh-blog-is-moving.aspx</link><pubDate>Wed, 11 Feb 2009 19:01:18 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9412919</guid><dc:creator>ploeh</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/ploeh/rsscomments.aspx?WeblogPostID=9412919</wfw:commentRss><comments>http://blogs.msdn.com/b/ploeh/archive/2009/02/11/ploeh-blog-is-moving.aspx#comments</comments><description>&lt;p&gt;After three years on MSDN blogs, I'll be moving my blog to &lt;a title="http://blog.ploeh.dk" href="http://blog.ploeh.dk"&gt;http://blog.ploeh.dk&lt;/a&gt;. Why? &lt;a href="http://blog.ploeh.dk/2009/01/28/LivingInInterestingTimes.aspx"&gt;Read the answer here&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;This is most likely the last post on this blog, so if you'd like to stay tuned, please go and subscribe to the &lt;a href="http://blog.ploeh.dk"&gt;new blog&lt;/a&gt;.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9412919" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/ploeh/archive/tags/Miscellaneous/">Miscellaneous</category></item><item><title>Faking The Windows Azure Table Service</title><link>http://blogs.msdn.com/b/ploeh/archive/2009/01/24/faking-the-windows-azure-table-service.aspx</link><pubDate>Sat, 24 Jan 2009 13:21:36 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9373882</guid><dc:creator>ploeh</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/ploeh/rsscomments.aspx?WeblogPostID=9373882</wfw:commentRss><comments>http://blogs.msdn.com/b/ploeh/archive/2009/01/24/faking-the-windows-azure-table-service.aspx#comments</comments><description>&lt;p&gt;When presented with a new technology, one of my primary evaluation criteria is its testability, so it should come as no surprise to the regular reader of this blog that this was also foremost in my mind when &lt;a href="http://www.microsoft.com/azure"&gt;Windows Azure&lt;/a&gt; was presented at PDC. Much of the Azure Services Platform is just .NET, so testability is really up to you, just like with all other .NET code.&lt;/p&gt; &lt;p&gt;However, when it comes to the Windows Azure storage, the developer story is different. The Windows Azure Table Service is essentially a REST-based service, and to use it, you'll need to install the Azure Visual Studio SDK.&lt;/p&gt; &lt;p&gt;That's not a problem in itself, but it seems to be implemented as a REST service on top of SQL Server Express. While that may be good enough for casual development, that's quite heavyweight for unit testing. Imagine having to somehow deal with populating and cleaning this database between each test case. That will likely involve a fair amount of complex and undocumented &lt;a href="http://xunitpatterns.com/Back%20Door%20Manipulation.html"&gt;Back Door Manipulation&lt;/a&gt;. As the service is out of process in relation to the test, this is brittle and slow, so a better approach is warranted.&lt;/p&gt; &lt;p&gt;Fortunately, I've already demonstrated &lt;a href="http://blogs.msdn.com/ploeh/archive/2009/01/20/a-general-purpose-fake-ado-net-data-service.aspx"&gt;how to Fake a REST-based Data Service&lt;/a&gt;, so faking the Windows Azure Table Service turns out to be fairly easy, and generally follows the same steps as previously outlined.&lt;/p&gt; &lt;p&gt;In the following example, I'm working with a simple service that echoes messages back to the caller. The Delay method saves the input message in table storage and returns the &lt;em&gt;previous&lt;/em&gt; message from storage. This test verifies that the input message is being correctly saved:&lt;/p&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red43\green145\blue175;\red0\green0\blue255;\red0\green128\blue0;\red163\green21\blue21;}??\fs20 [\cf3 TestMethod\cf0 ]\par ??\cf4 public\cf0  \cf4 void\cf0  DelayWillSaveMessage()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf4 string\cf0  expectedMessage = \cf6 "Anonymous text"\cf0 ;\par ??\par ??    \cf3 Uri\cf0  address = \cf4 new\cf0  \cf3 Uri\cf0 (\cf6 "http://localhost/EchoService"\cf0 );\par ??    \cf3 FakeDataService\cf0 &amp;lt;\cf3 MessageContainer\cf0 &amp;gt; service = \par ??        \cf4 new\cf0  \cf3 FakeDataService\cf0 &amp;lt;\cf3 MessageContainer\cf0 &amp;gt;(\par ??            \cf4 new\cf0  \cf3 MessageContainer\cf0 ());\par ??    \cf4 using\cf0  (\cf3 WebServiceHost\cf0  host = \par ??        \cf4 new\cf0  \cf3 WebServiceHost\cf0 (service, \cf4 new\cf0 [] \{ address \}))\par ??    \{\par ??        host.Open();\par ??\par ??        \cf3 StorageAccountInfo\cf0  account = \par ??            \cf3 EchoFacadeTest\cf0 .GetAccount();\par ??        \cf3 EchoFacade\cf0  sut = \cf4 new\cf0  \cf3 EchoFacade\cf0 (() =&amp;gt; account);\par ??        \cf5 // Exercise system\par ??\cf0         sut.Delay(expectedMessage);\par ??        \cf5 // Verify outcome\par ??\cf0         \cf3 Assert\cf0 .AreEqual&amp;lt;\cf4 string\cf0 &amp;gt;(expectedMessage,\par ??            service.Container.GetStore&amp;lt;\cf3 FakeMessage\cf0 &amp;gt;().\par ??            First().Message, \cf6 "Delay"\cf0 );\par ??        \cf5 // Teardown\par ??\cf0     \}\par ??\}}
--&gt; &lt;div style="font-size: 10pt; background: white; color: black; font-family: courier new"&gt;&lt;pre style="margin: 0px"&gt;[&lt;span style="color: #2b91af"&gt;TestMethod&lt;/span&gt;]&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; DelayWillSaveMessage()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;// Fixture setup&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;string&lt;/span&gt; expectedMessage = &lt;span style="color: #a31515"&gt;"Anonymous text"&lt;/span&gt;;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Uri&lt;/span&gt; address = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Uri&lt;/span&gt;(&lt;span style="color: #a31515"&gt;"http://localhost/EchoService"&lt;/span&gt;);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;FakeDataService&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;MessageContainer&lt;/span&gt;&amp;gt; service = &lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;FakeDataService&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;MessageContainer&lt;/span&gt;&amp;gt;(&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MessageContainer&lt;/span&gt;());&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;using&lt;/span&gt; (&lt;span style="color: #2b91af"&gt;WebServiceHost&lt;/span&gt; host = &lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;WebServiceHost&lt;/span&gt;(service, &lt;span style="color: blue"&gt;new&lt;/span&gt;[] { address }))&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; host.Open();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;StorageAccountInfo&lt;/span&gt; account = &lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;EchoFacadeTest&lt;/span&gt;.GetAccount();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;EchoFacade&lt;/span&gt; sut = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;EchoFacade&lt;/span&gt;(() =&amp;gt; account);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;// Exercise system&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sut.Delay(expectedMessage);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;// Verify outcome&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.AreEqual&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt;(expectedMessage,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; service.Container.GetStore&amp;lt;&lt;span style="color: #2b91af"&gt;FakeMessage&lt;/span&gt;&amp;gt;().&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; First().Message, &lt;span style="color: #a31515"&gt;"Delay"&lt;/span&gt;);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;// Teardown&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In the EchoFacade service, I've used the SDK sample StorageClient library that extents the ADO.NET Data Services Client API to deal with the particulars of Windows Azure Table Storage. One difference from regular ADO.NET Data Services lies in how StorageClient deals with the base URIs of the REST interface. This is a challenge, because a URI like &lt;em&gt;http://localhost/EchoService&lt;/em&gt; is interpreted as a service at the address &lt;em&gt;localhost&lt;/em&gt; belonging to the account &lt;em&gt;EchoService&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;This has the implication that I can't just pass the complete address directly to the client API (i.e. EchoFacade), since it would need to decompose it before it can make proper use of it. Fortunately, such a decomposition already exists in the form of the StorageAccountInfo class, so I just need to pass a properly configured instance to my &lt;a href="http://xunitpatterns.com/SUT.html"&gt;SUT&lt;/a&gt; to make it work.&lt;/p&gt;
&lt;p&gt;The test accomplishes this task like this:&lt;/p&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;\red43\green145\blue175;\red163\green21\blue21;}??\fs20 \cf1 private\cf0  \cf1 static\cf0  \cf4 StorageAccountInfo\cf0  GetAccount()\par ??\{\par ??    \cf4 Uri\cf0  address = \cf1 new\cf0  \cf4 Uri\cf0 (\cf5 "http://localhost/"\cf0 );\par ??    \cf1 return\cf0  \cf1 new\cf0  \cf4 StorageAccountInfo\cf0 (address, \cf1 true\cf0 ,\par ??        \cf5 "EchoService"\cf0 , \cf4 EchoFacadeTest\cf0 .TableKey);\par ??\}}
--&gt;
&lt;div style="font-size: 10pt; background: white; color: black; font-family: courier new"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: blue"&gt;static&lt;/span&gt; &lt;span style="color: #2b91af"&gt;StorageAccountInfo&lt;/span&gt; GetAccount()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Uri&lt;/span&gt; address = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Uri&lt;/span&gt;(&lt;span style="color: #a31515"&gt;"http://localhost/"&lt;/span&gt;);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;return&lt;/span&gt; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;StorageAccountInfo&lt;/span&gt;(address, &lt;span style="color: blue"&gt;true&lt;/span&gt;,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #a31515"&gt;"EchoService"&lt;/span&gt;, &lt;span style="color: #2b91af"&gt;EchoFacadeTest&lt;/span&gt;.TableKey);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;When the service is hosted in the full system (whether in the Development Fabric or on Azure proper), the StorageAccountInfo instance is instead initialized using the static StorageAccountInfo.GetDefaultTableStorageAccountFromConfiguration method.&lt;/p&gt;
&lt;p&gt;If you are using the StorageClient library for your data access code, you might as well also take advantage of it when defining the fake service. First of all, you can derive the entities exposed from the Fake service from TableStorageEntity, as this gives you the key properties for free.&lt;/p&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;\red43\green145\blue175;}??\fs20 \cf1 public\cf0  \cf1 class\cf0  \cf4 FakeMessage\cf0  : \cf4 TableStorageEntity\par ??\cf0 \{\par ??    \cf1 public\cf0  FakeMessage()\par ??    \{\par ??        \cf1 this\cf0 .CreationTime = \cf4 DateTime\cf0 .Now;\par ??    \}\par ??\par ??    \cf1 public\cf0  \cf4 DateTime\cf0  CreationTime \{ \cf1 get\cf0 ; \cf1 set\cf0 ; \}\par ??\par ??    \cf1 public\cf0  \cf1 string\cf0  Message \{ \cf1 get\cf0 ; \cf1 set\cf0 ; \}\par ??\}}
--&gt;
&lt;div style="font-size: 10pt; background: white; color: black; font-family: courier new"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;class&lt;/span&gt; &lt;span style="color: #2b91af"&gt;FakeMessage&lt;/span&gt; : &lt;span style="color: #2b91af"&gt;TableStorageEntity&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;public&lt;/span&gt; FakeMessage()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;this&lt;/span&gt;.CreationTime = &lt;span style="color: #2b91af"&gt;DateTime&lt;/span&gt;.Now;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: #2b91af"&gt;DateTime&lt;/span&gt; CreationTime { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;string&lt;/span&gt; Message { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The DataContainer then becomes equally simple:&lt;/p&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;\red43\green145\blue175;}??\fs20 \cf1 public\cf0  \cf1 class\cf0  \cf4 MessageContainer\cf0  : \cf4 DataContainer\par ??\cf0 \{\par ??    \cf1 public\cf0  \cf4 IQueryable\cf0 &amp;lt;\cf4 FakeMessage\cf0 &amp;gt; Messages\par ??    \{\par ??        \cf1 get\par ??\cf0         \{ \par ??            \cf1 return\cf0  \cf1 this\cf0 .GetStore&amp;lt;\cf4 FakeMessage\cf0 &amp;gt;().\par ??                AsQueryable(); \par ??        \}\par ??    \}\par ??\}}
--&gt;
&lt;div style="font-size: 10pt; background: white; color: black; font-family: courier new"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;class&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MessageContainer&lt;/span&gt; : &lt;span style="color: #2b91af"&gt;DataContainer&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: #2b91af"&gt;IQueryable&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;FakeMessage&lt;/span&gt;&amp;gt; Messages&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;get&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; { &lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;return&lt;/span&gt; &lt;span style="color: blue"&gt;this&lt;/span&gt;.GetStore&amp;lt;&lt;span style="color: #2b91af"&gt;FakeMessage&lt;/span&gt;&amp;gt;().&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; AsQueryable(); &lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Using a Fake ADO.NET Data Service enables you unit test clients of the Windows Azure Table Service in a light-weight manner.&lt;/p&gt;
&lt;p&gt;In this Fake, I have chosen not to deal with the Windows Azure Table Service's particular way of partitioning and sorting data according to PartitionKey and RowKey. Instead, I just rely on the Fixture of each test to insert the data entities in the expected order.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9373882" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/ploeh/archive/tags/Unit+Testing/">Unit Testing</category><category domain="http://blogs.msdn.com/b/ploeh/archive/tags/Services/">Services</category></item><item><title>A General-Purpose Fake ADO.NET Data Service</title><link>http://blogs.msdn.com/b/ploeh/archive/2009/01/20/a-general-purpose-fake-ado-net-data-service.aspx</link><pubDate>Tue, 20 Jan 2009 10:10:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9343868</guid><dc:creator>ploeh</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/ploeh/rsscomments.aspx?WeblogPostID=9343868</wfw:commentRss><comments>http://blogs.msdn.com/b/ploeh/archive/2009/01/20/a-general-purpose-fake-ado-net-data-service.aspx#comments</comments><description>&lt;P&gt;In my &lt;A href="http://blogs.msdn.com/ploeh/archive/2009/01/19/creating-a-fake-ado-net-data-service.aspx" mce_href="http://blogs.msdn.com/ploeh/archive/2009/01/19/creating-a-fake-ado-net-data-service.aspx"&gt;previous post&lt;/A&gt;, I discussed how to implement a &lt;A href="http://xunitpatterns.com/Fake%20Object.html" mce_href="http://xunitpatterns.com/Fake%20Object.html"&gt;Fake&lt;/A&gt; ADO.NET Data Service for use with unit testing, showing how you can develop a one-off service that addresses specific needs, such as querying parents and children in the example.&lt;/P&gt;
&lt;P&gt;As I hinted in that post, it's possible to create a reusable Fake ADO.NET Data Service that takes care of implementing &lt;A href="http://msdn.microsoft.com/en-us/library/system.data.services.iupdatable.aspx" mce_href="http://msdn.microsoft.com/en-us/library/system.data.services.iupdatable.aspx"&gt;IUpdatable&lt;/A&gt; based on in-memory data storage. To save you the trouble of doing that, I've created one that you can use, if you should so desire.&lt;/P&gt;
&lt;P&gt;Following the standard ADO.NET Data Service coding idiom, the Fake ADO.NET Data Service consists of two classes:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;FakeDataService&amp;lt;T&amp;gt;, which derives from DataService&amp;lt;T&amp;gt;. This is simply the counterpart of MyDataService from the previous example. You can use this class as is. 
&lt;LI&gt;DataContainer, which implements IUpdatable. Although I haven't made this class abstract, it only becomes useful when you derive from it and define your own queries.&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;Let's say that I want to use these two classes to unit test the same service client as in the previous example. Instead of MyService (where I manually had to implement IUpdatable), I can now define my data container as simply as this:&lt;/P&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;\red43\green145\blue175;}??\fs20 \cf1 public\cf0  \cf1 partial\cf0  \cf1 class\cf0  \cf4 ParentChildContainer\cf0  : \cf4 DataContainer\par ??\cf0 \{\par ??    \cf1 public\cf0  \cf4 IQueryable\cf0 &amp;lt;\cf4 Child\cf0 &amp;gt; Children\par ??    \{\par ??        \cf1 get\cf0  \par ??        \{\par ??            \cf1 return\cf0  \cf1 this\cf0 .GetStore&amp;lt;\cf4 Child\cf0 &amp;gt;().AsQueryable(); \par ??        \}\par ??    \}\par ??\par ??    \cf1 public\cf0  \cf4 IQueryable\cf0 &amp;lt;\cf4 Parent\cf0 &amp;gt; Parents\par ??    \{\par ??        \cf1 get\par ??\cf0         \{\par ??            \cf1 return\cf0  \cf1 this\cf0 .GetStore&amp;lt;\cf4 Parent\cf0 &amp;gt;().AsQueryable(); \par ??        \}\par ??    \}\par ??\}}
--&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: courier new"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;partial&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;class&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;ParentChildContainer&lt;/SPAN&gt; : &lt;SPAN style="COLOR: #2b91af"&gt;DataContainer&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;IQueryable&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Child&lt;/SPAN&gt;&amp;gt; Children&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt; &lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.GetStore&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Child&lt;/SPAN&gt;&amp;gt;().AsQueryable(); &lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;IQueryable&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Parent&lt;/SPAN&gt;&amp;gt; Parents&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.GetStore&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Parent&lt;/SPAN&gt;&amp;gt;().AsQueryable(); &lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;The only thing you have to do is just derive from DataContainer and specify your queries.&lt;/P&gt;
&lt;P&gt;Although you don't have to use FakeDataService&amp;lt;T&amp;gt; to host your Fake DataContainer, it makes it easy to set up the Fake service in a unit test:&lt;/P&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red43\green145\blue175;\red0\green0\blue255;\red0\green128\blue0;\red163\green21\blue21;}??\fs20 [\cf3 TestMethod\cf0 ]\par ??\cf4 public\cf0  \cf4 void\cf0  TestCanSetupFakeService()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf3 Uri\cf0  address = \cf4 new\cf0  \cf3 Uri\cf0 (\cf6 "http://localhost/MyDataService"\cf0 );\par ??    \cf3 FakeDataService\cf0 &amp;lt;\cf3 ParentChildContainer\cf0 &amp;gt; service = \par ??        \cf4 new\cf0  \cf3 FakeDataService\cf0 &amp;lt;\cf3 ParentChildContainer\cf0 &amp;gt;(\par ??            \cf4 new\cf0  \cf3 ParentChildContainer\cf0 ());\par ??    \cf4 using\cf0  (\cf3 WebServiceHost\cf0  host =\par ??        \cf4 new\cf0  \cf3 WebServiceHost\cf0 (service, \cf4 new\cf0 [] \{ address \}))\par ??    \{\par ??        host.Open();\par ??        \cf5 // Exercise system\par ??\cf0         \cf5 // ...\par ??\cf0         \cf5 // Verify outcome\par ??\cf0         \cf5 // ...\par ??\cf0         \cf5 // Teardown\par ??\cf0     \}\par ??\}}
--&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: courier new"&gt;&lt;PRE style="MARGIN: 0px"&gt;[&lt;SPAN style="COLOR: #2b91af"&gt;TestMethod&lt;/SPAN&gt;]&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; TestCanSetupFakeService()&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// Fixture setup&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;Uri&lt;/SPAN&gt; address = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;Uri&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #a31515"&gt;"http://localhost/MyDataService"&lt;/SPAN&gt;);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;FakeDataService&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;ParentChildContainer&lt;/SPAN&gt;&amp;gt; service = &lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;FakeDataService&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;ParentChildContainer&lt;/SPAN&gt;&amp;gt;(&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;ParentChildContainer&lt;/SPAN&gt;());&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;using&lt;/SPAN&gt; (&lt;SPAN style="COLOR: #2b91af"&gt;WebServiceHost&lt;/SPAN&gt; host =&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;WebServiceHost&lt;/SPAN&gt;(service, &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt;[] { address }))&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; host.Open();&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// Exercise system&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// ...&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// Verify outcome&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// ...&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// Teardown&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;How does this work? DataContainer stores each 'table' in a List&amp;lt;T&amp;gt;, and each list is again stored in a dictionary that maps the list to the type. Here's the part of DataContainer that doesn't contain the IUpdatable implementation:&lt;/P&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;\red43\green145\blue175;\red163\green21\blue21;}??\fs20 \cf1 public\cf0  \cf1 partial\cf0  \cf1 class\cf0  \cf4 DataContainer\par ??\cf0 \{\par ??    \cf1 private\cf0  \cf1 readonly\cf0  \cf4 Dictionary\cf0 &amp;lt;\cf4 Type\cf0 , \cf4 IList\cf0 &amp;gt; stores;\par ??\par ??    \cf1 public\cf0  DataContainer()\par ??    \{\par ??        \cf1 this\cf0 .stores = \cf1 new\cf0  \cf4 Dictionary\cf0 &amp;lt;\cf4 Type\cf0 , \cf4 IList\cf0 &amp;gt;();\par ??    \}\par ??\par ??    [System.Diagnostics.CodeAnalysis.\cf4 SuppressMessage\cf0 (\cf5 "Microsoft.Design"\cf0 , \cf5 "CA1004:GenericMethodsShouldProvideTypeParameter"\cf0 , Justification = \cf5 "As has been discussed on the FxCop DL, this is fair use of generics."\cf0 )]\par ??    \cf1 public\cf0  \cf4 IList\cf0 &amp;lt;T&amp;gt; GetStore&amp;lt;T&amp;gt;()\par ??    \{\par ??        \cf1 if\cf0  (!\cf1 this\cf0 .stores.ContainsKey(\cf1 typeof\cf0 (T)))\par ??        \{\par ??            \cf1 this\cf0 .AddStore&amp;lt;T&amp;gt;();\par ??        \}\par ??        \cf1 return\cf0  (\cf4 IList\cf0 &amp;lt;T&amp;gt;)\cf1 this\cf0 .stores[\cf1 typeof\cf0 (T)];\par ??    \}\par ??\par ??    \cf1 private\cf0  \cf1 void\cf0  AddStore&amp;lt;T&amp;gt;()\par ??    \{\par ??        \cf1 this\cf0 .stores.Add(\cf1 typeof\cf0 (T), \cf1 new\cf0  \cf4 List\cf0 &amp;lt;T&amp;gt;());\par ??    \}\par ??\}}
--&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: courier new"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;partial&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;class&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;DataContainer&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;private&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;readonly&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;Dictionary&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Type&lt;/SPAN&gt;, &lt;SPAN style="COLOR: #2b91af"&gt;IList&lt;/SPAN&gt;&amp;gt; stores;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; DataContainer()&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.stores = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;Dictionary&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Type&lt;/SPAN&gt;, &lt;SPAN style="COLOR: #2b91af"&gt;IList&lt;/SPAN&gt;&amp;gt;();&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;IList&lt;/SPAN&gt;&amp;lt;T&amp;gt; GetStore&amp;lt;T&amp;gt;()&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;if&lt;/SPAN&gt; (!&lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.stores.ContainsKey(&lt;SPAN style="COLOR: blue"&gt;typeof&lt;/SPAN&gt;(T)))&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.AddStore&amp;lt;T&amp;gt;();&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; (&lt;SPAN style="COLOR: #2b91af"&gt;IList&lt;/SPAN&gt;&amp;lt;T&amp;gt;)&lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.stores[&lt;SPAN style="COLOR: blue"&gt;typeof&lt;/SPAN&gt;(T)];&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;private&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; AddStore&amp;lt;T&amp;gt;()&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.stores.Add(&lt;SPAN style="COLOR: blue"&gt;typeof&lt;/SPAN&gt;(T), &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;List&lt;/SPAN&gt;&amp;lt;T&amp;gt;());&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;When you call GetStore&amp;lt;T&amp;gt;, DataContainer implicitly creates the list for the give type if it doesn't already exist.&lt;/P&gt;
&lt;P&gt;The rest of DataContainer deals with implementing IUpdatable:&lt;/P&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;\red43\green145\blue175;}??\fs20 \cf1 #region\cf0  IUpdatable Members\par ??\par ??\cf1 public\cf0  \cf1 void\cf0  AddReferenceToCollection(\cf1 object\cf0  targetResource,\par ??    \cf1 string\cf0  propertyName, \cf1 object\cf0  resourceToBeAdded)\par ??\{\par ??    \cf4 IList\cf0  list = (\cf4 IList\cf0 )targetResource.GetType().\par ??        GetProperty(propertyName).\par ??        GetValue(targetResource, \cf1 null\cf0 );\par ??    list.Add(resourceToBeAdded);\par ??\}\par ??\par ??\cf1 public\cf0  \cf1 void\cf0  ClearChanges()\par ??\{\par ??\}\par ??\par ??\cf1 public\cf0  \cf1 object\cf0  CreateResource(\cf1 string\cf0  containerName,\par ??    \cf1 string\cf0  fullTypeName)\par ??\{\par ??    \cf4 Type\cf0  t = \cf1 this\cf0 .ResolveType(fullTypeName);\par ??    \cf1 object\cf0  resource = \cf4 Activator\cf0 .CreateInstance(t);\par ??    \cf1 this\cf0 .stores[t].Add(resource);\par ??    \cf1 return\cf0  resource;\par ??\}\par ??\par ??\cf1 public\cf0  \cf1 void\cf0  DeleteResource(\cf1 object\cf0  targetResource)\par ??\{\par ??    \cf1 foreach\cf0  (\cf4 IList\cf0  list \cf1 in\cf0  \cf1 this\cf0 .stores.Values)\par ??    \{\par ??        list.Remove(targetResource);\par ??    \}\par ??\}\par ??\par ??\cf1 public\cf0  \cf1 object\cf0  GetResource(\cf4 IQueryable\cf0  query,\par ??    \cf1 string\cf0  fullTypeName)\par ??\{\par ??    \cf1 return\cf0  query.Cast&amp;lt;\cf1 object\cf0 &amp;gt;().AsEnumerable().\par ??        FirstOrDefault();\par ??\}\par ??\par ??\cf1 public\cf0  \cf1 object\cf0  GetValue(\cf1 object\cf0  targetResource,\par ??    \cf1 string\cf0  propertyName)\par ??\{\par ??    \cf1 return\cf0  targetResource.GetType().\par ??        GetProperty(propertyName).\par ??        GetValue(targetResource, \cf1 null\cf0 );\par ??\}\par ??\par ??\cf1 public\cf0  \cf1 void\cf0  RemoveReferenceFromCollection(\par ??    \cf1 object\cf0  targetResource,\par ??    \cf1 string\cf0  propertyName,\par ??    \cf1 object\cf0  resourceToBeRemoved)\par ??\{\par ??    \cf4 IList\cf0  list = (\cf4 IList\cf0 )targetResource.GetType().\par ??        GetProperty(propertyName).\par ??        GetValue(targetResource, \cf1 null\cf0 );\par ??    list.Remove(resourceToBeRemoved);\par ??\}\par ??\par ??\cf1 public\cf0  \cf1 object\cf0  ResetResource(\cf1 object\cf0  resource)\par ??\{\par ??    \cf1 return\cf0  resource;\par ??\}\par ??\par ??\cf1 public\cf0  \cf1 object\cf0  ResolveResource(\cf1 object\cf0  resource)\par ??\{\par ??    \cf1 return\cf0  resource;\par ??\}\par ??\par ??\cf1 public\cf0  \cf1 void\cf0  SaveChanges()\par ??\{\par ??\}\par ??\par ??\cf1 public\cf0  \cf1 void\cf0  SetReference(\cf1 object\cf0  targetResource,\par ??    \cf1 string\cf0  propertyName, \cf1 object\cf0  propertyValue)\par ??\{\par ??    targetResource.GetType().GetProperty(propertyName).\par ??        SetValue(targetResource, propertyValue, \cf1 null\cf0 );\par ??\}\par ??\par ??\cf1 public\cf0  \cf1 void\cf0  SetValue(\cf1 object\cf0  targetResource, \par ??    \cf1 string\cf0  propertyName, \cf1 object\cf0  propertyValue)\par ??\{\par ??    targetResource.GetType().GetProperty(propertyName).\par ??        SetValue(targetResource, propertyValue, \cf1 null\cf0 );\par ??\}\par ??\par ??\cf1 #endregion\par ??\par ??private\cf0  \cf4 Type\cf0  ResolveType(\cf1 string\cf0  fullTypeName)\par ??\{\par ??    \cf1 return\cf0  (\cf1 from\cf0  t \cf1 in\cf0  \cf1 this\cf0 .stores.Keys\par ??            \cf1 where\cf0  t.FullName == fullTypeName\par ??            \cf1 select\cf0  t).First();\par ??\}}
--&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: courier new"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;#region&lt;/SPAN&gt; IUpdatable Members&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; AddReferenceToCollection(&lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt; targetResource,&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; propertyName, &lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt; resourceToBeAdded)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;IList&lt;/SPAN&gt; list = (&lt;SPAN style="COLOR: #2b91af"&gt;IList&lt;/SPAN&gt;)targetResource.GetType().&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; GetProperty(propertyName).&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; GetValue(targetResource, &lt;SPAN style="COLOR: blue"&gt;null&lt;/SPAN&gt;);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; list.Add(resourceToBeAdded);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; ClearChanges()&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt; CreateResource(&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; containerName,&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; fullTypeName)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;Type&lt;/SPAN&gt; t = &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.ResolveType(fullTypeName);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt; resource = &lt;SPAN style="COLOR: #2b91af"&gt;Activator&lt;/SPAN&gt;.CreateInstance(t);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.stores[t].Add(resource);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; resource;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; DeleteResource(&lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt; targetResource)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;foreach&lt;/SPAN&gt; (&lt;SPAN style="COLOR: #2b91af"&gt;IList&lt;/SPAN&gt; list &lt;SPAN style="COLOR: blue"&gt;in&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.stores.Values)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; list.Remove(targetResource);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt; GetResource(&lt;SPAN style="COLOR: #2b91af"&gt;IQueryable&lt;/SPAN&gt; query,&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; fullTypeName)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; query.Cast&amp;lt;&lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt;&amp;gt;().AsEnumerable().&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; FirstOrDefault();&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt; GetValue(&lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt; targetResource,&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; propertyName)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; targetResource.GetType().&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; GetProperty(propertyName).&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; GetValue(targetResource, &lt;SPAN style="COLOR: blue"&gt;null&lt;/SPAN&gt;);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; RemoveReferenceFromCollection(&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt; targetResource,&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; propertyName,&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt; resourceToBeRemoved)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;IList&lt;/SPAN&gt; list = (&lt;SPAN style="COLOR: #2b91af"&gt;IList&lt;/SPAN&gt;)targetResource.GetType().&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; GetProperty(propertyName).&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; GetValue(targetResource, &lt;SPAN style="COLOR: blue"&gt;null&lt;/SPAN&gt;);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; list.Remove(resourceToBeRemoved);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt; ResetResource(&lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt; resource)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; resource;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt; ResolveResource(&lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt; resource)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; resource;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; SaveChanges()&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; SetReference(&lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt; targetResource,&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; propertyName, &lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt; propertyValue)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; targetResource.GetType().GetProperty(propertyName).&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; SetValue(targetResource, propertyValue, &lt;SPAN style="COLOR: blue"&gt;null&lt;/SPAN&gt;);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; SetValue(&lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt; targetResource, &lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; propertyName, &lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt; propertyValue)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; targetResource.GetType().GetProperty(propertyName).&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; SetValue(targetResource, propertyValue, &lt;SPAN style="COLOR: blue"&gt;null&lt;/SPAN&gt;);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;#endregion&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;private&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;Type&lt;/SPAN&gt; ResolveType(&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; fullTypeName)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; (&lt;SPAN style="COLOR: blue"&gt;from&lt;/SPAN&gt; t &lt;SPAN style="COLOR: blue"&gt;in&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.stores.Keys&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;where&lt;/SPAN&gt; t.FullName == fullTypeName&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;select&lt;/SPAN&gt; t).First();&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;If you find this useful, I've attached the code to this post for your downloading pleasure. As always, the standard disclaimers apply.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9343868" width="1" height="1"&gt;</description><enclosure url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-components-postattachments/00-09-34-38-68/DataServicesQualityTools.zip" length="81103" type="application/x-zip-compressed" /><category domain="http://blogs.msdn.com/b/ploeh/archive/tags/Unit+Testing/">Unit Testing</category><category domain="http://blogs.msdn.com/b/ploeh/archive/tags/Services/">Services</category></item><item><title>Creating A Fake ADO.NET Data Service</title><link>http://blogs.msdn.com/b/ploeh/archive/2009/01/19/creating-a-fake-ado-net-data-service.aspx</link><pubDate>Mon, 19 Jan 2009 11:00:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9339649</guid><dc:creator>ploeh</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/ploeh/rsscomments.aspx?WeblogPostID=9339649</wfw:commentRss><comments>http://blogs.msdn.com/b/ploeh/archive/2009/01/19/creating-a-fake-ado-net-data-service.aspx#comments</comments><description>&lt;P&gt;&lt;A href="http://blogs.msdn.com/ploeh/archive/2009/01/14/unit-testing-ado-net-data-service-clients.aspx" mce_href="http://blogs.msdn.com/ploeh/archive/2009/01/14/unit-testing-ado-net-data-service-clients.aspx"&gt;Previously&lt;/A&gt;, I discussed unit testing ADO.NET Data Service clients using a &lt;A href="http://xunitpatterns.com/Fake%20Object.html" mce_href="http://xunitpatterns.com/Fake%20Object.html"&gt;Fake&lt;/A&gt; ADO.NET Data Service, and I promised to demonstrate how to create such a service. In this article I will continue the previous example and implement the Fake MyService class.&lt;/P&gt;
&lt;P&gt;The basics of MyService is pretty simple:&lt;/P&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;\red43\green145\blue175;}??\fs20 \cf1 public\cf0  \cf1 partial\cf0  \cf1 class\cf0  \cf4 MyService\par ??\cf0 \{\par ??    \cf1 public\cf0  MyService()\par ??    \{\par ??        \cf1 this\cf0 .ParentStore = \cf1 new\cf0  \cf4 List\cf0 &amp;lt;\cf4 Parent\cf0 &amp;gt;();\par ??        \cf1 this\cf0 .ChildStore = \cf1 new\cf0  \cf4 List\cf0 &amp;lt;\cf4 Child\cf0 &amp;gt;();\par ??    \}\par ??\par ??    \cf1 public\cf0  \cf4 IQueryable\cf0 &amp;lt;\cf4 Child\cf0 &amp;gt; Children\par ??    \{\par ??        \cf1 get\cf0  \{ \cf1 return\cf0  \cf1 this\cf0 .ChildStore.AsQueryable(); \}\par ??    \}\par ??\par ??    \cf1 public\cf0  \cf4 IQueryable\cf0 &amp;lt;\cf4 Parent\cf0 &amp;gt; Parents\par ??    \{\par ??        \cf1 get\cf0  \{ \cf1 return\cf0  \cf1 this\cf0 .ParentStore.AsQueryable(); \}\par ??    \}\par ??\par ??    \cf1 public\cf0  \cf4 IList\cf0 &amp;lt;\cf4 Child\cf0 &amp;gt; ChildStore \{ \cf1 get\cf0 ; \cf1 private\cf0  \cf1 set\cf0 ; \}\par ??\par ??    \cf1 public\cf0  \cf4 IList\cf0 &amp;lt;\cf4 Parent\cf0 &amp;gt; ParentStore \{ \cf1 get\cf0 ; \cf1 private\cf0  \cf1 set\cf0 ; \}\par ??\}}
--&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: courier new"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;partial&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;class&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;MyService&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; MyService()&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.ParentStore = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;List&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Parent&lt;/SPAN&gt;&amp;gt;();&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.ChildStore = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;List&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Child&lt;/SPAN&gt;&amp;gt;();&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;IQueryable&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Child&lt;/SPAN&gt;&amp;gt; Children&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt; { &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.ChildStore.AsQueryable(); }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;IQueryable&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Parent&lt;/SPAN&gt;&amp;gt; Parents&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt; { &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.ParentStore.AsQueryable(); }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;IList&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Child&lt;/SPAN&gt;&amp;gt; ChildStore { &lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;; &lt;SPAN style="COLOR: blue"&gt;private&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;set&lt;/SPAN&gt;; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;IList&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Parent&lt;/SPAN&gt;&amp;gt; ParentStore { &lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;; &lt;SPAN style="COLOR: blue"&gt;private&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;set&lt;/SPAN&gt;; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;This service supports two 'tables': Parents and Children (Parents can have Children). Since this is a Fake, it only needs to store its data in memory, so I use two List&amp;lt;T&amp;gt; instances for that. To provide direct access to the service's data, MyService exposes both lists as public properties (you can make those properties internal if your Fake service is defined in the same assembly as your tests).&lt;/P&gt;
&lt;P&gt;This is all you need if you only need to support read-only scenarios. If the client you want to test only queries its service, you don't need to do anything else. On the other hand, if you also need to test Create, Update and Delete (CUD) operations, you will need to implement &lt;A href="http://msdn.microsoft.com/en-us/library/system.data.services.iupdatable.aspx" mce_href="http://msdn.microsoft.com/en-us/library/system.data.services.iupdatable.aspx"&gt;IUpdatable&lt;/A&gt;:&lt;/P&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;\red43\green145\blue175;}??\fs20 \cf1 public\cf0  \cf1 partial\cf0  \cf1 class\cf0  \cf4 MyService\cf0  : \cf4 IUpdatable}
--&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: courier new"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;partial&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;class&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;MyService&lt;/SPAN&gt; : &lt;SPAN style="COLOR: #2b91af"&gt;IUpdatable&lt;/SPAN&gt;&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;Implementing this interface enables you to support CUD operations. Since it contains twelve members, I'm not going to walk you through each and every one of them, but rather present some highlights. If you are interested in more details on the methods of this interface, I'll recommend &lt;A href="http://blogs.msdn.com/astoriateam/archive/2008/04/10/iupdatable-ado-net-data-services-framework.aspx" mce_href="http://blogs.msdn.com/astoriateam/archive/2008/04/10/iupdatable-ado-net-data-services-framework.aspx"&gt;this post&lt;/A&gt; on the &lt;A href="http://blogs.msdn.com/astoriateam/default.aspx" mce_href="http://blogs.msdn.com/astoriateam/default.aspx"&gt;Project Astoria Team Blog&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;To support each of the simple CUD operations, multiple methods must be implemented, although there's an overlap.&lt;/P&gt;
&lt;P&gt;To create a new item, the first method to implement is CreateResource.&lt;/P&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;\red163\green21\blue21;\red43\green145\blue175;}??\fs20 \cf1 public\cf0  \cf1 object\cf0  CreateResource(\cf1 string\cf0  containerName, \par ??    \cf1 string\cf0  fullTypeName)\par ??\{\par ??    \cf1 switch\cf0  (containerName)\par ??    \{\par ??        \cf1 case\cf0  \cf4 "Parents"\cf0 :\par ??            \cf5 Parent\cf0  p = \cf1 new\cf0  \cf5 Parent\cf0 ();\par ??            \cf1 this\cf0 .ParentStore.Add(p);\par ??            \cf1 return\cf0  p;\par ??        \cf1 case\cf0  \cf4 "Children"\cf0 :\par ??            \cf5 Child\cf0  c = \cf1 new\cf0  \cf5 Child\cf0 ();\par ??            \cf1 this\cf0 .ChildStore.Add(c);\par ??            \cf1 return\cf0  c;\par ??        \cf1 default\cf0 :\par ??            \cf1 throw\cf0  \cf1 new\cf0  \cf5 ArgumentException\cf0 (\par ??                \cf4 "Unknown container name."\cf0 );\par ??    \}\par ??\}}
--&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: courier new"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt; CreateResource(&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; containerName, &lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; fullTypeName)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;switch&lt;/SPAN&gt; (containerName)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;case&lt;/SPAN&gt; &lt;SPAN style="COLOR: #a31515"&gt;"Parents"&lt;/SPAN&gt;:&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;Parent&lt;/SPAN&gt; p = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;Parent&lt;/SPAN&gt;();&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.ParentStore.Add(p);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; p;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;case&lt;/SPAN&gt; &lt;SPAN style="COLOR: #a31515"&gt;"Children"&lt;/SPAN&gt;:&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;Child&lt;/SPAN&gt; c = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;Child&lt;/SPAN&gt;();&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.ChildStore.Add(c);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; c;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;default&lt;/SPAN&gt;:&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;throw&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;ArgumentException&lt;/SPAN&gt;(&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #a31515"&gt;"Unknown container name."&lt;/SPAN&gt;);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;This implementation simply creates a new instance of the requested item type and adds it to the in-memory list. Here, I was just being a bit lazy and simply switched on the containerName, but it's perfectly possible to create a more generic implementation using a bit of Reflection, since the fullTypeName parameter specifies which type of object to create.&lt;/P&gt;
&lt;P&gt;The CreateResource method's responsibility is to create a default instance of the requested type and add it to the underlying data store (in this case just an in-memory list). However, it doesn't assign values to the item's properties, since this is the duty of the SetValue method.&lt;/P&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;\red43\green145\blue175;}??\fs20 \cf1 public\cf0  \cf1 void\cf0  SetValue(\cf1 object\cf0  targetResource, \par ??    \cf1 string\cf0  propertyName, \cf1 object\cf0  propertyValue)\par ??\{\par ??    \cf4 Type\cf0  t = targetResource.GetType();\par ??    t.GetProperty(propertyName).SetValue(\par ??        targetResource, propertyValue, \cf1 null\cf0 );\par ??\}}
--&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: courier new"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; SetValue(&lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt; targetResource, &lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; propertyName, &lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt; propertyValue)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;Type&lt;/SPAN&gt; t = targetResource.GetType();&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; t.GetProperty(propertyName).SetValue(&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; targetResource, propertyValue, &lt;SPAN style="COLOR: blue"&gt;null&lt;/SPAN&gt;);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;At this point, I found it easier to implement the method using Reflection than explicitly switching on both the targetResource and propertyName parameters.&lt;/P&gt;
&lt;P&gt;The last method that must be implemented to support the Create scenario is ResolveResource.&lt;/P&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;}??\fs20 \cf1 public\cf0  \cf1 object\cf0  ResolveResource(\cf1 object\cf0  resource)\par ??\{\par ??    \cf1 return\cf0  resource;\par ??\}}
--&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: courier new"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt; ResolveResource(&lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt; resource)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; resource;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;Since the Fake service is already working with in-memory objects, it can just return the object itself.&lt;/P&gt;
&lt;P&gt;With these methods implemented, updating an item is not quite as involved, since it reuses the SetValue and ResolveResource methods, so I only need to implement the GetResource method.&lt;/P&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;\red43\green145\blue175;}??\fs20 \cf1 public\cf0  \cf1 object\cf0  GetResource(\cf4 IQueryable\cf0  query,\par ??    \cf1 string\cf0  fullTypeName)\par ??\{\par ??    \cf1 return\cf0  query.Cast&amp;lt;\cf1 object\cf0 &amp;gt;().AsEnumerable().\par ??        FirstOrDefault();\par ??\}}
--&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: courier new"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt; GetResource(&lt;SPAN style="COLOR: #2b91af"&gt;IQueryable&lt;/SPAN&gt; query,&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; fullTypeName)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; query.Cast&amp;lt;&lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt;&amp;gt;().AsEnumerable().&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; FirstOrDefault();&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;This implementation requires a bit of explanation. The query argument that the method receives is actually an &lt;EM&gt;expression&lt;/EM&gt; over the service's exposed queries (i.e. Parents and Children). Since the service operates on in-memory objects, it doesn't need to transform the expression, but can simply evaluate it directly, which is what the AsEnumerable extension method does. However, &lt;A href="http://msdn.microsoft.com/en-us/library/system.linq.iqueryable.aspx" mce_href="http://msdn.microsoft.com/en-us/library/system.linq.iqueryable.aspx"&gt;IQueryable&lt;/A&gt; doesn't have an AsEnumerable extension method, whereas &lt;A href="http://msdn.microsoft.com/en-us/library/bb351562.aspx" mce_href="http://msdn.microsoft.com/en-us/library/bb351562.aspx"&gt;IQueryable&amp;lt;T&amp;gt;&lt;/A&gt; does - hence the cast to object.&lt;/P&gt;
&lt;P&gt;Deleting an item reuses GetResource and ResolveResource, so to support this scenario, the DeleteResource method is the only extra method that must be implemented.&lt;/P&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;\red43\green145\blue175;}??\fs20 \cf1 public\cf0  \cf1 void\cf0  DeleteResource(\cf1 object\cf0  targetResource)\par ??\{\par ??    ((\cf4 IList\cf0 )\cf1 this\cf0 .ParentStore).Remove(targetResource);\par ??    ((\cf4 IList\cf0 )\cf1 this\cf0 .ChildStore).Remove(targetResource);\par ??\}}
--&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: courier new"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; DeleteResource(&lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt; targetResource)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ((&lt;SPAN style="COLOR: #2b91af"&gt;IList&lt;/SPAN&gt;)&lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.ParentStore).Remove(targetResource);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ((&lt;SPAN style="COLOR: #2b91af"&gt;IList&lt;/SPAN&gt;)&lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.ChildStore).Remove(targetResource);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;Once again, I decided to keep things simple and simply hard-code knowledge of ParentStore and ChildStore into the method, but it would be easy to generalize this approach.&lt;/P&gt;
&lt;P&gt;The rest of the methods of IUpdatable address more advanced scenarios concerning references between items, and I'm not going to cover them in this post, since it's already becoming quite long. If you are interested, I've provided the entire sample code as an attachment to this post, so download and peruse it at your leisure (obviously, the usual disclaimers apply).&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Update:&lt;/STRONG&gt; I've now posted &lt;A class="" href="http://blogs.msdn.com/ploeh/archive/2009/01/20/a-general-purpose-fake-ado-net-data-service.aspx" mce_href="http://blogs.msdn.com/ploeh/archive/2009/01/20/a-general-purpose-fake-ado-net-data-service.aspx"&gt;a general-purpose Fake ADO.NET Data Service&lt;/A&gt; that should hopefully address most of your needs.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9339649" width="1" height="1"&gt;</description><enclosure url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-components-postattachments/00-09-33-96-49/DataServiceTesting.zip" length="47330" type="application/x-zip-compressed" /><category domain="http://blogs.msdn.com/b/ploeh/archive/tags/Unit+Testing/">Unit Testing</category><category domain="http://blogs.msdn.com/b/ploeh/archive/tags/Services/">Services</category></item><item><title>Unit Testing ADO.NET Data Service Clients</title><link>http://blogs.msdn.com/b/ploeh/archive/2009/01/14/unit-testing-ado-net-data-service-clients.aspx</link><pubDate>Wed, 14 Jan 2009 18:51:13 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9318879</guid><dc:creator>ploeh</dc:creator><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/ploeh/rsscomments.aspx?WeblogPostID=9318879</wfw:commentRss><comments>http://blogs.msdn.com/b/ploeh/archive/2009/01/14/unit-testing-ado-net-data-service-clients.aspx#comments</comments><description>&lt;p&gt;In my &lt;a href="http://blogs.msdn.com/ploeh/archive/2009/01/13/unit-testing-ado-net-data-services.aspx"&gt;previous post&lt;/a&gt;, I discussed unit testing ADO.NET Data Services and how you can host and test the service itself. In this post, I'll take a look at the opposite scenario: You have a client of an ADO.NET Data Service, and you want to unit test the client without relying on the real, production service.&lt;/p&gt; &lt;p&gt;In other words, you want to test the real client against a &lt;a href="http://xunitpatterns.com/Test%20Double.html"&gt;Test Double&lt;/a&gt; service. Presently, I'll show you how to create and host a Fake ADO.NET Data Service that the client code can invoke. Obviously, the client in this example uses the ADO.NET Data Services Client API, so I'll demonstrate how you can use that to invoke the fake service from within the same test.&lt;/p&gt; &lt;p&gt;Before we dive into the specifics of the implementation, here's a simple test that verifies that the client can retrieve all Parent instances from the service:&lt;/p&gt; &lt;p&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="346" alt="image" src="http://blogs.msdn.com/blogfiles/ploeh/WindowsLiveWriter/UnitTestingADO.NETDataServiceClients_A9EC/image_6.png" width="503" border="0"&gt; &lt;/p&gt; &lt;p&gt;As you can see from the figure, the test combines two logical tiers into one test: The service and the client.&lt;/p&gt; &lt;p&gt;The service tier is simply part of the test's &lt;a href="http://xunitpatterns.com/test%20fixture%20-%20xUnit.html"&gt;Fixture&lt;/a&gt;, and while I'm not concerned with testing it in this context, I still need to set it up. This part uses the ADO.NET Data Services API to define and host the service.&lt;/p&gt; &lt;p&gt;The client part uses the ADO.NET Data Services &lt;em&gt;Client&lt;/em&gt; API to query the service via its REST interface and verify the result.&lt;/p&gt; &lt;p&gt;Notice that I use an &lt;em&gt;instance&lt;/em&gt; of MyDataService to configure the host. While not strictly necessary in this test, when hosting a Fake service, it's desirable to have a reference to the fake instance itself, since that allows you to configure and query it through its &lt;a href="http://xunitpatterns.com/back%20door.html"&gt;Back Door&lt;/a&gt;. Here's a test that uses the MyDataService instance to verify that a Parent instance was deleted from the service:&lt;/p&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red43\green145\blue175;\red0\green0\blue255;\red0\green128\blue0;\red163\green21\blue21;}??\fs20 [\cf3 TestMethod\cf0 ]\par ??\cf4 public\cf0  \cf4 void\cf0  ClientCanDeleteInstance()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf3 Uri\cf0  address = \cf4 new\cf0  \cf3 Uri\cf0 (\cf6 "http://localhost/MyDataService"\cf0 );\par ??    \cf3 MyDataService\cf0  service = \cf4 new\cf0  \cf3 MyDataService\cf0 ();\par ??    \cf4 using\cf0  (\cf3 WebServiceHost\cf0  host = \par ??        \cf4 new\cf0  \cf3 WebServiceHost\cf0 (service, \cf4 new\cf0 [] \{ address \}))\par ??    \{\par ??        host.Open();\par ??\par ??        \cf3 MyDataServiceContext\cf0  ctx =\par ??            \cf4 new\cf0  \cf3 MyDataServiceContext\cf0 (address);\par ??        \cf4 var\cf0  victim = (\cf4 from\cf0  p \cf4 in\cf0  ctx.Parents\par ??                      \cf4 where\cf0  p.Id == 3\par ??                      \cf4 select\cf0  p).Single();\par ??        \cf5 // Exercise system\par ??\cf0         ctx.DeleteObject(victim);\par ??        ctx.SaveChanges();\par ??        \cf5 // Verify outcome\par ??\cf0         \cf3 Assert\cf0 .IsFalse(service.Data.Parents.Any(\par ??            p =&amp;gt; p.Id == victim.Id), \cf6 "Deleted"\cf0 );\par ??        \cf5 // Teardown\par ??\cf0     \}\par ??\}}
--&gt; &lt;div style="font-size: 10pt; background: white; color: black; font-family: courier new"&gt;&lt;pre style="margin: 0px"&gt;[&lt;span style="color: #2b91af"&gt;TestMethod&lt;/span&gt;]&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; ClientCanDeleteInstance()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;// Fixture setup&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Uri&lt;/span&gt; address = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Uri&lt;/span&gt;(&lt;span style="color: #a31515"&gt;"http://localhost/MyDataService"&lt;/span&gt;);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;MyDataService&lt;/span&gt; service = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MyDataService&lt;/span&gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;using&lt;/span&gt; (&lt;span style="color: #2b91af"&gt;WebServiceHost&lt;/span&gt; host = &lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;WebServiceHost&lt;/span&gt;(service, &lt;span style="color: blue"&gt;new&lt;/span&gt;[] { address }))&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; host.Open();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;MyDataServiceContext&lt;/span&gt; ctx =&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MyDataServiceContext&lt;/span&gt;(address);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;var&lt;/span&gt; victim = (&lt;span style="color: blue"&gt;from&lt;/span&gt; p &lt;span style="color: blue"&gt;in&lt;/span&gt; ctx.Parents&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;where&lt;/span&gt; p.Id == 3&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;select&lt;/span&gt; p).Single();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;// Exercise system&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ctx.DeleteObject(victim);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ctx.SaveChanges();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;// Verify outcome&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.IsFalse(service.Data.Parents.Any(&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; p =&amp;gt; p.Id == victim.Id), &lt;span style="color: #a31515"&gt;"Deleted"&lt;/span&gt;);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;// Teardown&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;As in the previous example, all &lt;a href="http://xunitpatterns.com/fixture%20setup.html"&gt;Fixture Setup&lt;/a&gt; before the declaration of the MyDataServiceContext instance is part of the logical service tier, while the remaining Fixture Setup code, as well as the execution of the &lt;a href="http://xunitpatterns.com/SUT.html"&gt;SUT&lt;/a&gt; is part of the logical client tier. However, notice that the verification now takes place on the logical service tier, since I'm using the fake service instance as a Back Door, instead of relying on the REST interface to query the service.&lt;/p&gt;
&lt;p&gt;Since &lt;a href="http://msdn.microsoft.com/en-us/library/system.data.services.dataservicehost.aspx"&gt;DataServiceHost&lt;/a&gt; doesn't include a constructor that takes a single instance as a parameter, I use its base class &lt;a href="http://msdn.microsoft.com/en-us/library/system.servicemodel.web.webservicehost.aspx"&gt;WebServiceHost&lt;/a&gt; instead. &lt;a href="http://www.red-gate.com/products/reflector/"&gt;Reflector&lt;/a&gt; shows that DataServiceHost really adds no behavior to its base class, so using WebServiceHost as a host should be safe - for now (let's hope it stays that way, or even better, that it gets a constructor overload that accepts a service instance).&lt;/p&gt;
&lt;p&gt;In normal cases, I consider it best practice to explicitly load data into the fake service as part of the Fixture Setup phase, but to keep these examples simple, I decided to implicitly load some test data as part of creating the MyDataService instance. That is why, in the above example, I can successfully retrieve a Parent instance with an Id of 3. Please be aware that this is not a strategy I endorse - I only did it to keep the example a bit less complex.&lt;/p&gt;
&lt;p&gt;Here's the MyDataService class in its entirety:&lt;/p&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red43\green145\blue175;\red0\green0\blue255;\red163\green21\blue21;}??\fs20 [\cf3 ServiceBehavior\cf0 (IncludeExceptionDetailInFaults = \cf4 true\cf0 , \par ??    InstanceContextMode = \cf3 InstanceContextMode\cf0 .Single)]\par ??\cf4 public\cf0  \cf4 class\cf0  \cf3 MyDataService\cf0  : \cf3 DataService\cf0 &amp;lt;\cf3 MyService\cf0 &amp;gt;\par ??\{\par ??    \cf4 private\cf0  \cf4 readonly\cf0  \cf3 MyService\cf0  service_;\par ??\par ??    \cf4 public\cf0  MyDataService()\par ??    \{\par ??        \cf4 this\cf0 .service_ = \cf4 new\cf0  \cf3 MyService\cf0 ();\par ??\par ??        \cf3 IList\cf0 &amp;lt;\cf3 Parent\cf0 &amp;gt; ps = \cf4 this\cf0 .service_.ParentStore;\par ??        ps.Add(\cf4 new\cf0  \cf3 Parent\cf0 () \{ Id = 1, Text = \cf5 "Ploeh"\cf0  \});\par ??        ps.Add(\cf4 new\cf0  \cf3 Parent\cf0 () \{ Id = 2, Text = \cf5 "Fnaah"\cf0  \});\par ??        ps.Add(\cf4 new\cf0  \cf3 Parent\cf0 () \{ Id = 3, Text = \cf5 "Nd\u248 ?h"\cf0  \});\par ??        ps.Add(\cf4 new\cf0  \cf3 Parent\cf0 () \{ Id = 4, Text = \cf5 "Foo"\cf0  \});\par ??        ps.Add(\cf4 new\cf0  \cf3 Parent\cf0 () \{ Id = 5, Text = \cf5 "Bar"\cf0  \});\par ??\par ??        \cf3 IList\cf0 &amp;lt;\cf3 Child\cf0 &amp;gt; cs = \cf4 this\cf0 .service_.ChildStore;\par ??        cs.Add(\cf4 new\cf0  \cf3 Child\cf0 () \{ Id = 1, Text = \cf5 "Child 1"\cf0  \});\par ??        cs.Add(\cf4 new\cf0  \cf3 Child\cf0 () \{ Id = 2, Text = \cf5 "Child 2"\cf0  \});\par ??        cs.Add(\cf4 new\cf0  \cf3 Child\cf0 () \{ Id = 3, Text = \cf5 "Child 3"\cf0  \});\par ??    \}\par ??\par ??    \cf4 public\cf0  \cf3 MyService\cf0  Data\par ??    \{\par ??        \cf4 get\cf0  \{ \cf4 return\cf0  \cf4 this\cf0 .service_; \}\par ??    \}\par ??\par ??    \cf4 public\cf0  \cf4 static\cf0  \cf4 void\cf0  InitializeService(\par ??        \cf3 IDataServiceConfiguration\cf0  config)\par ??    \{\par ??        config.SetEntitySetAccessRule(\cf5 "*"\cf0 ,\par ??            \cf3 EntitySetRights\cf0 .All);\par ??        config.UseVerboseErrors = \cf4 true\cf0 ;\par ??    \}\par ??\par ??    \cf4 protected\cf0  \cf4 override\cf0  \cf3 MyService\cf0  CreateDataSource()\par ??    \{\par ??        \cf4 return\cf0  \cf4 this\cf0 .service_;\par ??    \}\par ??\}}
--&gt;
&lt;div style="font-size: 10pt; background: white; color: black; font-family: courier new"&gt;&lt;pre style="margin: 0px"&gt;[&lt;span style="color: #2b91af"&gt;ServiceBehavior&lt;/span&gt;(IncludeExceptionDetailInFaults = &lt;span style="color: blue"&gt;true&lt;/span&gt;, &lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; InstanceContextMode = &lt;span style="color: #2b91af"&gt;InstanceContextMode&lt;/span&gt;.Single)]&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;class&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MyDataService&lt;/span&gt; : &lt;span style="color: #2b91af"&gt;DataService&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;MyService&lt;/span&gt;&amp;gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: blue"&gt;readonly&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MyService&lt;/span&gt; service_;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;public&lt;/span&gt; MyDataService()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;this&lt;/span&gt;.service_ = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MyService&lt;/span&gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;IList&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Parent&lt;/span&gt;&amp;gt; ps = &lt;span style="color: blue"&gt;this&lt;/span&gt;.service_.ParentStore;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ps.Add(&lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Parent&lt;/span&gt;() { Id = 1, Text = &lt;span style="color: #a31515"&gt;"Ploeh"&lt;/span&gt; });&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ps.Add(&lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Parent&lt;/span&gt;() { Id = 2, Text = &lt;span style="color: #a31515"&gt;"Fnaah"&lt;/span&gt; });&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ps.Add(&lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Parent&lt;/span&gt;() { Id = 3, Text = &lt;span style="color: #a31515"&gt;"Ndøh"&lt;/span&gt; });&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ps.Add(&lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Parent&lt;/span&gt;() { Id = 4, Text = &lt;span style="color: #a31515"&gt;"Foo"&lt;/span&gt; });&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ps.Add(&lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Parent&lt;/span&gt;() { Id = 5, Text = &lt;span style="color: #a31515"&gt;"Bar"&lt;/span&gt; });&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;IList&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Child&lt;/span&gt;&amp;gt; cs = &lt;span style="color: blue"&gt;this&lt;/span&gt;.service_.ChildStore;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cs.Add(&lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Child&lt;/span&gt;() { Id = 1, Text = &lt;span style="color: #a31515"&gt;"Child 1"&lt;/span&gt; });&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cs.Add(&lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Child&lt;/span&gt;() { Id = 2, Text = &lt;span style="color: #a31515"&gt;"Child 2"&lt;/span&gt; });&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cs.Add(&lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Child&lt;/span&gt;() { Id = 3, Text = &lt;span style="color: #a31515"&gt;"Child 3"&lt;/span&gt; });&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MyService&lt;/span&gt; Data&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;get&lt;/span&gt; { &lt;span style="color: blue"&gt;return&lt;/span&gt; &lt;span style="color: blue"&gt;this&lt;/span&gt;.service_; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;static&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; InitializeService(&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;IDataServiceConfiguration&lt;/span&gt; config)&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; config.SetEntitySetAccessRule(&lt;span style="color: #a31515"&gt;"*"&lt;/span&gt;,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;EntitySetRights&lt;/span&gt;.All);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; config.UseVerboseErrors = &lt;span style="color: blue"&gt;true&lt;/span&gt;;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;protected&lt;/span&gt; &lt;span style="color: blue"&gt;override&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MyService&lt;/span&gt; CreateDataSource()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;return&lt;/span&gt; &lt;span style="color: blue"&gt;this&lt;/span&gt;.service_;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;As I described, I use the constructor to set up some implicit test data. While I want to reiterate that this is not something I would recommend, I chose to include this code to make it clear what I was talking about.&lt;/p&gt;
&lt;p&gt;The service is decorated with the &lt;a href="http://msdn.microsoft.com/en-us/library/system.servicemodel.servicebehaviorattribute.aspx"&gt;ServiceBehavior&lt;/a&gt; attribute, which sets its &lt;a href="http://msdn.microsoft.com/en-us/library/system.servicemodel.servicebehaviorattribute.instancecontextmode.aspx"&gt;InstanceContextMode&lt;/a&gt; to &lt;a href="http://msdn.microsoft.com/en-us/library/system.servicemodel.instancecontextmode.aspx"&gt;Single&lt;/a&gt; to allow it to be hosted as a Singleton service by WebServiceHost. This, again, is what allows me to query the fake service's data via its Back Door.&lt;/p&gt;
&lt;p&gt;Since MyService simply stores data in memory, MyDataService needs to keep the same instance around for each request, so it overrides CreateDataSource to ensure that the same data container instance is always being referenced for the lifetime of the instance.&lt;/p&gt;
&lt;p&gt;In a later post, I will describe how to create the fake data service, MyService.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9318879" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/ploeh/archive/tags/Unit+Testing/">Unit Testing</category><category domain="http://blogs.msdn.com/b/ploeh/archive/tags/Services/">Services</category></item><item><title>Unit Testing ADO.NET Data Services</title><link>http://blogs.msdn.com/b/ploeh/archive/2009/01/13/unit-testing-ado-net-data-services.aspx</link><pubDate>Tue, 13 Jan 2009 15:05:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9314606</guid><dc:creator>ploeh</dc:creator><slash:comments>4</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/ploeh/rsscomments.aspx?WeblogPostID=9314606</wfw:commentRss><comments>http://blogs.msdn.com/b/ploeh/archive/2009/01/13/unit-testing-ado-net-data-services.aspx#comments</comments><description>&lt;P&gt;&lt;A href="http://msdn.microsoft.com/en-us/data/bb931106.aspx" mce_href="http://msdn.microsoft.com/en-us/data/bb931106.aspx"&gt;ADO.NET Data Services&lt;/A&gt; enables you to expose data (including, but not limited to, relational data) as REST services. Since it's built on top of WCF, it can be tested &lt;A href="http://blogs.msdn.com/ploeh/archive/2006/12/04/IntegrationTestingWcfServices.aspx" mce_href="http://blogs.msdn.com/ploeh/archive/2006/12/04/IntegrationTestingWcfServices.aspx"&gt;utilizing similar techniques&lt;/A&gt;, but allow me to elaborate a bit on the subject.&lt;/P&gt;
&lt;P&gt;As with WCF, you might be interested in testing one or both tiers:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;The service 
&lt;LI&gt;The client&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;Testing the service is the simplest undertaking, so I'll start with that and return to testing the client in a later post.&lt;/P&gt;
&lt;P&gt;Why would you want to unit test an ADO.NET Data Service?&lt;/P&gt;
&lt;P&gt;The mainstream scenario for ADO.NET Data Services is to expose a relational database using an ADO.NET Entity Model. Is it relevant to unit test such a service? Perhaps not. It depends on the amount of code you inject into the generated (partial) classes. If the amount of custom code is minimal, it may not make much sense to test simple CRUD operations.&lt;/P&gt;
&lt;P&gt;Conversely, if you have a greater degree of custom code, or if you implement an ADO.NET Data Service using non-relational data, it makes a lot of sense.&lt;/P&gt;
&lt;P&gt;My loyal readers will probably (correctly) suspect that I would tend to expose a Façade over a Domain Model as a data service, which implies going the non-relational route (even if the underlying data store is, in fact, relational). That is also the context for the rest of this post.&lt;/P&gt;
&lt;P&gt;First of all, you should keep in mind that the data service is just a normal class, so &lt;A href="http://blogs.msdn.com/ploeh/archive/2006/12/03/UnitTestingWCFServices.aspx" mce_href="http://blogs.msdn.com/ploeh/archive/2006/12/03/UnitTestingWCFServices.aspx"&gt;you can test much of its functionality by just creating it and start using it&lt;/A&gt;:&lt;/P&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red43\green145\blue175;\red0\green0\blue255;\red0\green128\blue0;\red163\green21\blue21;}??\fs20 [\cf3 TestMethod\cf0 ]\par ??\cf4 public\cf0  \cf4 void\cf0  ServiceCanReturnRequestedItem()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf4 int\cf0  anonymousId = 3;\par ??    \cf4 string\cf0  expectedText = \cf6 "Ploeh"\cf0 ;\par ??\par ??    \cf3 Parent\cf0  anonymousParent = \cf4 new\cf0  \cf3 Parent\cf0 ();\par ??    anonymousParent.Id = anonymousId;\par ??    anonymousParent.Text = expectedText;\par ??\par ??    \cf3 MyService\cf0  sut = \cf4 new\cf0  \cf3 MyService\cf0 ();\par ??    sut.ParentStore.Add(anonymousParent);\par ??    \cf5 // Exercise system\par ??\cf0     \cf3 Parent\cf0  result = (\cf4 from\cf0  p \cf4 in\cf0  sut.Parents\par ??                     \cf4 where\cf0  p.Id == anonymousId\par ??                     \cf4 select\cf0  p).Single();\par ??    \cf5 // Verify outcome\par ??\cf0     \cf3 Assert\cf0 .AreEqual&amp;lt;\cf4 string\cf0 &amp;gt;(expectedText, result.Text,\par ??        \cf6 "Text"\cf0 );\par ??    \cf5 // Teardown\par ??\cf0 \}}
--&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: courier new"&gt;&lt;PRE style="MARGIN: 0px"&gt;[&lt;SPAN style="COLOR: #2b91af"&gt;TestMethod&lt;/SPAN&gt;]&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; ServiceCanReturnRequestedItem()&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// Fixture setup&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt; anonymousId = 3;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; expectedText = &lt;SPAN style="COLOR: #a31515"&gt;"Ploeh"&lt;/SPAN&gt;;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;Parent&lt;/SPAN&gt; anonymousParent = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;Parent&lt;/SPAN&gt;();&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; anonymousParent.Id = anonymousId;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; anonymousParent.Text = expectedText;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;MyService&lt;/SPAN&gt; sut = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;MyService&lt;/SPAN&gt;();&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; sut.ParentStore.Add(anonymousParent);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// Exercise system&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;Parent&lt;/SPAN&gt; result = (&lt;SPAN style="COLOR: blue"&gt;from&lt;/SPAN&gt; p &lt;SPAN style="COLOR: blue"&gt;in&lt;/SPAN&gt; sut.Parents&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;where&lt;/SPAN&gt; p.Id == anonymousId&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;select&lt;/SPAN&gt; p).Single();&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// Verify outcome&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;Assert&lt;/SPAN&gt;.AreEqual&amp;lt;&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt;&amp;gt;(expectedText, result.Text,&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #a31515"&gt;"Text"&lt;/SPAN&gt;);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// Teardown&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;Notice how most of the test just contains code setting up the &lt;A href="http://xunitpatterns.com/test%20fixture%20-%20xUnit.html" mce_href="http://xunitpatterns.com/test%20fixture%20-%20xUnit.html"&gt;Fixture&lt;/A&gt; by adding the relevant data to the service's underlying data store, while execution and verification is quickly accomplished.&lt;/P&gt;
&lt;P&gt;At some point, however, you will probably want to test the service through its REST interface. To do this, you must host the service within the test and then invoke it.&lt;/P&gt;
&lt;P&gt;Hosting the service in a test is easy:&lt;/P&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red43\green145\blue175;\red0\green0\blue255;\red0\green128\blue0;\red163\green21\blue21;}??\fs20 [\cf3 TestMethod\cf0 ]\par ??\cf4 public\cf0  \cf4 void\cf0  TestCanHostService()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf3 Uri\cf0  address = \cf4 new\cf0  \cf3 Uri\cf0 (\cf6 "http://localhost/MyDataService"\cf0 );\par ??    \cf4 using\cf0  (\cf3 DataServiceHost\cf0  host = \par ??        \cf4 new\cf0  \cf3 DataServiceHost\cf0 (\cf4 typeof\cf0 (\cf3 MyDataService\cf0 ),\par ??            \cf4 new\cf0 [] \{ address \}))\par ??    \{\par ??        host.Open();\par ??        \cf5 // Exercise system\par ??\cf0         \cf5 // ...\par ??\cf0         \cf5 // Verify outcome\par ??\cf0         \cf5 // ...\par ??\cf0         \cf5 // Teardown\par ??\cf0     \}\par ??\}}
--&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: courier new"&gt;&lt;PRE style="MARGIN: 0px"&gt;[&lt;SPAN style="COLOR: #2b91af"&gt;TestMethod&lt;/SPAN&gt;]&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; TestCanHostService()&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// Fixture setup&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;Uri&lt;/SPAN&gt; address = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;Uri&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #a31515"&gt;"http://localhost/MyDataService"&lt;/SPAN&gt;);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;using&lt;/SPAN&gt; (&lt;SPAN style="COLOR: #2b91af"&gt;DataServiceHost&lt;/SPAN&gt; host = &lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;DataServiceHost&lt;/SPAN&gt;(&lt;SPAN style="COLOR: blue"&gt;typeof&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #2b91af"&gt;MyDataService&lt;/SPAN&gt;),&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt;[] { address }))&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; host.Open();&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// Exercise system&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// ...&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// Verify outcome&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// ...&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// Teardown&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;Since the service is exposed through a REST interface, you can use more than one technology to invoke it. The ADO.NET Data Services client API is a natural fit, and I'll cover this in a later post. For now, just imagine that the test above contains client code that invokes the service and verifies the result.&lt;/P&gt;
&lt;P&gt;As it turns out, unit testing an ADO.NET Data Service isn't particularly difficult. Unit testing ADO.NET Data Service Clients is a bit more involved, so I'll cover that topic in a separate post. &lt;STRONG&gt;Update:&lt;/STRONG&gt; This post is now available &lt;A class="" href="http://blogs.msdn.com/ploeh/archive/2009/01/14/unit-testing-ado-net-data-service-clients.aspx" mce_href="http://blogs.msdn.com/ploeh/archive/2009/01/14/unit-testing-ado-net-data-service-clients.aspx"&gt;here&lt;/A&gt;.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9314606" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/ploeh/archive/tags/Unit+Testing/">Unit Testing</category><category domain="http://blogs.msdn.com/b/ploeh/archive/tags/Services/">Services</category></item><item><title>Test-Driven Properties</title><link>http://blogs.msdn.com/b/ploeh/archive/2009/01/12/test-driven-properties.aspx</link><pubDate>Mon, 12 Jan 2009 11:26:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9307262</guid><dc:creator>ploeh</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/ploeh/rsscomments.aspx?WeblogPostID=9307262</wfw:commentRss><comments>http://blogs.msdn.com/b/ploeh/archive/2009/01/12/test-driven-properties.aspx#comments</comments><description>&lt;P&gt;&lt;A class="" href="http://blog.ploeh.dk/2009/01/28/ZeroFrictionTDD.aspx" mce_href="http://blog.ploeh.dk/2009/01/28/ZeroFrictionTDD.aspx"&gt;Zero-Friction TDD&lt;/A&gt; post #10:&lt;/P&gt;
&lt;P&gt;In principle, defining properties (or fields, for that matter) while TDD'ing should be no different than &lt;A href="http://blogs.msdn.com/ploeh/archive/2009/01/05/use-the-generate-method-stub-smart-tag-to-stay-in-the-zone.aspx" mce_href="http://blogs.msdn.com/ploeh/archive/2009/01/05/use-the-generate-method-stub-smart-tag-to-stay-in-the-zone.aspx"&gt;defining methods&lt;/A&gt;. Again, the important part is to stay focused on the test at hand, and not become bogged down by implementation details.&lt;/P&gt;
&lt;P&gt;Unfortunately, neither Visual Studio 2005 nor 2008 has a &lt;EM&gt;Generate property stub&lt;/EM&gt; smart tag, so until the day arrives when we get support for such in our IDE of choice (and for the record: I have no idea about when this is going to happen), we'll have to resort to more primitive means:&lt;/P&gt;
&lt;P&gt;Just reference the non-existing property and move on:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/ploeh/WindowsLiveWriter/TestDrivenProperties_1307E/UndefinedProperty_2.png" mce_href="http://blogs.msdn.com/blogfiles/ploeh/WindowsLiveWriter/TestDrivenProperties_1307E/UndefinedProperty_2.png"&gt;&lt;IMG style="BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-RIGHT-WIDTH: 0px" height=139 alt=UndefinedProperty src="http://blogs.msdn.com/blogfiles/ploeh/WindowsLiveWriter/TestDrivenProperties_1307E/UndefinedProperty_thumb.png" width=502 border=0 mce_src="http://blogs.msdn.com/blogfiles/ploeh/WindowsLiveWriter/TestDrivenProperties_1307E/UndefinedProperty_thumb.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;As you can see from the picture, my test code gets squiggly lines under the MyProperty property because this property doesn't exist (yet).&lt;/P&gt;
&lt;P&gt;Once more, the important point is to move on and resist the urge to fix this immediately. Don't even waste memory (your own, that is) on the fact that you need to implement this soon; just forget about it right away. The compiler will remind you in due time.&lt;/P&gt;
&lt;P&gt;This technique is a essentially what &lt;A href="http://blog.objectmentor.com/articles/category/michaels-musings" mce_href="http://blog.objectmentor.com/articles/category/michaels-musings"&gt;Michael Feathers&lt;/A&gt; calls &lt;EM&gt;leaning on the compiler&lt;/EM&gt;, although the context is different&lt;EM&gt;.&lt;/EM&gt;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9307262" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/ploeh/archive/tags/Unit+Testing/">Unit Testing</category><category domain="http://blogs.msdn.com/b/ploeh/archive/tags/Productivity/">Productivity</category></item><item><title>Use The Generate Method Stub Smart Tag To Stay In The Zone</title><link>http://blogs.msdn.com/b/ploeh/archive/2009/01/05/use-the-generate-method-stub-smart-tag-to-stay-in-the-zone.aspx</link><pubDate>Mon, 05 Jan 2009 11:53:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9278509</guid><dc:creator>ploeh</dc:creator><slash:comments>6</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/ploeh/rsscomments.aspx?WeblogPostID=9278509</wfw:commentRss><comments>http://blogs.msdn.com/b/ploeh/archive/2009/01/05/use-the-generate-method-stub-smart-tag-to-stay-in-the-zone.aspx#comments</comments><description>&lt;P&gt;Yet Another &lt;A class="" href="http://blog.ploeh.dk/2009/01/28/ZeroFrictionTDD.aspx" mce_href="http://blog.ploeh.dk/2009/01/28/ZeroFrictionTDD.aspx"&gt;Zero-Friction TDD&lt;/A&gt; Article (YAZFTA):&lt;/P&gt;
&lt;P&gt;When writing unit tests in the TDD fashion, it's important to stay in the zone and not get side-tracked by irrelevant issues. You need to avoid what I call mental context switching. Focus on writing the test and postpone all else to later, but make sure that you do it in such a way that you don't have to waste brainpower on remembering what you postponed.&lt;/P&gt;
&lt;P&gt;One easy way to do this is by using the &lt;EM&gt;Generate method stub&lt;/EM&gt; smart tag in Visual Studio:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/ploeh/WindowsLiveWriter/UseTheCreateMethodStubSmartTagToStayInTh_E74B/GenerateMethodStubSmartTag_2.png" mce_href="http://blogs.msdn.com/blogfiles/ploeh/WindowsLiveWriter/UseTheCreateMethodStubSmartTagToStayInTh_E74B/GenerateMethodStubSmartTag_2.png"&gt;&lt;IMG height=125 alt=GenerateMethodStubSmartTag src="http://blogs.msdn.com/blogfiles/ploeh/WindowsLiveWriter/UseTheCreateMethodStubSmartTagToStayInTh_E74B/GenerateMethodStubSmartTag_thumb.png" width=515 border=0 mce_src="http://blogs.msdn.com/blogfiles/ploeh/WindowsLiveWriter/UseTheCreateMethodStubSmartTagToStayInTh_E74B/GenerateMethodStubSmartTag_thumb.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;As we all know, this will generate the following implementation in MyClass:&lt;/P&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;\red43\green145\blue175;}??\fs20 \cf1 public\cf0  \cf1 void\cf0  MyMethod()\par ??\{\par ??    \cf1 throw\cf0  \cf1 new\cf0  \cf4 NotImplementedException\cf0 ();\par ??\}}
--&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: courier new"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; MyMethod()&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;throw&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;NotImplementedException&lt;/SPAN&gt;();&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;but since you already know that, you don't have to go look at it, and you don't even need to commit to memory that you will have to implement the method. When you run the test, it's going to throw a NotImplementedException, and that's all the reminder you need.&lt;/P&gt;
&lt;P&gt;So just use the smart tag and move on. Right away, forget about the method you just created. Resist the temptation to go implement it immediately, no matter how simple it might be. Stay focused on writing the test.&lt;/P&gt;
&lt;P&gt;You can use the same technique when it comes to evolving &lt;A href="http://xunitpatterns.com/Test%20Utility%20Method.html#SUT%20API%20Encapsulation" mce_href="http://xunitpatterns.com/Test%20Utility%20Method.html#SUT%20API%20Encapsulation"&gt;SUT API Encapsulation&lt;/A&gt;: Sometimes, I know that I'd really like a test helper method that can do something for me, but I have only a vague idea about how to implement it. In such cases, I just write the name of the method, use the smart tag and move on. This helps me stay focused on writing the test, and I can always come back later and implement the helper method at a more convenient time.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9278509" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/ploeh/archive/tags/Unit+Testing/">Unit Testing</category><category domain="http://blogs.msdn.com/b/ploeh/archive/tags/Productivity/">Productivity</category></item><item><title>Assert Messages Are Not Optional</title><link>http://blogs.msdn.com/b/ploeh/archive/2008/12/17/assert-messages-are-not-optional.aspx</link><pubDate>Wed, 17 Dec 2008 15:08:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9230759</guid><dc:creator>ploeh</dc:creator><slash:comments>4</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/ploeh/rsscomments.aspx?WeblogPostID=9230759</wfw:commentRss><comments>http://blogs.msdn.com/b/ploeh/archive/2008/12/17/assert-messages-are-not-optional.aspx#comments</comments><description>&lt;P&gt;...and now, in this week's episode of &lt;A class="" href="http://blog.ploeh.dk/2009/01/28/ZeroFrictionTDD.aspx" mce_href="http://blog.ploeh.dk/2009/01/28/ZeroFrictionTDD.aspx"&gt;Zero-Friction TDD&lt;/A&gt;: Optional Assert messages that aren't optional anyway!&lt;/P&gt;
&lt;P&gt;Actually, this piece of advice comes almost directly from the &lt;A href="http://xunitpatterns.com/" mce_href="http://xunitpatterns.com/"&gt;xUnit Test Patterns&lt;/A&gt; book, so I was in doubt whether I should post it all, but it bears repeating, and I guess I still have a few things to add.&lt;/P&gt;
&lt;P&gt;The various &lt;A href="http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.testtools.unittesting.assert.aspx" mce_href="http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.testtools.unittesting.assert.aspx"&gt;Assert&lt;/A&gt; methods all have an overload that takes a message string that is basically just echoed to the Test Results list if the test fails, appended to the assertion's more generic message.&lt;/P&gt;
&lt;P&gt;For example, this assertion&lt;/P&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red43\green145\blue175;\red255\green255\blue255;\red0\green0\blue0;\red0\green0\blue255;\red163\green21\blue21;}??\fs20 \cf1 Assert\cf0 .AreEqual&amp;lt;\cf4 string\cf0 &amp;gt;(expectedResult, result, \cf5 "DoStuff"\cf0 );}
--&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: courier new"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Assert&lt;/SPAN&gt;.AreEqual&amp;lt;&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt;&amp;gt;(expectedResult, result, &lt;SPAN style="COLOR: #a31515"&gt;"DoStuff"&lt;/SPAN&gt;);&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;will produce this output:&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;Assert.AreEqual failed. Expected:&amp;lt;ploeh&amp;gt;. Actual:&amp;lt;&amp;gt;. DoStuff&lt;/EM&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As there are also overloads available that does &lt;EM&gt;not&lt;/EM&gt; take such a message string, it is tempting to treat it as optional. As far as the compiler is concerned, I might as well have written this:&lt;/P&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red43\green145\blue175;\red255\green255\blue255;\red0\green0\blue0;\red0\green0\blue255;}??\fs20 \cf1 Assert\cf0 .AreEqual&amp;lt;\cf4 string\cf0 &amp;gt;(expectedResult, result);}
--&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: courier new"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Assert&lt;/SPAN&gt;.AreEqual&amp;lt;&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt;&amp;gt;(expectedResult, result);&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;which would have yielded this output:&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;Assert.AreEqual failed. Expected:&amp;lt;ploeh&amp;gt;. Actual:&amp;lt;&amp;gt;.&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;Not much of a difference, so it may seem quite redundant to supply this extra message.&lt;/P&gt;
&lt;P&gt;Well, not only will I argue that it provides value, I will even go so far as to insist that you should treat it as mandatory. Here's the reason:&lt;/P&gt;
&lt;P&gt;When one or more test failures appear in the Test Results window, my eyes naturally seek out the Error Message text, so it's very nice to have all the information I need present in the same context. All I really need is just a small hint that will jog my memory enough so that I know what I did wrong.&lt;/P&gt;
&lt;P&gt;Please note that I'm not really talking about using the message in the initial Red/Green/Refactor development cycle. In this phase, I rarely read the test failure text at all - I only register that there was a failure, which was expected, since the code isn't implemented yet.&lt;/P&gt;
&lt;P&gt;I'm talking about what happens when you refactor a code base with hundreds of tests. In such cases, I often inadvertently break existing tests, and my eyes immediately seek out the Error Message.&lt;/P&gt;
&lt;P&gt;Imagine that this is the error message:&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;Assert.AreEqual failed. Expected:&amp;lt;ploeh&amp;gt;. Actual:&amp;lt;&amp;gt;.&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;All this really tells me is that in one of my tests (and I don't know which one yet), something was expected to have the value &lt;EM&gt;ploeh&lt;/EM&gt;, but ended up having no value. That's not a lot to go after, so I will probably have to open the test and read it through to get enough context to know what I broke.&lt;/P&gt;
&lt;P&gt;On the other hand, I've experienced that just a &lt;EM&gt;little&lt;/EM&gt; extra message often provides &lt;EM&gt;just enough&lt;/EM&gt; context that I &lt;EM&gt;immediately&lt;/EM&gt; realize what I broke:&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;Assert.AreEqual failed. Expected:&amp;lt;ploeh&amp;gt;. Actual:&amp;lt;&amp;gt;. DoStuff&lt;/EM&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In this case, &lt;EM&gt;DoStuff&lt;/EM&gt; is the name of the method being tested. It may seem hard to believe, but in most cases, this is context enough to help my brain figuring out what went wrong. Keep in mind that the test failure was a result of a code change that &lt;EM&gt;I just implemented&lt;/EM&gt;, so my mind has a lot of context already, and it needs almost no help to make the final connection.&lt;/P&gt;
&lt;P&gt;When I first encountered this guidance, I was sceptic, but after having worked with it for some time, I'm not going back. It has increased my productivity while refactoring, because I don't need to investigate failure causes quite as often as I did before.&lt;/P&gt;
&lt;P&gt;Most developers dislike having to write this optional message because they feel that they ought to write a long piece of fluent prose. That's not the case: Just write a single cue that can trigger your brain at a later date. Normally, I just write the name of the method or property I'm testing - I find that I can easily do that and still stay in the zone when I'm writing my tests.&lt;/P&gt;
&lt;P&gt;You are probably still skeptical, but try it out for a couple of months and see what you think!&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9230759" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/ploeh/archive/tags/Unit+Testing/">Unit Testing</category><category domain="http://blogs.msdn.com/b/ploeh/archive/tags/Productivity/">Productivity</category></item><item><title>First Dynamics Mobile Post</title><link>http://blogs.msdn.com/b/ploeh/archive/2008/12/12/first-dynamics-mobile-post.aspx</link><pubDate>Fri, 12 Dec 2008 18:00:51 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9202399</guid><dc:creator>ploeh</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/ploeh/rsscomments.aspx?WeblogPostID=9202399</wfw:commentRss><comments>http://blogs.msdn.com/b/ploeh/archive/2008/12/12/first-dynamics-mobile-post.aspx#comments</comments><description>&lt;p&gt;Yesterday, I posted my &lt;a href="http://blogs.msdn.com/dynamicsmobile/archive/2008/12/11/documenthandler-behind-the-scenes.aspx"&gt;first post&lt;/a&gt; over on the &lt;a href="http://blogs.msdn.com/dynamicsmobile/default.aspx"&gt;Microsoft Dynamics Mobile Team blog&lt;/a&gt;. More are likely to follow.&lt;/p&gt; &lt;p&gt;In case you are wondering what this means for &lt;em&gt;this&lt;/em&gt; blog, the answer is: Nothing. I plan to continue blogging in more or less the same vein as I've done for the last three years now.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9202399" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/b/ploeh/archive/tags/Miscellaneous/">Miscellaneous</category></item></channel></rss>