<?xml-stylesheet type='text/xsl' href='http://blogs.u2u.be/rss.xsl' media='screen'?><rss xmlns:a10="http://www.w3.org/2005/Atom" version="2.0"><channel><title>U2U all posts</title><description /><item><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7603534</guid><link>http://weblogs.asp.net/jan/archive/2010/09/02/deploying-and-using-jquery-with-a-sharepoint-2010-sandboxed-solution.aspx</link><category domain="http://weblogs.asp.net/jan/archive/tags/SharePoint/default.aspx">SharePoint</category><title>Deploying and Using jQuery with a SharePoint 2010 Sandboxed Solution</title><description>&lt;P&gt;If you’re an avid reader of this blog, you probably now that I’m quite a big fan of the &lt;A href="http://jquery.com/" mce_href="http://jquery.com"&gt;jQuery Javascript library&lt;/A&gt;, especially in combination with SharePoint sites. For a while I’ve been trying to find a mechanism to deploy jQuery and add a reference to it on all SharePoint pages of a site, by just using a Sandboxed Solution. The first part, deploying the jQuery file, is really easy; this can be done in a &lt;STRONG&gt;Module&lt;/STRONG&gt; in a SharePoint project. The second part was more of a challenge; making sure the jQuery file got referenced in all pages of a SharePoint site. My initial thought was to do this by using the AdditionalPageHead Delegate control, but it’s not possible in a Sandboxed Solution (you need to deploy the .ASCX to the SharePoint root). My second thought was to use the new ScriptSrc attribute you can use in a CustomAction (&lt;A href="http://weblogs.asp.net/jan/archive/2010/03/01/scriptsrc-referencing-javascript-files-with-sharepoint-2010-custom-actions.aspx" mce_href="http://weblogs.asp.net/jan/archive/2010/03/01/scriptsrc-referencing-javascript-files-with-sharepoint-2010-custom-actions.aspx"&gt;I wrote about this attribute some time ago&lt;/A&gt;). But initially I was under the impression that the ScriptSrc attribute could only target Javascript files loaded from the SharePoint Root (the \LAYOUTS) folder, resulting in a no-go for a Sandboxed Solution as well. Daniel McPherson stumbled upon this issue as well, but &lt;A href="http://community.zevenseas.com/Blogs/Daniel/archive/2010/07/06/how-to-bootstrap-jquery-on-every-sharepoint-page-even-in-the-sandbox.aspx" mce_href="http://community.zevenseas.com/Blogs/Daniel/archive/2010/07/06/how-to-bootstrap-jquery-on-every-sharepoint-page-even-in-the-sandbox.aspx"&gt;neatly solved it using Javascript to add the reference dynamically&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;But apparently you can make use of the ScriptSrc attribute to reference files from the Virtual File System (e.g. a Document Library in a site) as well! (thank you &lt;A href="http://twitter.com/fdobon" mce_href="http://twitter.com/fdobon"&gt;Frederic Dobon&lt;/A&gt; for mentioning this!). The trick is to use the &lt;STRONG&gt;~SiteCollection&lt;/STRONG&gt; prefix in the ScriptSrc attribute, for example:&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;lt;Elements xmlns="http://schemas.microsoft.com/sharepoint/"&amp;gt;&lt;BR&gt;&amp;nbsp; &amp;lt;CustomAction&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ScriptSrc="&lt;STRONG&gt;&lt;FONT color=#ff0000&gt;~SiteCollection&lt;/FONT&gt;&lt;/STRONG&gt;/SiteAssets/jquery-1.4.2.min.js"&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Location="ScriptLink"&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Sequence="10"&amp;gt;&lt;BR&gt;&amp;nbsp; &amp;lt;/CustomAction&amp;gt;&lt;BR&gt;&amp;lt;/Elements&amp;gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;It’s that easy! For your convenience I’ve uploaded &lt;A href="http://smarttools2010.codeplex.com/releases/view/51710" mce_href="http://smarttools2010.codeplex.com/releases/view/51710"&gt;the source code and the resulting SharePoint Solution (WSP file)&lt;/A&gt; to CodePlex.&lt;/P&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7603534" width="1" height="1"&gt;</description><pubDate>Thu, 02 Sep 2010 14:20:00 Z</pubDate><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Jan Tielens</dc:creator><slash:comments xmlns:slash="http://purl.org/rss/1.0/modules/slash/">0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/jan/rsscomments.aspx?PostID=7603534</wfw:commentRss><comments>http://weblogs.asp.net/jan/archive/2010/09/02/deploying-and-using-jquery-with-a-sharepoint-2010-sandboxed-solution.aspx#comments</comments></item><item><guid isPermaLink="false">http://blogs.u2u.be/diederik/post.aspx?id=359658f3-355b-47e6-a18d-fd204065f7d1</guid><link>http://blogs.u2u.be/diederik/post/2010/08/27/Get-your-WCF-client-configuration-from-anywhere.aspx</link><author>Diederik Krols</author><title>Get your WCF client configuration from anywhere</title><description>&lt;p&gt;An application that consumes one or more WCF services needs to provide the address, binding protocol, and contract (ABC) of each end point in its &lt;em&gt;app.config&lt;/em&gt; file. This article shows an easy way to let the application get its WCF client configuration data from anywhere: a flat file, a database, even pigeon post if you like. The approach leverages the native XML configuration schema as well as the proxy generated by Visual Studio.NET. The following is a screenshot from the attached solutin, showing a WPF client that calls the canonical &lt;em&gt;Hello World&lt;/em&gt; service synchronously and asynchronously, but using an end point configuration from a local string variable:&lt;/p&gt;
&lt;p&gt;&lt;img src="http://blogs.u2u.be/diederik/image.axd?picture=2010%2f8%2fWCFConfig.png" alt="" /&gt;&lt;/p&gt;
&lt;h2&gt;Rolling your own WCF custom client&lt;/h2&gt;
&lt;p&gt;Basically there are two approaches if you want to build a custom WCF client:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;create your own proxy entirely from scratch , or&lt;/li&gt;
&lt;li&gt;extend an existing proxy with a custom channel factory.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Both of these require extensive coding. In this article, I elaborate on the latter approach: I'll build a custom channel factory -derived from &lt;em&gt;ChannelFactory&amp;lt;T&amp;gt;&lt;/em&gt;- that reads its WCF client configuration from a custom configuration file. Then I plug this channel factory into the proxy that was generated by Visual Studio.NET.&lt;/p&gt;
&lt;p&gt;Oh wait, in the title of this article I mentioned that the configuration data could come from anywhere - not just a file. As it stands, we always need to persist the configuration data in a physical file. Some of the non-virtual members and constructors of the classes in the &lt;em&gt;ChannelFactory&amp;lt;T&amp;gt;&lt;/em&gt; hierarchy use the &lt;em&gt;System.Configuration&lt;/em&gt; namespace to parse the address, binding, and contract information. The classes in this namespace are specialized in reading configuration sections from physical XML configuration files &amp;agrave; la app.config. These classes require a file path to do their work. So there's no way around a config file - at least a temporary one.&lt;/p&gt;
&lt;h2&gt;Creating a custom channel factory&lt;/h2&gt;
&lt;p&gt;When you create a custom channel factory, you inherit from ChannelFactory&amp;lt;T&amp;gt; and then override one of the following methods:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;em&gt;CreateDescription&lt;/em&gt;, or&lt;/li&gt;
&lt;li&gt;&lt;em&gt;ApplyConfiguration&lt;/em&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The attached solution at the end of this article uses the latter approach: it overrides &lt;em&gt;ApplyConfiguration&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;&lt;img src="http://blogs.u2u.be/diederik/image.axd?picture=2010%2f8%2fCustomChannelFactory.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;The code is borrowed from &lt;a href="http://social.msdn.microsoft.com/forums/en-US/wcf/thread/f33e620a-e332-4fd4-ae21-88c750437355/"&gt;this msdn forum article&lt;/a&gt;. It reads the configuration from an arbitrary config file using the very same &lt;em&gt;System.Configuration&lt;/em&gt; classes. This is convenient: we can copy/paste the -complex- config file from the project holding the service reference!&lt;/p&gt;
&lt;h2&gt;Reusing the &lt;em&gt;svcutil&lt;/em&gt; proxy&lt;/h2&gt;
&lt;p&gt;It's perfectly possible to use a custom channel factory and still leverage all the code from the proxy that was generated by Visual Studio.NET through &lt;em&gt;svcutil&lt;/em&gt;, e.g. the asynchronous calls and the fault classes. All you need to do is create a child class from the proxy, and override its &lt;em&gt;CreateChannel&lt;/em&gt; method.&lt;/p&gt;
&lt;p&gt;Here's the architecture:&lt;/p&gt;
&lt;p&gt;&lt;img src="http://blogs.u2u.be/diederik/image.axd?picture=2010%2f8%2fclassdiagram.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Here's the &lt;em&gt;CreateChannel&lt;/em&gt; method:&lt;/p&gt;
&lt;div style="color: black; background: azure; font-family: Courier New; font-size: 9pt;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: gray;"&gt;///&lt;/span&gt;&lt;span style="color: green;"&gt; &lt;/span&gt;&lt;span style="color: gray;"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: gray;"&gt;///&lt;/span&gt;&lt;span style="color: green;"&gt; Creates a channel that implements the service interface.&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: gray;"&gt;///&lt;/span&gt;&lt;span style="color: green;"&gt; &lt;/span&gt;&lt;span style="color: gray;"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: gray;"&gt;///&lt;/span&gt;&lt;span style="color: green;"&gt; &lt;/span&gt;&lt;span style="color: gray;"&gt;&amp;lt;returns&amp;gt;&lt;/span&gt;&lt;span style="color: green;"&gt;A channel from the end point in the custom configuration file.&lt;/span&gt;&lt;span style="color: gray;"&gt;&amp;lt;/returns&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&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;IDummyService&lt;/span&gt; CreateChannel()&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af;"&gt;CustomChannelFactory&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af;"&gt;IDummyService&lt;/span&gt;&amp;gt; factory =&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&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;CustomChannelFactory&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af;"&gt;IDummyService&lt;/span&gt;&amp;gt;(&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #a31515;"&gt;"IDummyServiceBinding"&lt;/span&gt;, &lt;span style="color: green;"&gt;/* or use "*" for the first end point */&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&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;.configurationFilePath);&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;return&lt;/span&gt; factory.CreateChannel();&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;At runtime, the first thing to do is fetching the configuration information from the store of your choice. The attached solution just uses a string variable:&lt;/p&gt;
&lt;div style="color: black; background: azure; font-family: Courier New; font-size: 9pt;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: blue;"&gt;string&lt;/span&gt; endpointConfiguration =&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #a31515;"&gt;@"&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: #a31515;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;configuration&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: #a31515;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;system.serviceModel&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: #a31515;"&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;lt;bindings&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: #a31515;"&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;lt;basicHttpBinding&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: #a31515;"&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;lt;binding name='BasicHttpBinding_IDummyService' closeTimeout='00:01:00'&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: #a31515;"&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; openTimeout='00:01:00' receiveTimeout='00:10:00' sendTimeout='00:01:00'&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: #a31515;"&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; allowCookies='false' bypassProxyOnLocal='false' hostNameComparisonMode='StrongWildcard'&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: #a31515;"&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; maxBufferSize='65536' maxBufferPoolSize='524288' maxReceivedMessageSize='65536'&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: #a31515;"&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; messageEncoding='Text' textEncoding='utf-8' transferMode='Buffered'&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: #a31515;"&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; useDefaultWebProxy='true'&amp;gt; etc.etc. etc.&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Then we need to save this information in a file, preferably in a fast and safe place. The ideal candidate is of course &lt;em&gt;Isolated storage&lt;/em&gt;.&lt;/p&gt;
&lt;h2&gt;Storing configuration files in isolated storage file&lt;/h2&gt;
&lt;p&gt;The following method saves the configuration information in isolated storage and returns the physical path to that file through a small hack (Reflection):&lt;/p&gt;
&lt;div style="color: black; background: azure; font-family: Courier New; font-size: 9pt;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: gray;"&gt;///&lt;/span&gt;&lt;span style="color: green;"&gt; &lt;/span&gt;&lt;span style="color: gray;"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: gray;"&gt;///&lt;/span&gt;&lt;span style="color: green;"&gt; Saves the configuration information in a file in isolated storage, and returns the path.&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: gray;"&gt;///&lt;/span&gt;&lt;span style="color: green;"&gt; &lt;/span&gt;&lt;span style="color: gray;"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: gray;"&gt;///&lt;/span&gt;&lt;span style="color: green;"&gt; &lt;/span&gt;&lt;span style="color: gray;"&gt;&amp;lt;param name="configuration"&amp;gt;&lt;/span&gt;&lt;span style="color: green;"&gt;XML configuration elements.&lt;/span&gt;&lt;span style="color: gray;"&gt;&amp;lt;/param&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: gray;"&gt;///&lt;/span&gt;&lt;span style="color: green;"&gt; &lt;/span&gt;&lt;span style="color: gray;"&gt;&amp;lt;param name="fileName"&amp;gt;&lt;/span&gt;&lt;span style="color: green;"&gt;Configuration file name&lt;/span&gt;&lt;span style="color: gray;"&gt;&amp;lt;/param&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: gray;"&gt;///&lt;/span&gt;&lt;span style="color: green;"&gt; &lt;/span&gt;&lt;span style="color: gray;"&gt;&amp;lt;returns&amp;gt;&lt;/span&gt;&lt;span style="color: green;"&gt;Full path name of the configuration file.&lt;/span&gt;&lt;span style="color: gray;"&gt;&amp;lt;/returns&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: blue;"&gt;string&lt;/span&gt; GetConfigurationFilePath(&lt;span style="color: blue;"&gt;string&lt;/span&gt; configuration, &lt;span style="color: blue;"&gt;string&lt;/span&gt; fileName)&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;// Create a file in isolated storage. &lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af;"&gt;IsolatedStorageFile&lt;/span&gt; store = &lt;span style="color: #2b91af;"&gt;IsolatedStorageFile&lt;/span&gt;.GetStore(&lt;span style="color: #2b91af;"&gt;IsolatedStorageScope&lt;/span&gt;.User | &lt;span style="color: #2b91af;"&gt;IsolatedStorageScope&lt;/span&gt;.Assembly, &lt;span style="color: blue;"&gt;null&lt;/span&gt;, &lt;span style="color: blue;"&gt;null&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af;"&gt;IsolatedStorageFileStream&lt;/span&gt; stream = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;IsolatedStorageFileStream&lt;/span&gt;(fileName, &lt;span style="color: #2b91af;"&gt;FileMode&lt;/span&gt;.Create, store);&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af;"&gt;StreamWriter&lt;/span&gt; writer = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;StreamWriter&lt;/span&gt;(stream);&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; writer.WriteLine(configuration);&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; writer.Close();&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; stream.Close();&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;// Retrieve the actual path of the file (using Reflection). &lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;string&lt;/span&gt; proxyConfigurationPath = stream.GetType().GetField(&lt;span style="color: #a31515;"&gt;"m_FullPath"&lt;/span&gt;, &lt;span style="color: #2b91af;"&gt;BindingFlags&lt;/span&gt;.Instance | &lt;span style="color: #2b91af;"&gt;BindingFlags&lt;/span&gt;.NonPublic).GetValue(stream).ToString();&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;return&lt;/span&gt; proxyConfigurationPath;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;That went easy, so where's the catch ?&lt;/h2&gt;
&lt;p&gt;This solution comes with a small price: the &lt;em&gt;app.config&lt;/em&gt; still needs an end point definition. This is because the constructor of &lt;em&gt;ClientBase&amp;lt;T&amp;gt;&lt;/em&gt; -the base class of the Visual Studio proxy- parses the config file for a syntactically valid end point: there must be an address (a fake URI will do), a binding protocol (any type will do), and a full contract definition. This end point is never used however, because our child class overrides the creation of the channel. So we can get away with a dummy end point in the standard configuration file - only the contract needs to match:&lt;/p&gt;
&lt;div style="color: black; background: azure; font-family: Courier New; font-size: 9pt;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;configuration&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;&amp;nbsp; &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;system.serviceModel&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;&amp;nbsp; &amp;nbsp; &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;client&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;!--&lt;/span&gt;&lt;span style="color: green;"&gt; Look mom: no real configuration. &lt;/span&gt;&lt;span style="color: blue;"&gt;--&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;endpoint&lt;/span&gt;&lt;span style="color: blue;"&gt; &lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style="color: red;"&gt;address&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;"&lt;span style="color: blue;"&gt;http://dummyAddress&lt;/span&gt;"&lt;span style="color: blue;"&gt; &lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style="color: red;"&gt;binding&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;"&lt;span style="color: blue;"&gt;netMsmqBinding&lt;/span&gt;"&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style="color: red;"&gt;contract&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;"&lt;span style="color: blue;"&gt;ServiceReference.IDummyService&lt;/span&gt;"&lt;span style="color: blue;"&gt; /&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;&amp;nbsp; &amp;nbsp; &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;client&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;&amp;nbsp; &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;system.serviceModel&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;configuration&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;Source code&lt;/h2&gt;
&lt;p&gt;Here's the sample solution: &lt;a href="http://blogs.u2u.be/diederik/file.axd?file=2010%2f8%2fU2UConsult.WCF.Configuration.Sample.zip"&gt;U2UConsult.WCF.Configuration.Sample.zip (98,53 kb)&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Enjoy!&lt;/p&gt;</description><pubDate>Fri, 27 Aug 2010 07:03:00 -1200</pubDate><comments>http://blogs.u2u.be/diederik/post/2010/08/27/Get-your-WCF-client-configuration-from-anywhere.aspx#comment</comments><dc:publisher xmlns:dc="http://purl.org/dc/elements/1.1/">Diederik Krols</dc:publisher><pingback:server xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/">http://blogs.u2u.be/diederik/pingback.axd</pingback:server><pingback:target xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/">http://blogs.u2u.be/diederik/post.aspx?id=359658f3-355b-47e6-a18d-fd204065f7d1</pingback:target><slash:comments xmlns:slash="http://purl.org/rss/1.0/modules/slash/">0</slash:comments><trackback:ping xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">http://blogs.u2u.be/diederik/trackback.axd?id=359658f3-355b-47e6-a18d-fd204065f7d1</trackback:ping><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.u2u.be/diederik/post/2010/08/27/Get-your-WCF-client-configuration-from-anywhere.aspx#comment</wfw:comment><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.u2u.be/diederik/syndication.axd?post=359658f3-355b-47e6-a18d-fd204065f7d1</wfw:commentRss></item><item><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7589842</guid><link>http://weblogs.asp.net/jan/archive/2010/08/12/quot-object-reference-not-set-to-an-instance-of-an-object-quot-when-using-sharepoint-s-clientcontext-current-in-silverlight.aspx</link><category domain="http://weblogs.asp.net/jan/archive/tags/SharePoint/default.aspx">SharePoint</category><title>"Object reference not set to an instance of an object" when using SharePoint's ClientContext.Current in Silverlight</title><description>&lt;P mce_keep="true"&gt;SharePoint 2010 and Silverlight is a fantastic combination, especially when you use the Client Object Model for Silverlight to access SharePoint data. But maybe you’ve encountered the following situation: your Silverlight application works great when it’s hosted in the out-of-the-box Silverlight Web Part, but when you build your own custom Web Part to show the Silverlight Application it just doesn’t work anymore: the browser tells you there is an &lt;STRONG&gt;Error on page&lt;/STRONG&gt;. The details of the error are: &lt;STRONG&gt;Unhandled Error in Silverlight Application Object reference not set to an instance of an object&lt;/STRONG&gt;.&lt;/P&gt;
&lt;P mce_keep="true"&gt;&lt;IMG src="http://storage.tielens.name/slObjectreferencenotsettoaninstanceofanobject.jpg" mce_src="http://storage.tielens.name/slObjectreferencenotsettoaninstanceofanobject.jpg"&gt;&lt;/P&gt;
&lt;P mce_keep="true"&gt;When you debug your Silverlight code, you’ll notice that the issue is that the &lt;STRONG&gt;ClientContext.Current &lt;/STRONG&gt;is a &lt;STRONG&gt;Null Reference&lt;/STRONG&gt;. To solution for this problem is that fact that if you want to use the Current ClientContext you need to add the &lt;STRONG&gt;MS.SP.url&lt;/STRONG&gt; parameter to the initParams in the HTML object that loads the XAP. This &lt;STRONG&gt;MS.SP.url&lt;/STRONG&gt; parameter needs to contain the URL of the site in which the Silverlight Application is currently being displayed. The code of your Silverlight Web Part could look as follows:&lt;/P&gt;
&lt;P mce_keep="true"&gt;&lt;FONT face="courier new,courier"&gt;public class SLWebPartDemo : WebPart&lt;BR&gt;{&lt;BR&gt;&amp;nbsp; protected override void RenderContents(HtmlTextWriter writer)&lt;BR&gt;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; writer.Write(string.Format(&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; @"&amp;lt;object data='data:application/x-silverlight-2,' type='application/x-silverlight-2' width='100%' height='100%'&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;param name='source' value='{1}XAPs/SLWebPartDemoControl.xap'/&amp;gt;&lt;BR&gt;&lt;FONT color=#cc0000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;param name='initParams' value='MS.SP.url={0}/{1}'/&amp;gt;&lt;BR&gt;&lt;/FONT&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;param name='onError' value='onSilverlightError' /&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;param name='background' value='white' /&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;param name='minRuntimeVersion' value='3.0.40818.0' /&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;param name='autoUpgrade' value='true' /&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;a href='http://go.microsoft.com/fwlink/?LinkID=149156&amp;amp;v=3.0.40818.0' style='text-decoration:none'&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;img src='http://go.microsoft.com/fwlink/?LinkId=161376' alt='Get Microsoft Silverlight' style='border-style:none'/&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/a&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/object&amp;gt;", &lt;FONT color=#cc0000&gt;SPContext.Current.Site.Url, &lt;BR&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; SPContext.Current.Web.ServerRelativeUrl));&lt;BR&gt;&lt;/FONT&gt;&amp;nbsp; }&lt;BR&gt;}&lt;/FONT&gt;&lt;BR&gt;&lt;/P&gt;&lt;A class=twitter-share-button href="http://twitter.com/share" data-via="jantielens" data-count="horizontal"&gt;Tweet&lt;/A&gt;
&lt;SCRIPT type=text/javascript src="http://platform.twitter.com/widgets.js"&gt;&lt;/SCRIPT&gt;
 &lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7589842" width="1" height="1"&gt;</description><pubDate>Thu, 12 Aug 2010 14:35:00 Z</pubDate><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Jan Tielens</dc:creator><slash:comments xmlns:slash="http://purl.org/rss/1.0/modules/slash/">0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/jan/rsscomments.aspx?PostID=7589842</wfw:commentRss><comments>http://weblogs.asp.net/jan/archive/2010/08/12/quot-object-reference-not-set-to-an-instance-of-an-object-quot-when-using-sharepoint-s-clientcontext-current-in-silverlight.aspx#comments</comments></item><item><guid isPermaLink="false">http://blogs.u2u.be/peter/post.aspx?id=12aeedaa-6e13-4132-b67c-75b3c698a439</guid><link>http://blogs.u2u.be/peter/post/2010/08/09/Nog-een-beetje-tijd-over-deze-(natte)-zomer.aspx</link><author>Peter Himschoot</author><title>Nog een beetje tijd over deze (natte) zomer?</title><description>&lt;p&gt;&lt;a href="http://blogs.u2u.be/peter/image.axd?picture=msdnsummer.jpg"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="msdnsummer" border="0" alt="msdnsummer" src="http://blogs.u2u.be/peter/image.axd?picture=msdnsummer_thumb.jpg" width="244" height="107" /&gt;&lt;/a&gt; Duik erin met MSDN. &lt;/p&gt;  &lt;p&gt;Microsoft Belgie heeft een aantal &lt;a href="http://msdn.microsoft.com/nl-be/ff872142.aspx"&gt;zelf-trainingen&lt;/a&gt; staan om je up-to-speed te krijgen met:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Silverlight 4 &lt;/li&gt;    &lt;li&gt;Visual Studio 2010 &lt;/li&gt;    &lt;li&gt;Windows Phone 7 &lt;/li&gt;    &lt;li&gt;Windows Azure &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;De laatste komt binnenkort online.&lt;/p&gt;</description><pubDate>Mon, 09 Aug 2010 13:38:48 -1200</pubDate><comments>http://blogs.u2u.be/peter/post/2010/08/09/Nog-een-beetje-tijd-over-deze-(natte)-zomer.aspx#comment</comments><dc:publisher xmlns:dc="http://purl.org/dc/elements/1.1/">Peter Himschoot</dc:publisher><pingback:server xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/">http://blogs.u2u.be/peter/pingback.axd</pingback:server><pingback:target xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/">http://blogs.u2u.be/peter/post.aspx?id=12aeedaa-6e13-4132-b67c-75b3c698a439</pingback:target><slash:comments xmlns:slash="http://purl.org/rss/1.0/modules/slash/">0</slash:comments><trackback:ping xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">http://blogs.u2u.be/peter/trackback.axd?id=12aeedaa-6e13-4132-b67c-75b3c698a439</trackback:ping><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.u2u.be/peter/post/2010/08/09/Nog-een-beetje-tijd-over-deze-(natte)-zomer.aspx#comment</wfw:comment><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.u2u.be/peter/syndication.axd?post=12aeedaa-6e13-4132-b67c-75b3c698a439</wfw:commentRss></item><item><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7582656</guid><link>http://weblogs.asp.net/jan/archive/2010/08/08/upgrading-features-in-sharepoint-2010.aspx</link><category domain="http://weblogs.asp.net/jan/archive/tags/SharePoint/default.aspx">SharePoint</category><title>Upgrading Features in SharePoint 2010</title><description>&lt;P mce_keep="true"&gt;If you have developed Features for SharePoint 2007, you’re probably aware of a very serious limitation of Feature Development: upgradability. Deploying version 1 of your customization is no problem, but If you want to deploy a bug fix or additional functionality in version 2, you’re basically on your own. The good news is that in SharePoint 2010, this problem is solved: as a developer you can now build features that are upgradable. The basic concept goes as follows, you built your feature as usual, and optionally you’ve got the opportunity to specify what should happen when the feature gets upgraded from a previous version.&lt;/P&gt;
&lt;P mce_keep="true"&gt;&lt;A href="http://www.sharepointnutsandbolts.com/" mce_href="http://www.sharepointnutsandbolts.com/"&gt;Chris O’Brian&lt;/A&gt; has written an excellent series of articles about this topic, so go to his blog to read the details:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;
&lt;DIV mce_keep="true"&gt;&lt;A href="http://www.sharepointnutsandbolts.com/2010/06/feature-upgrade-part-1-fundamentals.html" mce_href="http://www.sharepointnutsandbolts.com/2010/06/feature-upgrade-part-1-fundamentals.html"&gt;Feature upgrade (part 1) – fundamentals&lt;/A&gt;&lt;/DIV&gt;&lt;/LI&gt;
&lt;LI&gt;
&lt;DIV mce_keep="true"&gt;&lt;A href="http://www.sharepointnutsandbolts.com/2010/07/feature-upgrade-part-2-sample-to-play.html" mce_href="http://www.sharepointnutsandbolts.com/2010/07/feature-upgrade-part-2-sample-to-play.html"&gt;Feature upgrade (part 2) – a sample to play with &lt;/A&gt;&lt;/DIV&gt;&lt;/LI&gt;
&lt;LI&gt;
&lt;DIV mce_keep="true"&gt;&lt;A href="http://www.sharepointnutsandbolts.com/2010/07/feature-upgrade-part-3-introducing.html" mce_href="http://www.sharepointnutsandbolts.com/2010/07/feature-upgrade-part-3-introducing.html"&gt;Feature upgrade (part 3) – introducing SPFeatureUpgrade kit &lt;/A&gt;&lt;/DIV&gt;&lt;/LI&gt;
&lt;LI&gt;
&lt;DIV mce_keep="true"&gt;&lt;A href="http://www.sharepointnutsandbolts.com/2010/08/feature-upgrade-part-4-advanced.html" mce_href="http://www.sharepointnutsandbolts.com/2010/08/feature-upgrade-part-4-advanced.html"&gt;Feature upgrade (part 4) – advanced scenarios&lt;/A&gt;&lt;/DIV&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7582656" width="1" height="1"&gt;</description><pubDate>Sun, 08 Aug 2010 17:14:00 Z</pubDate><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Jan Tielens</dc:creator><slash:comments xmlns:slash="http://purl.org/rss/1.0/modules/slash/">1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/jan/rsscomments.aspx?PostID=7582656</wfw:commentRss><comments>http://weblogs.asp.net/jan/archive/2010/08/08/upgrading-features-in-sharepoint-2010.aspx#comments</comments></item><item><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7579724</guid><link>http://weblogs.asp.net/jan/archive/2010/08/06/easily-adding-safecontrols-in-sharepoint-2010-with-visual-studio-2010.aspx</link><category domain="http://weblogs.asp.net/jan/archive/tags/SharePoint/default.aspx">SharePoint</category><title>Easily Adding SafeControls in SharePoint 2010 with Visual Studio 2010</title><description>&lt;P mce_keep="true"&gt;Many SharePoint developers know that deploying your SharePoint customization to a SharePoint server often requires you to tinker the web.config of the sites where you’d like to see the customization in action. You can do these web.config modifications manually (not advisable), or automate them with the help of the SPWebConfigModification class from the Object Model. But quite often it’s not even necessary to use that class; when you deploy your customizations with the help of a Solution (.WSP file), like you should, the Solution deployment mechanism can modify the Web.Config to apply the most commonly used changes. This is defined in the Solution Manifest of the Solution file. The following Solution Manifest will add for example a SafeControl element for the current Assembly, which is required to display controls like Web Parts, User Controls etc. in the SharePoint sites.&lt;/P&gt;
&lt;P mce_keep="true"&gt;&lt;FONT face="courier new,courier"&gt;&amp;lt;Solution xmlns="&lt;/FONT&gt;&lt;A href="http://schemas.microsoft.com/sharepoint/"&gt;&lt;FONT face="courier new,courier"&gt;http://schemas.microsoft.com/sharepoint/&lt;/FONT&gt;&lt;/A&gt;&lt;FONT face="courier new,courier"&gt;" SolutionId="684047e7-a990-4917-8a84-5d2ae5c8b1f3" SharePointProductVersion="14.0"&amp;gt;&lt;BR&gt;&amp;nbsp; &amp;lt;Assemblies&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;Assembly Location="SafeControlsDemo.dll" DeploymentTarget="GlobalAssemblyCache"&amp;gt;&lt;BR&gt;&lt;STRONG&gt;&lt;FONT color=#ff0000&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;SafeControls&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;SafeControl Assembly="$SharePoint.Project.AssemblyFullName$" Namespace="SafeControlsDemo.EmptyElement1" TypeName="*" /&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/SafeControls&amp;gt;&lt;BR&gt;&lt;/FONT&gt;&lt;/STRONG&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/Assembly&amp;gt;&lt;BR&gt;&amp;nbsp; &amp;lt;/Assemblies&amp;gt;&lt;BR&gt;&amp;nbsp; &amp;lt;FeatureManifests&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;FeatureManifest Location="SafeControlsDemo_Feature1\Feature.xml" /&amp;gt;&lt;BR&gt;&amp;nbsp; &amp;lt;/FeatureManifests&amp;gt;&lt;BR&gt;&amp;lt;/Solution&amp;gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P mce_keep="true"&gt;Visual Studio 2010 will automatically add the necessary SafeControl element to the Solution manifest when you add for example a Web Part item to the Visual Studio SharePoint project. But sometimes Visual Studio is not smart enough, or doesn’t have enough information to automatically do this for you. A pretty common scenario is when you build a CustomAction using the ControlClass and ControlAssembly attributes, pointing to a User Control that should be rendering the UI element defined in the CustomAction. In this case you explicitly have to add the required SafeControl element (for the User Control) to the Solution Manifest. As you probably know you can modify the Package.Template.xml to add the required SafeControl elemen, but you have to write that XML manually. A better idea is to use the power of the Visual Studio 2010 SharePoint developer tools, available out-of-the-box! The little trick is to select the item in your SharePoint 2010 project in the Visual Studio Solution Explorer window. The Properties window will show, for the selected project item, a property called Safe Control Entries.&lt;/P&gt;
&lt;P mce_keep="true"&gt;&lt;IMG src="http://storage.tielens.name/easysafecontrols1.jpg" mce_src="http://storage.tielens.name/easysafecontrols1.jpg"&gt;&lt;/P&gt;
&lt;P mce_keep="true"&gt;When you click the ellipsis (the three dots), Visual Studio will show a dialog in which you can easily add the required SafeControl entries. Visual Studio will even fill out default values for you, referencing the assembly of the current project. As a result, the Solution Manifest will be updated with the corresponding XML (you can verify that by opening the Package item and selecting the Manifest view at the bottom). Easy isn’t it?&lt;/P&gt;
&lt;P mce_keep="true"&gt;&lt;IMG src="http://storage.tielens.name/easysafecontrols2.jpg" mce_src="http://storage.tielens.name/easysafecontrols2.jpg"&gt;&lt;/P&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7579724" width="1" height="1"&gt;</description><pubDate>Fri, 06 Aug 2010 08:14:00 Z</pubDate><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Jan Tielens</dc:creator><slash:comments xmlns:slash="http://purl.org/rss/1.0/modules/slash/">5</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/jan/rsscomments.aspx?PostID=7579724</wfw:commentRss><comments>http://weblogs.asp.net/jan/archive/2010/08/06/easily-adding-safecontrols-in-sharepoint-2010-with-visual-studio-2010.aspx#comments</comments></item><item><guid isPermaLink="false">http://blogs.u2u.be/kris/post.aspx?id=8f797c0e-a04e-4ed8-9693-b2b96578f932</guid><link>http://blogs.u2u.be/kris/post/2010/07/22/Lambda-Curry.aspx</link><author>Kris Vandermotten</author><title>Lambda Curry</title><description>&lt;p&gt;&lt;em&gt;Note: if you’re looking for &lt;a href="http://www.google.co.uk/search?hl=en&amp;amp;q=lamb+curry" target="_blank"&gt;lamb curry&lt;/a&gt;, you came to the wrong place. This post is about C# programming techniques.&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://en.wikipedia.org/wiki/Currying" target="_blank"&gt;Currying&lt;/a&gt; a function is a technique named after &lt;a href="http://en.wikipedia.org/wiki/Haskell_Curry" target="_blank"&gt;Haskell Curry&lt;/a&gt;, to transform a function with multiple parameters into a series of functions having one parameter each. The technique is important, because it opens the door to an optimization technique called partial evaluation. Let’s look at an example.&lt;/p&gt;  &lt;p&gt;Let’s say you need to write a program that sums a two-dimensional function f(x,y) over a two-dimensional range, e.g. –1000 ≤ x ≤ 1000 and –1000 ≤ y ≤ 1000.&lt;/p&gt;  &lt;p&gt;Such a two-dimensional function, assuming double parameters and result, can be represented by &lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;double&lt;/span&gt;, &lt;span style="color: blue"&gt;double&lt;/span&gt;, &lt;span style="color: blue"&gt;double&lt;/span&gt;&amp;gt;, and we can sum it using the following method:&lt;/p&gt;  &lt;pre style="font-family: consolas"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;private&lt;/span&gt;&amp;#160;&lt;span style="color: blue"&gt;static&lt;/span&gt;&amp;#160;&lt;span style="color: blue"&gt;double&lt;/span&gt; Sum(&lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;double&lt;/span&gt;, &lt;span style="color: blue"&gt;double&lt;/span&gt;, &lt;span style="color: blue"&gt;double&lt;/span&gt;&amp;gt; f)&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;double&lt;/span&gt; sum = 0;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;for&lt;/span&gt; (&lt;span style="color: blue"&gt;int&lt;/span&gt; x = -1000; x &amp;lt;= 1000; x++)&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;for&lt;/span&gt; (&lt;span style="color: blue"&gt;int&lt;/span&gt; y = -1000; y &amp;lt;= 1000; y++)&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; sum += f(x, y);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;br /&gt; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;return&lt;/span&gt; sum;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/pre&gt;

&lt;pre style="font-family: consolas"&gt;&amp;#160;&lt;/pre&gt;

&lt;p&gt;We can apply this to an arbitrary function, for example:&lt;/p&gt;

&lt;pre style="font-family: consolas"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;double&lt;/span&gt;, &lt;span style="color: blue"&gt;double&lt;/span&gt;, &lt;span style="color: blue"&gt;double&lt;/span&gt;&amp;gt; f = (x, y) =&amp;gt; &lt;span style="color: #2b91af"&gt;Math&lt;/span&gt;.Sin(x) * &lt;span style="color: #2b91af"&gt;Math&lt;/span&gt;.Sin(y);&lt;br /&gt; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;double&lt;/span&gt; result = Sum(f);&lt;/pre&gt;

&lt;pre style="font-family: consolas"&gt;&amp;#160;&lt;/pre&gt;

&lt;p&gt;Currying this is now a simple textual transformation. Instead of defining f as &lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;double&lt;/span&gt;, &lt;span style="color: blue"&gt;double&lt;/span&gt;, &lt;span style="color: blue"&gt;double&lt;/span&gt;&amp;gt;, we define it as &lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;double&lt;/span&gt;, &lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;double&lt;/span&gt;, &lt;span style="color: blue"&gt;double&lt;/span&gt;&amp;gt;&amp;gt;. &lt;/p&gt;

&lt;pre style="font-family: consolas"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;using&lt;/span&gt; System;&lt;br /&gt; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;internal&lt;/span&gt;&amp;#160;&lt;span style="color: blue"&gt;static&lt;/span&gt;&amp;#160;&lt;span style="color: blue"&gt;class&lt;/span&gt;&amp;#160;&lt;span style="color: #2b91af"&gt;Curried&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;public&lt;/span&gt;&amp;#160;&lt;span style="color: blue"&gt;static&lt;/span&gt;&amp;#160;&lt;span style="color: blue"&gt;void&lt;/span&gt; Main()&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;double&lt;/span&gt;, &lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;double&lt;/span&gt;, &lt;span style="color: blue"&gt;double&lt;/span&gt;&amp;gt;&amp;gt; f = x =&amp;gt; y =&amp;gt; &lt;span style="color: #2b91af"&gt;Math&lt;/span&gt;.Sin(x) * &lt;span style="color: #2b91af"&gt;Math&lt;/span&gt;.Sin(y);&lt;br /&gt; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;double&lt;/span&gt; result = Sum(f);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;br /&gt; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;private&lt;/span&gt;&amp;#160;&lt;span style="color: blue"&gt;static&lt;/span&gt;&amp;#160;&lt;span style="color: blue"&gt;double&lt;/span&gt; Sum(&lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;double&lt;/span&gt;, &lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;double&lt;/span&gt;, &lt;span style="color: blue"&gt;double&lt;/span&gt;&amp;gt;&amp;gt; f)&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;double&lt;/span&gt; sum = 0;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;for&lt;/span&gt; (&lt;span style="color: blue"&gt;int&lt;/span&gt; x = -1000; x &amp;lt;= 1000; x++)&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;for&lt;/span&gt; (&lt;span style="color: blue"&gt;int&lt;/span&gt; y = -1000; y &amp;lt;= 1000; y++)&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; sum += f(x)(y);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;br /&gt; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;return&lt;/span&gt; sum;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/pre&gt;

&lt;pre style="font-family: consolas"&gt;&amp;#160;&lt;/pre&gt;

&lt;p&gt;Effectively, a function that took two parameters and returned a result is replaced by a function that takes one parameter and returns a function that takes the second parameter and returns the result. It looks a lot simpler than it sounds. Instead of writing f = (x, y) =&amp;gt; &lt;span style="color: #2b91af"&gt;Math&lt;/span&gt;.Sin(x) + &lt;span style="color: #2b91af"&gt;Math&lt;/span&gt;.Sin(y), we write f = x =&amp;gt; y =&amp;gt; &lt;span style="color: #2b91af"&gt;Math&lt;/span&gt;.Sin(x) + &lt;span style="color: #2b91af"&gt;Math&lt;/span&gt;.Sin(y). And when calling it, instead of writing f(x, y), we write f(x)(y). Simple.&lt;/p&gt;

&lt;p&gt;Unfortunately, every call to f(x) now allocates a new &lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;double&lt;/span&gt;, &lt;span style="color: blue"&gt;double&lt;/span&gt;&amp;gt; object, and that can become quite expensive. But that can be fixed easily, so here is a smarter solution:&lt;/p&gt;

&lt;pre style="font-family: consolas"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;using&lt;/span&gt; System;&lt;br /&gt; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;internal&lt;/span&gt;&amp;#160;&lt;span style="color: blue"&gt;static&lt;/span&gt;&amp;#160;&lt;span style="color: blue"&gt;class&lt;/span&gt;&amp;#160;&lt;span style="color: #2b91af"&gt;Smarter&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;public&lt;/span&gt;&amp;#160;&lt;span style="color: blue"&gt;static&lt;/span&gt;&amp;#160;&lt;span style="color: blue"&gt;void&lt;/span&gt; Main()&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;double&lt;/span&gt;, &lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;double&lt;/span&gt;, &lt;span style="color: blue"&gt;double&lt;/span&gt;&amp;gt;&amp;gt; f = x =&amp;gt; y =&amp;gt; &lt;span style="color: #2b91af"&gt;Math&lt;/span&gt;.Sin(x) * &lt;span style="color: #2b91af"&gt;Math&lt;/span&gt;.Sin(y);&lt;br /&gt; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;double&lt;/span&gt; result = Sum(f);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;br /&gt; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;private&lt;/span&gt;&amp;#160;&lt;span style="color: blue"&gt;static&lt;/span&gt;&amp;#160;&lt;span style="color: blue"&gt;double&lt;/span&gt; Sum(&lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;double&lt;/span&gt;, &lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;double&lt;/span&gt;, &lt;span style="color: blue"&gt;double&lt;/span&gt;&amp;gt;&amp;gt; f)&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;double&lt;/span&gt; sum = 0;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;for&lt;/span&gt; (&lt;span style="color: blue"&gt;int&lt;/span&gt; x = -1000; x &amp;lt;= 1000; x++)&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;var&lt;/span&gt; fx = f(x);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;for&lt;/span&gt; (&lt;span style="color: blue"&gt;int&lt;/span&gt; y = -1000; y &amp;lt;= 1000; y++)&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; sum += fx(y);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;br /&gt; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;return&lt;/span&gt; sum;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/pre&gt;

&lt;pre style="font-family: consolas"&gt;&amp;#160;&lt;/pre&gt;

&lt;p&gt;I ran a little benchmark on this code. The benchmark executes the main function 20 times, and measures the shortest execution time. It also measures the number of generation 0 garbage collections. This is the result:&lt;/p&gt;

&lt;pre&gt;Naive: 261 msec, 0 collections&lt;br /&gt;Curried: 340 msec, 733 collections&lt;br /&gt;Smarter: 254 msec, 0 collections&lt;/pre&gt;

&lt;pre&gt;&amp;#160;&lt;/pre&gt;

&lt;p&gt;As we can see, the curried version was initially slower due to all the memory allocations, but when we fixed that, the smarter version was as fast as the original. In fact is was just a little bit faster, though nothing to get existed about.&lt;/p&gt;

&lt;p&gt;However, this is where partial evaluation kicks in. Currently, we are calculating the sinus of x over one million times, not taking advantage of the fact that we could reuse each calculated value a thousand times! So let’s change our definition of f as follows:&lt;/p&gt;

&lt;pre style="font-family: consolas"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;double&lt;/span&gt;, &lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;double&lt;/span&gt;, &lt;span style="color: blue"&gt;double&lt;/span&gt;&amp;gt;&amp;gt; f = x =&amp;gt; { &lt;span style="color: blue"&gt;var&lt;/span&gt; sinx = &lt;span style="color: #2b91af"&gt;Math&lt;/span&gt;.Sin(x); &lt;span style="color: blue"&gt;return&lt;/span&gt; y =&amp;gt; sinx * &lt;span style="color: #2b91af"&gt;Math&lt;/span&gt;.Sin(y); };&lt;/pre&gt;

&lt;pre style="font-family: consolas"&gt;&amp;#160;&lt;/pre&gt;

&lt;p&gt;Now the benchmark shows a completely different result:&lt;/p&gt;

&lt;pre&gt;Optimized: 143 msec, 0 collections&lt;/pre&gt;

&lt;pre&gt;&amp;#160;&lt;/pre&gt;

&lt;p&gt;We went from 261 milliseconds in the original version to 143 milliseconds in this version, in fact almost dividing execution time by two! That’s because, to be precise, in the original version we had two times 2001 * 1001 = 8,008,002 Math.Sin calls, and in the optimized version we have 1 time 2001 plus 1 time 2001 * 2001 = 4,006,002 Math.Sin calls. That is a division by a factor of 1.999, yielding a total execution time reduction by a factor of 1.825 (there is some overhead of course).&lt;/p&gt;

&lt;p&gt;Of course, the technique is very much related to loop invariant code motion in imperative programming. For example, imagine a Sum function hardcoded for Sin(x) + Sin(y). Would you write is like this?&lt;/p&gt;

&lt;pre style="font-family: consolas"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;private&lt;/span&gt;&amp;#160;&lt;span style="color: blue"&gt;static&lt;/span&gt;&amp;#160;&lt;span style="color: blue"&gt;double&lt;/span&gt; SumSinSin()&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;double&lt;/span&gt; sum = 0;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;for&lt;/span&gt; (&lt;span style="color: blue"&gt;int&lt;/span&gt; x = -1000; x &amp;lt;= 1000; x++)&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;for&lt;/span&gt; (&lt;span style="color: blue"&gt;int&lt;/span&gt; y = -1000; y &amp;lt;= 1000; y++)&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; sum += &lt;span style="color: #2b91af"&gt;Math&lt;/span&gt;.Sin(x) * &lt;span style="color: #2b91af"&gt;Math&lt;/span&gt;.Sin(y);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;br /&gt; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;return&lt;/span&gt; sum;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;br /&gt;&lt;/pre&gt;

&lt;p&gt;Of course not! At least you would move the calculation of Sin(x) out of the loop over y:&lt;/p&gt;

&lt;pre style="font-family: consolas"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;private&lt;/span&gt;&amp;#160;&lt;span style="color: blue"&gt;static&lt;/span&gt;&amp;#160;&lt;span style="color: blue"&gt;double&lt;/span&gt; SumSinSin()&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;double&lt;/span&gt; sum = 0;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;for&lt;/span&gt; (&lt;span style="color: blue"&gt;int&lt;/span&gt; x = -1000; x &amp;lt;= 1000; x++)&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;var&lt;/span&gt; sinx = &lt;span style="color: #2b91af"&gt;Math&lt;/span&gt;.Sin(x);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;for&lt;/span&gt; (&lt;span style="color: blue"&gt;int&lt;/span&gt; y = -1000; y &amp;lt;= 1000; y++)&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; sum += sinx * &lt;span style="color: #2b91af"&gt;Math&lt;/span&gt;.Sin(y);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;br /&gt; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;return&lt;/span&gt; sum;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/pre&gt;

&lt;pre style="font-family: consolas"&gt;&amp;#160;&lt;/pre&gt;

&lt;p&gt;And that is exactly what we did, except of course that the sum function is parameterized and not hardcoded.&lt;/p&gt;

&lt;p&gt;So when would you apply this technique? You would apply it when performance matters, and you have a function that you need to call a lot, that takes more than one parameter, where one parameter varies more than another one (in our example, x remained the same for a long time, while y was different on every call), and part of the function can be evaluated knowing only the value of the parameter that varies the least.&lt;/p&gt;

&lt;p&gt;In our example above, we could go even further. For example, we could eliminate the multiplication and even the Sin(y) calculation completely is case Sin(x) is 0 (which would be the case in our example only for x == 0).&lt;/p&gt;

&lt;pre style="font-family: consolas"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;double&lt;/span&gt;, &lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;double&lt;/span&gt;, &lt;span style="color: blue"&gt;double&lt;/span&gt;&amp;gt;&amp;gt; f = x =&amp;gt; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; { &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;if&lt;/span&gt; (x != 0.0)&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;var&lt;/span&gt; sinx = &lt;span style="color: #2b91af"&gt;Math&lt;/span&gt;.Sin(x);&lt;br /&gt; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;return&lt;/span&gt; y =&amp;gt; sinx * &lt;span style="color: #2b91af"&gt;Math&lt;/span&gt;.Sin(y);&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;else&lt;/span&gt;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;return&lt;/span&gt; y =&amp;gt; 0.0;&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; };&lt;/pre&gt;

&lt;pre style="font-family: consolas"&gt;&amp;#160;&lt;/pre&gt;

&lt;p&gt;That is not worth it in this scenario (because the special case applies to less than 0.05 % of all cases), but in some scenarios &lt;a href="http://en.wikipedia.org/wiki/Run-time_algorithm_specialisation" target="_blank"&gt;runtime algorithm specialization&lt;/a&gt; can be very significant.&lt;/p&gt;</description><pubDate>Thu, 22 Jul 2010 14:20:23 -1200</pubDate><comments>http://blogs.u2u.be/kris/post/2010/07/22/Lambda-Curry.aspx#comment</comments><dc:publisher xmlns:dc="http://purl.org/dc/elements/1.1/">Kris Vandermotten</dc:publisher><pingback:server xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/">http://blogs.u2u.be/kris/pingback.axd</pingback:server><pingback:target xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/">http://blogs.u2u.be/kris/post.aspx?id=8f797c0e-a04e-4ed8-9693-b2b96578f932</pingback:target><slash:comments xmlns:slash="http://purl.org/rss/1.0/modules/slash/">20</slash:comments><trackback:ping xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">http://blogs.u2u.be/kris/trackback.axd?id=8f797c0e-a04e-4ed8-9693-b2b96578f932</trackback:ping><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.u2u.be/kris/post/2010/07/22/Lambda-Curry.aspx#comment</wfw:comment><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.u2u.be/kris/syndication.axd?post=8f797c0e-a04e-4ed8-9693-b2b96578f932</wfw:commentRss></item><item><guid isPermaLink="false">http://blogs.u2u.be/diederik/post.aspx?id=76c8a755-d122-4cae-a379-e20baa62d10b</guid><link>http://blogs.u2u.be/diederik/post/2010/07/21/Transactions-and-Connections-in-Entity-Framework-40.aspx</link><author>Diederik Krols</author><category>Entity Framework 4.0</category><category>SQL Server</category><title>Transactions and Connections in Entity Framework 4.0</title><description>&lt;p&gt;This article describes where, why, and how to use TransactionScope in the Entity Framework 4.0. The proposed best practices apply to a medium to large data access layer, e.g. a DAL that is implemented as one or more WCF services with lots of internal calls. Allow me to start with the conclusions:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Always execute all data base access inside a TransactionScope,&lt;/li&gt;
&lt;li&gt;always explicitely specify an isolation level, and&lt;/li&gt;
&lt;li&gt;always explicitely open the entities' connection.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;If you trust me, implement these rules in all your projects. If you don't trust me, continue reading to figure out why.&lt;/p&gt;
&lt;h2&gt;Why you should open the connection explicitely.&lt;/h2&gt;
&lt;p&gt;So you decided to continue reading. Thanks for the confidence &lt;img title="Smile" src="http://blogs.u2u.be/diederik/editors/tiny_mce3/plugins/emotions/img/smiley-smile.gif" border="0" alt="Smile" /&gt;.&lt;/p&gt;
&lt;h3&gt;Default data adapter behavior&lt;/h3&gt;
&lt;p&gt;Under the hood of the newest 4.0 Entity Framework, the real work is still done by ye olde ADO.NET 1.* data adapter. Such a data adapter needs a connection and a command to do its work. If the connection is open, the adapter executes the command and leaves the connection open. If the connection is closed, the adapter opens it, executes the command, and then politely closes the connection again. When using the Entity Framework, you can open the connection explicitely as follows:&lt;/p&gt;
&lt;div style="color: black; background: azure ;font-family: Courier New; font-size: 9pt;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;using&lt;/span&gt; (&lt;span style="color: #2b91af;"&gt;AdventureWorks2008Entities&lt;/span&gt; entities = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;AdventureWorks2008Entities&lt;/span&gt;())&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; entities.Connection.Open();&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;// Your stuff here ...&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;If you don't open the connection explicitely, it will be opened and closed for you. That is convenient, but in a busy complex data access layer the same physical connection will be openened and closed over and over again. I now hear you saying 'What's wrong with that, the connection is pooled anyway, so there's no overhead in opening and closing.' Well, actually the data adapters behavior comes with a price, and I'm sure you will not always want to pay that price.&lt;/p&gt;
&lt;h3&gt;Performance impact&lt;/h3&gt;
&lt;p&gt;Data base connections are not created each time you need one. In most cases a connection is fetched from the connection pool. A pooled connection is always first cleaned up by the .NET data client with a call to the &lt;em&gt;sp_reset_connection&lt;/em&gt; system procedure. A complete list of that procedure's actions can be found &lt;a href="http://stackoverflow.com/questions/596365/what-does-sp-reset-connection-do"&gt;here&lt;/a&gt;. The list includes the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;It resets all error states and numbers (like @@error),&lt;/li&gt;
&lt;li&gt;it stops all execution contexts (EC) that are child threads of a parent EC executing a parallel query,&lt;/li&gt;
&lt;li&gt;it waits for any outstanding I/O operations,&lt;/li&gt;
&lt;li&gt;it frees any held buffers on the server by the connection,&lt;/li&gt;
&lt;li&gt;it unlocks any buffer resources that are used by the connection,&lt;/li&gt;
&lt;li&gt;it releases all memory allocated to the connection,&lt;/li&gt;
&lt;li&gt;it clears any work or temporary tables that are created by the connection,&lt;/li&gt;
&lt;li&gt;it kills all global cursors owned by the connection,&lt;/li&gt;
&lt;li&gt;it closes any open SQL-XML handles that are opened by the connection,&lt;/li&gt;
&lt;li&gt;it deletes any open SQL-XML related work tables,&lt;/li&gt;
&lt;li&gt;it closes all system tables,&lt;/li&gt;
&lt;li&gt;it closes all user tables,&lt;/li&gt;
&lt;li&gt;it drops all temporary objects,&lt;/li&gt;
&lt;li&gt;it aborts open transactions,&lt;/li&gt;
&lt;li&gt;it defects from a distributed transaction when enlisted,&lt;/li&gt;
&lt;li&gt;it decrements the reference count for users in current database,&lt;/li&gt;
&lt;li&gt;it frees acquired locks,&lt;/li&gt;
&lt;li&gt;it resets all SET options to the default values,&lt;/li&gt;
&lt;li&gt;it resets the @@rowcount value,&lt;/li&gt;
&lt;li&gt;it resets the @@identity value,&lt;/li&gt;
&lt;li&gt;it resets any session level trace options using dbcc traceon(), and&lt;/li&gt;
&lt;li&gt;fires Audit Login and Audit Logout events.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;If you don't explicitely open the connection yourself, then &lt;strong&gt;every other call will be a call to &lt;em&gt;sp_reset_connection&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Please don't start panicking now: SQL Server does all of this extremely fast. The demo application that you find at the end of this article clearly shows that even a high number of calls to the procedure incurs no noticable performance impact. These numerous calls only disturb your monitoring experience with SQL Profiler. The following images show a profiler session where the same workload is executed with and without explicitely opening the connection:&lt;/p&gt;
&lt;table border="0"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;th&gt;Default behavior&lt;/th&gt;&lt;th&gt;Explicit open&lt;/th&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;p&gt;&amp;nbsp;&lt;img src="http://blogs.u2u.be/diederik/image.axd?picture=2010%2f7%2fprofiler1a.png" alt="" /&gt;&lt;/p&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;p&gt;&lt;img src="http://blogs.u2u.be/diederik/image.axd?picture=2010%2f7%2fprofiler2.png" alt="" /&gt;&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;Although every other SQL command is a call to &lt;em&gt;sp_reset_connection&lt;/em&gt;, there's hardly any impact on the response time:&lt;/p&gt;
&lt;p&gt;&lt;img src="http://blogs.u2u.be/diederik/image.axd?picture=2010%2f7%2fpooling.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;But anyway there &amp;iacute;s a performance penalty, and you can avoid it.&lt;/p&gt;
&lt;h3&gt;Distributed transaction escalation&lt;/h3&gt;
&lt;p&gt;The continuing opening and closing of a (pooled) connection has more than just a small performanct impact when you're running inside a TransactionScope. It can cause a local transaction to escalate to a distributed transaction &lt;strong&gt;unnecesarily&lt;/strong&gt;. When a transaction spans more than one resource manager (database or queuing system) or involves too many connections, the .NET client decides to call help from the Distributed Transaction Coordinator service (MSDTC) in the Operating System. When that escalation exactly takes place depends largely on the version of SQL Server you're working against. The more recent versions sustain local mode longer. I only have SQL Server 2008R2 instances in my network. These instances even allow multiple connections to share the same transaction without escalating to MSDTC. The only way to force an escalation on my machine, is disabling connection pooling in the connection string (by adding &lt;em&gt;Pooling=False&lt;/em&gt;).&lt;/p&gt;
&lt;p&gt;Escalating to a distributed transaction takes a lot of resources: different services need to initialize and communicate. Here's a screenshot of the demo application, compare the response time for a regular escalated call&amp;nbsp;to the rest (the very first escalation -when stuff needs to be initialized- takes between 4 and 8 seconds):&lt;/p&gt;
&lt;p&gt;&lt;img src="http://blogs.u2u.be/diederik/image.axd?picture=2010%2f7%2fmsdtc.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Of course some of the observed overhead comes from the lack of pooling. Anyway, I believe that the difference in response time is high enough to catch your attention. You can strongly reduce the escalation risk by minimizing the number of physical connections per transaction by explicitely opening the connection and keeping it open until you commit. Here's again a comparison of profiler traces, with and without explicit opening:&lt;/p&gt;
&lt;table border="0"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;th&gt;Default behavior&lt;/th&gt;&lt;th&gt;Explicit open&lt;/th&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;p&gt;&lt;img src="http://blogs.u2u.be/diederik/image.axd?picture=2010%2f7%2fprofiler3b.png" alt="" /&gt;&lt;/p&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;p&gt;&lt;img src="http://blogs.u2u.be/diederik/image.axd?picture=2010%2f7%2fprofiler4.png" alt="" /&gt;&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;Opening the connection brings the response time of the sample workload back to normal:&lt;/p&gt;
&lt;p&gt;&lt;img src="http://blogs.u2u.be/diederik/image.axd?picture=2010%2f7%2fnomsdtc.png" alt="" /&gt;&lt;/p&gt;
&lt;h2&gt;Why you should use TransactionScope&lt;/h2&gt;
&lt;p&gt;Local transactions can only escalate to distributed transactions when you're running in a TransactionScope. So you might decide not to use TransactionScope after all. Unfortunately this is not an option. &lt;strong&gt;One of the few things that &lt;em&gt;sp_reset_connection&lt;/em&gt; doesn't do is resetting the transaction isolation level&lt;/strong&gt;. I personally consider this a frakking bug. &lt;a href="http://connect.microsoft.com/SQLServer/feedback/details/243527/sp-reset-connection-doesnt-reset-isolation-level"&gt;According to Microsoft, the behavior is by design&lt;/a&gt; so this bug will never be solved.&lt;/p&gt;
&lt;p&gt;If you want tight control over the performance and scalability of your data access, then managing the isolation level is absolutely crucial. There are two ways to control the isolation level in a DAL method:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;explicitely start a transaction, or&lt;/li&gt;
&lt;li&gt;set the isolation level on the connection programmatically by issuing a &lt;a href="http://msdn.microsoft.com/en-us/library/ms173763.aspx"&gt;SET TRANSACTION ISOLATION LEVEL&lt;/a&gt; statement.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;Explicitely starting a transaction&lt;/h3&gt;
&lt;p&gt;I see three ways to start a transaction:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;using SQL statements BEGIN-COMMIT-ROLLBACK&lt;/li&gt;
&lt;li&gt;starting a transaction on the connection using .NET code, or&lt;/li&gt;
&lt;li&gt;using TransactionScope.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The good thing about the first two techniques is that their default isolation level is READ COMMITTED, which is a nice trade-off between performance (minimal locking overhead) and reliability (no dirty reads possible). For this reason, READ COMMITTED is the default isolation level in the ANSI and ISO standards. Of course you could specify another level, if required. I will not elaborate on T-SQL transactions. You should only use these inside stored procedures and batch scripts, never in C# programs. Here's an example on using C# to start a transaction on the connection. Inside the transaction, you always know the isolation level (because you set it, or because there is a reliable default):&lt;/p&gt;
&lt;div style="color: black; background: azure; font-family: Courier New; font-size: 9pt;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;using&lt;/span&gt; (&lt;span style="color: #2b91af;"&gt;AdventureWorks2008Entities&lt;/span&gt; entities = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;AdventureWorks2008Entities&lt;/span&gt;(entitiesConnectionstring))&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; entities.Connection.Open();&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;// Always returns 'Read Committed'&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af;"&gt;DbTransaction&lt;/span&gt; trx = entities.Connection.BeginTransaction();&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;this&lt;/span&gt;.ErrorLabel.Content = trx.IsolationLevel.ToString();&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;// Your stuff here ...&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; trx.Commit();&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;In the context of a larger application, these first two techniques are too tightly connected to the database: you need a reference to the physical connection to control the transaction and its isolation level. So these techniques are only applicable inside the data access layer. The DAL is not the place where we define what 'a logical unit of works' i.e. 'a transaction' is. That's a decision that should be taken in the business layer, that layer doesn't own the connection. So there are reasons enough to use TransactionScope. Here's how to use TransactionScope. As you see the scope requires no access to the physical connection:&lt;/p&gt;
&lt;div style="color: black; background: azure; font-family: Courier New; font-size: 9pt;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: green;"&gt;// In the Business Layer&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;using&lt;/span&gt; (System.Transactions.&lt;span style="color: #2b91af;"&gt;TransactionScope&lt;/span&gt; scope = &lt;span style="color: blue;"&gt;new&lt;/span&gt; System.Transactions.&lt;span style="color: #2b91af;"&gt;TransactionScope&lt;/span&gt;(&lt;span style="color: #2b91af;"&gt;TransactionScopeOption&lt;/span&gt;.RequiresNew))&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;// In the Data Access Layer&lt;/span&gt;&lt;/p&gt;
&lt;p 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;AdventureWorks2008Entities&lt;/span&gt; entities = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;AdventureWorks2008Entities&lt;/span&gt;(entitiesConnectionstring))&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;// Always returns 'Serializable'&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;this&lt;/span&gt;.ErrorLabel.Content = &lt;span style="color: #2b91af;"&gt;Transaction&lt;/span&gt;.Current.IsolationLevel.ToString();&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;// Your stuff here ...&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; scope.Complete();&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;For reasons that I don't understand, Microsoft has chosen the second worst default for TransactionScope isolation level: SERIALIZABLE. [In case you were having doubts: READ UNCOMMITTED would be the worst choice.] This means that if you don't specify a better isolation level yourself, you place a lock on everything you read, and due to lock escalation you'll probably even lock rows that you don't even read. Needless to say you'll end up with&amp;nbsp;scalability issues.&lt;/p&gt;
&lt;h3&gt;Setting the isolation level in T-SQL&lt;/h3&gt;
&lt;p&gt;When starting a transaction on the connection, or when opening a TransactionScope, you can (and should) specify the appropriate isolation level. There's a third way to control the isolation level on a connection: T-SQL. This is again a low level technique that requires access to the physical data base connection. Here's an example on how to set the isolation level when using Entity Framework:&lt;/p&gt;
&lt;div style="color: black; background: azure; font-family: Courier New; font-size: 9pt;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;using&lt;/span&gt; (&lt;span style="color: #2b91af;"&gt;AdventureWorks2008Entities&lt;/span&gt; entities = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;AdventureWorks2008Entities&lt;/span&gt;())&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; entities.Connection.Open();&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; entities.ExecuteStoreCommand(&lt;span style="color: #a31515;"&gt;"SET TRANSACTION ISOLATION LEVEL READ COMMITTED"&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;// Your stuff here ...&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;The isolation level and its corresponding locking strategy will be applied to all your queries on the connection, whether or not you wrap them in a transaction. If you use this technique, you should not forget to reset the isolation level to its original value at the end of your method. Unfortunately there's no straightforward way to determine the current isolation level for a connection. [Keep reading this blog: I'm working on a solution] If you don't explicitely set the isolation level to its previous value, the new value will remain active on the connection. .NET nor SQL Server will reset it, not even when your code was called from inside a transaction!&lt;/p&gt;
&lt;h3&gt;Ignoring the isolation level&lt;/h3&gt;
&lt;p&gt;If you don't take control of it, &lt;strong&gt;you have no idea in which transaction isolation level your queries will be running&lt;/strong&gt;. After all, you don't know where the connection that you got from the pool has been: it could have come from a SSIS-package doing some bulk maintenance (where SERIALIZABLE is the default), or it could&amp;nbsp;been returned from an application that monitors that same SSIS-package (through a READUNCOMMITTED transaction allowing dirty reads). You simply inherit the last used isolation level on the connection, so you have no idea which type of locks are taken (or worse: ignored) by your queries and for how long these locks will be held. On a busy database, &lt;strong&gt;this will definitely lead to random errors, time-outs and deadlocks&lt;/strong&gt;.&lt;/p&gt;
&lt;h3&gt;Demo&lt;/h3&gt;
&lt;p&gt;To illustrate the arbitrary isolation level, the sample application contains the following code snippet that is executed after the transaction. It executes the T-SQL version of &lt;em&gt;Thread.Sleep&lt;/em&gt;, which gives you some time to monitor it from the outside:&lt;/p&gt;
&lt;div style="color: black; background: azure; font-family: Courier New; font-size: 9pt;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;using&lt;/span&gt; (&lt;span style="color: #2b91af;"&gt;AdventureWorks2008Entities&lt;/span&gt; entities = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;AdventureWorks2008Entities&lt;/span&gt;(entitiesConnectionstring))&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;// Script will reveal different values, depending on connection pooling&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; entities.ExecuteStoreCommand(&lt;span style="color: #a31515;"&gt;"WAITFOR DELAY '00:00:10'"&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;While you run the main application, you can monitor the connections with the following SQL script:&lt;/p&gt;
&lt;div style="color: black; background: azure; font-family: Courier New; font-size: 9pt;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;SELECT&lt;/span&gt; &lt;span style="color: blue;"&gt;CASE&lt;/span&gt; &lt;span style="color: teal;"&gt;transaction_isolation_level&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;WHEN&lt;/span&gt; 0 &lt;span style="color: blue;"&gt;THEN&lt;/span&gt; &lt;span style="color: red;"&gt;'Unspecified'&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;WHEN&lt;/span&gt; 1 &lt;span style="color: blue;"&gt;THEN&lt;/span&gt; &lt;span style="color: red;"&gt;'Read Uncommitted'&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;WHEN&lt;/span&gt; 2 &lt;span style="color: blue;"&gt;THEN&lt;/span&gt; &lt;span style="color: red;"&gt;'Read Committed'&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;WHEN&lt;/span&gt; 3 &lt;span style="color: blue;"&gt;THEN&lt;/span&gt; &lt;span style="color: red;"&gt;'Repeatable Read'&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;WHEN&lt;/span&gt; 4 &lt;span style="color: blue;"&gt;THEN&lt;/span&gt; &lt;span style="color: red;"&gt;'Serializable'&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;WHEN&lt;/span&gt; 5 &lt;span style="color: blue;"&gt;THEN&lt;/span&gt; &lt;span style="color: red;"&gt;'Snapshot'&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;ELSE&lt;/span&gt; &lt;span style="color: red;"&gt;'Bazinga'&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;END&lt;/span&gt; &lt;span style="color: blue;"&gt;AS&lt;/span&gt; &lt;span style="color: teal;"&gt;[Transaction Isolation Level]&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; &lt;span style="color: gray;"&gt;,&lt;/span&gt;&lt;span style="color: teal;"&gt;session_id&lt;/span&gt; &lt;span style="color: blue;"&gt;AS&lt;/span&gt; &lt;span style="color: teal;"&gt;[Session Id]&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; &lt;span style="color: gray;"&gt;,&lt;/span&gt;&lt;span style="color: teal;"&gt;login_time&lt;/span&gt; &lt;span style="color: blue;"&gt;AS&lt;/span&gt; &lt;span style="color: teal;"&gt;[Login Time]&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp; &lt;span style="color: blue;"&gt;FROM&lt;/span&gt; &lt;span style="color: lime;"&gt;sys&lt;/span&gt;&lt;span style="color: gray;"&gt;.&lt;/span&gt;&lt;span style="color: lime;"&gt;dm_exec_sessions&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;WHERE&lt;/span&gt; &lt;span style="color: fuchsia;"&gt;program_name&lt;/span&gt; &lt;span style="color: gray;"&gt;=&lt;/span&gt; &lt;span style="color: red;"&gt;'.Net SqlClient Data Provider'&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;For the record: &lt;a href="http://www.sheldonfan.com/origin-sheldon-coopers-bazinga.html"&gt;Bazinga&lt;/a&gt; is not an official isolation level&amp;nbsp;&lt;img title="Tongue out" src="http://blogs.u2u.be/diederik/editors/tiny_mce3/plugins/emotions/img/smiley-tongue-out.gif" border="0" alt="Tongue out" /&gt;.&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;You'll see that the isolation level for the connection is not stable:&lt;/p&gt;
&lt;p&gt;&lt;img src="http://blogs.u2u.be/diederik/image.axd?picture=2010%2f7%2funpredictable_isolation_level.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;So you should always specify an isolation level yourself. According to SQL Server Books-on-line, setting the isolation level &lt;em&gt;requires a solid understanding of transaction processing theory and the semantics of the transaction itself, the concurrency issues involved, and the consequences for system consistency&lt;/em&gt;. I couldn't agree more.&lt;/p&gt;
&lt;h2&gt;How to detect escalation&lt;/h2&gt;
&lt;p&gt;When using TransactionScope, there's always the risk of escalation to distributed transactions. So you should keep an eye on these. There are a couple of techniques to detect and monitor distributed transactions. Here are three of them:&lt;/p&gt;
&lt;h3&gt;Component services&lt;/h3&gt;
&lt;p&gt;Via &lt;em&gt;Control Panel -&amp;gt; Administrative Tools -&amp;gt; Component Services&lt;/em&gt; you reach the management console snap-in, that allows you to monitor MSDTC:&lt;/p&gt;
&lt;p&gt;&lt;img src="http://blogs.u2u.be/diederik/image.axd?picture=2010%2f7%2fcomponent_services.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;That same tool also alows you to configure MSDTC, and setup logging.&lt;/p&gt;
&lt;h3&gt;SQL Server Profiler&lt;/h3&gt;
&lt;p&gt;You can also trace distributed transactions with SQL Server Profiler:&lt;/p&gt;
&lt;p&gt;&lt;img src="http://blogs.u2u.be/diederik/image.axd?picture=2010%2f7%2fprofiler_dtc.png" alt="" /&gt;&lt;/p&gt;
&lt;h3&gt;Stop MSDTC and watch the exceptions in your program&lt;/h3&gt;
&lt;p&gt;Letting the application crash by stopping the MSDTC service,&amp;nbsp;is my favorite solution &lt;img title="Innocent" src="http://blogs.u2u.be/diederik/editors/tiny_mce3/plugins/emotions/img/smiley-innocent.gif" border="0" alt="Innocent" /&gt;&amp;nbsp;(well, at least in the developer or test environment). The trick only works with newer versions of .NET/SQL Server. In the past, MSDTC should run even for local transactions. That bug was fixed.&lt;/p&gt;
&lt;p&gt;Here's&amp;nbsp;a way to stop MSDTC from C# (must run as administrator):&lt;/p&gt;
&lt;div style="color: black; background: azure; font-family: Courier New; font-size: 9pt;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;using&lt;/span&gt; (&lt;span style="color: #2b91af;"&gt;ServiceController&lt;/span&gt; sc = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;ServiceController&lt;/span&gt;(&lt;span style="color: #a31515;"&gt;"Distributed Transaction Coordinator"&lt;/span&gt;))&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;if&lt;/span&gt; (sc.Status == &lt;span style="color: #2b91af;"&gt;ServiceControllerStatus&lt;/span&gt;.Running)&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; sc.Stop(); &lt;span style="color: green;"&gt;// Admins only !!&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;Here's the result in the test application:&lt;/p&gt;
&lt;p&gt;&lt;img src="http://blogs.u2u.be/diederik/image.axd?picture=2010%2f7%2fstopped_msdtc.png" alt="" /&gt;&lt;/p&gt;
&lt;h2&gt;Source code&lt;/h2&gt;
&lt;p&gt;Here's the full test project: &lt;a href="http://blogs.u2u.be/diederik/file.axd?file=2010%2f7%2fU2UConsult.EF40.TransactionScope.Sample.zip"&gt;U2UConsult.EF40.TransactionScope.Sample.zip (73,24 kb)&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;[You might want to change the AdventureWorks2008 connection strings in &lt;em&gt;app.config&lt;/em&gt;.]&lt;/p&gt;
&lt;p&gt;Enjoy!&lt;/p&gt;</description><pubDate>Wed, 21 Jul 2010 10:00:00 -1200</pubDate><comments>http://blogs.u2u.be/diederik/post/2010/07/21/Transactions-and-Connections-in-Entity-Framework-40.aspx#comment</comments><dc:publisher xmlns:dc="http://purl.org/dc/elements/1.1/">Diederik Krols</dc:publisher><pingback:server xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/">http://blogs.u2u.be/diederik/pingback.axd</pingback:server><pingback:target xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/">http://blogs.u2u.be/diederik/post.aspx?id=76c8a755-d122-4cae-a379-e20baa62d10b</pingback:target><slash:comments xmlns:slash="http://purl.org/rss/1.0/modules/slash/">1</slash:comments><trackback:ping xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">http://blogs.u2u.be/diederik/trackback.axd?id=76c8a755-d122-4cae-a379-e20baa62d10b</trackback:ping><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.u2u.be/diederik/post/2010/07/21/Transactions-and-Connections-in-Entity-Framework-40.aspx#comment</wfw:comment><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.u2u.be/diederik/syndication.axd?post=76c8a755-d122-4cae-a379-e20baa62d10b</wfw:commentRss></item><item><guid isPermaLink="false">http://blogs.u2u.be/peter/post.aspx?id=fd67d95b-6759-43e8-bf5d-d8dfdde7b0c4</guid><link>http://blogs.u2u.be/peter/post/2010/07/11/Add-Search-support-for-Add-Reference.aspx</link><author>Peter Himschoot</author><category>VS2010</category><title>Add Search support for Add Reference</title><description>&lt;p&gt;Visual Studio 2010 now comes packed with too many assemblies. The list of assemblies to pick from in the Add Reference dialog is now a little too large for my taste. Today, while browsing around in the Visual Studio 2010 Extension Manager, I found the Search References extension:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.u2u.be/peter/image.axd?picture=image_222.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://blogs.u2u.be/peter/image.axd?picture=image_thumb_222.png" width="559" height="55" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;After installing it my Add Reference dialog now looks like this:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.u2u.be/peter/image.axd?picture=image_223.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://blogs.u2u.be/peter/image.axd?picture=image_thumb_223.png" width="644" height="231" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;And typing in the search box instantly filters the list of assemblies. Nice. Microsoft should have provided this out of the box! It is also a nice sample of how extensible Visual Studio is.&lt;/p&gt;</description><pubDate>Sun, 11 Jul 2010 09:46:00 -1200</pubDate><comments>http://blogs.u2u.be/peter/post/2010/07/11/Add-Search-support-for-Add-Reference.aspx#comment</comments><dc:publisher xmlns:dc="http://purl.org/dc/elements/1.1/">Peter Himschoot</dc:publisher><pingback:server xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/">http://blogs.u2u.be/peter/pingback.axd</pingback:server><pingback:target xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/">http://blogs.u2u.be/peter/post.aspx?id=fd67d95b-6759-43e8-bf5d-d8dfdde7b0c4</pingback:target><slash:comments xmlns:slash="http://purl.org/rss/1.0/modules/slash/">2</slash:comments><trackback:ping xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">http://blogs.u2u.be/peter/trackback.axd?id=fd67d95b-6759-43e8-bf5d-d8dfdde7b0c4</trackback:ping><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.u2u.be/peter/post/2010/07/11/Add-Search-support-for-Add-Reference.aspx#comment</wfw:comment><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.u2u.be/peter/syndication.axd?post=fd67d95b-6759-43e8-bf5d-d8dfdde7b0c4</wfw:commentRss></item><item><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7533480</guid><link>http://weblogs.asp.net/jan/archive/2010/06/16/where-did-ulstracelog-go-to-in-the-sharepoint-2010-logging-database.aspx</link><category domain="http://weblogs.asp.net/jan/archive/tags/SharePoint/default.aspx">SharePoint</category><title>Where did ULSTraceLog go to in the SharePoint 2010 Logging Database?</title><description>&lt;P&gt;The Logging Database is one of the many new concepts that will make the life of many SharePoint administrators quite a bit more enjoyable. In SharePoint 2007 the Unified Logging System (ULS) logged all of its data to text files, typically found on your SharePoint server in 12\LOGS. We still have that in SharePoint 2010, but besides those text files, ULS can also write the data to a database! The advantages are obvious: easy to query, one central location for all servers in the farm, easy to build reports etc. You can find this ULS data in the SharePoint 2010 logging database (typically called WSS_Logging), in the view &lt;STRONG&gt;ULSTraceLog&lt;/STRONG&gt;. &lt;/P&gt;
&lt;P&gt;Quite recently on one of my demo machines (standalone installation on Windows 7) I noticed the ULSTraceLog view was not available in the logging database. It turned out that there is a Timer Job that’s responsible for writing the data to the database, when the Timer Job hasn’t executed, the view is not there (the first time it executes, the view is created). Even more, the timer job was disabled, so the view would never be created, nor any data would be written to the database. If you encounter this situation as well, it’s quite easy to solve:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Open the &lt;STRONG&gt;SharePoint Central Administration &lt;/STRONG&gt;site&lt;/LI&gt;
&lt;LI&gt;Navigate to the &lt;STRONG&gt;Monitoring &lt;/STRONG&gt;section&lt;/LI&gt;
&lt;LI&gt;Select &lt;STRONG&gt;Review Job Definitions&lt;/STRONG&gt;&lt;/LI&gt;
&lt;LI&gt;Click on the job with the name &lt;STRONG&gt;Diagnostic Data Provider: Trace Log&lt;/STRONG&gt;&lt;/LI&gt;
&lt;LI&gt;Click on the &lt;STRONG&gt;Enable &lt;/STRONG&gt;button to enable it&lt;/LI&gt;
&lt;LI&gt;&lt;STRONG&gt;Optionally &lt;/STRONG&gt;click on &lt;STRONG&gt;Run Now &lt;/STRONG&gt;afterwards, to start it immediately&lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;There you go, the ULSTraceLog will be created and the ULS messages will appear in the database!&lt;/P&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7533480" width="1" height="1"&gt;</description><pubDate>Wed, 16 Jun 2010 14:09:00 Z</pubDate><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Jan Tielens</dc:creator><slash:comments xmlns:slash="http://purl.org/rss/1.0/modules/slash/">0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/jan/rsscomments.aspx?PostID=7533480</wfw:commentRss><comments>http://weblogs.asp.net/jan/archive/2010/06/16/where-did-ulstracelog-go-to-in-the-sharepoint-2010-logging-database.aspx#comments</comments></item><item><guid isPermaLink="false">http://blogs.u2u.be/peter/post.aspx?id=b0baf6e4-0b42-41e7-873a-9ea91bc343bf</guid><link>http://blogs.u2u.be/peter/post/2010/06/14/How-to-implement-INotifyPropertyChanged-without-strings.aspx</link><author>Peter Himschoot</author><category>.NET Development</category><title>How to implement INotifyPropertyChanged without strings</title><description>&lt;p&gt;&lt;a href="http://blogs.u2u.be/peter/post/2010/03/21/Using-Model-View-ViewModel-with-WPF.aspx" target="_blank"&gt;INotifyPropertyChanged&lt;/a&gt; is an interface which is very important to do proper databinding and is heavily used in the MVVM pattern.&lt;/p&gt;  &lt;p&gt;However, to implement it you have to raise the PropertyChanged event whenever a data-bound property changes value, and you have to pass the name of the property to it. This results in string based programming and this generally is not good for maintenance. Some people create a RaisePropertyChanged method in the base class and then invoke this method in each property setter. Again this is not ideal because you cannot always derive from this base class, especially if you already have a base class. In this post I want to show you an alternative way of implementing INotifyPropertyChanged that doesn’t require a base class (you simple implement the INotifyPropertyChanged on each class) with a nice, string-less way of raising the PropertyChanged event. For example, look at this class:&lt;/p&gt;  &lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;   &lt;div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;     &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum1"&gt;   1:&lt;/span&gt; &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; Customer : INotifyPropertyChanged&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum2"&gt;   2:&lt;/span&gt; {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum3"&gt;   3:&lt;/span&gt;   &lt;span style="color: #0000ff"&gt;private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; firstName;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum4"&gt;   4:&lt;/span&gt;&amp;#160; &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum5"&gt;   5:&lt;/span&gt;   &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; FirstName&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum6"&gt;   6:&lt;/span&gt;   {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum7"&gt;   7:&lt;/span&gt;     get { &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; firstName; }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum8"&gt;   8:&lt;/span&gt;     set&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum9"&gt;   9:&lt;/span&gt;     {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum10"&gt;  10:&lt;/span&gt;       &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (!&lt;span style="color: #0000ff"&gt;object&lt;/span&gt;.Equals(&lt;span style="color: #0000ff"&gt;value&lt;/span&gt;, firstName))&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum11"&gt;  11:&lt;/span&gt;       {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum12"&gt;  12:&lt;/span&gt;         firstName = &lt;span style="color: #0000ff"&gt;value&lt;/span&gt;;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum13"&gt;  13:&lt;/span&gt;         PropertyChanged.Raise(&lt;span style="color: #0000ff"&gt;this&lt;/span&gt;, o =&amp;gt; o.FirstName);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum14"&gt;  14:&lt;/span&gt;       }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum15"&gt;  15:&lt;/span&gt;     }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum16"&gt;  16:&lt;/span&gt;   }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum17"&gt;  17:&lt;/span&gt; }&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;As you can see, the PropertyChanged.Raise does all the work, and you pass in the sender and the property using a Lambda expression. Simple and no strings. To make things even simpler, I use a code snippet that implements the property directly for me:&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;
    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum1"&gt;   1:&lt;/span&gt; &lt;span style="color: #0000ff"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color: #800000"&gt;xml&lt;/span&gt; &lt;span style="color: #ff0000"&gt;version&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span style="color: #ff0000"&gt;encoding&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;utf-8&amp;quot;&lt;/span&gt; ?&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum2"&gt;   2:&lt;/span&gt; &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;CodeSnippet&lt;/span&gt; &lt;span style="color: #ff0000"&gt;Format&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;1.0.0&amp;quot;&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum3"&gt;   3:&lt;/span&gt;              &lt;span style="color: #ff0000"&gt;xmlns&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet&amp;quot;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum4"&gt;   4:&lt;/span&gt;   &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;Header&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum5"&gt;   5:&lt;/span&gt;     &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;Title&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;NPC&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;Title&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum6"&gt;   6:&lt;/span&gt;     &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;Author&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;Peter&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;Author&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum7"&gt;   7:&lt;/span&gt;     &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;Shortcut&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;npc&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;Shortcut&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum8"&gt;   8:&lt;/span&gt;     &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;Description&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;NotifyPropertyChangedProperty&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;Description&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum9"&gt;   9:&lt;/span&gt;     &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;SnippetTypes&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum10"&gt;  10:&lt;/span&gt;       &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;SnippetType&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;SurroundsWith&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;SnippetType&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum11"&gt;  11:&lt;/span&gt;       &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;SnippetType&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;Expansion&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;SnippetType&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum12"&gt;  12:&lt;/span&gt;     &lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;SnippetTypes&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum13"&gt;  13:&lt;/span&gt;   &lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;Header&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum14"&gt;  14:&lt;/span&gt;   &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;Snippet&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum15"&gt;  15:&lt;/span&gt;     &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;Declarations&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum16"&gt;  16:&lt;/span&gt;       &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;Literal&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum17"&gt;  17:&lt;/span&gt;         &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;ID&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;type&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;ID&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum18"&gt;  18:&lt;/span&gt;         &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;Default&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;int&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;Default&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum19"&gt;  19:&lt;/span&gt;       &lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;Literal&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum20"&gt;  20:&lt;/span&gt;       &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;Literal&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum21"&gt;  21:&lt;/span&gt;         &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;ID&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;field&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;ID&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum22"&gt;  22:&lt;/span&gt;         &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;Default&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;prop&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;Default&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum23"&gt;  23:&lt;/span&gt;       &lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;Literal&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum24"&gt;  24:&lt;/span&gt;       &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;Literal&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum25"&gt;  25:&lt;/span&gt;         &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;ID&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;name&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;ID&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum26"&gt;  26:&lt;/span&gt;         &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;Default&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;Prop&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;Default&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum27"&gt;  27:&lt;/span&gt;       &lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;Literal&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum28"&gt;  28:&lt;/span&gt;     &lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;Declarations&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum29"&gt;  29:&lt;/span&gt;     &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;Code&lt;/span&gt; &lt;span style="color: #ff0000"&gt;Language&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;CSharp&amp;quot;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum30"&gt;  30:&lt;/span&gt;       &lt;span style="color: #0000ff"&gt;&amp;lt;!&lt;/span&gt;[CDATA[&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum31"&gt;  31:&lt;/span&gt;       [System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.Never)]&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum32"&gt;  32:&lt;/span&gt;       private $type$ $field$;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum33"&gt;  33:&lt;/span&gt;       &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum34"&gt;  34:&lt;/span&gt;       public $type$ $name$ {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum35"&gt;  35:&lt;/span&gt;         get { return $field$; }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum36"&gt;  36:&lt;/span&gt;         set {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum37"&gt;  37:&lt;/span&gt;           if( ! object.Equals( value, $field$ ) )&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum38"&gt;  38:&lt;/span&gt;           {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum39"&gt;  39:&lt;/span&gt;             $field$ = value;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum40"&gt;  40:&lt;/span&gt;             PropertyChanged.Raise( this, o =&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt; o.$name$ );&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum41"&gt;  41:&lt;/span&gt;           }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum42"&gt;  42:&lt;/span&gt;         }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum43"&gt;  43:&lt;/span&gt;       }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum44"&gt;  44:&lt;/span&gt;       ]]&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum45"&gt;  45:&lt;/span&gt;     &lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;Code&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum46"&gt;  46:&lt;/span&gt;   &lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;Snippet&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum47"&gt;  47:&lt;/span&gt; &lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;CodeSnippet&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;So how does it work? Here is the definition of the Raise Extension method:&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;
    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum1"&gt;   1:&lt;/span&gt; &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; Raise&amp;lt;T, P&amp;gt;(&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum2"&gt;   2:&lt;/span&gt;   &lt;span style="color: #0000ff"&gt;this&lt;/span&gt; PropertyChangedEventHandler pc&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum3"&gt;   3:&lt;/span&gt; , T source&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum4"&gt;   4:&lt;/span&gt; , Expression&amp;lt;Func&amp;lt;T, P&amp;gt;&amp;gt; pe)&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum5"&gt;   5:&lt;/span&gt; {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum6"&gt;   6:&lt;/span&gt;   &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (pc != &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;)&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum7"&gt;   7:&lt;/span&gt;   {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum8"&gt;   8:&lt;/span&gt;     pc.Invoke(source, &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum9"&gt;   9:&lt;/span&gt;       &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; PropertyChangedEventArgs(((MemberExpression)pe.Body).Member.Name));&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum10"&gt;  10:&lt;/span&gt;   }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum11"&gt;  11:&lt;/span&gt; }&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;This extension method has three arguments, the first two should be clear. The last is a LINQ expression. &lt;/p&gt;

&lt;p&gt;When you invoke it the compiler converts this into a tree of objects, which represents the lambda expression o =&amp;gt; o.FirstName, and passes it as an argument. This tree of objects is then traversed to find the MemberExpression which contains the name of the property. So the overhead is the creation and traversal of this small tree of objects. So it is a little less fast then passing a string, but the code is compile-time safe and supported by Visual Studio refactoring. This technique is generally known as &lt;a href="http://ayende.com/Blog/archive/2005/10/29/StaticReflection.aspx" target="_blank"&gt;static reflection&lt;/a&gt;.&lt;/p&gt;</description><pubDate>Mon, 14 Jun 2010 10:29:35 -1200</pubDate><comments>http://blogs.u2u.be/peter/post/2010/06/14/How-to-implement-INotifyPropertyChanged-without-strings.aspx#comment</comments><dc:publisher xmlns:dc="http://purl.org/dc/elements/1.1/">Peter Himschoot</dc:publisher><pingback:server xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/">http://blogs.u2u.be/peter/pingback.axd</pingback:server><pingback:target xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/">http://blogs.u2u.be/peter/post.aspx?id=b0baf6e4-0b42-41e7-873a-9ea91bc343bf</pingback:target><slash:comments xmlns:slash="http://purl.org/rss/1.0/modules/slash/">1</slash:comments><trackback:ping xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">http://blogs.u2u.be/peter/trackback.axd?id=b0baf6e4-0b42-41e7-873a-9ea91bc343bf</trackback:ping><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.u2u.be/peter/post/2010/06/14/How-to-implement-INotifyPropertyChanged-without-strings.aspx#comment</wfw:comment><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.u2u.be/peter/syndication.axd?post=b0baf6e4-0b42-41e7-873a-9ea91bc343bf</wfw:commentRss></item><item><guid isPermaLink="false">http://blogs.u2u.be/peter/post.aspx?id=5b8dad66-0523-4a82-9610-6853bfe38756</guid><link>http://blogs.u2u.be/peter/post/2010/06/09/Farewell-father.aspx</link><author>Peter Himschoot</author><title>Farewell father</title><description>&lt;p&gt;Today, at 18h30, my father-in-law Gaston Dhaese passed away after a 9 year battle against cancer. &lt;/p&gt;  &lt;p&gt;I’ve been told he didn’t suffer any pain, and I am glad he could die in piece with his loving family close by.&lt;/p&gt;  &lt;p&gt;Dear father, you have always been good and fair to me, my wife Isabelle and our children Liesbeth, Wouter and Edward. Words fall short to express my gratitude for all you’ve done…&lt;/p&gt;  &lt;p&gt;You will never be forgotten. Don’t worry, we’ll take care of mother Lieve and daughter Siegrid.&lt;/p&gt;</description><pubDate>Wed, 09 Jun 2010 03:17:12 -1200</pubDate><comments>http://blogs.u2u.be/peter/post/2010/06/09/Farewell-father.aspx#comment</comments><dc:publisher xmlns:dc="http://purl.org/dc/elements/1.1/">Peter Himschoot</dc:publisher><pingback:server xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/">http://blogs.u2u.be/peter/pingback.axd</pingback:server><pingback:target xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/">http://blogs.u2u.be/peter/post.aspx?id=5b8dad66-0523-4a82-9610-6853bfe38756</pingback:target><slash:comments xmlns:slash="http://purl.org/rss/1.0/modules/slash/">1</slash:comments><trackback:ping xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">http://blogs.u2u.be/peter/trackback.axd?id=5b8dad66-0523-4a82-9610-6853bfe38756</trackback:ping><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.u2u.be/peter/post/2010/06/09/Farewell-father.aspx#comment</wfw:comment><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.u2u.be/peter/syndication.axd?post=5b8dad66-0523-4a82-9610-6853bfe38756</wfw:commentRss></item><item><guid isPermaLink="false">http://blogs.u2u.be/diederik/post.aspx?id=6ed715d9-b79d-4a7c-abe8-cc37dd1ea4ff</guid><link>http://blogs.u2u.be/diederik/post/2010/06/07/Optimistic-concurrency-using-a-SQL-DateTime-in-Entity-Framework-40.aspx</link><author>Diederik Krols</author><category>.NET 4.0</category><category>Entity Framework 4.0</category><category>SQL Server</category><category>WPF</category><title>Optimistic concurrency using a SQL DateTime in Entity Framework 4.0</title><description>&lt;p&gt;This article explains how to implement optimistic concurrency checking using a SQL Server DateTime or DateTime2 column. It's a follow-up of my &lt;a href="http://blogs.u2u.be/diederik/post/2010/06/04/Optimistic-concurrency-using-a-SQL-Timestamp-in-Entity-Framework-40.aspx"&gt;previous article&lt;/a&gt; on using a TimeStamp column for that same purpose. In most -if not all- concurrency checking cases it actually makes more sense to use a DateTime column instead of a TimeStamp. The DateTime data types occupy the same storage (8 bytes) as a TimeStamp, or even less: DateTime2 with 'low' precision takes only 6 bytes. On top of that: their content makes sense to the end user. Unfortunately the DateTime data&amp;nbsp;types are 'a little bit' less evident to use for concurrency checking: you need to declare a trigger (or a stored procedure) on the table, and you need to hack the entity model.&lt;/p&gt;
&lt;h2&gt;A sample table&lt;/h2&gt;
&lt;p&gt;Sample time! Let's start with creating a table to hold some data.&lt;/p&gt;
&lt;h3&gt;Table definition&lt;/h3&gt;
&lt;p&gt;Here's how the table looks like (the solution at the end of the article contains a full T-SQL script). The LastModified column will be used for optimistic concurrency checking:&lt;/p&gt;
&lt;div style="color: black; background: azure; font-family: Courier New; font-size: 9pt;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;CREATE&lt;/span&gt; &lt;span style="color: blue;"&gt;TABLE&lt;/span&gt; &lt;span style="color: teal;"&gt;[dbo]&lt;/span&gt;&lt;span style="color: gray;"&gt;.&lt;/span&gt;&lt;span style="color: teal;"&gt;[Hero]&lt;/span&gt;&lt;span style="color: gray;"&gt;(&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: teal;"&gt;[Id]&lt;/span&gt; &lt;span style="color: teal;"&gt;[int]&lt;/span&gt; &lt;span style="color: blue;"&gt;IDENTITY&lt;/span&gt;&lt;span style="color: gray;"&gt;(&lt;/span&gt;1&lt;span style="color: gray;"&gt;,&lt;/span&gt;1&lt;span style="color: gray;"&gt;)&lt;/span&gt; &lt;span style="color: gray;"&gt;NOT&lt;/span&gt; &lt;span style="color: gray;"&gt;NULL,&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: teal;"&gt;[Name]&lt;/span&gt; &lt;span style="color: teal;"&gt;[nvarchar]&lt;/span&gt;&lt;span style="color: gray;"&gt;(&lt;/span&gt;50&lt;span style="color: gray;"&gt;)&lt;/span&gt; &lt;span style="color: gray;"&gt;NOT&lt;/span&gt; &lt;span style="color: gray;"&gt;NULL,&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: teal;"&gt;[Brand]&lt;/span&gt; &lt;span style="color: teal;"&gt;[nvarchar]&lt;/span&gt;&lt;span style="color: gray;"&gt;(&lt;/span&gt;50&lt;span style="color: gray;"&gt;)&lt;/span&gt; &lt;span style="color: gray;"&gt;NULL,&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: teal;"&gt;[LastModified]&lt;/span&gt; &lt;span style="color: teal;"&gt;[datetime]&lt;/span&gt; &lt;span style="color: gray;"&gt;NULL,&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;CONSTRAINT&lt;/span&gt; &lt;span style="color: teal;"&gt;[PK_Hero]&lt;/span&gt; &lt;span style="color: blue;"&gt;PRIMARY&lt;/span&gt; &lt;span style="color: blue;"&gt;KEY&lt;/span&gt; &lt;span style="color: blue;"&gt;CLUSTERED&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: gray;"&gt;(&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: teal;"&gt;[Id]&lt;/span&gt; &lt;span style="color: blue;"&gt;ASC&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: gray;"&gt;))&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;h3&gt;Trigger definition&lt;/h3&gt;
&lt;p&gt;Unlike an Identity or a TimeStamp value, a DateTime value is not automatically generated and/or updated. So we have to give the database a little help, e.g. by creating a trigger for insert and update on that table:&lt;/p&gt;
&lt;div style="color: black; background: azure; font-family: Courier New; font-size: 9pt;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;CREATE&lt;/span&gt; &lt;span style="color: blue;"&gt;TRIGGER&lt;/span&gt; &lt;span style="color: teal;"&gt;[dbo]&lt;/span&gt;&lt;span style="color: gray;"&gt;.&lt;/span&gt;&lt;span style="color: teal;"&gt;[trg_iu_Hero]&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;ON&lt;/span&gt; &lt;span style="color: teal;"&gt;[dbo]&lt;/span&gt;&lt;span style="color: gray;"&gt;.&lt;/span&gt;&lt;span style="color: teal;"&gt;[Hero]&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;AFTER&lt;/span&gt; &lt;span style="color: blue;"&gt;INSERT&lt;/span&gt;&lt;span style="color: gray;"&gt;,&lt;/span&gt; &lt;span style="color: blue;"&gt;UPDATE&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;AS&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;BEGIN&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;SET&lt;/span&gt; &lt;span style="color: blue;"&gt;NOCOUNT&lt;/span&gt; &lt;span style="color: blue;"&gt;ON&lt;/span&gt;&lt;span style="color: gray;"&gt;;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;UPDATE&lt;/span&gt; &lt;span style="color: teal;"&gt;[dbo]&lt;/span&gt;&lt;span style="color: gray;"&gt;.&lt;/span&gt;&lt;span style="color: teal;"&gt;[Hero]&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; &lt;span style="color: blue;"&gt;SET&lt;/span&gt; &lt;span style="color: teal;"&gt;LastModified&lt;/span&gt; &lt;span style="color: gray;"&gt;=&lt;/span&gt; &lt;span style="color: fuchsia;"&gt;GETDATE&lt;/span&gt;&lt;span style="color: gray;"&gt;()&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;WHERE&lt;/span&gt; &lt;span style="color: teal;"&gt;Id&lt;/span&gt; &lt;span style="color: gray;"&gt;IN&lt;/span&gt;&lt;span style="color: blue;"&gt; &lt;/span&gt;&lt;span style="color: gray;"&gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;SELECT&lt;/span&gt; &lt;span style="color: teal;"&gt;Id&lt;/span&gt; &lt;span style="color: blue;"&gt;FROM&lt;/span&gt; &lt;span style="color: teal;"&gt;inserted&lt;/span&gt;&lt;span style="color: gray;"&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;END&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;Alternatively, you could insert through a stored procedure.&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;A sample application&lt;/h2&gt;
&lt;p&gt;I already prepared for you a small sample application. Here's how the main window looks like:&lt;/p&gt;
&lt;p&gt;&lt;img src="http://blogs.u2u.be/diederik/image.axd?picture=2010%2f6%2fform.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Cute, isn't it ?&lt;/p&gt;
&lt;h3&gt;The problem&lt;/h3&gt;
&lt;p&gt;In the entity model, you have to make sure that the &lt;em&gt;LastModified&lt;/em&gt; column has the correct settings (&lt;em&gt;Fixed&lt;/em&gt; and &lt;em&gt;Computed&lt;/em&gt;):&lt;/p&gt;
&lt;p&gt;&lt;img src="http://blogs.u2u.be/diederik/image.axd?picture=2010%2f6%2fentitymodel.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Run the application with just the generated code. You will observe that when you update a record, the entity's &lt;em&gt;LastModified&lt;/em&gt; property will NOT be updated. SQL Server Profiler will reveal that only an update statement is issued. The new value of &lt;em&gt;LastModified&lt;/em&gt; is assigned by the trigger but NOT fetched:&lt;/p&gt;
&lt;p&gt;&lt;img src="http://blogs.u2u.be/diederik/image.axd?picture=2010%2f6%2fprofiler_before.png" alt="" /&gt;&lt;/p&gt;
&lt;h3&gt;The solution&lt;/h3&gt;
&lt;p&gt;In order to let the Entity Framework fetch the new value of the DateTime column -or whatever column that is modified by a trigger-, you need to hack the model's XML and &lt;strong&gt;&lt;span style="text-decoration: underline;"&gt;manually&lt;/span&gt;&lt;/strong&gt; add the following attribute in the SSDL:&lt;/p&gt;
&lt;p&gt;&lt;img src="http://blogs.u2u.be/diederik/image.axd?picture=2010%2f6%2fssdl.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Somewhere in Redmond there will certainly be an architect who will provide an excuse for this behavior. To us developers, this sure smells like a bug. Anyway, if you re-run the application with the modified SSDL, the new DateTime value will appear after insert or update. SQL Server profiler reveals the extra select statement:&lt;/p&gt;
&lt;p&gt;&lt;img src="http://blogs.u2u.be/diederik/image.axd?picture=2010%2f6%2fprofiler_after.png" alt="" /&gt;&lt;/p&gt;
&lt;h2&gt;Source Code&lt;/h2&gt;
&lt;p&gt;Here's the source code, the whole source code, and nothing but the source code: &lt;a href="http://blogs.u2u.be/diederik/file.axd?file=2010%2f6%2fU2UConsult.EF40.DateTimeConcurrency.Sample.zip"&gt;U2UConsult.EF40.DateTimeConcurrency.Sample.zip (616,27 kb)&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Enjoy!&lt;/p&gt;
&lt;table border="0"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;p&gt;&lt;img src="http://blogs.u2u.be/diederik/image.axd?picture=2010%2f6%2fmerel.jpg" alt="" width="300" /&gt;&lt;/p&gt;
&lt;/td&gt;
&lt;td width="300" valign="bottom"&gt;
&lt;h2&gt;Thank you&lt;/h2&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;This article is dedicated to my 3-year old daughter Merel. Last week she briefly turned into a real angel, but then decided to come back.&lt;/p&gt;
&lt;p&gt;I want to thank from the bottom of my heart everybody who helped saving her life: her mama, her mammie, the &lt;a href="http://www.ambuce.be/klini.html"&gt;MUG&lt;/a&gt;, and the emergency, reanimation, intensive care, and pediatric departments of the &lt;a href="http://www.uza.be/UZA"&gt;uza hospital&lt;/a&gt;.&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;</description><pubDate>Mon, 07 Jun 2010 20:24:00 -1200</pubDate><comments>http://blogs.u2u.be/diederik/post/2010/06/07/Optimistic-concurrency-using-a-SQL-DateTime-in-Entity-Framework-40.aspx#comment</comments><dc:publisher xmlns:dc="http://purl.org/dc/elements/1.1/">Diederik Krols</dc:publisher><pingback:server xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/">http://blogs.u2u.be/diederik/pingback.axd</pingback:server><pingback:target xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/">http://blogs.u2u.be/diederik/post.aspx?id=6ed715d9-b79d-4a7c-abe8-cc37dd1ea4ff</pingback:target><slash:comments xmlns:slash="http://purl.org/rss/1.0/modules/slash/">1</slash:comments><trackback:ping xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">http://blogs.u2u.be/diederik/trackback.axd?id=6ed715d9-b79d-4a7c-abe8-cc37dd1ea4ff</trackback:ping><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.u2u.be/diederik/post/2010/06/07/Optimistic-concurrency-using-a-SQL-DateTime-in-Entity-Framework-40.aspx#comment</wfw:comment><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.u2u.be/diederik/syndication.axd?post=6ed715d9-b79d-4a7c-abe8-cc37dd1ea4ff</wfw:commentRss></item><item><guid isPermaLink="false">http://blogs.u2u.be/diederik/post.aspx?id=c27cbd08-52f6-4d4b-9044-a2e9c45259bb</guid><link>http://blogs.u2u.be/diederik/post/2010/05/28/Optimistic-concurrency-using-a-SQL-Timestamp-in-Entity-Framework-40.aspx</link><author>Diederik Krols</author><category>.NET 4.0</category><category>WPF</category><category>Entity Framework 4.0</category><title>Optimistic concurrency using a SQL Timestamp in Entity Framework 4.0</title><description>&lt;p&gt;This article explains how to implement optimistic concurrency checking in the Entity Framework 4.0, using a SQL Server Timestamp column. But you could have derived that from its title.&lt;/p&gt;
&lt;h2&gt;What is a Timestamp?&lt;/h2&gt;
&lt;p&gt;Despite its name, the SQL Server Timestamp data type has nothing to do with time. DateTime2 on the other hand, is DateTime too [sorry, I couldn't resist that]. A timestamp is just an eight-bytes binary number that indicates the relative sequence in which data modifications took place in a database. The value in a column of the type Timestamp is always provided by SQL Server: it is calculated when a row is inserted, and augmented with each update to the row, to a ever increasing value that is unique in the whole database. The Timestamp data type was initially conceived for assisting in recovery operations, and&amp;nbsp;you also use it to synchronize distributed databases: based on the timestamp you can detect the order in which data was added or updated, and replay a sequence of modifications. But the most common usage of timestamp is&amp;nbsp;optimistic concurrency checking: when updating a row you can compare its current timestamp with the one you fetched originally. If the values are different, you know that someone else updated the row behind your back. And you know this without holding any locks on the server while you were busy, so its a very scalable solution. This type of concurrency checking is called 'optimistic': you assume that in most cases you will be able to successfully update without the need for conflict resolution.&lt;/p&gt;
&lt;h2&gt;A sample of the geeky kind&lt;/h2&gt;
&lt;p&gt;Let's create a table with such a column (the scripts-folder in the provided solution at the end of this article contains a full script):&lt;/p&gt;
&lt;div style="color: black; background: azure; font-family: Courier New; font-size: 9pt;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;CREATE&lt;/span&gt; &lt;span style="color: blue;"&gt;TABLE&lt;/span&gt; &lt;span style="color: teal;"&gt;[dbo]&lt;/span&gt;&lt;span style="color: gray;"&gt;.&lt;/span&gt;&lt;span style="color: teal;"&gt;[FerengiRule]&lt;/span&gt;&lt;span style="color: gray;"&gt;(&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: teal;"&gt;[ID]&lt;/span&gt; &lt;span style="color: teal;"&gt;[int]&lt;/span&gt; &lt;span style="color: gray;"&gt;NOT&lt;/span&gt; &lt;span style="color: gray;"&gt;NULL,&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: teal;"&gt;[Text]&lt;/span&gt; &lt;span style="color: teal;"&gt;[nvarchar]&lt;/span&gt;&lt;span style="color: gray;"&gt;(&lt;/span&gt;100&lt;span style="color: gray;"&gt;)&lt;/span&gt; &lt;span style="color: gray;"&gt;NOT&lt;/span&gt; &lt;span style="color: gray;"&gt;NULL,&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: teal;"&gt;[Source]&lt;/span&gt; &lt;span style="color: teal;"&gt;[nvarchar]&lt;/span&gt;&lt;span style="color: gray;"&gt;(&lt;/span&gt;100&lt;span style="color: gray;"&gt;)&lt;/span&gt; &lt;span style="color: gray;"&gt;NOT&lt;/span&gt; &lt;span style="color: gray;"&gt;NULL,&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: teal;"&gt;[Timestamp]&lt;/span&gt; &lt;span style="color: teal;"&gt;[timestamp]&lt;/span&gt; &lt;span style="color: gray;"&gt;NOT&lt;/span&gt; &lt;span style="color: gray;"&gt;NULL,&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;span style="color: blue;"&gt;CONSTRAINT&lt;/span&gt; &lt;span style="color: teal;"&gt;[PK_FerengiRule]&lt;/span&gt; &lt;span style="color: blue;"&gt;PRIMARY&lt;/span&gt; &lt;span style="color: blue;"&gt;KEY&lt;/span&gt; &lt;span style="color: blue;"&gt;CLUSTERED&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: gray;"&gt;(&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: teal;"&gt;[ID]&lt;/span&gt; &lt;span style="color: blue;"&gt;ASC&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: gray;"&gt;)&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;Populate it with your favorite&amp;nbsp;'Ferengi Rules of Acquisition'.&amp;nbsp;You find all of these &lt;a href="http://memory-alpha.org/wiki/Rules_of_Acquisition"&gt;here&lt;/a&gt;. In a WPF solution, create an entity model, and add the table to it. You see that the timestamp column has&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;em&gt;Fixed&lt;/em&gt; as value for the &lt;em&gt;Concurrency Mode &lt;/em&gt;property, so&amp;nbsp;the column&amp;nbsp;will appear in the WHERE-clause of any insert, update,&amp;nbsp;or delete query, and &lt;/li&gt;
&lt;li&gt;&lt;em&gt;Computed&lt;/em&gt; as value for the &lt;em&gt;StoreGeneratedPattern &lt;/em&gt;property, so a new value is expected from the server after insert or update.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img src="http://blogs.u2u.be/diederik/image.axd?picture=2010%2f6%2fProperties.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Next, build an application with a&amp;nbsp;fancy transparent&amp;nbsp;startup screen, that allows you to open multiple edit windows on the same data. The startup screen could look like this:&lt;/p&gt;
&lt;p&gt;&lt;img src="http://blogs.u2u.be/diederik/image.axd?picture=2010%2f6%2fStartUpWindow.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;The mainwindow of the application contains just an editable grid on the table's contents. It allows you to&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;set the concurrency resolution mode,&lt;/li&gt;
&lt;li&gt;upload&amp;nbsp;local modifications to the database,&lt;/li&gt;
&lt;li&gt;get the current server data, and last but not least&lt;/li&gt;
&lt;li&gt;restore the table to its original contents (that's an extremely useful feature in this type of application).&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Here's how the window looks like:&lt;/p&gt;
&lt;p&gt;&lt;img src="http://blogs.u2u.be/diederik/image.axd?picture=2010%2f6%2fMainWindow.png" alt="" /&gt;&lt;/p&gt;
&lt;h2&gt;Visualizing a Timestamp value&lt;/h2&gt;
&lt;p&gt;Just like&amp;nbsp;GUIDs and technical keys, you should avoid showing timestamp values on the user interface. This demo is an exceptional to this general rule, so I built a TimestampToDouble converter to translate the eight-byte binary number to something more readable. I don't guarantee a readable output on an old active database where the timestamp value is very high, but it works fine for a demo on a fresh database:&lt;/p&gt;
&lt;div style="color: black; background: azure; font-family: Courier New; font-size: 9pt;"&gt;
&lt;p 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;SqlTimestampToDoubleConverter&lt;/span&gt;: &lt;span style="color: #2b91af;"&gt;IValueConverter&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;
&lt;p 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;object&lt;/span&gt; Convert(&lt;span style="color: blue;"&gt;object&lt;/span&gt; value, &lt;span style="color: #2b91af;"&gt;Type&lt;/span&gt; targetType, &lt;span style="color: blue;"&gt;object&lt;/span&gt; parameter, System.Globalization.&lt;span style="color: #2b91af;"&gt;CultureInfo&lt;/span&gt; culture)&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;if&lt;/span&gt; (value == &lt;span style="color: blue;"&gt;null&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&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;null&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;byte&lt;/span&gt;[] bytes = value &lt;span style="color: blue;"&gt;as&lt;/span&gt; &lt;span style="color: blue;"&gt;byte&lt;/span&gt;[];&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;double&lt;/span&gt; result = 0;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;double&lt;/span&gt; inter = 0;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;for&lt;/span&gt; (&lt;span style="color: blue;"&gt;int&lt;/span&gt; i = 0; i &amp;lt; bytes.Length; i++)&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; inter = System.&lt;span style="color: #2b91af;"&gt;Convert&lt;/span&gt;.ToDouble(bytes[i]);&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; inter = inter * &lt;span style="color: #2b91af;"&gt;Math&lt;/span&gt;.Pow(2, ((7 - i) * 8));&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; result += inter;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;return&lt;/span&gt; result;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p 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;object&lt;/span&gt; ConvertBack(&lt;span style="color: blue;"&gt;object&lt;/span&gt; value, &lt;span style="color: #2b91af;"&gt;Type&lt;/span&gt; targetType, &lt;span style="color: blue;"&gt;object&lt;/span&gt; parameter, System.Globalization.&lt;span style="color: #2b91af;"&gt;CultureInfo&lt;/span&gt; culture)&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&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;NotImplementedException&lt;/span&gt;();&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;Concurrency&lt;/h2&gt;
&lt;h3&gt;Generated SQL Queries&lt;/h3&gt;
&lt;p&gt;If you run your SQL Profiler while modifying data through via the main window, you'll see that the Entity Framework query's WHERE-clause contains the timestamp, and that after the update the new value is fetched for you:&lt;/p&gt;
&lt;p&gt;&lt;img src="http://blogs.u2u.be/diederik/image.axd?picture=2010%2f6%2fSqlProfiler.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;If there's no row to be updated, then someone else must have modified (or deleted) it. You can test that very easily with the sample application by opening multiple edit windows and playing around with the data.&lt;/p&gt;
&lt;h3&gt;Client side conflict resolution&lt;/h3&gt;
&lt;p&gt;When the entity framework discovers a concurrency violation when saving your changes, it appropriately throws an &lt;em&gt;OptimisticConcurrencyException&lt;/em&gt;. It's then time for you to solve the conflict. In most cases, that means&amp;nbsp;fetching the current values, sending these to the GUI, and let the end user decide what should happen. The data access layer&amp;nbsp;code for inserts and updates will look like this:&lt;/p&gt;
&lt;div style="color: black; background: azure; font-family: Courier New; font-size: 9pt;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;foreach&lt;/span&gt; (&lt;span style="color: blue;"&gt;var&lt;/span&gt; rule &lt;span style="color: blue;"&gt;in&lt;/span&gt; &lt;span style="color: blue;"&gt;this&lt;/span&gt;.rules)&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;try&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;switch&lt;/span&gt; (rule.ChangeTracker.State)&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&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: #2b91af;"&gt;ObjectState&lt;/span&gt;.Added:&lt;/p&gt;
&lt;p 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; entities.FerengiRules.AddObject(rule);&lt;/p&gt;
&lt;p 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; entities.SaveChanges();&lt;/p&gt;
&lt;p 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; &lt;span style="color: blue;"&gt;break&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&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: #2b91af;"&gt;ObjectState&lt;/span&gt;.Modified:&lt;/p&gt;
&lt;p 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; entities.FerengiRules.ApplyChanges(rule);&lt;/p&gt;
&lt;p 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; entities.SaveChanges();&lt;/p&gt;
&lt;p 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; &lt;span style="color: blue;"&gt;break&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&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;/p&gt;
&lt;p 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; &lt;span style="color: blue;"&gt;break&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;catch&lt;/span&gt; (&lt;span style="color: #2b91af;"&gt;OptimisticConcurrencyException&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;// Return Current Values&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;// ...&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h3&gt;Server side conflict resolution&lt;/h3&gt;
&lt;p&gt;The Entity Framework also provides automated conflict resolution strategies that might be useful in some scenarios (although to be honest: I can't think of any). There's a &lt;em&gt;Refresh&lt;/em&gt; method that you can use to decide whether it's the client version or the server (&lt;em&gt;store&lt;/em&gt;) version that should be persisted when there's a conflict. Here's how the catch-block could look like:&lt;/p&gt;
&lt;div style="color: black; background: azure; font-family: Courier New; font-size: 9pt;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;catch&lt;/span&gt; (&lt;span style="color: #2b91af;"&gt;OptimisticConcurrencyException&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;switch&lt;/span&gt; (&lt;span style="color: blue;"&gt;this&lt;/span&gt;.conflictResolution)&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&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: #2b91af;"&gt;ConflictResolution&lt;/span&gt;.Default:&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&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;/p&gt;
&lt;p style="margin: 0px;"&gt;&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: #2b91af;"&gt;ConflictResolution&lt;/span&gt;.ServerWins:&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; entities.Refresh(&lt;span style="color: #2b91af;"&gt;RefreshMode&lt;/span&gt;.StoreWins, rule);&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; entities.SaveChanges();&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;break&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&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: #2b91af;"&gt;ConflictResolution&lt;/span&gt;.ClientWins:&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; entities.Refresh(&lt;span style="color: #2b91af;"&gt;RefreshMode&lt;/span&gt;.ClientWins, rule);&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; entities.SaveChanges();&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;break&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;default&lt;/span&gt;:&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;break&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;Source Code&lt;/h2&gt;
&lt;p&gt;Oops ... almost forgot: here's the source code of the whole thing: &lt;a href="http://blogs.u2u.be/diederik/file.axd?file=2010%2f6%2fU2UConsult.EF40.OptimisticConcurrency.Sample.zip"&gt;U2UConsult.EF40.OptimisticConcurrency.Sample.zip (470,96 kb)&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Enjoy!&lt;/p&gt;</description><pubDate>Fri, 28 May 2010 01:19:00 -1200</pubDate><comments>http://blogs.u2u.be/diederik/post/2010/05/28/Optimistic-concurrency-using-a-SQL-Timestamp-in-Entity-Framework-40.aspx#comment</comments><dc:publisher xmlns:dc="http://purl.org/dc/elements/1.1/">Diederik Krols</dc:publisher><pingback:server xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/">http://blogs.u2u.be/diederik/pingback.axd</pingback:server><pingback:target xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/">http://blogs.u2u.be/diederik/post.aspx?id=c27cbd08-52f6-4d4b-9044-a2e9c45259bb</pingback:target><slash:comments xmlns:slash="http://purl.org/rss/1.0/modules/slash/">1</slash:comments><trackback:ping xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">http://blogs.u2u.be/diederik/trackback.axd?id=c27cbd08-52f6-4d4b-9044-a2e9c45259bb</trackback:ping><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.u2u.be/diederik/post/2010/05/28/Optimistic-concurrency-using-a-SQL-Timestamp-in-Entity-Framework-40.aspx#comment</wfw:comment><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.u2u.be/diederik/syndication.axd?post=c27cbd08-52f6-4d4b-9044-a2e9c45259bb</wfw:commentRss></item><item><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7506930</guid><link>http://weblogs.asp.net/jan/archive/2010/05/26/highly-recommended-quot-5-things-sql-server-does-different-from-what-many-developers-expect-quot-by-nico-jacobs.aspx</link><title>Highly recommended: "5 Things SQL Server does different from what many developers expect" by Nico Jacobs</title><description>&lt;P mce_keep="true"&gt;A couple of weeks ago, the &lt;A href="http://www.microsoft.com/belux/techdays/2010/" mce_href="http://www.microsoft.com/belux/techdays/2010/"&gt;Belgian Techdays&lt;/A&gt; were held in Antwerp. Together with &lt;A href="http://shillier.com/" mce_href="http://shillier.com/"&gt;Scott Hillier&lt;/A&gt; I presented the SharePoint pre-conference sessions (watch them online &lt;A href="http://www.microsoft.com/belux/techdays/2010/videos.aspx" mce_href="http://www.microsoft.com/belux/techdays/2010/videos.aspx"&gt;over here&lt;/A&gt;, search for pre-conference or SharePoint). Even though Belgium is not a very big country, the Microsoft team managed to get some high profile speakers like Anders Hejlsberg and Scott Hanselman. But if you have like 60 minutes to spare there is one session that I'd really recommend to check out, not related to SharePoint, but very interesting and entertaining nonetheless: &lt;A href="http://channel9.msdn.com/posts/kreekman/TechDays-2010-5-Things-SQL-Server-does-different-from-what-many-developers-expect/" mce_href="http://channel9.msdn.com/posts/kreekman/TechDays-2010-5-Things-SQL-Server-does-different-from-what-many-developers-expect/"&gt;5 Things SQL Server does different from what many developers expect&lt;/A&gt; by my U2U colleague Nico Jacobs. Nico did a fantastic job, so sit back and enjoy! :-)&lt;/P&gt;
&lt;P mce_keep="true"&gt;&lt;A href="http://ecn.channel9.msdn.com/o9/ch9/3/6/3/3/4/5/TD2010DEV314_2MB_ch9.wmv" mce_href="http://ecn.channel9.msdn.com/o9/ch9/3/6/3/3/4/5/TD2010DEV314_2MB_ch9.wmv"&gt;&lt;IMG src="http://ecn.channel9.msdn.com/o9/ch9/3/6/3/3/4/5/TD2010DEV314_512_ch9.png" mce_src="http://ecn.channel9.msdn.com/o9/ch9/3/6/3/3/4/5/TD2010DEV314_512_ch9.png"&gt;&lt;/A&gt;&lt;/P&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7506930" width="1" height="1"&gt;</description><pubDate>Wed, 26 May 2010 20:01:00 Z</pubDate><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Jan Tielens</dc:creator><slash:comments xmlns:slash="http://purl.org/rss/1.0/modules/slash/">2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/jan/rsscomments.aspx?PostID=7506930</wfw:commentRss><comments>http://weblogs.asp.net/jan/archive/2010/05/26/highly-recommended-quot-5-things-sql-server-does-different-from-what-many-developers-expect-quot-by-nico-jacobs.aspx#comments</comments></item><item><guid isPermaLink="false">http://blogs.u2u.be/diederik/post.aspx?id=58a76f09-fb3e-42c1-82e5-dae4641949f1</guid><link>http://blogs.u2u.be/diederik/post/2010/05/19/Globalizing-and-Localizating-a-WPF-application.aspx</link><author>Diederik Krols</author><category>WPF</category><title>Globalizing and Localizing a WPF application</title><description>&lt;p&gt;This article describes how to localize a WPF application using the &lt;a href="http://wpflocalizeextension.codeplex.com/"&gt;WPF Localization Extension&lt;/a&gt; project from Codeplex. On one hand, this is a fine tool. On the other hand, it's a crying shame that WPF developers need to rely on external sources for something as basic as localization. As you know, localization support is nicely integrated in Microsoft's other development stacks, like WinForms and ASP.NET. So far for the ranting, let's get to the solution.&lt;/p&gt;
&lt;h2&gt;Terminology&lt;/h2&gt;
&lt;p&gt;&lt;em&gt;Globalization&lt;/em&gt; is making your application ready to be localized.&amp;nbsp;This&amp;nbsp;boils down to separating culture-specific elements (texts, images, colors, ...) from the code. The &lt;a href="http://msdn.microsoft.com/en-us/library/ms182184  (v=VS.100).aspx"&gt;globalization code analysis rules&lt;/a&gt; may help you to do this.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Localization&lt;/em&gt; is tailoring your application toward a specific language and region by providing these culture-specific elements (e.g. in a resource file or a database).&lt;/p&gt;
&lt;h2&gt;Options for WPF localization&lt;/h2&gt;
&lt;p&gt;These are a couple of options for localizing a WPF application:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;use the clumsy and error prone &lt;a href="http://msdn.microsoft.com/en-us/library/ms746621.aspx"&gt;LocBaml&lt;/a&gt; tool. Don't get me wrong: I really like the idea of skipping the globalization step and then do localization as an afterthought. But just read the intro of &lt;a href="http://www.codeproject.com/KB/WPF/WPF_Resx_Localization.aspx"&gt;this article&lt;/a&gt; for a list of reasons&amp;nbsp;NOT to use LocBaml,&lt;/li&gt;
&lt;li&gt;use classic RESX files, or&lt;/li&gt;
&lt;li&gt;use WPF ResourceDictionaries.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;More details about the pros and cons of these alternatives can be found &lt;a href="http://sachabarber.net/?p=286"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;h2&gt;The WPF Localization Extension project&lt;/h2&gt;
&lt;p&gt;The WPF Localization Extension allows you to localize your application using classic .resx files. The project revolves around a custom markup extension that allows you to declaratively (in XAML) bind control properties to localized resources. The library offers a lot of functionality, but more important: it is well-written and well-documented (at least in its source code). I built a small sample application that walks through some of its features. Here's how this application looks like. You find its source code at the end of this article:&lt;/p&gt;
&lt;p&gt;&lt;img src="http://blogs.u2u.be/diederik/image.axd?picture=2010%2f5%2fruntime.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;The project is decorated with the following resource files:&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;img src="http://blogs.u2u.be/diederik/image.axd?picture=2010%2f5%2fresources.png" alt="" /&gt;&lt;/p&gt;
&lt;h3&gt;Declarative Localization&lt;/h3&gt;
&lt;p&gt;The markup extension and its namespaces are registered by the following declaration on top your XAML file (you might want to change the project's source code to choose a more appropriate namespace):&lt;/p&gt;
&lt;div style="color: black; background: azure; font-family: Courier New; font-size: 9pt;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: red;"&gt;xmlns&lt;/span&gt;&lt;span style="color: blue;"&gt;:&lt;/span&gt;&lt;span style="color: red;"&gt;lex&lt;/span&gt;&lt;span style="color: blue;"&gt;="http://schemas.root- project.org/xaml/presentation"&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;You can now set up a binding from a control's property to a resource, like this:&lt;/p&gt;
&lt;div style="color: black; background: azure; font-family: Courier New; font-size: 9pt;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;TextBlock&lt;/span&gt;&lt;span style="color: red;"&gt; Text&lt;/span&gt;&lt;span style="color: blue;"&gt;="{&lt;/span&gt;&lt;span style="color:   #a31515;"&gt;lex&lt;/span&gt;&lt;span style="color: blue;"&gt;:&lt;/span&gt;&lt;span style="color: #a31515;"&gt;LocText&lt;/span&gt;&lt;span style="color: red;"&gt; Key&lt;/span&gt;&lt;span style="color: blue;"&gt;=Tomato,&lt;/span&gt;&lt;span style="color: red;"&gt; Dict&lt;/span&gt;&lt;span style="color: blue;"&gt;=Translations,&lt;/span&gt;&lt;span style="color: red;"&gt; Assembly&lt;/span&gt;&lt;span style="color: blue;"&gt;=U2UConsult.WPF.Localization.Sample}" /&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;It's possible to ignore the current culture, and force a specific culture for a binding, like this:&lt;/p&gt;
&lt;div style="color: black; background: azure; font-family: Courier New; font-size: 9pt;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;TextBlock&lt;/span&gt;&lt;span style="color: red;"&gt; Text&lt;/span&gt;&lt;span style="color: blue;"&gt;="{&lt;/span&gt;&lt;span style="color:   #a31515;"&gt;lex&lt;/span&gt;&lt;span style="color: blue;"&gt;:&lt;/span&gt;&lt;span style="color: #a31515;"&gt;LocText&lt;/span&gt;&lt;span style="color: red;"&gt; Key&lt;/span&gt;&lt;span style="color: blue;"&gt;=Duck,&lt;/span&gt;&lt;span style="color: red;"&gt; Dict&lt;/span&gt;&lt;span style="color: blue;"&gt;=Translations,&lt;/span&gt;&lt;span style="color: red;"&gt; Assembly&lt;/span&gt;&lt;span style="color: blue;"&gt;=U2UConsult.WPF.Localization.Sample,&lt;/span&gt;&lt;span style="color: red;"&gt; ForceCulture&lt;/span&gt;&lt;span style="color:   blue;"&gt;=nl}" /&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;My sample application only fetches string resources, but WPF Localization Extension&amp;gt; supports the following resource types out of the box:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Brush,&lt;/li&gt;
&lt;li&gt;Double,&lt;/li&gt;
&lt;li&gt;FlowDirection,&lt;/li&gt;
&lt;li&gt;Image,&lt;/li&gt;
&lt;li&gt;Text, and&lt;/li&gt;
&lt;li&gt;Thickness.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;For other data types you'll need to plug in a &lt;a href="http://msdn.microsoft.com/en-us/library/system.windows.data.ivalueconverter.aspx"&gt;Converter&lt;/a&gt;.&lt;/p&gt;
&lt;h3&gt;Programmatic Localization&lt;/h3&gt;
&lt;p&gt;Sometimes we need to localize a control in source code, e.g. when the control is dynamically created. In this case we can set the binding programmatically, like this:&lt;/p&gt;
&lt;div style="color: black; background: azure; font-family: Courier New; font-size: 9pt;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: #2b91af;"&gt;LocTextExtension&lt;/span&gt; loc = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;LocTextExtension&lt;/span&gt;(&lt;span style="color:   #a31515;"&gt;"U2UConsult.WPF.Localization.Sample:Translations:Pumpkin"&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;loc.SetBinding(&lt;span style="color: blue;"&gt;this&lt;/span&gt;.PumpkinTextBlock, &lt;span style="color: #2b91af;"&gt;TextBlock&lt;/span&gt;.TextProperty);&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;If data binding is not needed (or not possible), the you can fallback to a lookup in the .resx file using &lt;em&gt;GetLocalizedObject&lt;/em&gt;, like this:&lt;/p&gt;
&lt;div style="color: black; background: azure; font-family: Courier New; font-size: 9pt;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;string&lt;/span&gt; pinguin = &lt;span style="color: #2b91af;"&gt;LocalizeDictionary&lt;/span&gt;.Instance.GetLocalizedObject&amp;lt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;gt;(&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #a31515;"&gt;"U2UConsult.WPF.Localization.Sample"&lt;/span&gt;,&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #a31515;"&gt;"Translations"&lt;/span&gt;,&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #a31515;"&gt;"Pinguin"&lt;/span&gt;,&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af;"&gt;CultureInfo&lt;/span&gt;.GetCultureInfo(&lt;span style="color: #a31515;"&gt;"nl-BE"&lt;/span&gt;));&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;this&lt;/span&gt;.PinguinTextBlock.Text = pinguin;&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h3&gt;Design time support&lt;/h3&gt;
&lt;p&gt;Localization at run time is one thing, but as a developer we like to see the result without running the application. The &lt;em&gt;LocalizeDictionary.DesignCulture&lt;/em&gt; dependency property provides a Culture for Visual Studio's designers. It can be set in XAML, as follows:&lt;/p&gt;
&lt;div style="color: black; background: azure; font-family: Courier New; font-size: 9pt;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: red;"&gt;lex&lt;/span&gt;&lt;span style="color: blue;"&gt;:&lt;/span&gt;&lt;span style="color: red;"&gt;LocalizeDictionary.DesignCulture&lt;/span&gt;&lt;span style="color: blue;"&gt;="nl-BE"&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;[By the way: the design time support only started working on my VS2010 after I recompiled the codeplex source with VS2010.]&lt;/p&gt;
&lt;p&gt;On top of that, the markup extension provides a &lt;em&gt;DesignValue&lt;/em&gt; property that sets the value in Visual Studio's Designer, regardless of the (Design-)Culture. Here's a sample:&lt;/p&gt;
&lt;div style="color: black; background: azure; font-family: Courier New; font-size: 9pt;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;TextBlock&lt;/span&gt;&lt;span style="color: red;"&gt; Text&lt;/span&gt;&lt;span style="color: blue;"&gt;="{&lt;/span&gt;&lt;span style="color:   #a31515;"&gt;lex&lt;/span&gt;&lt;span style="color: blue;"&gt;:&lt;/span&gt;&lt;span style="color: #a31515;"&gt;LocText&lt;/span&gt;&lt;span style="color: red;"&gt; Key&lt;/span&gt;&lt;span style="color: blue;"&gt;=Tomato,&lt;/span&gt;&lt;span style="color: red;"&gt; Dict&lt;/span&gt;&lt;span style="color: blue;"&gt;=Translations,&lt;/span&gt;&lt;span style="color: red;"&gt; Assembly&lt;/span&gt;&lt;span style="color: blue;"&gt;=U2UConsult.WPF.Localization.Sample,&lt;/span&gt;&lt;span style="color: red;"&gt; DesignValue&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;#&lt;span style="color: blue;"&gt;Tomato&lt;/span&gt;#&lt;span style="color: blue;"&gt;}" /&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;Here's how these two features look like at design time:&lt;/p&gt;
&lt;p&gt;&lt;img src="http://blogs.u2u.be/diederik/image.axd?picture=2010%2f5%2fdesigntime.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Finally, the markup extension also provides a InitialValue for Blend support.&lt;/p&gt;
&lt;h2&gt;Missing cultures and missing translations&lt;/h2&gt;
&lt;p&gt;I guess we all agree that our application should never totally crash if a string can not be found in a resource file, or if the user has an unexpected or unsupported locale. Here are some techniques to get around this.&lt;/p&gt;
&lt;h3&gt;Missing resources&lt;/h3&gt;
&lt;p&gt;It's not a good idea to just fallback to a default language for missing entries, without a warning to the end user.&amp;nbsp;In the best case this will&amp;nbsp;only create confusion. But just take a look at the following interlingual homographs and consider what would happen if they appear in the middle of a half-translated window:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;em&gt;coin&lt;/em&gt; is french for &lt;em&gt;corner&lt;/em&gt;,&lt;/li&gt;
&lt;li&gt;&lt;em&gt;file&lt;/em&gt; is dutch for &lt;em&gt;traffic jam&lt;/em&gt;, and&lt;/li&gt;
&lt;li&gt;&lt;em&gt;gift&lt;/em&gt; is german for &lt;em&gt;poison&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;So the invariant culture should NOT be English (or whatever 'real' language), but still remain comprehensible. In my app, the invariant resource file looks like this:&lt;/p&gt;
&lt;p&gt;&lt;img src="http://blogs.u2u.be/diederik/image.axd?picture=2010%2f5%2finvariantresource.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;When a translation (or the whole culture) falls back to the invariant culture,&amp;nbsp;the user interface&amp;nbsp;looks like this (Tomato and Pumpkin are not translated):&lt;/p&gt;
&lt;p&gt;&lt;img src="http://blogs.u2u.be/diederik/image.axd?picture=2010%2f5%2finvariantculture.png" alt="" /&gt;&lt;/p&gt;
&lt;h3&gt;Missing cultures&lt;/h3&gt;
&lt;p&gt;If a specific culture is not found (e.g. 'fr-NL') then it makes sense to first check if the language exists ('fr'), and only use the invariant culture as a last resort. That's exactly what the following code does. The 'French' and 'Dzongkha' buttons in the sample application test all code paths:&lt;/p&gt;
&lt;div style="color: black; background: azure; font-family: Courier New; font-size: 9pt;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; SwitchCulture(&lt;span style="color: blue;"&gt;string&lt;/span&gt; culture)&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af;"&gt;CultureInfo&lt;/span&gt; ci = &lt;span style="color: #2b91af;"&gt;CultureInfo&lt;/span&gt;.InvariantCulture;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;try&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; ci = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;CultureInfo&lt;/span&gt;(culture);&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;catch&lt;/span&gt; (&lt;span style="color: #2b91af;"&gt;CultureNotFoundException&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;try&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;// Try language without region&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; ci = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;CultureInfo&lt;/span&gt;(culture.Substring(0, 2));&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;catch&lt;/span&gt; (&lt;span style="color: #2b91af;"&gt;Exception&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; ci = &lt;span style="color: #2b91af;"&gt;CultureInfo&lt;/span&gt;.InvariantCulture;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;finally&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af;"&gt;LocalizeDictionary&lt;/span&gt;.Instance.Culture = ci;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;this&lt;/span&gt;.CultureTextBlock.Text = ci.EnglishName;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;Source code&lt;/h2&gt;
&lt;p&gt;Last but not least, here's the source code of the small sample project: &lt;a href="http://blogs.u2u.be/diederik/file.axd?file=2010%2f5%2fU2UConsult.WPF.Localization.Sample.zip"&gt;U2UConsult.WPF.Localization.Sample.zip (2,41 mb)&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Enjoy!&lt;/p&gt;</description><pubDate>Wed, 19 May 2010 13:56:00 -1200</pubDate><comments>http://blogs.u2u.be/diederik/post/2010/05/19/Globalizing-and-Localizating-a-WPF-application.aspx#comment</comments><dc:publisher xmlns:dc="http://purl.org/dc/elements/1.1/">Diederik Krols</dc:publisher><pingback:server xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/">http://blogs.u2u.be/diederik/pingback.axd</pingback:server><pingback:target xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/">http://blogs.u2u.be/diederik/post.aspx?id=58a76f09-fb3e-42c1-82e5-dae4641949f1</pingback:target><slash:comments xmlns:slash="http://purl.org/rss/1.0/modules/slash/">0</slash:comments><trackback:ping xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">http://blogs.u2u.be/diederik/trackback.axd?id=58a76f09-fb3e-42c1-82e5-dae4641949f1</trackback:ping><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.u2u.be/diederik/post/2010/05/19/Globalizing-and-Localizating-a-WPF-application.aspx#comment</wfw:comment><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.u2u.be/diederik/syndication.axd?post=58a76f09-fb3e-42c1-82e5-dae4641949f1</wfw:commentRss></item><item><guid isPermaLink="false">http://blogs.u2u.be/diederik/post.aspx?id=fc66208e-6c06-4998-92f4-5c17c893cbf6</guid><link>http://blogs.u2u.be/diederik/post/2010/05/18/Self-Tracking-Entities-with-Validation-and-Tracking-State-Change-Notification.aspx</link><author>Diederik Krols</author><category>.NET 4.0</category><category>WPF</category><category>Entity Framework 4.0</category><title>Self-Tracking Entities with Validation and Tracking State Change Notification</title><description>&lt;p&gt;This article explains how to extend Self-Tracking Entities (STE) from Entity Framework (EF) 4.0 with validation logic and (tracking) state change notification, with just minimal impact on the T4 files. We'll build a two-tier application that submits local changes in a WPF application via a WCF service to a database table. The STE are extended with validation logic that is reusable on client and server. The client is notified when the change tracker of an entity changes its state. The tracking state is displayed to the end user as an icon. Here's the client application in action:&lt;/p&gt;
&lt;p&gt;&lt;img src="http://blogs.u2u.be/diederik/image.axd?picture=2010%2f5%2fclient.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;For more details on the foundations of building N-Tier apps with EF 4.0, please read &lt;a href="http://blogs.u2u.be/peter/post/2010/01/20/Building-an-Enterprise-Application-with-Entity-Framework-4.aspx"&gt;Peter Himschoots article&lt;/a&gt;.&lt;/p&gt;
&lt;h2&gt;Source Code&lt;/h2&gt;
&lt;p&gt;For the fans of the source-code-first approach, here it is: &lt;a href="http://blogs.u2u.be/diederik/file.axd?file=2010%2f5%2fU2UConsult.SelfTrackingEntities.Sample.zip"&gt;U2UConsult.SelfTrackingEntities.Sample.zip (622,23 kb)&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The structure of the solution is as follows:&lt;/p&gt;
&lt;p&gt;&lt;img src="http://blogs.u2u.be/diederik/image.axd?picture=2010%2f5%2fsolution.png" alt="" /&gt;&lt;/p&gt;
&lt;h2&gt;Preparation&lt;/h2&gt;
&lt;h3&gt;Database table&lt;/h3&gt;
&lt;p&gt;First you need a SQL Server table. The provided source code contains a script to generate a working copy of the SalesReason table in the AdventureWorks2008 sample database. This is its initial content:&lt;/p&gt;
&lt;p&gt;&lt;img src="http://blogs.u2u.be/diederik/image.axd?picture=2010%2f5%2ftable.png" alt="" /&gt;&lt;/p&gt;
&lt;h3&gt;Data Access Layer&lt;/h3&gt;
&lt;p&gt;When you have it, it's time to fire up Visual Studio.NET. Create a WCF web service project with an ADO.NET Entity Model. Add the SalesReason2 table to the model (I renamed the entity and entity set to &lt;em&gt;SalesReason&lt;/em&gt; and &lt;em&gt;SalesReasons&lt;/em&gt; respectively). While you're in the designer, generate the code for the ObjectContext and the Self-Tracking Entities (right click in the designer, select "&lt;em&gt;Add Code Generation Item&lt;/em&gt;", select "ADO.NET Self-Tracking Entity Generator&lt;em&gt;&lt;/em&gt;"). Add the canonical service methods to fetch the full list of SalesReasons, and to add, delete, and update an individual SalesReason. Here's an example (I personally like to combine &lt;em&gt;Add&lt;/em&gt; and &lt;em&gt;Update&lt;/em&gt; operations in a &lt;em&gt;Save&lt;/em&gt; method):&lt;/p&gt;
&lt;div style="color: black; background: azure; font-family: Courier New; font-size: 9pt;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af;"&gt;SalesReason&lt;/span&gt;&amp;gt; GetSalesReasons()&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;
&lt;p 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;AdventureWorks2008Entities&lt;/span&gt; model = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;AdventureWorks2008Entities&lt;/span&gt;())&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af;"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af;"&gt;SalesReason&lt;/span&gt;&amp;gt; result = &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;SalesReason&lt;/span&gt;&amp;gt;();&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; result.AddRange(model.SalesReasons);&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;return&lt;/span&gt; result;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; DeleteSalesReason(&lt;span style="color: #2b91af;"&gt;SalesReason&lt;/span&gt; reason)&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;
&lt;p 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;AdventureWorks2008Entities&lt;/span&gt; model = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;AdventureWorks2008Entities&lt;/span&gt;())&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; model.SalesReasons.Attach(reason);&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; model.SalesReasons.DeleteObject(reason);&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; model.SaveChanges();&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;SalesReason&lt;/span&gt; SaveSalesReason(&lt;span style="color: #2b91af;"&gt;SalesReason&lt;/span&gt; reason)&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;
&lt;p 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;AdventureWorks2008Entities&lt;/span&gt; model = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;AdventureWorks2008Entities&lt;/span&gt;())&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; reason.ModifiedDate = &lt;span style="color: #2b91af;"&gt;DateTime&lt;/span&gt;.Now;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;if&lt;/span&gt; (reason.ChangeTracker.State == &lt;span style="color: #2b91af;"&gt;ObjectState&lt;/span&gt;.Added)&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; model.SalesReasons.AddObject(reason);&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; model.SaveChanges();&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; reason.AcceptChanges();&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&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; reason;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;else&lt;/span&gt; &lt;span style="color: blue;"&gt;if&lt;/span&gt; (reason.ChangeTracker.State == &lt;span style="color: #2b91af;"&gt;ObjectState&lt;/span&gt;.Modified)&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; model.SalesReasons.ApplyChanges(reason);&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; model.SaveChanges();&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&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; reason;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;else&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&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;null&lt;/span&gt;; &lt;span style="color: green;"&gt;// or an exception&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h3&gt;Self Tracking Entities&lt;/h3&gt;
&lt;p&gt;Add a new class library to the project, call it STE. Drag the &lt;em&gt;Model.tt&lt;/em&gt; T4 template from the DAL to the STE project. Add a reference to &lt;em&gt;serialization&lt;/em&gt; in the STE project. Add a reference to the STE in the DAL project. Everything should compile again now.&lt;/p&gt;
&lt;h3&gt;WPF Client&lt;/h3&gt;
&lt;p&gt;Add a WPF application to the solution. In this client project, add a reference to the STE, and a service reference to the DAL. Add a ListBox and some buttons, with straightforward code behind:&lt;/p&gt;
&lt;div style="color: black; background: azure; font-family: Courier New; font-size: 9pt;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; RefreshSalesReasons()&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;this&lt;/span&gt;.salesReasons = &lt;span style="color: blue;"&gt;this&lt;/span&gt;.GetSalesReasons();&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;this&lt;/span&gt;.SalesReasonsListBox.ItemsSource = &lt;span style="color: blue;"&gt;this&lt;/span&gt;.salesReasons;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;ObservableCollection&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af;"&gt;SalesReason&lt;/span&gt;&amp;gt; GetSalesReasons()&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;using&lt;/span&gt; (DAL.&lt;span style="color: #2b91af;"&gt;SalesReasonServiceClient&lt;/span&gt; client = &lt;span style="color: blue;"&gt;new&lt;/span&gt; DAL.&lt;span style="color: #2b91af;"&gt;SalesReasonServiceClient&lt;/span&gt;())&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af;"&gt;ObservableCollection&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af;"&gt;SalesReason&lt;/span&gt;&amp;gt; result = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;ObservableCollection&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af;"&gt;SalesReason&lt;/span&gt;&amp;gt;();&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;foreach&lt;/span&gt; (&lt;span style="color: blue;"&gt;var&lt;/span&gt; item &lt;span style="color: blue;"&gt;in&lt;/span&gt; client.GetSalesReasons())&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; result.Add(item);&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;return&lt;/span&gt; result;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; Update_Click(&lt;span style="color: blue;"&gt;object&lt;/span&gt; sender, &lt;span style="color: #2b91af;"&gt;RoutedEventArgs&lt;/span&gt; e)&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af;"&gt;SalesReason&lt;/span&gt; reason = &lt;span style="color: blue;"&gt;this&lt;/span&gt;.SalesReasonsListBox.SelectedItem &lt;span style="color: blue;"&gt;as&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;SalesReason&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;if&lt;/span&gt; (reason != &lt;span style="color: blue;"&gt;null&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; reason.Name += &lt;span style="color: #a31515;"&gt;" (updated)"&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; Insert_Click(&lt;span style="color: blue;"&gt;object&lt;/span&gt; sender, &lt;span style="color: #2b91af;"&gt;RoutedEventArgs&lt;/span&gt; e)&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af;"&gt;SalesReason&lt;/span&gt; reason = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;SalesReason&lt;/span&gt;()&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Name = &lt;span style="color: #a31515;"&gt;"Inserted Reason"&lt;/span&gt;,&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; ReasonType = &lt;span style="color: #a31515;"&gt;"Promotion"&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; };&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; reason.MarkAsAdded();&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;this&lt;/span&gt;.salesReasons.Add(reason);&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;this&lt;/span&gt;.SalesReasonsListBox.ScrollIntoView(reason);&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; Delete_Click(&lt;span style="color: blue;"&gt;object&lt;/span&gt; sender, &lt;span style="color: #2b91af;"&gt;RoutedEventArgs&lt;/span&gt; e)&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af;"&gt;SalesReason&lt;/span&gt; reason = &lt;span style="color: blue;"&gt;this&lt;/span&gt;.SalesReasonsListBox.SelectedItem &lt;span style="color: blue;"&gt;as&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;SalesReason&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;if&lt;/span&gt; (reason != &lt;span style="color: blue;"&gt;null&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; reason.MarkAsDeleted();&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; Commit_Click(&lt;span style="color: blue;"&gt;object&lt;/span&gt; sender, &lt;span style="color: #2b91af;"&gt;RoutedEventArgs&lt;/span&gt; e)&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;using&lt;/span&gt; (DAL.&lt;span style="color: #2b91af;"&gt;SalesReasonServiceClient&lt;/span&gt; client = &lt;span style="color: blue;"&gt;new&lt;/span&gt; DAL.&lt;span style="color: #2b91af;"&gt;SalesReasonServiceClient&lt;/span&gt;())&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;foreach&lt;/span&gt; (&lt;span style="color: blue;"&gt;var&lt;/span&gt; item &lt;span style="color: blue;"&gt;in&lt;/span&gt; &lt;span style="color: blue;"&gt;this&lt;/span&gt;.salesReasons)&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;switch&lt;/span&gt; (item.ChangeTracker.State)&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p 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; &lt;span style="color: blue;"&gt;case&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;ObjectState&lt;/span&gt;.Unchanged:&lt;/p&gt;
&lt;p 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: blue;"&gt;break&lt;/span&gt;;&lt;/p&gt;
&lt;p 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; &lt;span style="color: blue;"&gt;case&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;ObjectState&lt;/span&gt;.Added:&lt;/p&gt;
&lt;p 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; client.SaveSalesReason(item);&lt;/p&gt;
&lt;p 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: blue;"&gt;break&lt;/span&gt;;&lt;/p&gt;
&lt;p 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; &lt;span style="color: blue;"&gt;case&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;ObjectState&lt;/span&gt;.Modified:&lt;/p&gt;
&lt;p 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; client.SaveSalesReason(item);&lt;/p&gt;
&lt;p 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: blue;"&gt;break&lt;/span&gt;;&lt;/p&gt;
&lt;p 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; &lt;span style="color: blue;"&gt;case&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;ObjectState&lt;/span&gt;.Deleted:&lt;/p&gt;
&lt;p 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; client.DeleteSalesReason(item);&lt;/p&gt;
&lt;p 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: blue;"&gt;break&lt;/span&gt;;&lt;/p&gt;
&lt;p 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; &lt;span style="color: blue;"&gt;default&lt;/span&gt;:&lt;/p&gt;
&lt;p 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: blue;"&gt;break&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;this&lt;/span&gt;.RefreshSalesReasons();&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;Now you're ready to extend the STE with some extra functionality.&lt;/p&gt;
&lt;h2&gt;Validation&lt;/h2&gt;
&lt;p&gt;It's nice to have some business rules that may be checked on the client (to provide immediate feedback to the user) as well as on the server (to prevent corrupt data in the database). This can be accomplished by letting the self-tracking entities implement the &lt;a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.idataerrorinfo_members.aspx"&gt;IDataErrorInfo&lt;/a&gt; interface. This interface just contains an indexer (&lt;em&gt;this[]&lt;/em&gt;) to validate an individual property, and an &lt;em&gt;Error&lt;/em&gt; property that returns the validation state of the whole instance. Letting the STE implement this interface can be easily done by adding a partial class file. The following example lets the entity complain if its name gets shorter than 5 characters:&lt;/p&gt;
&lt;div style="color: black; background: azure; font-family: Courier New; font-size: 9pt;"&gt;
&lt;p 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;SalesReason&lt;/span&gt; : &lt;span style="color: #2b91af;"&gt;IDataErrorInfo&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;
&lt;p 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; Error&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;get&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&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;[&lt;span style="color: #a31515;"&gt;"Name"&lt;/span&gt;];&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p 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; &lt;span style="color: blue;"&gt;this&lt;/span&gt;[&lt;span style="color: blue;"&gt;string&lt;/span&gt; columnName]&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;get&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;if&lt;/span&gt; (columnName == &lt;span style="color: #a31515;"&gt;"Name"&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p 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; &lt;span style="color: blue;"&gt;if&lt;/span&gt; (&lt;span style="color: blue;"&gt;string&lt;/span&gt;.IsNullOrWhiteSpace(&lt;span style="color: blue;"&gt;this&lt;/span&gt;.Name) || &lt;span style="color: blue;"&gt;this&lt;/span&gt;.Name.Length &amp;lt; 5)&lt;/p&gt;
&lt;p 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; {&lt;/p&gt;
&lt;p 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: blue;"&gt;return&lt;/span&gt; &lt;span style="color: #a31515;"&gt;"Name should have at least 5 characters."&lt;/span&gt;;&lt;/p&gt;
&lt;p 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; }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&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;string&lt;/span&gt;.Empty;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;If you add a data template to the XAML with &lt;em&gt;ValidatesOnDataErrors=true&lt;/em&gt; in the binding, then the GUI will respond immediately if a business rule is broken.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;XAML:&lt;/strong&gt;&lt;/p&gt;
&lt;div style="color: black; background: azure; font-family: Courier New; font-size: 9pt;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;TextBox&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp; &lt;span style="color: red;"&gt;Width&lt;/span&gt;&lt;span style="color: blue;"&gt;="180"&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp; &lt;span style="color: red;"&gt;Margin&lt;/span&gt;&lt;span style="color: blue;"&gt;="0 0 10 0"&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: #a31515;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;Binding&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &lt;span style="color: red;"&gt;Path&lt;/span&gt;&lt;span style="color: blue;"&gt;="Name"&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &lt;span style="color: red;"&gt;Mode&lt;/span&gt;&lt;span style="color: blue;"&gt;="TwoWay"&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &lt;span style="color: red;"&gt;UpdateSourceTrigger&lt;/span&gt;&lt;span style="color: blue;"&gt;="PropertyChanged"&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &lt;span style="color: red;"&gt;NotifyOnSourceUpdated&lt;/span&gt;&lt;span style="color: blue;"&gt;="True"&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &lt;span style="color: red;"&gt;NotifyOnTargetUpdated&lt;/span&gt;&lt;span style="color: blue;"&gt;="True"&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &lt;span style="color: red;"&gt;ValidatesOnDataErrors&lt;/span&gt;&lt;span style="color: blue;"&gt;="True"&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &lt;span style="color: red;"&gt;ValidatesOnExceptions&lt;/span&gt;&lt;span style="color: blue;"&gt;="True"/&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;TextBox&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;&lt;strong&gt;Result:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;img src="http://blogs.u2u.be/diederik/image.axd?picture=2010%2f5%2fvalidation.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;The same rule can also be checked on the server side, to prevent persisting invalid data in the underlying table:&lt;/p&gt;
&lt;div style="color: black; background: azure; font-family: Courier New; font-size: 9pt;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;SalesReason&lt;/span&gt; SaveSalesReason(&lt;span style="color: #2b91af;"&gt;SalesReason&lt;/span&gt; reason)&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;if&lt;/span&gt; (!&lt;span style="color: blue;"&gt;string&lt;/span&gt;.IsNullOrEmpty(reason.Error))&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&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;null&lt;/span&gt;; &lt;span style="color: green;"&gt;// or an exception&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ...&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;Notification of Tracking State Change&lt;/h2&gt;
&lt;p&gt;By default, an STE's tracking state can be fetched by &lt;em&gt;instance.ChangeTracker.State&lt;/em&gt;. This is NOT a dependency property, and its setter doesn't call &lt;em&gt;PropertyChanged&lt;/em&gt;. Clients can hook an event handler to the &lt;em&gt;ObjectStateChanging&lt;/em&gt; event that is raised just before the state changes (there is no &lt;em&gt;ObjectStateChang&lt;span style="text-decoration: underline;"&gt;ed&lt;/span&gt;&lt;/em&gt; event out of the box). You're free to&amp;nbsp;register even handlers&amp;nbsp;in your client, but then you need to continuously keep track of which change tracker belongs to which entity: assignment, lazy loading, and (de)serialization will make this a cumbersome and error prone endeavour.&lt;/p&gt;
&lt;p&gt;To me, it seems more logical that an entity would expose its state as a direct property, with change notification through &lt;em&gt;INotifyPropertyChanged&lt;/em&gt;. This can be achieved -again- by adding a partial class file:&lt;/p&gt;
&lt;div style="color: black; background: azure; font-family: Courier New; font-size: 9pt;"&gt;
&lt;p 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;SalesReason&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;
&lt;p 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;string&lt;/span&gt; trackingState;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; [&lt;span style="color: #2b91af;"&gt;DataMember&lt;/span&gt;]&lt;/p&gt;
&lt;p 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; TrackingState&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;get&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&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;.trackingState;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;set&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&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;.trackingState != &lt;span style="color: blue;"&gt;value&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p 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; &lt;span style="color: blue;"&gt;this&lt;/span&gt;.trackingState = &lt;span style="color: blue;"&gt;value&lt;/span&gt;;&lt;/p&gt;
&lt;p 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; &lt;span style="color: blue;"&gt;this&lt;/span&gt;.OnTrackingStateChanged();&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;partial&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; SetTrackingState(&lt;span style="color: blue;"&gt;string&lt;/span&gt; newTrackingState)&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;this&lt;/span&gt;.TrackingState = newTrackingState;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p 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;virtual&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; OnTrackingStateChanged()&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;if&lt;/span&gt; (_propertyChanged != &lt;span style="color: blue;"&gt;null&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; _propertyChanged(&lt;span style="color: blue;"&gt;this&lt;/span&gt;, &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;PropertyChangedEventArgs&lt;/span&gt;(&lt;span style="color: #a31515;"&gt;"TrackingState"&lt;/span&gt;));&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;The only thing you need to do now, is to make sure that the &lt;em&gt;SetTrackingState&lt;/em&gt; method is called at the right moment. The end of the &lt;em&gt;HandleObjectStateChanging&lt;/em&gt; looks like a nice candidate. Unfortunately this requires a modification of the code that was generated by the T4 template. For performance reasons I used a partial method for this. This is the extract from the &lt;em&gt;SalesReason.cs&lt;/em&gt; file:&lt;/p&gt;
&lt;div style="color: black; background: azure; font-family: Courier New; font-size: 9pt;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: green;"&gt;// This is a new definition&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;partial&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; SetTrackingState(&lt;span style="color: blue;"&gt;string&lt;/span&gt; trackingState);&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: green;"&gt;//&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; HandleObjectStateChanging(&lt;span style="color: blue;"&gt;object&lt;/span&gt; sender, &lt;span style="color: #2b91af;"&gt;ObjectStateChangingEventArgs&lt;/span&gt; e)&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;//&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;this&lt;/span&gt;.SetTrackingState(e.NewState.ToString()); &lt;span style="color: green;"&gt;// This is a new line&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;//&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;if&lt;/span&gt; (e.NewState == &lt;span style="color: #2b91af;"&gt;ObjectState&lt;/span&gt;.Deleted)&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; ClearNavigationProperties();&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;Just modifiying the generated code is probably not good enough: if later on you need to update your STE (e.g. after adding a column to the underlying table), the modifications will get overriden again. So you might want to modify the source code of the T4 template (search for the &lt;em&gt;HandleObjectStateChanding&lt;/em&gt; method and adapt the source code). Fortunately this is a no-brainer: most of the T4 template is just like a C# source code file, but without IntelliSense. The rest of the file&amp;nbsp;looks more like&amp;nbsp;classic ASP - ugh.&lt;/p&gt;
&lt;p&gt;Anyway, you end up with a &lt;em&gt;TrackingStatus&lt;/em&gt; property to which you can bind user interface elements, wiith or without a &lt;em&gt;converter&lt;/em&gt; in between. In the sample application I bound an image to the tracking state:&lt;/p&gt;
&lt;div style="color: black; background: azure; font-family: Courier New; font-size: 9pt;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;Image&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp; &lt;span style="color: red;"&gt;Source&lt;/span&gt;&lt;span style="color: blue;"&gt;="{&lt;/span&gt;&lt;span style="color: #a31515;"&gt;Binding&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &lt;span style="color: red;"&gt;Path&lt;/span&gt;&lt;span style="color: blue;"&gt;=TrackingState,&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &lt;span style="color: red;"&gt;Converter&lt;/span&gt;&lt;span style="color: blue;"&gt;={&lt;/span&gt;&lt;span style="color: #a31515;"&gt;StaticResource&lt;/span&gt;&lt;span style="color: red;"&gt; ImageConverter&lt;/span&gt;&lt;span style="color: blue;"&gt;}}"&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp; &lt;span style="color: red;"&gt;Width&lt;/span&gt;&lt;span style="color: blue;"&gt;="24"&lt;/span&gt;&lt;span style="color: red;"&gt; Height&lt;/span&gt;&lt;span style="color: blue;"&gt;="24" /&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;Here's how it looks like:&lt;/p&gt;
&lt;p&gt;&lt;img src="http://blogs.u2u.be/diederik/image.axd?picture=2010%2f5%2ftracking.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;In general, I think there are not enough partial methods defined and called in the Entity Framework T4 templates. To be frank: 'not enough' is an understatement: I didn't find a single partial method.&lt;/p&gt;</description><pubDate>Tue, 18 May 2010 14:51:00 -1200</pubDate><comments>http://blogs.u2u.be/diederik/post/2010/05/18/Self-Tracking-Entities-with-Validation-and-Tracking-State-Change-Notification.aspx#comment</comments><dc:publisher xmlns:dc="http://purl.org/dc/elements/1.1/">Diederik Krols</dc:publisher><pingback:server xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/">http://blogs.u2u.be/diederik/pingback.axd</pingback:server><pingback:target xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/">http://blogs.u2u.be/diederik/post.aspx?id=fc66208e-6c06-4998-92f4-5c17c893cbf6</pingback:target><slash:comments xmlns:slash="http://purl.org/rss/1.0/modules/slash/">0</slash:comments><trackback:ping xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">http://blogs.u2u.be/diederik/trackback.axd?id=fc66208e-6c06-4998-92f4-5c17c893cbf6</trackback:ping><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.u2u.be/diederik/post/2010/05/18/Self-Tracking-Entities-with-Validation-and-Tracking-State-Change-Notification.aspx#comment</wfw:comment><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.u2u.be/diederik/syndication.axd?post=fc66208e-6c06-4998-92f4-5c17c893cbf6</wfw:commentRss></item><item><guid isPermaLink="false">http://blogs.u2u.be/peter/post.aspx?id=aaba17fc-da4d-4ea3-b1f1-8c4dee884084</guid><link>http://blogs.u2u.be/peter/post/2010/05/11/Keeping-a-file-checked-out-after-check-in-with-Team-System.aspx</link><author>Peter Himschoot</author><category>Team System</category><category>VS2010</category><title>Keeping a file checked out after check-in with Team System</title><description>&lt;p&gt;This week I got a question whether it is still possible to keep some file checked-out during a check-in. This is something you can do with Source Safe for example. To be honest I had no idea if this is possible, so I decided to have a look. So the answer is yes (although in general I think this is dangerous because this way people tend to check out files for long periods…).&lt;/p&gt;  &lt;p&gt;Simply go to options and check the fourth check-box from the top.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.u2u.be/peter/image.axd?picture=image_221.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://blogs.u2u.be/peter/image.axd?picture=image_thumb_221.png" width="464" height="284" /&gt;&lt;/a&gt;&lt;/p&gt;</description><pubDate>Tue, 11 May 2010 06:08:59 -1200</pubDate><comments>http://blogs.u2u.be/peter/post/2010/05/11/Keeping-a-file-checked-out-after-check-in-with-Team-System.aspx#comment</comments><dc:publisher xmlns:dc="http://purl.org/dc/elements/1.1/">Peter Himschoot</dc:publisher><pingback:server xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/">http://blogs.u2u.be/peter/pingback.axd</pingback:server><pingback:target xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/">http://blogs.u2u.be/peter/post.aspx?id=aaba17fc-da4d-4ea3-b1f1-8c4dee884084</pingback:target><slash:comments xmlns:slash="http://purl.org/rss/1.0/modules/slash/">0</slash:comments><trackback:ping xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">http://blogs.u2u.be/peter/trackback.axd?id=aaba17fc-da4d-4ea3-b1f1-8c4dee884084</trackback:ping><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.u2u.be/peter/post/2010/05/11/Keeping-a-file-checked-out-after-check-in-with-Team-System.aspx#comment</wfw:comment><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.u2u.be/peter/syndication.axd?post=aaba17fc-da4d-4ea3-b1f1-8c4dee884084</wfw:commentRss></item><item><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7473596</guid><link>http://weblogs.asp.net/jan/archive/2010/05/07/sharepoint-2010-bdc-model-deployment-issue-the-default-web-application-could-not-be-determined.aspx</link><category domain="http://weblogs.asp.net/jan/archive/tags/SharePoint/default.aspx">SharePoint</category><title>SharePoint 2010 BDC Model Deployment Issue: “The default web application could not be determined.”</title><description>&lt;P&gt;Yesterday I tried to deploy a Business Data Connectivity Model project created in Visual Studio 2010 to my SharePoint 2010 test server (all RTM versions), but during the deployment of the solution, SharePoint threw my following error:&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;Add Solution:&lt;BR&gt;&amp;nbsp; Adding solution 'BCSDemo2.wsp'...&lt;BR&gt;&amp;nbsp; Deploying solution 'BCSDemo2.wsp'...&lt;BR&gt;Error occurred in deployment step 'Add Solution': The default web application could not be determined. Set the SiteUrl property in feature BCSDemo2_Feature1 to the URL of the desired site and retry activation.&lt;BR&gt;Parameter name: properties&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;A little bit of searching on the internet taught me that I was not the only one having this issue, actually &lt;A href="http://blogs.msdn.com/pandrew" mce_href="http://blogs.msdn.com/pandrew"&gt;Paul Andrew&lt;/A&gt; describes how to solve it in &lt;A href="http://blogs.msdn.com/pandrew/archive/2010/04/08/deploying-an-external-content-type-error-the-default-web-application-could-not-be-determined.aspx" mce_href="http://blogs.msdn.com/pandrew/archive/2010/04/08/deploying-an-external-content-type-error-the-default-web-application-could-not-be-determined.aspx"&gt;this post&lt;/A&gt;. Although Paul describes what to do, his explanation is not, let’s say, very elaborate. :-) So let’s describe the steps a little bit more in detail:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Create a new Business Data Connectivity Model project in Visual Studio 2010 and (optionally) implement all your code, change the model etc. When you try to deploy you get the error mentioned above.&lt;/LI&gt;
&lt;LI&gt;To fix it, in the &lt;STRONG&gt;Solution Explorer&lt;/STRONG&gt;, navigate to and open the &lt;STRONG&gt;Feature1.Template.xml &lt;/STRONG&gt;file (the name could be different if you decided to give your feature a different name of course).&lt;BR&gt;&lt;IMG src="http://storage.tielens.name/BDCModelIssue.png" mce_src="http://storage.tielens.name/BDCModelIssue.png"&gt;&lt;/LI&gt;
&lt;LI&gt;Add the following XML in the Feature element that’s already there (replace the Value with the URL of your site of course):&lt;BR&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &amp;lt;Properties&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;Property Key='SiteUrl' Value='http://spf.u2ucourse.com'/&amp;gt;&lt;BR&gt;&amp;nbsp; &amp;lt;/Properties&amp;gt;&lt;BR&gt;&lt;/FONT&gt;&lt;BR&gt;The resulting XML should look like:&lt;BR&gt;&lt;BR&gt;&lt;FONT face="courier new,courier"&gt;&amp;lt;?xml version="1.0" encoding="utf-8" ?&amp;gt;&lt;BR&gt;&amp;lt;Feature xmlns="&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;http://schemas.microsoft.com/sharepoint/&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;"&amp;gt;&lt;BR&gt;&amp;nbsp; &amp;lt;Properties&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;Property Key='SiteUrl' Value='http://spf.u2ucourse.com'/&amp;gt;&lt;BR&gt;&amp;nbsp; &amp;lt;/Properties&amp;gt;&lt;BR&gt;&amp;lt;/Feature&amp;gt;&lt;/FONT&gt;&lt;/LI&gt;
&lt;LI&gt;Deploy the solution, now without any issues. :-)&lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;What happens now, is that when Visual Studio creates the SharePoint Solution (the WSP file), it will use the Feature template XML to generate the Feature manifest, which will now include the missing property.&lt;/P&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7473596" width="1" height="1"&gt;</description><pubDate>Fri, 07 May 2010 09:41:00 Z</pubDate><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Jan Tielens</dc:creator><slash:comments xmlns:slash="http://purl.org/rss/1.0/modules/slash/">29</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/jan/rsscomments.aspx?PostID=7473596</wfw:commentRss><comments>http://weblogs.asp.net/jan/archive/2010/05/07/sharepoint-2010-bdc-model-deployment-issue-the-default-web-application-could-not-be-determined.aspx#comments</comments></item><item><guid isPermaLink="false">http://blogs.u2u.be/peter/post.aspx?id=286bc196-908f-467e-8938-e19e335cd8dd</guid><link>http://blogs.u2u.be/peter/post/2010/05/03/How-to-upgrade-from-Windows-XP.aspx</link><author>Peter Himschoot</author><title>How to upgrade from Windows XP</title><description>&lt;a title="Dilbert.com" href="http://dilbert.com/strips/comic/2010-05-04/"&gt;&lt;img border="0" alt="Dilbert.com" src="http://dilbert.com/dyn/str_strip/000000000/00000000/0000000/000000/80000/9000/000/89034/89034.strip.gif" /&gt;&lt;/a&gt;   &lt;p&gt;Sorry, just lo-ove this dilbert cartoon!&lt;/p&gt;</description><pubDate>Mon, 03 May 2010 15:39:59 -1200</pubDate><comments>http://blogs.u2u.be/peter/post/2010/05/03/How-to-upgrade-from-Windows-XP.aspx#comment</comments><dc:publisher xmlns:dc="http://purl.org/dc/elements/1.1/">Peter Himschoot</dc:publisher><pingback:server xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/">http://blogs.u2u.be/peter/pingback.axd</pingback:server><pingback:target xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/">http://blogs.u2u.be/peter/post.aspx?id=286bc196-908f-467e-8938-e19e335cd8dd</pingback:target><slash:comments xmlns:slash="http://purl.org/rss/1.0/modules/slash/">1</slash:comments><trackback:ping xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">http://blogs.u2u.be/peter/trackback.axd?id=286bc196-908f-467e-8938-e19e335cd8dd</trackback:ping><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.u2u.be/peter/post/2010/05/03/How-to-upgrade-from-Windows-XP.aspx#comment</wfw:comment><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.u2u.be/peter/syndication.axd?post=286bc196-908f-467e-8938-e19e335cd8dd</wfw:commentRss></item><item><guid isPermaLink="false">http://blogs.u2u.be/diederik/post.aspx?id=d9ddb951-a342-49e5-8998-50fb13d87c49</guid><link>http://blogs.u2u.be/diederik/post/2010/05/02/WPF-40-and-Windows-7-get-more-Functionality-per-Square-Inch.aspx</link><author>Diederik Krols</author><category>.NET 4.0</category><category>WPF</category><title>WPF 4.0 and Windows 7: get more Functionality per Square Inch</title><description>&lt;p&gt;The Windows 7 taskbar&amp;nbsp;comes with&amp;nbsp;nice features like a thumbnail preview with clickable thumb buttons, a progress bar in the taskbar item, and jump list items and task items in its context menu. Access to the shell integration API from the managed world is done through the &lt;em&gt;System.Windows.Shell&lt;/em&gt; namespace&amp;nbsp;that comes with&amp;nbsp;.NET 4.0. This namespace decorates &lt;em&gt;Window&lt;/em&gt; and &lt;em&gt;Application&lt;/em&gt; with some new dependency properties, so you better make use of it.&lt;/p&gt;
&lt;p&gt;Here's an overview of the &lt;em&gt;TaskbarItemIfo&lt;/em&gt; object model:&lt;/p&gt;
&lt;p&gt;&lt;img src="http://blogs.u2u.be/diederik/image.axd?picture=2010%2f5%2fTaxonomy.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;The screenshot comes from a small application to demonstrate this object model. Here's how the main form looks like:&lt;/p&gt;
&lt;p&gt;&lt;img src="http://blogs.u2u.be/diederik/image.axd?picture=2010%2f5%2fmaxi1.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;Chart&lt;/em&gt; and &lt;em&gt;Pie&lt;/em&gt; buttons switch the displayed chart type, the &lt;em&gt;Recalculate&lt;/em&gt; button simulates a long running operation (just an excuse for showing the progress bar). Here's the alternative view:&lt;/p&gt;
&lt;p&gt;&lt;img src="http://blogs.u2u.be/diederik/image.axd?picture=2010%2f5%2fmaxi2.png" alt="" /&gt;&lt;/p&gt;
&lt;h2&gt;TaskBarItemInfo&lt;/h2&gt;
&lt;p&gt;When hovering with the mouse over the taskbar item, it shows &lt;span style="text-decoration: underline;"&gt;only the relevant part of the screen&lt;/span&gt; in its thumbnail, and operational buttons&amp;nbsp;- just like in the first image above.&lt;/p&gt;
&lt;p&gt;Everything is defined in the xaml as follows:&lt;/p&gt;
&lt;div style="color: black; background: azure; font-family: Courier New; font-size: 9pt;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;Window.TaskbarItemInfo&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: #a31515;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;TaskbarItemInfo&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &lt;span style="color: red;"&gt;x&lt;/span&gt;&lt;span style="color: blue;"&gt;:&lt;/span&gt;&lt;span style="color: red;"&gt;Name&lt;/span&gt;&lt;span style="color: blue;"&gt;="taskBarItemInfo1"&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &lt;span style="color: red;"&gt;ThumbnailClipMargin&lt;/span&gt;&lt;span style="color: blue;"&gt;="5 95 100 65"&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &lt;span style="color: red;"&gt;Description&lt;/span&gt;&lt;span style="color: blue;"&gt;="Taskbar Item Info Sample"&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: #a31515;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;TaskbarItemInfo.ThumbButtonInfos&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: #a31515;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;ThumbButtonInfoCollection&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: #a31515;"&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; &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;ThumbButtonInfo&lt;/span&gt;&lt;/p&gt;
&lt;p 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; &lt;span style="color: red;"&gt;Click&lt;/span&gt;&lt;span style="color: blue;"&gt;="Recalculate_Click"&lt;/span&gt;&lt;/p&gt;
&lt;p 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; &lt;span style="color: red;"&gt;Description&lt;/span&gt;&lt;span style="color: blue;"&gt;="Recalculate"&lt;/span&gt;&lt;/p&gt;
&lt;p 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; &lt;span style="color: red;"&gt;ImageSource&lt;/span&gt;&lt;span style="color: blue;"&gt;="/Assets/Images/refresh.png" /&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: #a31515;"&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; &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;ThumbButtonInfo&lt;/span&gt;&lt;/p&gt;
&lt;p 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; &lt;span style="color: red;"&gt;Click&lt;/span&gt;&lt;span style="color: blue;"&gt;="BarChart_Click"&lt;/span&gt;&lt;/p&gt;
&lt;p 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; &lt;span style="color: red;"&gt;Description&lt;/span&gt;&lt;span style="color: blue;"&gt;="Bar Chart"&lt;/span&gt;&lt;/p&gt;
&lt;p 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; &lt;span style="color: red;"&gt;ImageSource&lt;/span&gt;&lt;span style="color: blue;"&gt;="/Assets/Images/chart.png" /&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: #a31515;"&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; &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;ThumbButtonInfo&lt;/span&gt;&lt;/p&gt;
&lt;p 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; &lt;span style="color: red;"&gt;Click&lt;/span&gt;&lt;span style="color: blue;"&gt;="Pie_Click"&lt;/span&gt;&lt;/p&gt;
&lt;p 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; &lt;span style="color: red;"&gt;Description&lt;/span&gt;&lt;span style="color: blue;"&gt;="Pie Chart"&lt;/span&gt;&lt;/p&gt;
&lt;p 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; &lt;span style="color: red;"&gt;ImageSource&lt;/span&gt;&lt;span style="color: blue;"&gt;="/Assets/Images/pie.png"/&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: #a31515;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;ThumbButtonInfoCollection&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: #a31515;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;TaskbarItemInfo.ThumbButtonInfos&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: #a31515;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;TaskbarItemInfo&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;Window.TaskbarItemInfo&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;I hooked the thumb buttons to the same click event as their counterparts on the main form, but everything also works with command bindings. In fact, &lt;strong&gt;the taskbar item has more FSI than the original app&lt;/strong&gt;, that's &lt;em&gt;Functionality per Square Inch&lt;/em&gt;.&lt;/p&gt;
&lt;h2&gt;Overlay&lt;/h2&gt;
&lt;p&gt;The taskbar icon can be decorated with an extra image to inform the user about the state of the application. This overlay image can be set in&amp;nbsp;XAML (through data binding, if you want), but in a lot of cases you'll want to change the image from source code. Here's how this is done:&lt;/p&gt;
&lt;div style="color: black; background: azure; font-family: Courier New; font-size: 9pt;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;this&lt;/span&gt;.taskBarItemInfo1.Overlay = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;BitmapImage&lt;/span&gt;(&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;Uri&lt;/span&gt;(&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #a31515;"&gt;@"..\..\Assets\Images\pie.png"&lt;/span&gt;,&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af;"&gt;UriKind&lt;/span&gt;.Relative));&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;&lt;strong&gt;ProgressBar&lt;/strong&gt;&lt;/h2&gt;
&lt;p&gt;The task bar item can be decorated with a progress bar. This is how it's done from C#:&lt;/p&gt;
&lt;div style="color: black; background: azure; font-family: Courier New; font-size: 9pt;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;this&lt;/span&gt;.taskBarItemInfo1.ProgressState = &lt;span style="color: #2b91af;"&gt;TaskbarItemProgressState&lt;/span&gt;.Normal;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;for&lt;/span&gt; (&lt;span style="color: blue;"&gt;double&lt;/span&gt; d = 0; d &amp;lt; 80; d++)&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;this&lt;/span&gt;.taskBarItemInfo1.ProgressValue = d / 100;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af;"&gt;Thread&lt;/span&gt;.Sleep(50);&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;this&lt;/span&gt;.taskBarItemInfo1.ProgressState = &lt;span style="color: #2b91af;"&gt;TaskbarItemProgressState&lt;/span&gt;.Paused;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: #2b91af;"&gt;Thread&lt;/span&gt;.Sleep(2000);&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;this&lt;/span&gt;.taskBarItemInfo1.ProgressState = &lt;span style="color: #2b91af;"&gt;TaskbarItemProgressState&lt;/span&gt;.Error;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;for&lt;/span&gt; (&lt;span style="color: blue;"&gt;double&lt;/span&gt; d = 80; d &amp;gt; 0; d--)&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;this&lt;/span&gt;.taskBarItemInfo1.ProgressValue = d / 100;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af;"&gt;Thread&lt;/span&gt;.Sleep(50);&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;this&lt;/span&gt;.taskBarItemInfo1.ProgressState = &lt;span style="color: #2b91af;"&gt;TaskbarItemProgressState&lt;/span&gt;.None;&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;Here's the progress bar in action:&lt;/p&gt;
&lt;p&gt;&lt;img src="http://blogs.u2u.be/diederik/image.axd?picture=2010%2f5%2fmini3.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;ProgressValue&lt;/em&gt; is a value between 0 and 1, while &lt;em&gt;ProgressState&lt;/em&gt; is one of the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;None: &lt;/strong&gt;well - no progress bar,&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Normal: &lt;/strong&gt;a green bar,&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Paused: &lt;/strong&gt;a yellow bar,&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Error: &lt;/strong&gt;a red bar,&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Indeteminate: &lt;/strong&gt;a zigzagging green bar (very nice in combination with a wait cursor on the form)&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;Source Code&lt;/h2&gt;
&lt;p&gt;As always you get the full source code (VS 2010): &lt;a href="http://blogs.u2u.be/diederik/file.axd?file=2010%2f5%2fU2UConsult.Windows7.Sample.zip"&gt;U2UConsult.Windows7.Sample.zip (864,23 kb)&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Enjoy!&lt;/p&gt;</description><pubDate>Sun, 02 May 2010 20:37:00 -1200</pubDate><comments>http://blogs.u2u.be/diederik/post/2010/05/02/WPF-40-and-Windows-7-get-more-Functionality-per-Square-Inch.aspx#comment</comments><dc:publisher xmlns:dc="http://purl.org/dc/elements/1.1/">Diederik Krols</dc:publisher><pingback:server xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/">http://blogs.u2u.be/diederik/pingback.axd</pingback:server><pingback:target xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/">http://blogs.u2u.be/diederik/post.aspx?id=d9ddb951-a342-49e5-8998-50fb13d87c49</pingback:target><slash:comments xmlns:slash="http://purl.org/rss/1.0/modules/slash/">0</slash:comments><trackback:ping xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">http://blogs.u2u.be/diederik/trackback.axd?id=d9ddb951-a342-49e5-8998-50fb13d87c49</trackback:ping><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.u2u.be/diederik/post/2010/05/02/WPF-40-and-Windows-7-get-more-Functionality-per-Square-Inch.aspx#comment</wfw:comment><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.u2u.be/diederik/syndication.axd?post=d9ddb951-a342-49e5-8998-50fb13d87c49</wfw:commentRss></item><item><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7453772</guid><link>http://weblogs.asp.net/jan/archive/2010/04/22/accessing-sharepoint-2010-data-with-rest-odata-on-windows-phone-7.aspx</link><category domain="http://weblogs.asp.net/jan/archive/tags/SharePoint/default.aspx">SharePoint</category><category domain="http://weblogs.asp.net/jan/archive/tags/silverlight/default.aspx">silverlight</category><category domain="http://weblogs.asp.net/jan/archive/tags/WindowsPhone/default.aspx">WindowsPhone</category><title>Accessing SharePoint 2010 Data with REST/OData on Windows Phone 7</title><description>&lt;P mce_keep="true"&gt;Consuming SharePoint 2010 data in Windows Phone 7 applications using the &lt;A href="http://developer.windowsphone.com/" mce_href="http://developer.windowsphone.com/"&gt;CTP version of the developer tools&lt;/A&gt; is quite a challenge. The issue is that the SharePoint 2010 data is not anonymously available; users need to authenticate to be able to access the data. When I first tried to access SharePoint 2010 data from my first Hello-World-type Windows Phone 7 application I thought “Hey, this should be easy!” because Windows Phone 7 development based on Silverlight and SharePoint 2010 has a Client Object Model for Silverlight. Unfortunately you can’t use the Client Object Model of SharePoint 2010 on the Windows Phone platform; there’s a reference to an assembly that’s not available (System.Windows.Browser).&lt;/P&gt;
&lt;P mce_keep="true"&gt;My second thought was “OK, no problem!” because SharePoint 2010 also exposes a REST/OData API to access SharePoint data. Using the REST API in SharePoint 2010 is as easy as making a web request for a URL (in which you specify the data you’d like to retrieve), e.g. &lt;A href="http://yoursiteurl/_vti_bin/listdata.svc/Announcements"&gt;http://yoursiteurl/_vti_bin/listdata.svc/Announcements&lt;/A&gt;. This is very easy to accomplish in a Silverlight application that’s running in the context of a page in a SharePoint site, because the credentials of the currently logged on user are automatically picked up and passed to the WCF service. But a Windows Phone application is of course running outside of the SharePoint site’s page, so the application should build credentials that have to be passed to SharePoint’s WCF service. This turns out to be a small challenge in Silverlight 3, &lt;A href="http://mark.mymonster.nl/2009/03/22/silverlight-3-webclient-webrequest-and-wcf-calls-using-credentials/" mce_href="http://mark.mymonster.nl/2009/03/22/silverlight-3-webclient-webrequest-and-wcf-calls-using-credentials/"&gt;the WebClient doesn’t support authentication&lt;/A&gt;; there is a Credentials property but when you set it and make the request you get a NotImplementedException exception.&lt;/P&gt;
&lt;P mce_keep="true"&gt;Probably this issued will be solved in the very near future, since Silverlight 4 does support authentication, and there’s already a &lt;A href="http://blogs.msdn.com/astoriateam/archive/2010/04/20/wcf-data-services-for-silverlight-4-now-available-for-download.aspx" mce_href="http://blogs.msdn.com/astoriateam/archive/2010/04/20/wcf-data-services-for-silverlight-4-now-available-for-download.aspx"&gt;WCF Data Services download&lt;/A&gt; that uses this new platform feature of Silverlight 4. So when Windows Phone platform switches to Silverlight 4, you can just use the WebClient to get the data. Even more, if the &lt;A href="http://www.odata.org/developers/odata-sdk" mce_href="http://www.odata.org/developers/odata-sdk"&gt;OData Client Library for Windows Phone 7&lt;/A&gt; gets updated after that, things should get even easier! &lt;EM&gt;By the way: the things I’m writing in this paragraph are just assumptions that I make which make a lot of sense IMHO, I don’t have any info all of this will happen, but I really hope so.&lt;/EM&gt;&lt;/P&gt;
&lt;P mce_keep="true"&gt;So are SharePoint developers out of the Windows Phone development game until they get this fixed? Well luckily not, when the HttpWebRequest class is being used instead, you can pass credentials! Using the HttpWebRequest class is slightly more complex than using the WebClient class, but the end result is that you have access to your precious SharePoint 2010 data. The following code snippet is getting all the announcements of an Annoucements list in a SharePoint site:&lt;/P&gt;
&lt;P mce_keep="true"&gt;&lt;FONT face="courier new,courier"&gt;HttpWebRequest webReq = &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; (HttpWebRequest)HttpWebRequest.Create("&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;http://yoursite/_vti_bin/listdata.svc/Announcements&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;");&lt;BR&gt;webReq.Credentials = new NetworkCredential("username", "password");&lt;/FONT&gt;&lt;/P&gt;
&lt;P mce_keep="true"&gt;&lt;FONT face="courier new,courier"&gt;webReq.BeginGetResponse(&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; (result) =&amp;gt; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; HttpWebRequest asyncReq = (HttpWebRequest)result.AsyncState;&lt;/FONT&gt;&lt;/P&gt;
&lt;P mce_keep="true"&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; XDocument xdoc = XDocument.Load(&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ((HttpWebResponse)asyncReq.EndGetResponse(result)).GetResponseStream());&lt;/FONT&gt;&lt;/P&gt;
&lt;P mce_keep="true"&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; XNamespace ns = "&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;http://www.w3.org/2005/Atom&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;";&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; var items = from item in xdoc.Root.Elements(ns + "entry")&lt;BR&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; select new { Title = item.Element(ns + "title").Value };&lt;/FONT&gt;&lt;/P&gt;
&lt;P mce_keep="true"&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; this.Dispatcher.BeginInvoke(() =&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; foreach (var item in items)&lt;BR&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; MessageBox.Show(item.Title);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; });&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }, webReq);&lt;/FONT&gt;&lt;/P&gt;
&lt;P mce_keep="true"&gt;When you try this in a Windows Phone 7 application, make sure you add a reference to the System.Xml.Linq assembly, because the code uses Linq to XML to parse the resulting Atom feed, so the Title of every announcement is being displayed in a MessageBox. Check out my previous post if you’d like to see a more polished sample Windows Phone 7 application that displays SharePoint 2010 data.&lt;BR&gt;When you plan to use this technique, it’s of course a good idea to encapsulate the code doing the request, so it becomes really easy to get the data that you need. In the following code snippet you can find the GetAtomFeed method that gets the contents of any Atom feed, even if you need to authenticate to get access to the feed.&lt;/P&gt;
&lt;P mce_keep="true"&gt;delegate void GetAtomFeedCallback(Stream responseStream);&lt;/P&gt;
&lt;P mce_keep="true"&gt;&lt;FONT face="courier new,courier"&gt;public MainPage()&lt;BR&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; InitializeComponent();&lt;/FONT&gt;&lt;/P&gt;
&lt;P mce_keep="true"&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; SupportedOrientations = SupportedPageOrientation.Portrait | &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; SupportedPageOrientation.Landscape;&lt;/FONT&gt;&lt;/P&gt;
&lt;P mce_keep="true"&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; string url = "&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;http://yoursite/_vti_bin/listdata.svc/Announcements&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;";&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; string username = "username";&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; string password = "password";&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; string domain = "";&lt;/FONT&gt;&lt;/P&gt;
&lt;P mce_keep="true"&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; GetAtomFeed(url, username, password, domain, (s) =&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; XNamespace ns = "&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;http://www.w3.org/2005/Atom&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;";&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; XDocument xdoc = XDocument.Load(s);&lt;/FONT&gt;&lt;/P&gt;
&lt;P mce_keep="true"&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; var items = from item in xdoc.Root.Elements(ns + "entry")&lt;BR&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; select new { Title = item.Element(ns + "title").Value };&lt;/FONT&gt;&lt;/P&gt;
&lt;P mce_keep="true"&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; this.Dispatcher.BeginInvoke(() =&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; foreach (var item in items)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&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; MessageBox.Show(item.Title);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; });&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; });&lt;BR&gt;}&lt;/FONT&gt;&lt;/P&gt;
&lt;P mce_keep="true"&gt;&lt;FONT face="courier new,courier"&gt;private static void GetAtomFeed(string url, string username, &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; string password, string domain, GetAtomFeedCallback cb)&lt;BR&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; HttpWebRequest webReq = (HttpWebRequest)HttpWebRequest.Create(url);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; webReq.Credentials = new NetworkCredential(username, password, domain);&lt;/FONT&gt;&lt;/P&gt;
&lt;P mce_keep="true"&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; webReq.BeginGetResponse(&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; (result) =&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; HttpWebRequest asyncReq = (HttpWebRequest)result.AsyncState;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; HttpWebResponse resp = (HttpWebResponse)asyncReq.EndGetResponse(result);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cb(resp.GetResponseStream());&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }, webReq);&lt;BR&gt;}&lt;/FONT&gt;&lt;/P&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7453772" width="1" height="1"&gt;</description><pubDate>Thu, 22 Apr 2010 06:48:00 Z</pubDate><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Jan Tielens</dc:creator><slash:comments xmlns:slash="http://purl.org/rss/1.0/modules/slash/">25</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/jan/rsscomments.aspx?PostID=7453772</wfw:commentRss><comments>http://weblogs.asp.net/jan/archive/2010/04/22/accessing-sharepoint-2010-data-with-rest-odata-on-windows-phone-7.aspx#comments</comments></item><item><guid isPermaLink="false">http://blogs.u2u.be/diederik/post.aspx?id=174c82fa-ddde-411c-b0f4-df24bb3fc926</guid><link>http://blogs.u2u.be/diederik/post/2010/04/19/WCF-Data-Services-40-in-less-than-5-minutes.aspx</link><author>Diederik Krols</author><category>.NET 4.0</category><category>WPF</category><category>Entity Framework 4.0</category><title>WCF Data Services 4.0 in less than 5 minutes</title><description>&lt;p&gt;WCF Data Services 4.0 (formerly known as ADO.NET Data Services, formerly known as Astoria) is one of the ways to expose an Entity Data Model from Entity Framework 4.0 in a RESTful / &lt;a href="http://www.odata.org/"&gt;OData&lt;/a&gt; way. This article explains how to create such a data service and how to consume it with a browser and with a WPF client.&lt;/p&gt;
&lt;h2&gt;The Data Service&lt;/h2&gt;
&lt;p&gt;Start with an empty ASP.NET Application:&lt;/p&gt;
&lt;p&gt;&lt;img src="http://blogs.u2u.be/diederik/image.axd?picture=2010%2f4%2fEmpty+ASPNET+Web+Application.png" alt="" width="50%" /&gt;&lt;/p&gt;
&lt;p&gt;Add a WCF Data Service to it:&lt;/p&gt;
&lt;p&gt;&lt;img src="http://blogs.u2u.be/diederik/image.axd?picture=2010%2f4%2fWCF+Data+Service.png" alt="" width="50%" /&gt;&lt;/p&gt;
&lt;p&gt;Also add an Entity Data Model to the ASP.NET project:&lt;/p&gt;
&lt;p&gt;&lt;img src="http://blogs.u2u.be/diederik/image.axd?picture=2010%2f4%2fEntityDataModel.png" alt="" width="50%" /&gt;&lt;/p&gt;
&lt;p&gt;Follow the Model Wizard to create a model containing entities on top of the &lt;em&gt;Employee&lt;/em&gt; and &lt;em&gt;Person&lt;/em&gt; tables from the &lt;em&gt;AdventureWorks2008&lt;/em&gt; database:&lt;/p&gt;
&lt;table border="0"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;img src="http://blogs.u2u.be/diederik/image.axd?picture=2010%2f4%2fModelWizard1.png" alt="" width="300" /&gt;&lt;/td&gt;
&lt;td&gt;&lt;img src="http://blogs.u2u.be/diederik/image.axd?picture=2010%2f4%2fModelWizard2.png" alt="" width="300" /&gt;&lt;/td&gt;
&lt;td&gt;&lt;img src="http://blogs.u2u.be/diederik/image.axd?picture=2010%2f4%2fModelWizard3.png" alt="" width="300" /&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;In the designer, you should have something like this:&lt;/p&gt;
&lt;p&gt;&lt;img src="http://blogs.u2u.be/diederik/image.axd?picture=2010%2f4%2fEntities.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;A lot of code was generated, let's add our own 50ct in the service's code behind. First let it inherit from &lt;em&gt;DataService&amp;lt;AdventureWorks2008Entities&amp;gt;&lt;/em&gt;:&lt;/p&gt;
&lt;div style="color: black; background: azure; font-family: Courier New; font-size: 9pt;"&gt;
&lt;p 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;WcfDataService&lt;/span&gt; : &lt;span style="color: #2b91af;"&gt;DataService&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af;"&gt;AdventureWorks2008Entities&lt;/span&gt;&amp;gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{ .. }&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Then modify the &lt;em&gt;InitializeService&lt;/em&gt; method as follows. This exposes all operations and grants all access rights (not really a production setting):&lt;/p&gt;
&lt;div style="color: black; background: azure; font-family: Courier New; font-size: 9pt;"&gt;
&lt;p style="margin: 0px;"&gt;&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;span style="color: #2b91af;"&gt;DataServiceConfiguration&lt;/span&gt; config)&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; config.SetEntitySetAccessRule(&lt;span style="color: #a31515;"&gt;"*"&lt;/span&gt;, &lt;span style="color: #2b91af;"&gt;EntitySetRights&lt;/span&gt;.All);&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; config.SetServiceOperationAccessRule(&lt;span style="color: #a31515;"&gt;"*"&lt;/span&gt;, &lt;span style="color: #2b91af;"&gt;ServiceOperationRights&lt;/span&gt;.All);&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; config.DataServiceBehavior.MaxProtocolVersion = &lt;span style="color: #2b91af;"&gt;DataServiceProtocolVersion&lt;/span&gt;.V2;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;Believe it or not, we're done (for the first part): the entity model is now exposed in a RESTful way. At the root URL you get an overview of the exposed entities. In the attached sample the root URL is "http://localhost:1544/WcfDataService.svc", but you may of course end up with another port number:&lt;/p&gt;
&lt;p&gt;&lt;img src="http://blogs.u2u.be/diederik/image.axd?picture=2010%2f4%2fBrowser1.png" alt="" width="50%" /&gt;&lt;/p&gt;
&lt;p&gt;At the "/Employees" address you find all employees:&lt;/p&gt;
&lt;p&gt;&lt;img src="http://blogs.u2u.be/diederik/image.axd?picture=2010%2f4%2fBrowser2.png" alt="" width="50%" /&gt;&lt;/p&gt;
&lt;p&gt;In your browser this list of employees may appear like this:&lt;/p&gt;
&lt;p&gt;&lt;img src="http://blogs.u2u.be/diederik/image.axd?picture=2010%2f4%2frss.png" alt="" width="50%" /&gt;&lt;/p&gt;
&lt;p&gt;This means it's time to -at least temporarily- disable your rss feed reading view. Here's how to do this in IE:&lt;/p&gt;
&lt;table border="0"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;p&gt;&lt;img src="http://blogs.u2u.be/diederik/image.axd?picture=2010%2f4%2frss1.png" alt="" /&gt;&amp;nbsp;&lt;/p&gt;
&lt;/td&gt;
&lt;td&gt;&amp;nbsp;
&lt;p&gt;&lt;img src="http://blogs.u2u.be/diederik/image.axd?picture=2010%2f4%2frss2.png" alt="" /&gt;&lt;/p&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;To reach an individual entity, just type its primary key value in parentheses at the end of the URL, like "http://localhost:1544/WcFDataService.svc/Employees(1)":&lt;/p&gt;
&lt;p&gt;&lt;img src="http://blogs.u2u.be/diederik/image.axd?picture=2010%2f4%2fBrowser3.png" alt="" width="50%" /&gt;&lt;/p&gt;
&lt;p&gt;You can navigate via the relationships between entities. This is how to reach the &lt;em&gt;Person&lt;/em&gt; entity, connected to the first &lt;em&gt;Employee.&lt;/em&gt; The URL is "http://localhost:1544/WcfDataService.svc/Employees(1)/Person":&lt;/p&gt;
&lt;p&gt;&lt;img src="http://blogs.u2u.be/diederik/image.axd?picture=2010%2f4%2fBrowser4.png" alt="" width="50%" /&gt;&lt;/p&gt;
&lt;p&gt;Other OData URI options options can be found &lt;a href="http://msdn.microsoft.com/en-us/library/dd728283.aspx"&gt;here&lt;/a&gt;, including:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Filtering&lt;/strong&gt;: http://localhost:1544/WcfDataService.svc/Employees?$filter=JobTitle eq 'Chief Executive Officer'&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Projection&lt;/strong&gt;: http://localhost:1544/WcfDataService.svc/Employees?$select=JobTitle,Gender&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Client side paging&lt;/strong&gt;: http://localhost:1544/WcfDataService.svc/Employees?$skip=5&amp;amp;$top=2&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Version 4.0 also includes support for server side paging. This gives you some control over the resources. Add the following line in the&amp;nbsp;&lt;em&gt;InitializeService&lt;/em&gt; method:&lt;/p&gt;
&lt;div style="color: black; background: azure; font-family: Courier New; font-size: 9pt;"&gt;
&lt;p style="margin: 0px;"&gt;config.SetEntitySetPageSize(&lt;span style="color: #a31515;"&gt;"Employees"&lt;/span&gt;, 3);&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;Only 3 employees will be returned now, even if the client requested all:&lt;/p&gt;
&lt;p&gt;&lt;img src="http://blogs.u2u.be/diederik/image.axd?picture=2010%2f4%2fserverdrivenpaging.png" alt="" width="50%" /&gt;&lt;/p&gt;
&lt;h2&gt;A Client&lt;/h2&gt;
&lt;p&gt;Enough XML for now. WCF Data Services also expose a client side model that allows you to use LINQ.&lt;/p&gt;
&lt;p&gt;Create a new WPF application:&lt;/p&gt;
&lt;p&gt;&lt;img src="http://blogs.u2u.be/diederik/image.axd?picture=2010%2f4%2fWPF+Client.png" alt="" width="50%" /&gt;&lt;/p&gt;
&lt;p&gt;Add a Service Reference to the WFC Data Service:&lt;/p&gt;
&lt;p&gt;&lt;img src="http://blogs.u2u.be/diederik/image.axd?picture=2010%2f4%2fService+Reference.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Decorate the Window with two buttons and a listbox. It should look more or less like this:&lt;/p&gt;
&lt;p&gt;&lt;img src="http://blogs.u2u.be/diederik/image.axd?picture=2010%2f4%2fWPF+app+empty.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;The ListBox will display &lt;em&gt;Employee&lt;/em&gt; entities through a data template (OK, that's XML again):&lt;/p&gt;
&lt;div style="color: black; background: azure; font-family: Courier New; font-size: 9pt;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;ListBox&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp; &lt;span style="color: red;"&gt;Name&lt;/span&gt;&lt;span style="color: blue;"&gt;="employeesListBox"&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp; &lt;span style="color: red;"&gt;ItemTemplate&lt;/span&gt;&lt;span style="color: blue;"&gt;="{&lt;/span&gt;&lt;span style="color: #a31515;"&gt;StaticResource&lt;/span&gt;&lt;span style="color: red;"&gt; EmployeeTemplate&lt;/span&gt;&lt;span style="color: blue;"&gt;}"&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp; &lt;span style="color: red;"&gt;Margin&lt;/span&gt;&lt;span style="color: blue;"&gt;="4"&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp; &lt;span style="color: red;"&gt;Grid.Row&lt;/span&gt;&lt;span style="color: blue;"&gt;="1"/&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;Here's the template. It not only binds to &lt;em&gt;Employee&lt;/em&gt; properties, but also to &lt;em&gt;Person&lt;/em&gt; attributes:&lt;/p&gt;
&lt;div style="color: black; background: azure; font-family: Courier New; font-size: 9pt;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;DataTemplate&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp; &lt;span style="color: red;"&gt;x&lt;/span&gt;&lt;span style="color: blue;"&gt;:&lt;/span&gt;&lt;span style="color: red;"&gt;Key&lt;/span&gt;&lt;span style="color: blue;"&gt;="EmployeeTemplate"&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: #a31515;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;StackPanel&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: #a31515;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;StackPanel&lt;/span&gt;&lt;span style="color: red;"&gt; Orientation&lt;/span&gt;&lt;span style="color: blue;"&gt;="Horizontal"&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: #a31515;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;TextBlock&lt;/span&gt;&lt;/p&gt;
&lt;p 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: red;"&gt;Text&lt;/span&gt;&lt;span style="color: blue;"&gt;="{&lt;/span&gt;&lt;span style="color: #a31515;"&gt;Binding&lt;/span&gt;&lt;span style="color: red;"&gt; Path&lt;/span&gt;&lt;span style="color: blue;"&gt;=Person.FirstName}"&lt;/span&gt;&lt;/p&gt;
&lt;p 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: red;"&gt;FontWeight&lt;/span&gt;&lt;span style="color: blue;"&gt;="Bold"&lt;/span&gt;&lt;/p&gt;
&lt;p 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: red;"&gt;Padding&lt;/span&gt;&lt;span style="color: blue;"&gt;="0 0 2 0"/&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: #a31515;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;TextBlock&lt;/span&gt;&lt;/p&gt;
&lt;p 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: red;"&gt;Text&lt;/span&gt;&lt;span style="color: blue;"&gt;="{&lt;/span&gt;&lt;span style="color: #a31515;"&gt;Binding&lt;/span&gt;&lt;span style="color: red;"&gt; Path&lt;/span&gt;&lt;span style="color: blue;"&gt;=Person.LastName}"&lt;/span&gt;&lt;/p&gt;
&lt;p 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: red;"&gt;FontWeight&lt;/span&gt;&lt;span style="color: blue;"&gt;="Bold"/&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: #a31515;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;StackPanel&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: #a31515;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;StackPanel&lt;/span&gt;&lt;span style="color: red;"&gt; Orientation&lt;/span&gt;&lt;span style="color: blue;"&gt;="Horizontal"&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: #a31515;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;TextBlock&lt;/span&gt;&lt;/p&gt;
&lt;p 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: red;"&gt;Text&lt;/span&gt;&lt;span style="color: blue;"&gt;="{&lt;/span&gt;&lt;span style="color: #a31515;"&gt;Binding&lt;/span&gt;&lt;span style="color: red;"&gt; Path&lt;/span&gt;&lt;span style="color: blue;"&gt;=JobTitle}"&lt;/span&gt;&lt;/p&gt;
&lt;p 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: red;"&gt;Width&lt;/span&gt;&lt;span style="color: blue;"&gt;="180"/&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: #a31515;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;TextBlock&lt;/span&gt;&lt;/p&gt;
&lt;p 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: red;"&gt;Text&lt;/span&gt;&lt;span style="color: blue;"&gt;="{&lt;/span&gt;&lt;span style="color: #a31515;"&gt;Binding&lt;/span&gt;&lt;span style="color: red;"&gt; Path&lt;/span&gt;&lt;span style="color: blue;"&gt;=VacationHours}"&lt;/span&gt;&lt;/p&gt;
&lt;p 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: red;"&gt;Width&lt;/span&gt;&lt;span style="color: blue;"&gt;="60"&lt;/span&gt;&lt;/p&gt;
&lt;p 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: red;"&gt;TextAlignment&lt;/span&gt;&lt;span style="color: blue;"&gt;="Right" /&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: #a31515;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;TextBlock&lt;/span&gt;&lt;/p&gt;
&lt;p 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: red;"&gt;Text&lt;/span&gt;&lt;span style="color: blue;"&gt;=" vacation hours taken." /&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: #a31515;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;StackPanel&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: #a31515;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;StackPanel&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;DataTemplate&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;The &lt;em&gt;Populate&lt;/em&gt;-Button fetches some &lt;em&gt;Employee&lt;/em&gt; entities together&amp;nbsp;with their related &lt;em&gt;Person&lt;/em&gt; entity,&amp;nbsp;and binds the collection to the ListBox (in version 4.0 two-way bindings are supported for WPF):&lt;/p&gt;
&lt;div style="color: black; background: azure; font-family: Courier New; font-size: 9pt;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; Populate_Click(&lt;span style="color: blue;"&gt;object&lt;/span&gt; sender, &lt;span style="color: #2b91af;"&gt;RoutedEventArgs&lt;/span&gt; e)&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af;"&gt;AdventureWorks2008Entities&lt;/span&gt; svc =&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&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;AdventureWorks2008Entities&lt;/span&gt;(&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&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;Uri&lt;/span&gt;(&lt;span style="color: #a31515;"&gt;"http://localhost:1544/WcfDataService.svc"&lt;/span&gt;));&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;this&lt;/span&gt;.employeesListBox.ItemsSource =&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; svc.Employees.Expand(&lt;span style="color: #a31515;"&gt;"Person"&lt;/span&gt;).Where(emp =&amp;gt; emp.BusinessEntityID &amp;lt; 100);&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;Here's the result:&lt;/p&gt;
&lt;p&gt;&lt;img src="http://blogs.u2u.be/diederik/image.axd?picture=2010%2f4%2fWPF+app+in+action.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;Update&lt;/em&gt;-Button updates the number of vacation hours of the company's CEO. It fetches the &lt;em&gt;Employee&lt;/em&gt;, updates its &lt;em&gt;VacationHours&lt;/em&gt; property, then tells the state manager to update the employee's state, and eventually persists the data:&lt;/p&gt;
&lt;div style="color: black; background: azure; font-family: Courier New; font-size: 9pt;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; Update_Click(&lt;span style="color: blue;"&gt;object&lt;/span&gt; sender, &lt;span style="color: #2b91af;"&gt;RoutedEventArgs&lt;/span&gt; e)&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af;"&gt;AdventureWorks2008Entities&lt;/span&gt; svc =&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&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;AdventureWorks2008Entities&lt;/span&gt;(&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&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;Uri&lt;/span&gt;(&lt;span style="color: #a31515;"&gt;"http://localhost:1544/WcfDataService.svc"&lt;/span&gt;));&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af;"&gt;Employee&lt;/span&gt; employee =&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; svc.Employees.Where(emp =&amp;gt; emp.BusinessEntityID == 1).First();&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; employee.VacationHours++;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; svc.UpdateObject(employee);&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; svc.SaveChanges();&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;If you now repopulate the listbox, you will see the increased value:&lt;/p&gt;
&lt;p&gt;&lt;img src="http://blogs.u2u.be/diederik/image.axd?picture=2010%2f4%2fWPF+app+in+action2.png" alt="" /&gt;&lt;/p&gt;
&lt;h2&gt;Source Code&lt;/h2&gt;
&lt;p&gt;Here's the full source code of this sample (just requires VS2010 with no extra downloads): &lt;a href="http://blogs.u2u.be/diederik/file.axd?file=2010%2f4%2fU2UConsult.WcfDataServices.Sample.zip"&gt;U2UConsult.WcfDataServices.Sample.zip (96,59 kb)&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Enjoy!&lt;/p&gt;</description><pubDate>Mon, 19 Apr 2010 18:17:00 -1200</pubDate><comments>http://blogs.u2u.be/diederik/post/2010/04/19/WCF-Data-Services-40-in-less-than-5-minutes.aspx#comment</comments><dc:publisher xmlns:dc="http://purl.org/dc/elements/1.1/">Diederik Krols</dc:publisher><pingback:server xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/">http://blogs.u2u.be/diederik/pingback.axd</pingback:server><pingback:target xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/">http://blogs.u2u.be/diederik/post.aspx?id=174c82fa-ddde-411c-b0f4-df24bb3fc926</pingback:target><slash:comments xmlns:slash="http://purl.org/rss/1.0/modules/slash/">1</slash:comments><trackback:ping xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">http://blogs.u2u.be/diederik/trackback.axd?id=174c82fa-ddde-411c-b0f4-df24bb3fc926</trackback:ping><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.u2u.be/diederik/post/2010/04/19/WCF-Data-Services-40-in-less-than-5-minutes.aspx#comment</wfw:comment><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.u2u.be/diederik/syndication.axd?post=174c82fa-ddde-411c-b0f4-df24bb3fc926</wfw:commentRss></item><item><guid isPermaLink="false">http://blogs.u2u.be/u2u/post.aspx?id=29423f45-7391-4907-9f6a-8f14794912b5</guid><link>http://blogs.u2u.be/u2u/post/2010/04/19/Analysis-Services-documenter.aspx</link><author>Nico Jacobs</author><title>Analysis Services documenter</title><description>&lt;p&gt;Good news for those of you who are using our Analysis Services documenter tool: we just added a new feature. If you generate HTML documentation from a cube, then in the ‘full documentation mode’, the MDX script of your cube will be scripted as well into a bulleted list (one calculated member, named set or script line per bullet):&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.u2u.be/u2u/image.axd?picture=image.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blogs.u2u.be/u2u/image.axd?picture=image_thumb.png" width="244" height="51" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;If you have our Analysis Services documenter already installed, it will automatically download the most recent version. If you don’t have a copy yet, download your free copy &lt;a href="http://www.u2u.be/res/SQL05_ASDoc.aspx" target="_blank"&gt;here&lt;/a&gt;.&lt;/p&gt;</description><pubDate>Mon, 19 Apr 2010 17:30:06 -1200</pubDate><comments>http://blogs.u2u.be/u2u/post/2010/04/19/Analysis-Services-documenter.aspx#comment</comments><dc:publisher xmlns:dc="http://purl.org/dc/elements/1.1/">Nico Jacobs</dc:publisher><pingback:server xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/">http://blogs.u2u.be/u2u/pingback.axd</pingback:server><pingback:target xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/">http://blogs.u2u.be/u2u/post.aspx?id=29423f45-7391-4907-9f6a-8f14794912b5</pingback:target><slash:comments xmlns:slash="http://purl.org/rss/1.0/modules/slash/">8</slash:comments><trackback:ping xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">http://blogs.u2u.be/u2u/trackback.axd?id=29423f45-7391-4907-9f6a-8f14794912b5</trackback:ping><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.u2u.be/u2u/post/2010/04/19/Analysis-Services-documenter.aspx#comment</wfw:comment><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.u2u.be/u2u/syndication.axd?post=29423f45-7391-4907-9f6a-8f14794912b5</wfw:commentRss></item><item><guid isPermaLink="false">http://blogs.u2u.be/peter/post.aspx?id=0d23dc1a-67d5-4d80-8ca8-7ae36bf6d243</guid><link>http://blogs.u2u.be/peter/post/2010/04/15/Apple-wants-to-own-you-(especially-when-youe28099re-a-developer-e280a6).aspx</link><author>Peter Himschoot</author><title>Apple wants to own you (especially when you’re a developer …)</title><description>&lt;p&gt;Great read: &lt;a title="http://www.slate.com/id/2250993/" href="http://www.slate.com/id/2250993/"&gt;http://www.slate.com/id/2250993/&lt;/a&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;If Microsoft ever wanted to do something like this … what would happen do you think? How is it Apple can?&lt;/p&gt;</description><pubDate>Thu, 15 Apr 2010 10:26:08 -1200</pubDate><comments>http://blogs.u2u.be/peter/post/2010/04/15/Apple-wants-to-own-you-(especially-when-youe28099re-a-developer-e280a6).aspx#comment</comments><dc:publisher xmlns:dc="http://purl.org/dc/elements/1.1/">Peter Himschoot</dc:publisher><pingback:server xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/">http://blogs.u2u.be/peter/pingback.axd</pingback:server><pingback:target xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/">http://blogs.u2u.be/peter/post.aspx?id=0d23dc1a-67d5-4d80-8ca8-7ae36bf6d243</pingback:target><slash:comments xmlns:slash="http://purl.org/rss/1.0/modules/slash/">4</slash:comments><trackback:ping xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">http://blogs.u2u.be/peter/trackback.axd?id=0d23dc1a-67d5-4d80-8ca8-7ae36bf6d243</trackback:ping><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.u2u.be/peter/post/2010/04/15/Apple-wants-to-own-you-(especially-when-youe28099re-a-developer-e280a6).aspx#comment</wfw:comment><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.u2u.be/peter/syndication.axd?post=0d23dc1a-67d5-4d80-8ca8-7ae36bf6d243</wfw:commentRss></item><item><guid isPermaLink="false">http://blogs.u2u.be/diederik/post.aspx?id=0c0d032a-cd4b-49ac-951a-42ee5e40ddc3</guid><link>http://blogs.u2u.be/diederik/post/2010/04/12/PRISM-in-600-seconds.aspx</link><author>Diederik Krols</author><category>WPF</category><title>PRISM in 600 seconds</title><description>&lt;p&gt;Welcome to the lean, mean, no Vicodin, U2U Consult PRISM machine. (595 seconds left.) CompositeWPF, or Composite Application Guidance (CAG) including Composite Application Library (CAL) is still commonly referred to as PRISM. The software component -CAL- extends Windows Presentation Foundation (WPF) with Modularity, Dependency Injection, Loosely Coupled Events, Distributed Commanding, and Run-Time Module Discovery, while leveraging the classic WPF principles and design concepts such as Dependency Properties, Routed Events, Commanding, Data Binding and Data Templates. In a composite application, functionality is embedded in small parts called modules. Each module is an autonomous, dynamically discoverable and loadable piece, containing its own view, logic and services. A module is preferably designed and implemented according to the MVP-pattern. The main application contains one or more shells: empty windows with regions that contain the placeholders for the module's views. This article walks through a small sample CAL-based solution. Here's how&amp;nbsp;the GUI&amp;nbsp;looks like, in sober black and white:&lt;/p&gt;
&lt;p&gt;&lt;img src="http://blogs.u2u.be/diederik/image.axd?picture=2010%2f4%2fCAL_Sample.png" alt="" /&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;If you would build it as a single monolithic application, it would take you 10 minutes to develop. The&amp;nbsp; CAL-based&amp;nbsp;implementation is actually composed of 6 (six) independent projects with hardly any references to one another:&lt;/p&gt;
&lt;p&gt;&lt;img src="http://blogs.u2u.be/diederik/image.axd?picture=2010%2f4%2fstructure.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;And now 10 minutes is maybe even too long to explain how everything works. Anyway, I'm going to give it a try!&lt;/p&gt;
&lt;h2&gt;Shells and Regions&lt;/h2&gt;
&lt;p&gt;A Composite WPF application starts as a regular WPF application with references to all of the CAL-assemblies (the ones starting with &lt;em&gt;Microsoft&lt;/em&gt; in the next screen shot):&lt;/p&gt;
&lt;p&gt;&lt;img src="http://blogs.u2u.be/diederik/image.axd?picture=2010%2f4%2fCAL_References.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;CompositeWPF&lt;/em&gt; namespace is imported into the XAML of the Main Window:&lt;/p&gt;
&lt;div style="font-family: Courier New; font-size: 9pt; color: black; background: azure;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: red;"&gt;xmlns&lt;/span&gt;&lt;span style="color: blue;"&gt;:&lt;/span&gt;&lt;span style="color: red;"&gt;cal&lt;/span&gt;&lt;span style="color: blue;"&gt;="http://www.codeplex.com/CompositeWPF"&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;Regions are added in the XAML by creating &lt;em&gt;ItemControl&lt;/em&gt; and &lt;em&gt;ContentControl&lt;/em&gt; instances with a &lt;em&gt;RegionName&lt;/em&gt;. This dependency property exposes the region's name to CAL's region manager:&lt;/p&gt;
&lt;div style="font-family: Courier New; font-size: 9pt; color: black; background: azure;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;ItemsControl&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp; &lt;span style="color: red;"&gt;x&lt;/span&gt;&lt;span style="color: blue;"&gt;:&lt;/span&gt;&lt;span style="color: red;"&gt;Name&lt;/span&gt;&lt;span style="color: blue;"&gt;="MainRegion"&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp; &lt;span style="color: red;"&gt;cal&lt;/span&gt;&lt;span style="color: blue;"&gt;:&lt;/span&gt;&lt;span style="color: red;"&gt;RegionManager.RegionName&lt;/span&gt;&lt;span style="color: blue;"&gt;="MainRegion" /&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;Modules and Dependency Injection&lt;/h2&gt;
&lt;p&gt;The AboutModule is kind of a "Hello World" module: it just statically displays some company's logo. A module generally doesn't need to call the whole CAL-shebang, so it can get away with less references:&lt;/p&gt;
&lt;p&gt;&lt;img src="http://blogs.u2u.be/diederik/image.axd?picture=2010%2f4%2fModule_References.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;The module itself is a class that implements the &lt;em&gt;IModule&lt;/em&gt; interface. It is decorated with the &lt;em&gt;Module&lt;/em&gt; atttribute to allow dynamic discovery:&lt;/p&gt;
&lt;div style="font-family: Courier New; font-size: 9pt; color: black; background: azure;"&gt;
&lt;p style="margin: 0px;"&gt;[&lt;span style="color: #2b91af;"&gt;Module&lt;/span&gt;(ModuleName = &lt;span style="color: #a31515;"&gt;"AboutModule"&lt;/span&gt;)]&lt;/p&gt;
&lt;p 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;AboutModule&lt;/span&gt; : &lt;span style="color: #2b91af;"&gt;IModule&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;// ...&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;The graphical part of the module is implemented as a WPF UserControl and saved in a &lt;em&gt;Views&lt;/em&gt; subfolder. It needs no special references or interfaces:&lt;/p&gt;
&lt;p&gt;&lt;img src="http://blogs.u2u.be/diederik/image.axd?picture=2010%2f4%2fModule_View.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;IRegionManager&lt;/em&gt; parameter in the module's constructor will be discovered and populated by CAL's Dependency Injection Container (&lt;em&gt;Unity&lt;/em&gt;, that is). It gives the module and the view access to the shell's region manager:&lt;/p&gt;
&lt;div style="font-family: Courier New; font-size: 9pt; color: black; background: azure;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt; AboutModule(&lt;span style="color: #2b91af;"&gt;IRegionManager&lt;/span&gt; regionManager)&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;this&lt;/span&gt;.regionManager = regionManager;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;CAL will call the module's constructor, and then its &lt;em&gt;Initialize&lt;/em&gt; method, where the view is registered in the named region:&lt;/p&gt;
&lt;div style="font-family: Courier New; font-size: 9pt; color: black; background: azure;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; Initialize()&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;this&lt;/span&gt;.regionManager.RegisterViewWithRegion(&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #a31515;"&gt;"MainRegion"&lt;/span&gt;,&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;typeof&lt;/span&gt;(Views.&lt;span style="color: #2b91af;"&gt;U2UConsultLogo&lt;/span&gt;));&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;Bootstrapping the Application&lt;/h2&gt;
&lt;p&gt;The main project contains a bootstrapper. It makes sure that CAL's components are properly initialized before anything else happens:&lt;/p&gt;
&lt;div style="font-family: Courier New; font-size: 9pt; color: black; background: azure;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;internal&lt;/span&gt; &lt;span style="color: blue;"&gt;class&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;Bootstrapper&lt;/span&gt; : &lt;span style="color: #2b91af;"&gt;UnityBootstrapper&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;// ...&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;OnStartUp&lt;/em&gt; of the application is overridden:&lt;/p&gt;
&lt;div style="font-family: Courier New; font-size: 9pt; color: black; background: azure;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;protected&lt;/span&gt; &lt;span style="color: blue;"&gt;override&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; OnStartup(&lt;span style="color: #2b91af;"&gt;StartupEventArgs&lt;/span&gt; e)&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;base&lt;/span&gt;.OnStartup(e);&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af;"&gt;Bootstrapper&lt;/span&gt; bootstrapper = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;Bootstrapper&lt;/span&gt;();&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; bootstrapper.Run();&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Since the bootstrapper will do the initialization, the &lt;em&gt;StartupUri&lt;/em&gt; is removed from app.xaml:&lt;/p&gt;
&lt;div style="font-family: Courier New; font-size: 9pt; color: black; background: azure;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;Application&lt;/span&gt;&lt;span style="color: red;"&gt; x&lt;/span&gt;&lt;span style="color: blue;"&gt;:&lt;/span&gt;&lt;span style="color: red;"&gt;Class&lt;/span&gt;&lt;span style="color: blue;"&gt;="U2UConsult.CAL.Sample.App"&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp; &lt;span style="color: red;"&gt;xmlns&lt;/span&gt;&lt;span style="color: blue;"&gt;="http://schemas.microsoft.com/winfx/2006/xaml/presentation"&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp; &lt;span style="color: red;"&gt;xmlns&lt;/span&gt;&lt;span style="color: blue;"&gt;:&lt;/span&gt;&lt;span style="color: red;"&gt;x&lt;/span&gt;&lt;span style="color: blue;"&gt;="http://schemas.microsoft.com/winfx/2006/xaml"&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: #a31515;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: green;"&gt;&amp;lt;!--StartupUri="Shell.xaml"--&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;Application&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Back in the bootstrapper, the &lt;em&gt;CreateShell()&lt;/em&gt; method is overridden to make sure that the shell gets loaded and displayed:&lt;/p&gt;
&lt;div style="font-family: Courier New; font-size: 9pt; color: black; background: azure;"&gt;
&lt;p style="margin: 0px;"&gt;&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;DependencyObject&lt;/span&gt; CreateShell()&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af;"&gt;Shell&lt;/span&gt; shell = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;Shell&lt;/span&gt;();&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; shell.Show();&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;return&lt;/span&gt; shell;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;GetModuleCatalog()&lt;/em&gt; method is also overridden. It returns the collection of discovered modules. There are different types of module catalog subclasses that can inspect directories, configuration files, or even XAML files. The following code is an example of using the &lt;em&gt;ModuleCatalog&lt;/em&gt; base class itself. This way of statically loading modules requires a reference from the shell project to the module project(s), so it's only used while developing/debugging:&lt;/p&gt;
&lt;div style="font-family: Courier New; font-size: 9pt; color: black; background: azure;"&gt;
&lt;p style="margin: 0px;"&gt;&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;IModuleCatalog&lt;/span&gt; GetModuleCatalog()&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af;"&gt;ModuleCatalog&lt;/span&gt; catalog = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;ModuleCatalog&lt;/span&gt;();&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; catalog.AddModule(&lt;span style="color: blue;"&gt;typeof&lt;/span&gt;(AboutModule.&lt;span style="color: #2b91af;"&gt;AboutModule&lt;/span&gt;));&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;return&lt;/span&gt; catalog;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;Model View Presenter&lt;/h2&gt;
&lt;p&gt;The region at the bottom of the main window's shell displays the application's status. XAML-wise it's just a textblock. When it is is implemented using a (lightweight) MVP-pattern and packaged as a CAL module, it looks rather impressive:&lt;/p&gt;
&lt;p&gt;&lt;img src="http://blogs.u2u.be/diederik/image.axd?picture=2010%2f4%2fMVP.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;The view contains a textblock which is bound to a property in the PresentationModel:&lt;/p&gt;
&lt;div style="font-family: Courier New; font-size: 9pt; color: black; background: azure;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;TextBlock&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp; &lt;span style="color: red;"&gt;TextWrapping&lt;/span&gt;&lt;span style="color: blue;"&gt;="NoWrap"&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp; &lt;span style="color: red;"&gt;Text&lt;/span&gt;&lt;span style="color: blue;"&gt;="{&lt;/span&gt;&lt;span style="color: #a31515;"&gt;Binding&lt;/span&gt;&lt;span style="color: red;"&gt; StatusMessage&lt;/span&gt;&lt;span style="color: blue;"&gt;}"/&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The StatusModule requires more of the CAL feature set than the AboutModule, so it has more injected stuff (i.e. parameters in its constructor):&lt;/p&gt;
&lt;div style="font-family: Courier New; font-size: 9pt; color: black; background: azure;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt; StatusModule(&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af;"&gt;IUnityContainer&lt;/span&gt; container,&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af;"&gt;IRegionManager&lt;/span&gt; regionManager,&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af;"&gt;IEventAggregator&lt;/span&gt; eventAggregator)&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;this&lt;/span&gt;.container = container;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;this&lt;/span&gt;.regionManager = regionManager;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;this&lt;/span&gt;.eventAggregator = eventAggregator;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The registration of the views and services that are provided by the module, is nicely factored out to a seperate method:&lt;/p&gt;
&lt;div style="font-family: Courier New; font-size: 9pt; color: black; background: azure;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; RegisterViewsAndServices()&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;this&lt;/span&gt;.container.RegisterType&amp;lt;Views.&lt;span style="color: #2b91af;"&gt;IStatusView&lt;/span&gt;, Views.&lt;span style="color: #2b91af;"&gt;StatusView&lt;/span&gt;&amp;gt;(&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&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;ContainerControlledLifetimeManager&lt;/span&gt;());&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;this&lt;/span&gt;.container.RegisterType&amp;lt;PresentationModels.&lt;span style="color: #2b91af;"&gt;IStatusPresentationModel&lt;/span&gt;, PresentationModels.&lt;span style="color: #2b91af;"&gt;StatusPresentationModel&lt;/span&gt;&amp;gt;(&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&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;ContainerControlledLifetimeManager&lt;/span&gt;());&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;And here's the module's &lt;em&gt;Initialize&lt;/em&gt;-method:&lt;/p&gt;
&lt;div style="font-family: Courier New; font-size: 9pt; color: black; background: azure;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; Initialize()&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;this&lt;/span&gt;.RegisterViewsAndServices();&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; PresentationModels.&lt;span style="color: #2b91af;"&gt;IStatusPresentationModel&lt;/span&gt; model =&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;this&lt;/span&gt;.container.Resolve&amp;lt;PresentationModels.&lt;span style="color: #2b91af;"&gt;StatusPresentationModel&lt;/span&gt;&amp;gt;();&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af;"&gt;IRegion&lt;/span&gt; statusRegion = &lt;span style="color: blue;"&gt;this&lt;/span&gt;.regionManager.Regions[&lt;span style="color: #a31515;"&gt;"StatusRegion"&lt;/span&gt;];&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; statusRegion.Add(model.View);&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The PresentationModel gets its view&amp;nbsp;from Dependency Injection, again through a parameter in its constructor. So here's where the model and its view are connected:&lt;/p&gt;
&lt;div style="font-family: Courier New; font-size: 9pt; color: black; background: azure;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt; StatusPresentationModel(Views.&lt;span style="color: #2b91af;"&gt;IStatusView&lt;/span&gt; view)&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;this&lt;/span&gt;.view = view;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; view.Model = &lt;span style="color: blue;"&gt;this&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;this&lt;/span&gt;.StatusMessage = &lt;span style="color: #a31515;"&gt;"The status module is operational."&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;Distributed Events&lt;/h2&gt;
&lt;p&gt;The SelectionModule allows the end user to select an option, after which it will update the application's status. This status is displayed in the status region at the bottom of the shell:&lt;/p&gt;
&lt;p&gt;&lt;img src="http://blogs.u2u.be/diederik/image.axd?picture=2010%2f4%2fEvents.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;Some corners were cut here: the project only contains a Module and a View. Anyway, the SelectionModule needs to communicate with the StatusModule. Both modules share an event -an instance of &lt;em&gt;CompositePresentationEvent&lt;/em&gt;- through CAL's event aggregator, a mechanism for distributed events. The reference to this event aggregator comes from ... Dependency Injection indeed: the constructors of SelectionModule, SelectionView, StatusModule, and StatusPresentationModel take an &lt;em&gt;IEventAggrator&lt;/em&gt; parameter.&lt;/p&gt;
&lt;p&gt;A global &lt;em&gt;StatusReported&lt;/em&gt; event is declared in the application. The SelectionModule raises this event&amp;nbsp;via a call to &lt;em&gt;Publish()&lt;/em&gt;:&lt;/p&gt;
&lt;div style="font-family: Courier New; font-size: 9pt; color: black; background: azure;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; RadioButton_Checked(&lt;span style="color: blue;"&gt;object&lt;/span&gt; sender, &lt;span style="color: #2b91af;"&gt;RoutedEventArgs&lt;/span&gt; e)&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;this&lt;/span&gt;.selectedOption = e.Source &lt;span style="color: blue;"&gt;as&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;RadioButton&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;string&lt;/span&gt; status = &lt;span style="color: blue;"&gt;string&lt;/span&gt;.Format(&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #a31515;"&gt;"You selected '{0}'."&lt;/span&gt;,&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;this&lt;/span&gt;.selectedOption.Content.ToString());&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;this&lt;/span&gt;.eventAggregator.GetEvent&amp;lt;Infrastructure.&lt;span style="color: #2b91af;"&gt;StatusReportedEvent&lt;/span&gt;&amp;gt;().Publish(status);&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The StatusModule&amp;nbsp;has a local event handler for the same event:&lt;/p&gt;
&lt;div style="font-family: Courier New; font-size: 9pt; color: black; background: azure;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; OnAppStatusChanged(&lt;span style="color: blue;"&gt;string&lt;/span&gt; message)&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;this&lt;/span&gt;.StatusMessage = message;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;It registers that handler with CAL through a &lt;em&gt;Subscribe&lt;/em&gt;-call:&lt;/p&gt;
&lt;div style="font-family: Courier New; font-size: 9pt; color: black; background: azure;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;this&lt;/span&gt;.eventAggregator.GetEvent&amp;lt;Infrastructure.&lt;span style="color: #2b91af;"&gt;StatusReportedEvent&lt;/span&gt;&amp;gt;()&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; .Subscribe(&lt;span style="color: blue;"&gt;this&lt;/span&gt;.OnAppStatusChanged, &lt;span style="color: #2b91af;"&gt;ThreadOption&lt;/span&gt;.UIThread, &lt;span style="color: blue;"&gt;true&lt;/span&gt;);&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The handler itself is straightforward - data binding will update the view:&lt;/p&gt;
&lt;div style="font-family: Courier New; font-size: 9pt; color: black; background: azure;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; OnAppStatusChanged(&lt;span style="color: blue;"&gt;string&lt;/span&gt; message)&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;this&lt;/span&gt;.StatusMessage = message;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;As you observed, the reference to the event is not by name (like for a region), but it is made by type. Both modules, as well as all other modules that want to notify status changes, need to know the event type:&lt;/p&gt;
&lt;div style="font-family: Courier New; font-size: 9pt; color: black; background: azure;"&gt;
&lt;p 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;StatusReportedEvent&lt;/span&gt; : &lt;span style="color: #2b91af;"&gt;CompositePresentationEvent&lt;/span&gt;&amp;lt;&lt;span style="color: blue;"&gt;string&lt;/span&gt;&amp;gt; { }&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;The type is defined is a so-called &lt;em&gt;Infrastructure&lt;/em&gt; project to which multiple modules -and probably even the shell- will have a reference. To call a spade a bloody shovel: &lt;em&gt;GlobalVariables&lt;/em&gt; would be a more appropriate name for this assembly. But that name seems to upset software architects.&lt;/p&gt;
&lt;h2&gt;Composite Commands&lt;/h2&gt;
&lt;p&gt;In WPF we like to communicate through Commands, not Events. So here's the next use case. The shell has a toolbar region on top. This will host the toolbar buttons of each individual module, but also some global commands. A global "Clear" button&amp;nbsp;clears any registered view, and updates the status:&lt;/p&gt;
&lt;p&gt;&lt;img src="http://blogs.u2u.be/diederik/image.axd?picture=2010%2f4%2fCommands.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;The global command itself is an instance of CAL's &lt;em&gt;CompositeCommand&lt;/em&gt; class - a command that can have child commands. It is defined in the Infrastructure project, together with a proxy to it that can be overridden for unit testing:&lt;/p&gt;
&lt;div style="font-family: Courier New; font-size: 9pt; color: black; background: azure;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;namespace&lt;/span&gt; Infrastructure&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;using&lt;/span&gt; Microsoft.Practices.Composite.Presentation.Commands;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p 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;class&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;GlobalCommands&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &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: #2b91af;"&gt;CompositeCommand&lt;/span&gt; ClearCommand = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;CompositeCommand&lt;/span&gt;();&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p 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;class&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;ClearCommandProxy&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;virtual&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;CompositeCommand&lt;/span&gt; ClearCommand&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&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;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p 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; &lt;span style="color: blue;"&gt;return&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;GlobalCommands&lt;/span&gt;.ClearCommand;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;The button with the large "X" is linked to the global command through its XAML:&lt;/p&gt;
&lt;div style="font-family: Courier New; font-size: 9pt; color: black; background: azure;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;UserControl&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp; &lt;span style="color: red;"&gt;x&lt;/span&gt;&lt;span style="color: blue;"&gt;:&lt;/span&gt;&lt;span style="color: red;"&gt;Class&lt;/span&gt;&lt;span style="color: blue;"&gt;="GlobalCommandsModule.Views.SelectionCommandView"&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp; &lt;span style="color: red;"&gt;xmlns&lt;/span&gt;&lt;span style="color: blue;"&gt;="http://schemas.microsoft.com/winfx/2006/xaml/presentation"&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp; &lt;span style="color: red;"&gt;xmlns&lt;/span&gt;&lt;span style="color: blue;"&gt;:&lt;/span&gt;&lt;span style="color: red;"&gt;x&lt;/span&gt;&lt;span style="color: blue;"&gt;="http://schemas.microsoft.com/winfx/2006/xaml"&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp; &lt;span style="color: red;"&gt;xmlns&lt;/span&gt;&lt;span style="color: blue;"&gt;:&lt;/span&gt;&lt;span style="color: red;"&gt;inf&lt;/span&gt;&lt;span style="color: blue;"&gt;="clr-namespace:Infrastructure;assembly=Infrastructure" &amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: #a31515;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515;"&gt;Button&lt;/span&gt;&lt;span style="color: red;"&gt; Command&lt;/span&gt;&lt;span style="color: blue;"&gt;="{&lt;/span&gt;&lt;span style="color: #a31515;"&gt;x&lt;/span&gt;&lt;span style="color: blue;"&gt;:&lt;/span&gt;&lt;span style="color: #a31515;"&gt;Static&lt;/span&gt;&lt;span style="color: red;"&gt; inf&lt;/span&gt;&lt;span style="color: blue;"&gt;:&lt;/span&gt;&lt;span style="color: red;"&gt;GlobalCommands&lt;/span&gt;&lt;span style="color: blue;"&gt;.ClearCommand}" ... /&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515;"&gt;UserControl&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;When the SelectionView is loaded, it hooks a &lt;em&gt;DelegateCommand&lt;/em&gt; to the global ClearCommand:&lt;/p&gt;
&lt;div style="font-family: Courier New; font-size: 9pt; color: black; background: azure;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt; SelectionView(&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af;"&gt;IEventAggregator&lt;/span&gt; eventAggregator,&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af;"&gt;ClearCommandProxy&lt;/span&gt; commandProxy)&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; InitializeComponent();&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;this&lt;/span&gt;.eventAggregator = eventAggregator;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;this&lt;/span&gt;.clearCommand = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;DelegateCommand&lt;/span&gt;&amp;lt;&lt;span style="color: blue;"&gt;object&lt;/span&gt;&amp;gt;(&lt;span style="color: blue;"&gt;this&lt;/span&gt;.Clear, &lt;span style="color: blue;"&gt;this&lt;/span&gt;.CanClear);&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; commandProxy.ClearCommand.RegisterCommand(&lt;span style="color: blue;"&gt;this&lt;/span&gt;.clearCommand);&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;&lt;em&gt;DelegateCommand&lt;/em&gt; is yet another CAL-class. It&amp;nbsp;lets you directly specify the &lt;em&gt;ICommand&lt;/em&gt;-code for &lt;em&gt;Execute&lt;/em&gt; and &lt;em&gt;CanExecute&lt;/em&gt; as delegates.&lt;/p&gt;
&lt;h2&gt;Dynamic Module Lookup&lt;/h2&gt;
&lt;p&gt;During development it makes sense to keep a reference from the host application to each module, and load it statically into the module catalog. At the end of the day, it's better to cut that reference and go for dynamic lookup. So all modules are compiled and their dll's gathered in a "Modules"-folder:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;img src="http://blogs.u2u.be/diederik/image.axd?picture=2010%2f4%2fmodule_discovery.png" alt="" /&gt;&lt;/p&gt;
&lt;p&gt;The "production"-bootstrapper makes use of one of the more specialized catalog modules:&lt;/p&gt;
&lt;div style="font-family: Courier New; font-size: 9pt; color: black; background: azure;"&gt;
&lt;p style="margin: 0px;"&gt;&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;IModuleCatalog&lt;/span&gt; GetModuleCatalog()&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af;"&gt;DirectoryModuleCatalog&lt;/span&gt; catalog = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;DirectoryModuleCatalog&lt;/span&gt;();&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; catalog.ModulePath = &lt;span style="color: #a31515;"&gt;"../../Modules"&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;return&lt;/span&gt; catalog;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;Source Code&lt;/h2&gt;
&lt;p&gt;Here's the full source code of the sample application: &lt;a href="http://blogs.u2u.be/diederik/file.axd?file=2010%2f4%2fU2UConsult.CAL.Sample.zip"&gt;U2UConsult.CAL.Sample.zip (1,79 mb)&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Enjoy!&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;</description><pubDate>Mon, 12 Apr 2010 17:29:00 -1200</pubDate><comments>http://blogs.u2u.be/diederik/post/2010/04/12/PRISM-in-600-seconds.aspx#comment</comments><dc:publisher xmlns:dc="http://purl.org/dc/elements/1.1/">Diederik Krols</dc:publisher><pingback:server xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/">http://blogs.u2u.be/diederik/pingback.axd</pingback:server><pingback:target xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/">http://blogs.u2u.be/diederik/post.aspx?id=0c0d032a-cd4b-49ac-951a-42ee5e40ddc3</pingback:target><slash:comments xmlns:slash="http://purl.org/rss/1.0/modules/slash/">1</slash:comments><trackback:ping xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">http://blogs.u2u.be/diederik/trackback.axd?id=0c0d032a-cd4b-49ac-951a-42ee5e40ddc3</trackback:ping><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.u2u.be/diederik/post/2010/04/12/PRISM-in-600-seconds.aspx#comment</wfw:comment><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.u2u.be/diederik/syndication.axd?post=0c0d032a-cd4b-49ac-951a-42ee5e40ddc3</wfw:commentRss></item><item><guid isPermaLink="false">http://blogs.u2u.be/peter/post.aspx?id=2026fe0b-9190-4f9a-85ae-88f17c657c57</guid><link>http://blogs.u2u.be/peter/post/2010/04/09/Pex-and-Code-Contracts.aspx</link><author>Peter Himschoot</author><category>VS2010</category><category>.NET Development</category><title>Pex and Code Contracts</title><description>&lt;p&gt;I’m currently experimenting with Pex, Moles and Code Contracts, and I wondered what effect code contracts have on Pex tests. So I build this simple piece of code:&lt;/p&gt;  &lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;   &lt;div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;     &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum1"&gt;   1:&lt;/span&gt; &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; Demo&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum2"&gt;   2:&lt;/span&gt; {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum3"&gt;   3:&lt;/span&gt;   &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; Foo(&lt;span style="color: #0000ff"&gt;int&lt;/span&gt; a, &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; b)&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum4"&gt;   4:&lt;/span&gt;   {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum5"&gt;   5:&lt;/span&gt;     &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (a &amp;lt; 0)&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum6"&gt;   6:&lt;/span&gt;       &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; a;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum7"&gt;   7:&lt;/span&gt;     &lt;span style="color: #0000ff"&gt;else&lt;/span&gt; &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (a &amp;gt; 0)&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum8"&gt;   8:&lt;/span&gt;       &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; b;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum9"&gt;   9:&lt;/span&gt;     &lt;span style="color: #0000ff"&gt;else&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum10"&gt;  10:&lt;/span&gt;       &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; a + b;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum11"&gt;  11:&lt;/span&gt;   }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum12"&gt;  12:&lt;/span&gt; }&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;Then I make Pex generate its Parameterized Unit Tests (PUTs). This generates the following test:&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;
    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum1"&gt;   1:&lt;/span&gt; [PexClass(&lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(Demo))]&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum2"&gt;   2:&lt;/span&gt; [PexAllowedExceptionFromTypeUnderTest(&lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(InvalidOperationException))]&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum3"&gt;   3:&lt;/span&gt; [PexAllowedExceptionFromTypeUnderTest(&lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(ArgumentException), AcceptExceptionSubtypes = &lt;span style="color: #0000ff"&gt;true&lt;/span&gt;)]&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum4"&gt;   4:&lt;/span&gt; [TestClass]&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum5"&gt;   5:&lt;/span&gt; &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;partial&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; DemoTests&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum6"&gt;   6:&lt;/span&gt; {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum7"&gt;   7:&lt;/span&gt;     &lt;span style="color: #008000"&gt;/// &amp;lt;summary&amp;gt;Test stub for Foo(Int32, Int32)&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum8"&gt;   8:&lt;/span&gt;     [PexMethod]&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum9"&gt;   9:&lt;/span&gt;     &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; Foo(&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum10"&gt;  10:&lt;/span&gt;         [PexAssumeUnderTest]Demo target,&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum11"&gt;  11:&lt;/span&gt;         &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; a,&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum12"&gt;  12:&lt;/span&gt;         &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; b&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum13"&gt;  13:&lt;/span&gt;     )&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum14"&gt;  14:&lt;/span&gt;     {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum15"&gt;  15:&lt;/span&gt;         &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; result = target.Foo(a, b);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum16"&gt;  16:&lt;/span&gt;         &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; result;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum17"&gt;  17:&lt;/span&gt;         &lt;span style="color: #008000"&gt;// TODO: add assertions to method DemoTests.Foo(Demo, Int32, Int32)&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum18"&gt;  18:&lt;/span&gt;     }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum19"&gt;  19:&lt;/span&gt; }&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;I just leave the code, right-click on The Foo method of the DemoTests class, choose “Run Pex explorations” and I get this:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://blogs.u2u.be/peter/image.axd?picture=image_217.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://blogs.u2u.be/peter/image.axd?picture=image_thumb_217.png" width="644" height="135" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;As you can see the exploration calls my code with negative, zero and positive values. What happens when I add a contract stating that a should be positive?&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;
    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum1"&gt;   1:&lt;/span&gt; &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; Demo&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum2"&gt;   2:&lt;/span&gt; {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum3"&gt;   3:&lt;/span&gt;   &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; Foo(&lt;span style="color: #0000ff"&gt;int&lt;/span&gt; a, &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; b)&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum4"&gt;   4:&lt;/span&gt;   {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum5"&gt;   5:&lt;/span&gt;     Contract.Requires(a &amp;gt; 0);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum6"&gt;   6:&lt;/span&gt;     &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (a &amp;lt; 0)&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum7"&gt;   7:&lt;/span&gt;       &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; a;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum8"&gt;   8:&lt;/span&gt;     &lt;span style="color: #0000ff"&gt;else&lt;/span&gt; &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (a &amp;gt; 0)&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum9"&gt;   9:&lt;/span&gt;       &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; b;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum10"&gt;  10:&lt;/span&gt;     &lt;span style="color: #0000ff"&gt;else&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum11"&gt;  11:&lt;/span&gt;       &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; a + b;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum12"&gt;  12:&lt;/span&gt;   }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum13"&gt;  13:&lt;/span&gt; }&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;Only line 5 was added, but if I run the pex explorations again I get:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://blogs.u2u.be/peter/image.axd?picture=image_218.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://blogs.u2u.be/peter/image.axd?picture=image_thumb_218.png" width="644" height="110" /&gt;&lt;/a&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;The effect is that Pex now doesn’t explore negative numbers, because it can deduce from the contract not to even try.&lt;/p&gt;

&lt;p&gt;What if I use a contract to state that b should be negative?&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;
    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum1"&gt;   1:&lt;/span&gt; Contract.Requires(b &amp;lt; 0);&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;Again Pex sees this and explores my code with negative b:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://blogs.u2u.be/peter/image.axd?picture=image_219.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://blogs.u2u.be/peter/image.axd?picture=image_thumb_219.png" width="644" height="120" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;One more. When I change my code to do a little more with b, like this:&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;
    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum1"&gt;   1:&lt;/span&gt; &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; Foo(&lt;span style="color: #0000ff"&gt;int&lt;/span&gt; a, &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; b)&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum2"&gt;   2:&lt;/span&gt; {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum3"&gt;   3:&lt;/span&gt;   Contract.Requires(a &amp;gt; 0);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum4"&gt;   4:&lt;/span&gt;   Contract.Requires(b &amp;lt; 0 || b &amp;gt; 10);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum5"&gt;   5:&lt;/span&gt;   &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (a &amp;lt; 0)&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum6"&gt;   6:&lt;/span&gt;     &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; a;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum7"&gt;   7:&lt;/span&gt;   &lt;span style="color: #0000ff"&gt;else&lt;/span&gt; &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (a &amp;gt; 0)&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum8"&gt;   8:&lt;/span&gt;   {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum9"&gt;   9:&lt;/span&gt;     &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (b &amp;gt; a)&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum10"&gt;  10:&lt;/span&gt;       &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; a;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum11"&gt;  11:&lt;/span&gt;     &lt;span style="color: #0000ff"&gt;else&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum12"&gt;  12:&lt;/span&gt;       &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; b;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum13"&gt;  13:&lt;/span&gt;   }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum14"&gt;  14:&lt;/span&gt;   &lt;span style="color: #0000ff"&gt;else&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum15"&gt;  15:&lt;/span&gt;     &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; a + b;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum16"&gt;  16:&lt;/span&gt; }&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;and when I run the explorations again:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://blogs.u2u.be/peter/image.axd?picture=image_220.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://blogs.u2u.be/peter/image.axd?picture=image_thumb_220.png" width="644" height="144" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;So, you can guide Pex by supplying contracts on the arguments of your methods.&lt;/p&gt;</description><pubDate>Fri, 09 Apr 2010 08:55:29 -1200</pubDate><comments>http://blogs.u2u.be/peter/post/2010/04/09/Pex-and-Code-Contracts.aspx#comment</comments><dc:publisher xmlns:dc="http://purl.org/dc/elements/1.1/">Peter Himschoot</dc:publisher><pingback:server xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/">http://blogs.u2u.be/peter/pingback.axd</pingback:server><pingback:target xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/">http://blogs.u2u.be/peter/post.aspx?id=2026fe0b-9190-4f9a-85ae-88f17c657c57</pingback:target><slash:comments xmlns:slash="http://purl.org/rss/1.0/modules/slash/">2</slash:comments><trackback:ping xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">http://blogs.u2u.be/peter/trackback.axd?id=2026fe0b-9190-4f9a-85ae-88f17c657c57</trackback:ping><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.u2u.be/peter/post/2010/04/09/Pex-and-Code-Contracts.aspx#comment</wfw:comment><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.u2u.be/peter/syndication.axd?post=2026fe0b-9190-4f9a-85ae-88f17c657c57</wfw:commentRss></item><item><guid isPermaLink="false">http://blogs.u2u.be/peter/post.aspx?id=a0164f4a-9839-4e4d-9051-cd0ad7255667</guid><link>http://blogs.u2u.be/peter/post/2010/04/08/Overloading-CoContra-variance-could-break-your-code!.aspx</link><author>Peter Himschoot</author><category>.NET Development</category><category>VS2010</category><title>Overloading &amp; Co/Contra-variance could break your code!</title><description>&lt;p&gt;Co and contra-variance were introduced to VB.NET and C# to make working with certain classes more natural (“because it should work”). But beware, I was experimenting a bit with this and found following possible breaking change. I started with these two classes:&lt;/p&gt;  &lt;h3&gt;C#&lt;/h3&gt;  &lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;   &lt;div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;     &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum1"&gt;   1:&lt;/span&gt; &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; Person&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum2"&gt;   2:&lt;/span&gt; {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum3"&gt;   3:&lt;/span&gt;   &lt;span style="color: #008000"&gt;// ...&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum4"&gt;   4:&lt;/span&gt; }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum5"&gt;   5:&lt;/span&gt;&amp;#160; &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum6"&gt;   6:&lt;/span&gt; &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; Vip : Person&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum7"&gt;   7:&lt;/span&gt; {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum8"&gt;   8:&lt;/span&gt;   &lt;span style="color: #008000"&gt;// ...&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum9"&gt;   9:&lt;/span&gt; }&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;h3&gt;VB.NET&lt;/h3&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;
    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum1"&gt;   1:&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Class&lt;/span&gt; Person&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum2"&gt;   2:&lt;/span&gt;   &lt;span style="color: #008000"&gt;' &lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum3"&gt;   3:&lt;/span&gt; &lt;span style="color: #0000ff"&gt;End&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Class&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum4"&gt;   4:&lt;/span&gt;&amp;#160; &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum5"&gt;   5:&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Class&lt;/span&gt; Vip&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum6"&gt;   6:&lt;/span&gt;   &lt;span style="color: #0000ff"&gt;Inherits&lt;/span&gt; Person&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum7"&gt;   7:&lt;/span&gt;   &lt;span style="color: #008000"&gt;'&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum8"&gt;   8:&lt;/span&gt; &lt;span style="color: #0000ff"&gt;End&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Class&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;Then I added a collection of people:&lt;/p&gt;

&lt;h3&gt;C#&lt;/h3&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;
    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum1"&gt;   1:&lt;/span&gt; &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; PersonList&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum2"&gt;   2:&lt;/span&gt; {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum3"&gt;   3:&lt;/span&gt;   &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; Add(&lt;span style="color: #0000ff"&gt;object&lt;/span&gt; obj) {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum4"&gt;   4:&lt;/span&gt;     &lt;span style="color: #008000"&gt;// ...&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum5"&gt;   5:&lt;/span&gt;   }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum6"&gt;   6:&lt;/span&gt;&amp;#160; &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum7"&gt;   7:&lt;/span&gt;   &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; Add(Person p)&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum8"&gt;   8:&lt;/span&gt;   {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum9"&gt;   9:&lt;/span&gt;     &lt;span style="color: #008000"&gt;// ...&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum10"&gt;  10:&lt;/span&gt;   }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum11"&gt;  11:&lt;/span&gt;&amp;#160; &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum12"&gt;  12:&lt;/span&gt;   &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; Add(IEnumerable&amp;lt;Person&amp;gt; people)&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum13"&gt;  13:&lt;/span&gt;   {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum14"&gt;  14:&lt;/span&gt;     &lt;span style="color: #0000ff"&gt;foreach&lt;/span&gt; (Person p &lt;span style="color: #0000ff"&gt;in&lt;/span&gt; people)&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum15"&gt;  15:&lt;/span&gt;     {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum16"&gt;  16:&lt;/span&gt;       &lt;span style="color: #008000"&gt;// ...&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum17"&gt;  17:&lt;/span&gt;     }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum18"&gt;  18:&lt;/span&gt;   }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum19"&gt;  19:&lt;/span&gt; }&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;h3&gt;VB.NET&lt;/h3&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;
    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum1"&gt;   1:&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Class&lt;/span&gt; PersonList&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum2"&gt;   2:&lt;/span&gt;   &lt;span style="color: #0000ff"&gt;Public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Sub&lt;/span&gt; Add(&lt;span style="color: #0000ff"&gt;ByVal&lt;/span&gt; obj &lt;span style="color: #0000ff"&gt;As&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Object&lt;/span&gt;)&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum3"&gt;   3:&lt;/span&gt;     &lt;span style="color: #008000"&gt;'&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum4"&gt;   4:&lt;/span&gt;   &lt;span style="color: #0000ff"&gt;End&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Sub&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum5"&gt;   5:&lt;/span&gt;&amp;#160; &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum6"&gt;   6:&lt;/span&gt;   &lt;span style="color: #0000ff"&gt;Public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Sub&lt;/span&gt; Add(&lt;span style="color: #0000ff"&gt;ByVal&lt;/span&gt; person &lt;span style="color: #0000ff"&gt;As&lt;/span&gt; Person)&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum7"&gt;   7:&lt;/span&gt;     &lt;span style="color: #008000"&gt;' &lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum8"&gt;   8:&lt;/span&gt;   &lt;span style="color: #0000ff"&gt;End&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Sub&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum9"&gt;   9:&lt;/span&gt;&amp;#160; &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum10"&gt;  10:&lt;/span&gt;   &lt;span style="color: #0000ff"&gt;Public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Sub&lt;/span&gt; Add(&lt;span style="color: #0000ff"&gt;ByVal&lt;/span&gt; list &lt;span style="color: #0000ff"&gt;As&lt;/span&gt; IEnumerable(Of Person))&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum11"&gt;  11:&lt;/span&gt;     &lt;span style="color: #008000"&gt;' &lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum12"&gt;  12:&lt;/span&gt;   &lt;span style="color: #0000ff"&gt;End&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Sub&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum13"&gt;  13:&lt;/span&gt; &lt;span style="color: #0000ff"&gt;End&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Class&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;Next I create a PersonList collection and then add a list of Vip’s:&lt;/p&gt;

&lt;h3&gt;C#&lt;/h3&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;
    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum1"&gt;   1:&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; Program&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum2"&gt;   2:&lt;/span&gt; {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum3"&gt;   3:&lt;/span&gt;   &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; Main(&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;[] args)&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum4"&gt;   4:&lt;/span&gt;   {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum5"&gt;   5:&lt;/span&gt;     PersonList people = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; PersonList();&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum6"&gt;   6:&lt;/span&gt;&amp;#160; &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum7"&gt;   7:&lt;/span&gt;     List&amp;lt;Vip&amp;gt; others = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; List&amp;lt;Vip&amp;gt; {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum8"&gt;   8:&lt;/span&gt;       &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Vip(), &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Vip()&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum9"&gt;   9:&lt;/span&gt;     };&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum10"&gt;  10:&lt;/span&gt;&amp;#160; &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum11"&gt;  11:&lt;/span&gt;     people.Add(others);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum12"&gt;  12:&lt;/span&gt;   }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum13"&gt;  13:&lt;/span&gt; }&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;h3&gt;VB.NET&lt;/h3&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;
    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum1"&gt;   1:&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Sub&lt;/span&gt; Main()&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum2"&gt;   2:&lt;/span&gt;   &lt;span style="color: #0000ff"&gt;Dim&lt;/span&gt; people &lt;span style="color: #0000ff"&gt;As&lt;/span&gt; &lt;span style="color: #0000ff"&gt;New&lt;/span&gt; PersonList&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum3"&gt;   3:&lt;/span&gt;   &lt;span style="color: #0000ff"&gt;Dim&lt;/span&gt; others &lt;span style="color: #0000ff"&gt;As&lt;/span&gt; &lt;span style="color: #0000ff"&gt;New&lt;/span&gt; List(Of Vip)(&lt;span style="color: #0000ff"&gt;New&lt;/span&gt; Vip() {&lt;span style="color: #0000ff"&gt;New&lt;/span&gt; Vip(), &lt;span style="color: #0000ff"&gt;New&lt;/span&gt; Vip()})&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum4"&gt;   4:&lt;/span&gt;   people.Add(others)&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum5"&gt;   5:&lt;/span&gt; &lt;span style="color: #0000ff"&gt;End&lt;/span&gt; &lt;span style="color: #0000ff"&gt;Sub&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;When I compile this in Visual Studio 2008 the first Add method, taking the object argument, gets called. &lt;/p&gt;

&lt;p&gt;But with Visual Studio 2010 (using the .NET 4 target) the third Add method gets called. This is because now IEnumerable&amp;lt;T&amp;gt; is co-variant and will now convert the List&amp;lt;Vip&amp;gt; to an IEnumerable&amp;lt;Person&amp;gt;.&lt;/p&gt;

&lt;p&gt;Ok, granted. This is NOT something you will see every day, but if you ever encounter it, this might be very confusing.&lt;/p&gt;

&lt;p&gt;This kind of problem can also occur with extension methods and user-defined conversions…&lt;/p&gt;</description><pubDate>Thu, 08 Apr 2010 10:46:05 -1200</pubDate><comments>http://blogs.u2u.be/peter/post/2010/04/08/Overloading-CoContra-variance-could-break-your-code!.aspx#comment</comments><dc:publisher xmlns:dc="http://purl.org/dc/elements/1.1/">Peter Himschoot</dc:publisher><pingback:server xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/">http://blogs.u2u.be/peter/pingback.axd</pingback:server><pingback:target xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/">http://blogs.u2u.be/peter/post.aspx?id=a0164f4a-9839-4e4d-9051-cd0ad7255667</pingback:target><slash:comments xmlns:slash="http://purl.org/rss/1.0/modules/slash/">1</slash:comments><trackback:ping xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">http://blogs.u2u.be/peter/trackback.axd?id=a0164f4a-9839-4e4d-9051-cd0ad7255667</trackback:ping><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.u2u.be/peter/post/2010/04/08/Overloading-CoContra-variance-could-break-your-code!.aspx#comment</wfw:comment><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.u2u.be/peter/syndication.axd?post=a0164f4a-9839-4e4d-9051-cd0ad7255667</wfw:commentRss></item><item><guid isPermaLink="false">http://blogs.u2u.be/diederik/post.aspx?id=aa0c0080-eb39-4c1f-a802-6314fdce9a4f</guid><link>http://blogs.u2u.be/diederik/post/2010/04/08/MEF-in-300-seconds.aspx</link><author>Diederik Krols</author><category>.NET 4.0</category><title>MEF in 300 seconds</title><description>&lt;p&gt;The Managed Extensibility Framework (MEF) is the .NET framework for building applications that are extensible with external plugins after being deployed. I already hear you thinking: "D'oh! Not yet another &lt;em&gt;Reflection slash Inversion of Control slash Composition&lt;/em&gt; framework ?" And yes, that's what it is. But here's the good news: it's easy to use, it fits in a single assembly, and it comes with good credentials and a proven track record. The assembly -&lt;em&gt;System.ComponentModel.Composition&lt;/em&gt;- ships with the .NET framework 4.0 as well as with Silverlight 4.0. You can also download it from &lt;a href="http://mef.codeplex.com/Wikipage"&gt;CodePlex&lt;/a&gt; together with source code and some flashy (but unfortunately also quite complex) examples.&lt;/p&gt;
&lt;h2&gt;Terminology&lt;/h2&gt;
&lt;p&gt;The MEF objectmodel is built around the following concepts:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Part&lt;/strong&gt; - a component that delivers or consumes services,&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Export&lt;/strong&gt; - a service (class or interface) provided by a part,&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Import&lt;/strong&gt; - a service (class or interface) consumed by a part,&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Contract&lt;/strong&gt; - a formal description of imports and exports,&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Catalog&lt;/strong&gt; - a means to publish and discover parts, and &lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Composition&lt;/strong&gt; - building the object graph by hooking up exporters to importers.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;The sample&lt;/h2&gt;
&lt;p&gt;I built a small console app to demonstrate the core features. Here's how it looks like:&lt;/p&gt;
&lt;p&gt;&lt;img src="http://blogs.u2u.be/diederik/image.axd?picture=2010%2f4%2fMEF_Sample.png" alt="" width="50%" /&gt;&lt;/p&gt;
&lt;p&gt;It looks dull on the outside, doesn't it? I agree, but the beauty is within. This application actually composes its object graph dynamically -at run time- from assemblies that it discovers in a folder. Functionally,&amp;nbsp;the console application&amp;nbsp;hooks together electric devices (laptop, television, XBox) and electric power suppliers (nuclear power plants, wind parks, brown coal power stations). Technically, the solution looks like this. Except for the Contract assembly, there are NO references between the projects:&lt;/p&gt;
&lt;p&gt;&lt;img src="http://blogs.u2u.be/diederik/image.axd?picture=2010%2f4%2fSolution.png" alt="" /&gt;&lt;/p&gt;
&lt;h2&gt;Defining a Contract&lt;/h2&gt;
&lt;p&gt;I started with creating an assembly -called Contracts- that describes the services. It's nothing more than a class library with some interface definitions. It will be referenced by all application components - host and parts. Here's the definition of the interfaces:&lt;/p&gt;
&lt;div style="font-family: Courier New; font-size: 9pt; color: black; background: azure;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;namespace&lt;/span&gt; Contracts&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;
&lt;p 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;interface&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;IPowerSupplier&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;string&lt;/span&gt; EnergySource { &lt;span style="color: blue;"&gt;get&lt;/span&gt;; }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;div style="font-family: Courier New; font-size: 9pt; color: black; background: azure;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;namespace&lt;/span&gt; Contracts&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;
&lt;p 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;interface&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;IPowerConsumer&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;string&lt;/span&gt; Start();&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af;"&gt;IPowerSupplier&lt;/span&gt; PowerSupplier { &lt;span style="color: blue;"&gt;get&lt;/span&gt;; &lt;span style="color: blue;"&gt;set&lt;/span&gt;; }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;Export: Exposing a Service&lt;/h2&gt;
&lt;p&gt;The next step is to build some classes that implement these interfaces, and expose their services to MEF. A class&amp;nbsp;indicates&amp;nbsp;exposed services by&amp;nbsp;an &lt;em&gt;[Export]&lt;/em&gt; attribute. This is&amp;nbsp;my representation of the well-known &lt;a href="http://simpsons.wikia.com/wiki/100_Industrial_Way"&gt;Springfield Nuclear Power Plant&lt;/a&gt;:&lt;/p&gt;
&lt;div style="font-family: Courier New; font-size: 9pt; color: black; background: azure;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;namespace&lt;/span&gt; Springfield&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;using&lt;/span&gt; System.ComponentModel.Composition; &lt;span style="color: green;"&gt;// MEF&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;using&lt;/span&gt; Contracts; &lt;span style="color: green;"&gt;// Power Supplier Contract&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; [&lt;span style="color: #2b91af;"&gt;Export&lt;/span&gt;(&lt;span style="color: blue;"&gt;typeof&lt;/span&gt;(&lt;span style="color: #2b91af;"&gt;IPowerSupplier&lt;/span&gt;))]&lt;/p&gt;
&lt;p 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;class&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;NuclearPowerPlant&lt;/span&gt; : &lt;span style="color: #2b91af;"&gt;IPowerSupplier&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &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; EnergySource&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&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;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p 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; &lt;span style="color: blue;"&gt;return&lt;/span&gt; &lt;span style="color: #a31515;"&gt;"split atoms"&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;Import: Requiring a Service&lt;/h2&gt;
&lt;p&gt;A class indicates the services that it requires from MEF through&amp;nbsp;the &lt;em&gt;[Import]&lt;/em&gt; attribute. The following is a representation of &lt;a href="http://h10010.www1.hp.com/wwpc/us/en/sm/WF05a/321957-321957-64295-3740645-3955549-3784202.html"&gt;my laptop&lt;/a&gt;. It exports itself as an &lt;em&gt;IPowerConsumer&lt;/em&gt;, but it expects an &lt;em&gt;IPowerSupplier&lt;/em&gt; from MEF:&lt;/p&gt;
&lt;div style="font-family: Courier New; font-size: 9pt; color: black; background: azure;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;namespace&lt;/span&gt; My&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;using&lt;/span&gt; System.ComponentModel.Composition; &lt;span style="color: green;"&gt;// MEF&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;using&lt;/span&gt; Contracts;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; [&lt;span style="color: #2b91af;"&gt;Export&lt;/span&gt;(&lt;span style="color: blue;"&gt;typeof&lt;/span&gt;(&lt;span style="color: #2b91af;"&gt;IPowerConsumer&lt;/span&gt;))]&lt;/p&gt;
&lt;p 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;class&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;Laptop&lt;/span&gt; : &lt;span style="color: #2b91af;"&gt;IPowerConsumer&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; [&lt;span style="color: #2b91af;"&gt;Import&lt;/span&gt;]&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;IPowerSupplier&lt;/span&gt; PowerSupplier { &lt;span style="color: blue;"&gt;get&lt;/span&gt;; &lt;span style="color: blue;"&gt;set&lt;/span&gt;; }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &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; Start()&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&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;string&lt;/span&gt;.Format(&lt;/p&gt;
&lt;p 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; &lt;span style="color: #a31515;"&gt;"{0} {1} is writing blog entries thanks to the {2} from {3} {4}."&lt;/span&gt;,&lt;/p&gt;
&lt;p 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; &lt;span style="color: blue;"&gt;this&lt;/span&gt;.GetType().Namespace,&lt;/p&gt;
&lt;p 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; &lt;span style="color: blue;"&gt;this&lt;/span&gt;.GetType().Name,&lt;/p&gt;
&lt;p 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; &lt;span style="color: blue;"&gt;this&lt;/span&gt;.PowerSupplier.EnergySource,&lt;/p&gt;
&lt;p 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; &lt;span style="color: blue;"&gt;this&lt;/span&gt;.PowerSupplier.GetType().Namespace,&lt;/p&gt;
&lt;p 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; &lt;span style="color: blue;"&gt;this&lt;/span&gt;.PowerSupplier.GetType().Name);&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;Hooking an importer to an exporter&lt;/h2&gt;
&lt;p&gt;Let's now focus on the host application that will do the composition.&amp;nbsp;It contains a&amp;nbsp;&lt;em&gt;PowerPlug&lt;/em&gt; class that directly hooks a Power Consumer to a Power Supplier. Through the &lt;em&gt;[Import]&lt;/em&gt; attribute in its implementation, my Laptop already indicates to MEF that it requires a Supplier. So the PowerPlug itself only needs to inform MEF that it requires a Consumer:&lt;/p&gt;
&lt;div style="font-family: Courier New; font-size: 9pt; color: black; background: azure;"&gt;
&lt;p 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;PowerPlug&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; [&lt;span style="color: #2b91af;"&gt;Import&lt;/span&gt;(&lt;span style="color: blue;"&gt;typeof&lt;/span&gt;(&lt;span style="color: #2b91af;"&gt;IPowerConsumer&lt;/span&gt;))]&lt;/p&gt;
&lt;p 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;IPowerConsumer&lt;/span&gt; consumer { &lt;span style="color: blue;"&gt;get&lt;/span&gt;; &lt;span style="color: blue;"&gt;set&lt;/span&gt;; }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;PowerPlug's constructor looks up&amp;nbsp;all assemblies in a specified directory,&amp;nbsp;then loads the classes that are decorated with the mentioned attributes, and finally hooks them all up:&lt;/p&gt;
&lt;div style="font-family: Courier New; font-size: 9pt; color: black; background: azure;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt; PowerPlug()&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;var&lt;/span&gt; catalog = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;DirectoryCatalog&lt;/span&gt;(&lt;span style="color: #a31515;"&gt;@"../../Hooked"&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;var&lt;/span&gt; container = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;CompositionContainer&lt;/span&gt;(catalog);&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; container.ComposeParts(&lt;span style="color: blue;"&gt;this&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;All you need to do is instantiating the PowerPlug, and access the object graph:&lt;/p&gt;
&lt;div style="font-family: Courier New; font-size: 9pt; color: black; background: azure;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: #2b91af;"&gt;PowerPlug&lt;/span&gt; plug = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;PowerPlug&lt;/span&gt;();&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: #2b91af;"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: blue;"&gt;string&lt;/span&gt;.Format(&lt;span style="color: #a31515;"&gt;" {0}\n"&lt;/span&gt;, plug.consumer.Start()));&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Please observe that consumer &amp;agrave;nd supplier were discovered and connected.&lt;/p&gt;
&lt;h2&gt;Importing many Exports&lt;/h2&gt;
&lt;p&gt;MEF allows you to import whole collections of instances that expose the same service. So I created another power supplier, the &lt;a href="http://en.wikipedia.org/wiki/Thorntonbank_Wind_Farm"&gt;Thorntonbank Wind Farm&lt;/a&gt; near the belgian coast:&lt;/p&gt;
&lt;div style="font-family: Courier New; font-size: 9pt; color: black; background: azure;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;namespace&lt;/span&gt; ThorntonBank&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;using&lt;/span&gt; System.ComponentModel.Composition; &lt;span style="color: green;"&gt;// MEF&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;using&lt;/span&gt; Contracts; &lt;span style="color: green;"&gt;// Power Supplier Contract&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; [&lt;span style="color: #2b91af;"&gt;Export&lt;/span&gt;(&lt;span style="color: blue;"&gt;typeof&lt;/span&gt;(&lt;span style="color: #2b91af;"&gt;IPowerSupplier&lt;/span&gt;))]&lt;/p&gt;
&lt;p 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;class&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;WindFarm&lt;/span&gt; : &lt;span style="color: #2b91af;"&gt;IPowerSupplier&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &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; EnergySource&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&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;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p 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; &lt;span style="color: blue;"&gt;return&lt;/span&gt; &lt;span style="color: #a31515;"&gt;"wind"&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;In the host application I created a &lt;em&gt;DistributionOperator&lt;/em&gt; class that represents a power grid owner, like &lt;a href="http://www.elia.be"&gt;Elia&lt;/a&gt;. Instances of the class require a list of Suppliers from MEF.&amp;nbsp;DistributionOperator exposes that need through the &lt;em&gt;[ImportMany]&lt;/em&gt; attribute:&lt;/p&gt;
&lt;div style="font-family: Courier New; font-size: 9pt; color: black; background: azure;"&gt;
&lt;p style="margin: 0px;"&gt;[&lt;span style="color: #2b91af;"&gt;ImportMany&lt;/span&gt;]&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af;"&gt;IPowerSupplier&lt;/span&gt;&amp;gt; Suppliers { &lt;span style="color: blue;"&gt;get&lt;/span&gt;; &lt;span style="color: blue;"&gt;set&lt;/span&gt;; }&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;Its constructor is similar to the one from PowerPlug: it looks up all classes in a directory (please note that MEF is equipped with other container types). At run time, just instantiate, and the objectmodel will be available automagically:&lt;/p&gt;
&lt;div style="font-family: Courier New; font-size: 9pt; color: black; background: azure;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: #2b91af;"&gt;DistributionOperator&lt;/span&gt; elia = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;DistributionOperator&lt;/span&gt;();&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;foreach&lt;/span&gt; (&lt;span style="color: blue;"&gt;var&lt;/span&gt; supplier &lt;span style="color: blue;"&gt;in&lt;/span&gt; elia.Suppliers)&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af;"&gt;Console&lt;/span&gt;.WriteLine(&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #a31515;"&gt;" {0} {1} provides electricity from {2}."&lt;/span&gt;,&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; supplier.GetType().Namespace,&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; supplier.GetType().Name,&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; supplier.EnergySource);&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;Exposing Metadata&lt;/h2&gt;
&lt;p&gt;Parts can expose metadata in the form of Key-Value pairs to MEF through the &lt;em&gt;[ExportMetadata]&lt;/em&gt; attribute. The ThorntonBank WindFarm could indicate to MEF that it's a very Kyoto-friendly power source by decorating itself as follows:&lt;/p&gt;
&lt;div style="font-family: Courier New; font-size: 9pt; color: black; background: azure;"&gt;
&lt;p style="margin: 0px;"&gt;[&lt;span style="color: #2b91af;"&gt;ExportMetadata&lt;/span&gt;(&lt;span style="color: #a31515;"&gt;"EnergyType"&lt;/span&gt;, &lt;span style="color: #a31515;"&gt;"Renewable"&lt;/span&gt;)]&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;[&lt;span style="color: #2b91af;"&gt;Export&lt;/span&gt;(&lt;span style="color: blue;"&gt;typeof&lt;/span&gt;(&lt;span style="color: #2b91af;"&gt;IPowerSupplier&lt;/span&gt;))]&lt;/p&gt;
&lt;p 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;WindFarm&lt;/span&gt; : &lt;span style="color: #2b91af;"&gt;IPowerSupplier&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{...}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;This works, but in the strongly-typed world it just makes more sense to define some helper classes in the Contracts-assembly:&lt;/p&gt;
&lt;div style="font-family: Courier New; font-size: 9pt; color: black; background: azure;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;namespace&lt;/span&gt; Contracts&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;
&lt;p 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;enum&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;EnergyTypes&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Renewable,&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Questionable,&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; GloballyWarming&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p 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;class&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;PowerSupplierMetadata&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;const&lt;/span&gt; &lt;span style="color: blue;"&gt;string&lt;/span&gt; EnergyType = &lt;span style="color: #a31515;"&gt;"EnergyType"&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;This results in&amp;nbsp;a safer way to expose the same information, but also one that's easier to consume by other components:&lt;/p&gt;
&lt;div style="font-family: Courier New; font-size: 9pt; color: black; background: azure;"&gt;
&lt;p style="margin: 0px;"&gt;[&lt;span style="color: #2b91af;"&gt;ExportMetadata&lt;/span&gt;(&lt;span style="color: #2b91af;"&gt;PowerSupplierMetadata&lt;/span&gt;.EnergyType, &lt;span style="color: #2b91af;"&gt;EnergyTypes&lt;/span&gt;.Renewable)]&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;[&lt;span style="color: #2b91af;"&gt;Export&lt;/span&gt;(&lt;span style="color: blue;"&gt;typeof&lt;/span&gt;(&lt;span style="color: #2b91af;"&gt;IPowerSupplier&lt;/span&gt;))]&lt;/p&gt;
&lt;p 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;WindFarm&lt;/span&gt; : &lt;span style="color: #2b91af;"&gt;IPowerSupplier&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;// ...&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;Consuming Metadata&lt;/h2&gt;
&lt;p&gt;MEF components can access the metadata, otherwise the concept would make no sense.&amp;nbsp;&amp;nbsp;The MEF assembly contains the very useful (not MEF-specific)&lt;em&gt;&amp;nbsp;Lazy&amp;lt;T&amp;gt;&lt;/em&gt; class, together with (MEF-specific)&amp;nbsp;&lt;em&gt;Lazy&amp;lt;T, TMetadata&amp;gt;&lt;/em&gt;. The latter&amp;nbsp;gives the framework access to the class' metadata without the need for instantiation. TMetadata exposes the keys in the metadata as read-only properties. So I needed to create yet another helper (the Contracts-assembly seemed a good place):&lt;/p&gt;
&lt;div style="font-family: Courier New; font-size: 9pt; color: black; background: azure;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;interface&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;IPowerSupplierMetadata&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af;"&gt;EnergyTypes&lt;/span&gt; EnergyType { &lt;span style="color: blue;"&gt;get&lt;/span&gt;; }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The DistributionOperator class can now be extended with a list of &lt;em&gt;Lazy&amp;lt;IPowerSupplier, IPowerSupplierMetadata&amp;gt;&lt;/em&gt;:&lt;/p&gt;
&lt;div style="font-family: Courier New; font-size: 9pt; color: black; background: azure;"&gt;
&lt;p style="margin: 0px;"&gt;[&lt;span style="color: #2b91af;"&gt;ImportMany&lt;/span&gt;]&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af;"&gt;Lazy&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af;"&gt;IPowerSupplier&lt;/span&gt;, &lt;span style="color: #2b91af;"&gt;IPowerSupplierMetadata&lt;/span&gt;&amp;gt;&amp;gt; SuppliersWithMetadata { &lt;span style="color: blue;"&gt;get&lt;/span&gt;; &lt;span style="color: blue;"&gt;set&lt;/span&gt;; }&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;The instances of this list get a &lt;em&gt;MetaData&lt;/em&gt; property holding the metadata (obviously), and a &lt;em&gt;Value&lt;/em&gt; property that gives access to the PowerSupplier instance. So the DistributionOperator could expose a list of green power suppliers like this:&lt;/p&gt;
&lt;div style="font-family: Courier New; font-size: 9pt; color: black; background: azure;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af;"&gt;IPowerSupplier&lt;/span&gt;&amp;gt; KyotoFriendlySuppliers()&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;var&lt;/span&gt; query = &lt;span style="color: blue;"&gt;from&lt;/span&gt; s &lt;span style="color: blue;"&gt;in&lt;/span&gt; &lt;span style="color: blue;"&gt;this&lt;/span&gt;.SuppliersWithMetadata&lt;/p&gt;
&lt;p 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; &lt;span style="color: blue;"&gt;where&lt;/span&gt; s.Metadata.EnergyType.Equals(&lt;span style="color: #2b91af;"&gt;EnergyTypes&lt;/span&gt;.Renewable)&lt;/p&gt;
&lt;p 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; &lt;span style="color: blue;"&gt;select&lt;/span&gt; s.Value;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;return&lt;/span&gt; query;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Here's how the console app consumes the list:&lt;/p&gt;
&lt;div style="font-family: Courier New; font-size: 9pt; color: black; background: azure;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;foreach&lt;/span&gt; (&lt;span style="color: blue;"&gt;var&lt;/span&gt; supplier &lt;span style="color: blue;"&gt;in&lt;/span&gt; elia.KyotoFriendlySuppliers())&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;{&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af;"&gt;Console&lt;/span&gt;.WriteLine(&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #a31515;"&gt;" {0} {1} provides green electricity from {2}."&lt;/span&gt;,&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; supplier.GetType().Namespace,&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; supplier.GetType().Name,&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; supplier.EnergySource);&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;}&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;What's next&lt;/h2&gt;
&lt;p&gt;This is as good as it gets - at least in 300 seconds. If you're still interested in MEF, then make sure you don't skip &lt;a href="http://msdn.microsoft.com/en-us/magazine/ee291628.aspx"&gt;this excellent article&lt;/a&gt; in MSDN Magazine.&lt;/p&gt;
&lt;h2&gt;Source code&lt;/h2&gt;
&lt;p&gt;Here's the full source code. It is based on preview 9 of MEF from CodePlex, and ye olde Visual Studio.NET 2008: &lt;a href="http://blogs.u2u.be/diederik/file.axd?file=2010%2f4%2fU2UConsult.MEF.Sample.zip"&gt;U2UConsult.MEF.Sample.zip (2,52 mb)&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Enjoy!&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.u2u.be/diederik/file.axd?file=2010%2f4%2fU2UConsult.MEF.Sample.zip"&gt;&lt;/a&gt;&lt;/p&gt;</description><pubDate>Thu, 08 Apr 2010 08:35:00 -1200</pubDate><comments>http://blogs.u2u.be/diederik/post/2010/04/08/MEF-in-300-seconds.aspx#comment</comments><dc:publisher xmlns:dc="http://purl.org/dc/elements/1.1/">Diederik Krols</dc:publisher><pingback:server xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/">http://blogs.u2u.be/diederik/pingback.axd</pingback:server><pingback:target xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/">http://blogs.u2u.be/diederik/post.aspx?id=aa0c0080-eb39-4c1f-a802-6314fdce9a4f</pingback:target><slash:comments xmlns:slash="http://purl.org/rss/1.0/modules/slash/">3</slash:comments><trackback:ping xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">http://blogs.u2u.be/diederik/trackback.axd?id=aa0c0080-eb39-4c1f-a802-6314fdce9a4f</trackback:ping><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.u2u.be/diederik/post/2010/04/08/MEF-in-300-seconds.aspx#comment</wfw:comment><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.u2u.be/diederik/syndication.axd?post=aa0c0080-eb39-4c1f-a802-6314fdce9a4f</wfw:commentRss></item><item><guid isPermaLink="false">http://blogs.u2u.be/u2u/post.aspx?id=1b219c96-5ed3-4be8-a38b-a306a1b563a0</guid><link>http://blogs.u2u.be/u2u/post/2010/04/02/BCS-presentation-at-Techdays-Belgium.aspx</link><author>Lieven Iliano</author><title>BCS presentation at Techdays Belgium</title><description>&lt;p&gt;Last thursday I did a presentation at the Belgium Techdays in Antwerp. My session focussed on the Business Connectivity Service in SharePoint 2010. With the Business Connectivity Service you can integrate external data in the SharePoint and Office user experience. With the new powerfull .NET Assembly connector, you're now able to write code to do the read/write operations. I've promised the attendees to upload the demos I've showed at the event. So below you can find the Twitter BCS Model I've created and the Excel integration. If you are experiencing problems with the BCSSync.exe service which runs client side, make sure to install the correct version of Sql Server Compact x64:&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration: underline;"&gt;&lt;span style="color: #0066cc;"&gt;&lt;a href="http://www.microsoft.com/downloads/details.aspx?displaylang=en&amp;amp;FamilyID=411ba1c5-ba57-45b6-9148-91bed6e7a9f1"&gt;http://www.microsoft.com/downloads/details.aspx?displaylang=en&amp;amp;FamilyID=411ba1c5-ba57-45b6-9148-91bed6e7a9f1&lt;/a&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Thanks to all of you who have attended my session, see you next time.&lt;/p&gt;
&lt;p&gt;Lieven Iliano&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.u2u.be/u2u/file.axd?file=2010%2f4%2fTechdays_Lieven_Iliano_BCS.zip"&gt;Techdays_Lieven_Iliano_BCS.zip (417,53 kb)&lt;/a&gt;&lt;/p&gt;</description><pubDate>Fri, 02 Apr 2010 10:00:00 -1200</pubDate><comments>http://blogs.u2u.be/u2u/post/2010/04/02/BCS-presentation-at-Techdays-Belgium.aspx#comment</comments><dc:publisher xmlns:dc="http://purl.org/dc/elements/1.1/">Lieven Iliano</dc:publisher><pingback:server xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/">http://blogs.u2u.be/u2u/pingback.axd</pingback:server><pingback:target xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/">http://blogs.u2u.be/u2u/post.aspx?id=1b219c96-5ed3-4be8-a38b-a306a1b563a0</pingback:target><slash:comments xmlns:slash="http://purl.org/rss/1.0/modules/slash/">8</slash:comments><trackback:ping xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">http://blogs.u2u.be/u2u/trackback.axd?id=1b219c96-5ed3-4be8-a38b-a306a1b563a0</trackback:ping><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.u2u.be/u2u/post/2010/04/02/BCS-presentation-at-Techdays-Belgium.aspx#comment</wfw:comment><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.u2u.be/u2u/syndication.axd?post=1b219c96-5ed3-4be8-a38b-a306a1b563a0</wfw:commentRss></item><item><guid isPermaLink="false">http://blogs.u2u.be/peter/post.aspx?id=e413e30c-8d72-4008-8624-0aaee7e66428</guid><link>http://blogs.u2u.be/peter/post/2010/03/29/Building-a-declarative-WCF-service-using-Workflow-Foundation-4-and-Content-Based-Correlation.aspx</link><author>Peter Himschoot</author><category>WF 4</category><category>VS2010</category><category>.NET Development</category><title>Building a declarative WCF service using Workflow Foundation 4 and Content Based Correlation</title><description>&lt;p&gt;This blog post accompanies my session on Workflow Foundation 4 programming during the Belgian Tech Days (actually developers and IT-pro days :)). During this session I built a WCF service using Workflow Foundation 4, and I want to show you how to do this on your own…&lt;/p&gt;  &lt;p&gt;In the first part you’ll learn how to create a simple FlowChart workflow and test it, and then in the second part you’ll learn how to setup correlation so multiple players can play the game…&lt;/p&gt;  &lt;h2&gt;Preparing the lab&lt;/h2&gt;  &lt;p&gt;This lab starts with a new project, so start Visual Studio 2010 and create a new &lt;b&gt;workflow service project (call it NumberGuessingGame):&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.u2u.be/peter/image.axd?picture=image_202.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blogs.u2u.be/peter/image.axd?picture=image_thumb_202.png" width="518" height="359" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;This creates a new solution with a workflow project. &lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.u2u.be/peter/image.axd?picture=image_203.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blogs.u2u.be/peter/image.axd?picture=image_thumb_203.png" width="305" height="225" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;&lt;b&gt;Remove Service1.xamlx&lt;/b&gt;, we’re going to add a new service with a better name. &lt;/p&gt;  &lt;p&gt;Right-click the project and add a new item. Select the WCF Workflow Service template and name it &lt;b&gt;GuessIt.xamlx&lt;/b&gt;.&lt;/p&gt;  &lt;h2&gt;Creating the initial FlowChart&lt;/h2&gt;  &lt;p&gt;This adds a workflow service with a sequential activity containing a Receive and Send activity. &lt;b&gt;Delete the sequential activity&lt;/b&gt; leaving the workflow empty. Open the toolbox and drag a &lt;b&gt;FlowChart activity&lt;/b&gt; onto the workflow designer. This should look like this now:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.u2u.be/peter/image.axd?picture=image_204.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blogs.u2u.be/peter/image.axd?picture=image_thumb_204.png" width="359" height="89" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Next drag a &lt;b&gt;Receive activity&lt;/b&gt; onto the designer, below Start. Name it &lt;b&gt;Start A New Game&lt;/b&gt;. Select the receive activity and enter following the &lt;b&gt;OperationName&lt;/b&gt;, &lt;b&gt;ServiceContractName&lt;/b&gt; and &lt;b&gt;CanCreateInstance&lt;/b&gt; properties in the properties window:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.u2u.be/peter/image.axd?picture=image_205.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blogs.u2u.be/peter/image.axd?picture=image_thumb_205.png" width="376" height="288" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Next right-click on the receive activity and select the &lt;b&gt;Create SendReply&lt;/b&gt; option from the drop-down menu. This add a Send activity to the workflow. The Send is coupled to the receive activity through its Request property:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.u2u.be/peter/image.axd?picture=image_206.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blogs.u2u.be/peter/image.axd?picture=image_thumb_206.png" width="258" height="170" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Now connect the activities:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.u2u.be/peter/image.axd?picture=image_207.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blogs.u2u.be/peter/image.axd?picture=image_thumb_207.png" width="322" height="352" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;h2&gt;Adding variables&lt;/h2&gt;  &lt;p&gt;Now, in the workflow designer open the variables window and add a new variable called &lt;b&gt;player&lt;/b&gt; of type &lt;b&gt;String&lt;/b&gt;. This will hold the player name received through the “Start A New Game” Receive activity. You will also see a variable of type &lt;b&gt;CorrelationHandle&lt;/b&gt;. This is used to connect several receive and send activities through correlation. Rename this to &lt;b&gt;gameHandle&lt;/b&gt;.We’ll use this handle later to setup content-based correlation.&lt;/p&gt;  &lt;p&gt;Now add two new variables &lt;b&gt;theNumber&lt;/b&gt; and &lt;b&gt;guess&lt;/b&gt;, both of type &lt;b&gt;Int32&lt;/b&gt;. The first is the number the user needs to guess, to you need to initialize it to a random number between 0 and 100. Use the &lt;b&gt;Random&lt;/b&gt; type to do this:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.u2u.be/peter/image.axd?picture=image_208.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blogs.u2u.be/peter/image.axd?picture=image_thumb_208.png" width="609" height="129" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Go back to the first receive activity. Click on the content property. This opens the Content Definition window. Select the player as the message data (and set the Message type to String):&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.u2u.be/peter/image.axd?picture=image_209.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blogs.u2u.be/peter/image.axd?picture=image_thumb_209.png" width="144" height="94" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Do the same for the send activity, but now use following expression (of type String):&lt;/p&gt;  &lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;   &lt;div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;     &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum1"&gt;   1:&lt;/span&gt; player + &lt;span style="color: #006080"&gt;&amp;quot; guess the number between 0 and 100&amp;quot;&lt;/span&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;You might also get validation errors because you renamed the correlation handle to gameHandle. Change the CorrelationInitilizers property to use gameHandle (click on the … button).&lt;/p&gt;

&lt;p&gt;&lt;a href="http://blogs.u2u.be/peter/image.axd?picture=image_210.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blogs.u2u.be/peter/image.axd?picture=image_thumb_210.png" width="180" height="88" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;h2&gt;Testing the service&lt;/h2&gt;

&lt;p&gt;Press &lt;b&gt;F5&lt;/b&gt; in Visual Studio. The &lt;b&gt;WCF Test Client&lt;/b&gt; will start and automatically fetch meta-data from the service. Double click the &lt;b&gt;Begin&lt;/b&gt; method. This opens a new tab allowing you to invoke the Begin operation on the service. Enter your name and click Invoke. In the bottom area you should see the result of invoking the begin method.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://blogs.u2u.be/peter/image.axd?picture=image_211.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blogs.u2u.be/peter/image.axd?picture=image_thumb_211.png" width="565" height="359" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;This concludes the first step.&lt;/p&gt;

&lt;h2&gt;Adding the data contracts&lt;/h2&gt;

&lt;p&gt;Let’s add a couple of data contracts to the project, one for a game and another for a guess. Note that both contain the player name, this way our service will be able to distinguish between multiple games, as long as each player uses a unique player name:&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;
    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum1"&gt;   1:&lt;/span&gt; [DataContract]&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum2"&gt;   2:&lt;/span&gt;&amp;#160; &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum3"&gt;   3:&lt;/span&gt; &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; Game&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum4"&gt;   4:&lt;/span&gt;&amp;#160; &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum5"&gt;   5:&lt;/span&gt; {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum6"&gt;   6:&lt;/span&gt;&amp;#160; &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum7"&gt;   7:&lt;/span&gt; [DataMember]&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum8"&gt;   8:&lt;/span&gt;&amp;#160; &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum9"&gt;   9:&lt;/span&gt; &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; PlayerName { get; set; }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum10"&gt;  10:&lt;/span&gt;&amp;#160; &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum11"&gt;  11:&lt;/span&gt; [DataMember]&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum12"&gt;  12:&lt;/span&gt;&amp;#160; &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum13"&gt;  13:&lt;/span&gt; &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; Message { get; set; }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum14"&gt;  14:&lt;/span&gt;&amp;#160; &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum15"&gt;  15:&lt;/span&gt; }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum16"&gt;  16:&lt;/span&gt;&amp;#160; &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum17"&gt;  17:&lt;/span&gt; [DataContract]&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum18"&gt;  18:&lt;/span&gt;&amp;#160; &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum19"&gt;  19:&lt;/span&gt; &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; Guess&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum20"&gt;  20:&lt;/span&gt;&amp;#160; &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum21"&gt;  21:&lt;/span&gt; {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum22"&gt;  22:&lt;/span&gt;&amp;#160; &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum23"&gt;  23:&lt;/span&gt; [DataMember(IsRequired = &lt;span style="color: #0000ff"&gt;true&lt;/span&gt;)]&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum24"&gt;  24:&lt;/span&gt;&amp;#160; &lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #606060" id="lnum25"&gt;  25:&lt;/span&gt; &lt;span style="color: #0000ff