<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>DarioSantarelli.Blog(this);</title>
	<atom:link href="http://dariosantarelli.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://dariosantarelli.wordpress.com</link>
	<description></description>
	<lastBuildDate>Sat, 14 Jan 2012 13:14:43 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='dariosantarelli.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://0.gravatar.com/blavatar/20c2c4c6b8e223b7451d7f8294c2e35a?s=96&#038;d=http%3A%2F%2Fs2.wp.com%2Fi%2Fbuttonw-com.png</url>
		<title>DarioSantarelli.Blog(this);</title>
		<link>http://dariosantarelli.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://dariosantarelli.wordpress.com/osd.xml" title="DarioSantarelli.Blog(this);" />
	<atom:link rel='hub' href='http://dariosantarelli.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Using MEF in a Request/Response Service Layer</title>
		<link>http://dariosantarelli.wordpress.com/2011/12/30/using-mef-in-a-requestresponse-service-layer/</link>
		<comments>http://dariosantarelli.wordpress.com/2011/12/30/using-mef-in-a-requestresponse-service-layer/#comments</comments>
		<pubDate>Fri, 30 Dec 2011 15:17:07 +0000</pubDate>
		<dc:creator>dariosantarelli</dc:creator>
				<category><![CDATA[.NET Framework]]></category>
		<category><![CDATA[Microsoft Technology]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[MEF]]></category>
		<category><![CDATA[WCF]]></category>

		<guid isPermaLink="false">http://dariosantarelli.wordpress.com/?p=292</guid>
		<description><![CDATA[In a project of mine I’m using a simple Request/Response service layer very similar to the amazing Davy Brion’s Agatha project. Everything started a few time ago when I was searching for a smart way to design a client-server infrastructure which was focused on messages and not on operations. This layer would be not only [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dariosantarelli.wordpress.com&amp;blog=502627&amp;post=292&amp;subd=dariosantarelli&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>In a project of mine I’m using a simple Request/Response service layer very similar to the amazing <a href="http://davybrion.github.com/Agatha/">Davy Brion’s Agatha project</a>. Everything started a few time ago when I was searching for a smart way to design a client-server infrastructure which was focused on messages and not on operations. This layer would be not only a classic WCF-based service, but also a some kind of in-process facade to my business layer where I could centralize any cross cutting concern. So I was focused on the capability of moving my service layer and its business logic to a separate machine and hosting it through WCF without any significant modifications to my code (admitting that service layer doesn’t share state with upper layers like the presentation layer). After reading Davy’s “<a href="http://davybrion.com/blog/2009/07/why-i-dislike-classic-or-typical-wcf-usage/">Why I Dislike Classic Or Typical WCF Usage</a>” , I was convinced to address myself to one service contract with one service operation, avoiding to spend time thinking about how to design and implement service contracts and operations. In this way, the first (great) advantage I got is that I can add functionalities simply defining a request message, a response message and a request handler which executes the logic needed between a request receiving and a response sending. Very simple and effective!</p>
<h4>Overview of the service layer</h4>
<p>Everything starts from an interface called <em>IRequestHandler</em>:</p>
<div style="font-size:11px;">
<pre class="code"><span style="color:blue;">public interface </span><span style="color:#2b91af;">IRequestHandler </span>
{
  <span style="color:#2b91af;">Response </span>HandleRequest(<span style="color:#2b91af;">Request </span>request);
}</pre>
</div>
<p><em>Request</em> and <em>Response</em> are empty base abstract classes:</p>
<div style="font-size:11px;">
<pre class="code"><span style="color:blue;">public abstract class </span><span style="color:#2b91af;">Request </span>{}
<span style="color:blue;">public abstract class </span><span style="color:#2b91af;">Response </span>{}</pre>
</div>
<p>Now let&#8217;s define a simple rule: every concrete request type must be corresponded by a concrete response type. The idea is to basically consider each service operation as a request which must have a response. For each request you define, you need to provide an handler which does whatever it needs to do to handle the request and returns a response. In my solution, a simple generic base request handler has been defined in the following way:</p>
<div style="font-size:11px;">
<pre class="code"><span style="color:blue;">public abstract class </span><span style="color:#2b91af;">RequestHandlerBase</span>&lt;TRequest, TResponse&gt; : <span style="color:#2b91af;">IRequestHandler </span>
                                                             <span style="color:blue;"> where </span>TRequest : <span style="color:#2b91af;">Request </span>
                                                             <span style="color:blue;"> where </span>TResponse : <span style="color:#2b91af;">Response </span>
{
    <span style="color:blue;">public </span><span style="color:#2b91af;">Response </span>HandleRequest(<span style="color:#2b91af;">Request </span>request)
    {
        <span style="color:blue;">return </span>HandleRequest((TRequest)request);
    }

    <span style="color:blue;">public abstract </span>TResponse HandleRequest(TRequest request);
}</pre>
</div>
<p>For example, by inheriting this <em>RequestHandlerBase</em> base class, we could create a login request handler, which implements the business logic for validating a user:</p>
<div style="font-size:11px;">
<pre class="code"><span style="color:blue;">public class </span><span style="color:#2b91af;">LoginRequestHandler </span>: <span style="color:#2b91af;">RequestHandlerBase</span>&lt;<span style="color:#2b91af;">LoginRequest</span>, <span style="color:#2b91af;">LoginResponse</span>&gt;
{
    <span style="color:blue;">public override </span><span style="color:#2b91af;">LoginResponse </span>HandleRequest(<span style="color:#2b91af;">LoginRequest </span>request)
    {
     <span style="color:green;"> // validate the user credentials contained in the LoginRequest </span>
     <span style="color:green;"> // and return a LoginResponse containing, for example, a session token </span>
    }
}</pre>
</div>
<p>Having each request handler implemented in the same fashion as the <em>LoginRequestHandler</em>, I’d like to reach a service implementation like this:</p>
<div style="font-size:11px;">
<pre class="code"><span style="color:blue;">public class </span><span style="color:#2b91af;">MyService </span>: <span style="color:#2b91af;">IRequestHandler </span>
{
    <span style="color:blue;">private readonly </span><span style="color:#2b91af;">IRequestHandlerProvider </span>_requestHandlerProvider;

    <span style="color:blue;">public </span>MyService(<span style="color:#2b91af;">IRequestHandlerProvider </span>requestHandlerProvider)
    {
        _requestHandlerProvider = requestHandlerProvider;
    }

    <span style="color:blue;">public </span><span style="color:#2b91af;">Response </span>HandleRequest(<span style="color:#2b91af;">Request </span>request)
    {
        <span style="color:blue;">return </span>_requestHandlerProvider.GetRequestHandler(request.GetType()).HandleRequest(request);
    }
}</pre>
</div>
<p>As you can see, the service itself implements the <em>IRequestHandler</em> interface and the actual implementation is very minimal: it&#8217;s just a small class which resolves the appropriate handler through an abstraction called <em>IRequestHandlerProvider</em> which internally may use an IoC container capable of resolving the request handler associated to a request type. Then, the service delegates the execution to an handler by passing the request to it, and finally it returns a typed response to the client.</p>
<h4>Enter MEF</h4>
<p>Now we could need a IoC container to resolve and create the instances of the request handlers. As shown in this <a href="http://davybrion.com/blog/2008/07/the-request-response-service-layer/">post</a>, you can use the <a href="http://docs.castleproject.org/Windsor.MainPage.ashx">Castle Windsor</a> IoC container to use <a href="http://davybrion.com/blog/2007/07/introduction-to-dependency-injection/">dependency injection</a>. That basically allows you to register each valid request handler that is present, for example, in a given assembly. Even my purpose was to find a simple way to plug in request handlers defined in external assemblies, getting everything registered automatically in a centralized request handler provider when the application starts up. So, I was focused on <a href="http://mef.codeplex.com/wikipage?title=Overview">MEF</a>. What I tried to do is to treat each request handler as an extension, because in my project each tuple “Request-Response-Handler” represents an extension unit. Moreover,</p>
<ul>
<li>MEF is an integral part of the .NET Framework 4</li>
<li>MEF<em> </em>offers a set of discovery approaches for locating and loading available extensions even in a “lazy” fashion</li>
<li>MEF allows tagging extensions with additonal metadata which facilitates rich querying and filtering so an extensibility element can provide metadata to exported items.</li>
</ul>
<p>Following this philosophy, beyond the classic <a href="http://mef.codeplex.com/wikipage?title=Declaring%20Exports&amp;referringTitle=Overview">Export</a> attribute I introduced a <em>RequestHandlerMetadataAttribute</em> useful for simplifying the process of resolving an handler related to a specific request type. So, the <em>LoginRequestHandler</em> defined before could be decorated as the following&#8230;</p>
<div style="font-size:11px;">
<pre class="code">[<span style="color:#2b91af;">Export</span>(<span style="color:blue;">typeof</span>(<span style="color:#2b91af;">IRequestHandler</span>))] <span style="color:green;">// I am a request handler!!!</span>
[<span style="color:#2b91af;">RequestHandlerMetadata</span>(<span style="color:blue;">typeof</span>(<span style="color:#2b91af;">LoginRequest</span>))] <span style="color:green;">// I can handle Login requests!!!</span>
<span style="color:blue;">public class </span><span style="color:#2b91af;">LoginRequestHandler </span>: <span style="color:#2b91af;">RequestHandlerBase</span>&lt;<span style="color:#2b91af;">LoginRequest</span>, <span style="color:#2b91af;">LoginResponse</span>&gt;
{
  <span style="color:blue;">... </span>
}</pre>
</div>
<p>And here is the <em>RequestHandlerMetadataAttribute</em> definition.</p>
<div style="font-size:11px;">
<pre class="code"><span style="color:blue;">public interface </span><span style="color:#2b91af;">IRequestHandlerMetadata </span>
{
    <span style="color:#2b91af;">Type </span>RequestType { <span style="color:blue;">get</span>; }
}

[<span style="color:#2b91af;">MetadataAttribute</span>]
[<span style="color:#2b91af;">AttributeUsage</span>(<span style="color:#2b91af;">AttributeTargets</span>.Class, AllowMultiple = <span style="color:blue;">false</span>)]
<span style="color:blue;">public class </span><span style="color:#2b91af;">RequestHandlerMetadataAttribute </span>: <span style="color:#2b91af;">ExportAttribute </span>
{
    <span style="color:blue;">public </span><span style="color:#2b91af;">Type </span>RequestType { <span style="color:blue;">get</span>; <span style="color:blue;">set</span>; }

    <span style="color:blue;">public </span>RequestHandlerMetadataAttribute(<span style="color:#2b91af;">Type </span>requestType)
        : <span style="color:blue;">base</span>(<span style="color:blue;">typeof</span>(<span style="color:#2b91af;">IRequestHandlerMetadata</span>))
    {
        RequestType = requestType;
    }
}</pre>
</div>
<p>In order to allow us to access the metadata, MEF introduces a special kind of <a href="http://msdn.microsoft.com/en-us/library/dd986615(VS.96).aspx">Lazy&lt;T,M&gt;</a> that has attached metadata. <em>M</em> in this case is an interface (called “metadata view”) that contains only getter properties. <span style="text-decoration:underline;">MEF automatically generates a proxy class that implements this interface and it plugs all the metadata in for us</span>. This is very cool! What is happening behind the scenes is that MEF is using reflection emit in order to construct the typed metadata view.</p>
<p>As result, I&#8217;ve used the metadata view <em>IRequestHandlerMetadata</em> to easily find the right handler for a request of a given type, as shown in the <em>GetRequestHandler()</em> method of the following <em>MefRequestHandlerProvider</em>.</p>
<div style="font-size:11px;">
<pre class="code" style="overflow:auto;"><span style="color:blue;">public class </span><span style="color:#2b91af;">MefRequestHandlerProvider </span>: <span style="color:#2b91af;">IRequestHandlerProvider </span>
{
    <span style="color:blue;">private readonly </span><span style="color:#2b91af;">List</span>&lt;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">IRequestHandler</span>, <span style="color:#2b91af;">IRequestHandlerMetadata</span>&gt;&gt; _registeredHandlers =
                                              <span style="color:blue;">new </span><span style="color:#2b91af;">List</span>&lt;<span style="color:#2b91af;">Lazy</span>&lt;<span style="color:#2b91af;">IRequestHandler</span>, <span style="color:#2b91af;">IRequestHandlerMetadata</span>&gt;&gt;();

    <span style="color:blue;">public </span>MefRequestHandlerProvider(<span style="color:#2b91af;">IEnumerable</span>&lt;<span style="color:#2b91af;">ComposablePartCatalog</span>&gt; catalogs)
    {
        <span style="color:blue;">foreach </span>(<span style="color:blue;">var </span>catalog <span style="color:blue;">in </span>catalogs)
        {
            <span style="color:blue;">var </span>container = <span style="color:blue;">new </span><span style="color:#2b91af;">CompositionContainer</span>(catalog);
            <span style="color:blue;">var </span>exportedHandlers = container.GetExports&lt;<span style="color:#2b91af;">IRequestHandler</span>, <span style="color:#2b91af;">IRequestHandlerMetadata</span>&gt;();

            <span style="color:blue;">foreach </span>(<span style="color:blue;">var </span>exportedHandler <span style="color:blue;">in </span>exportedHandlers)
            {
                <span style="color:blue;">foreach </span>(<span style="color:blue;">var </span>registeredHandler <span style="color:blue;">in </span>_registeredHandlers)
                {
                    <span style="color:blue;">if </span>(registeredHandler.Metadata.RequestType == exportedHandler.Metadata.RequestType)
                        <span style="color:blue;">throw new </span><span style="color:#2b91af;">NotSupportedException</span>(<span style="color:blue;">string</span>.Format(<span style="color:#a31515;">"A request handler for type </span><span style="color:#3cb371;">{0} </span><span style="color:#a31515;">is already registered."</span>,
                                                                      exportedHandler.Metadata.RequestType));
                }
            }

            _registeredHandlers.AddRange(exportedHandlers);
        }
    }

    <span style="color:blue;">public </span><span style="color:#2b91af;">IRequestHandler </span>GetRequestHandler(<span style="color:#2b91af;">Type </span>requestType)
    {
        <span style="color:blue;">var </span>handler = _registeredHandlers.SingleOrDefault(r =&gt; r.Metadata.RequestType == requestType);
        <span style="color:blue;">if </span>(handler != <span style="color:blue;">null</span>) <span style="color:blue;">return </span>handler.Value;
        <span style="color:blue;">throw new </span><span style="color:#2b91af;">RequestHandlerNotFoundException</span>(<span style="color:blue;">string</span>.Format(<span style="color:#a31515;">"No request handler has been found for type </span><span style="color:#3cb371;">{0}</span><span style="color:#a31515;">"</span>, requestType));
    }
}</pre>
</div>
<p>Putting all together, in the application startup I placed the following initialization code which uses the <em>MefRequestHandlerProvider</em>. As you may know, request handlers can be located inside catalogs (e.g. <a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.composition.hosting.assemblycatalog.aspx">AssemblyCatalog</a>, <a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.composition.hosting.aggregatecatalog.aspx">AggregateCatalog</a> etc.). When using a client proxy, I need to expose just one method which never needs to be updated. That’s a good advantage!</p>
<div style="font-size:11px;">
<pre class="code"><span style="color:green;">// Server-Side </span>
<span style="color:blue;">var </span>catalog = <span style="color:blue;">new </span><span style="color:#2b91af;">AssemblyCatalog</span>(<span style="color:blue;">typeof</span>(<span style="color:#2b91af;">IRequestHandler</span>).Assembly);
<span style="color:blue;">var </span>mefRequestHandlerProvider = <span style="color:blue;">new </span><span style="color:#2b91af;">MefRequestHandlerProvider</span>(<span style="color:blue;">new</span>[] { catalog });
<span style="color:#2b91af;">MyService </span>service = <span style="color:blue;">new </span><span style="color:#2b91af;">MyService</span>(mefRequestHandlerProvider);

<span style="color:green;">// Client-Side (when using a proxy)</span>
<span style="color:#2b91af;">LoginResponse </span>response = (<span style="color:#2b91af;">LoginResponse</span>)serviceProxy.HandleRequest(<span style="color:blue;">new </span><span style="color:#2b91af;">LoginRequest</span>(<span style="color:#a31515;">"username"</span>, <span style="color:#a31515;">"password"</span>));
<span style="color:#2b91af;">Console</span>.WriteLine(response.AccessToken);</pre>
</div>
<h4>Conclusion</h4>
<p>In this post I’ve tried to show how I used MEF to plug request handlers in a request/response service layer similar to <a href="http://davybrion.github.com/Agatha/">Agatha</a>. I have not written anything about serialization issues when exposing the service layer through WCF. Moreover, I haven&#8217;t treated any aspects about request handlers such as objects lifecycles or error management. I think that any question about these service layer insights can find good answers after reading the <a href="http://davybrion.com/blog/2009/11/requestresponse-service-layer-series/">Davy Brion’s Request/Response Service Layer Series</a>.</p>
<p>HTH</p>
<br />Filed under: <a href='http://dariosantarelli.wordpress.com/category/net-framework/'>.NET Framework</a>, <a href='http://dariosantarelli.wordpress.com/category/microsoft-technology/'>Microsoft Technology</a>, <a href='http://dariosantarelli.wordpress.com/category/programming/'>Programming</a> Tagged: <a href='http://dariosantarelli.wordpress.com/tag/mef/'>MEF</a>, <a href='http://dariosantarelli.wordpress.com/tag/wcf/'>WCF</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/dariosantarelli.wordpress.com/292/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/dariosantarelli.wordpress.com/292/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/dariosantarelli.wordpress.com/292/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/dariosantarelli.wordpress.com/292/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/dariosantarelli.wordpress.com/292/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/dariosantarelli.wordpress.com/292/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/dariosantarelli.wordpress.com/292/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/dariosantarelli.wordpress.com/292/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/dariosantarelli.wordpress.com/292/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/dariosantarelli.wordpress.com/292/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/dariosantarelli.wordpress.com/292/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/dariosantarelli.wordpress.com/292/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/dariosantarelli.wordpress.com/292/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/dariosantarelli.wordpress.com/292/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dariosantarelli.wordpress.com&amp;blog=502627&amp;post=292&amp;subd=dariosantarelli&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://dariosantarelli.wordpress.com/2011/12/30/using-mef-in-a-requestresponse-service-layer/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/3805217b57ce1cc8bacbb870a2e3479f?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">dariosantarelli</media:title>
		</media:content>
	</item>
		<item>
		<title>[Entity Framework v4] Identity map pattern</title>
		<link>http://dariosantarelli.wordpress.com/2011/11/26/entity-framework-v4-identity-map-pattern/</link>
		<comments>http://dariosantarelli.wordpress.com/2011/11/26/entity-framework-v4-identity-map-pattern/#comments</comments>
		<pubDate>Sat, 26 Nov 2011 12:07:51 +0000</pubDate>
		<dc:creator>dariosantarelli</dc:creator>
				<category><![CDATA[Entity Framework]]></category>
		<category><![CDATA[EntityFramework]]></category>
		<category><![CDATA[IdentityMap]]></category>
		<category><![CDATA[Pattern]]></category>

		<guid isPermaLink="false">http://dariosantarelli.wordpress.com/?p=271</guid>
		<description><![CDATA[One of the most important pattern that a good ORM technology should support in order to face the object-relational impedance mismatch is the Identity Map pattern. It’s just one of a set of conceptual and technical difficulties emerging when objects or class definitions are mapped in a straightforward way to database tables or relational schemas. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dariosantarelli.wordpress.com&amp;blog=502627&amp;post=271&amp;subd=dariosantarelli&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>One of the most important pattern that a good <a href="http://en.wikipedia.org/wiki/Object-relational_mapping" target="_blank">ORM</a> technology should support in order to face the <a href="http://en.wikipedia.org/wiki/Object-relational_impedance_mismatch" target="_blank">object-relational impedance mismatch</a> is the <a href="http://en.wikipedia.org/wiki/Identity_map" target="_blank">Identity Map</a> pattern. It’s just one of a set of conceptual and technical difficulties emerging when objects or class definitions are mapped in a straightforward way to database tables or relational schemas.</p>
<h3>What’s Identity Map?</h3>
<p>In Martin Fowler’s book “<a href="http://books.google.com/books/about/Patterns_of_enterprise_application_archi.html?id=FyWZt5DdvFkC" target="_blank">Patterns of Enterprise Application Architecture</a>”, the <em>Identity Map</em> is defined as a way of ensuring <em>“that each object gets loaded only once by keeping every loaded object in a map. Looks up objects using the map when referring to them.”</em>  If the requested data has already been loaded from the database, the identity map has to return the same instance of the already instantiated object and if it has not been loaded yet, it should load it and stores the new object in the map. In this way, it follows a similar principle to <a href="http://en.wikipedia.org/wiki/Lazy_loading">lazy loading</a>. As result, the <em>Identity Map</em> design pattern introduces a consistent way of querying and persisting objects (e.g. through a context-specific in-memory cache) which prevents applications from duplicate retrievals of the same object data from the database.</p>
<p>Ok, in order to better understand this concept, let’s start from a non-Identity Map example. If we have an application that uses a simple persistence layer that performs a database query and then materializes one or more objects, we might see code that creates different instances of the same logical entity:</p>
<div style="font-size:1.2em;">
<pre class="code">[<span style="color:#2b91af;">TestMethod</span>]
<span style="color:blue;">public void </span>Non_IdentityMap_Solution_Provides_Different_Copies_Of_The_Same_Customer()
{
    <span style="color:#2b91af;">Customer </span>customer1 = DAL.<span style="color:#2b91af;">Customers</span>.GetCustomerById(<span style="color:#a31515;">"dsantarelli"</span>);
    <span style="color:#2b91af;">Customer </span>customer2 = DAL.<span style="color:#2b91af;">Customers</span>.GetCustomerById(<span style="color:#a31515;">"dsantarelli"</span>);

    <span style="color:green;">// customer1 and customer2 should represent the same customer...</span>        <span style="color:#2b91af;"> </span>
    <span style="color:#2b91af;">Assert</span>.AreEqual(customer1.CustomerId, customer2.CustomerId);
    <span style="color:#2b91af;">Assert</span>.AreEqual(customer1.Email, customer2.Email);

    <span style="color:green;">// ... but they are two separate instances! </span><span style="color:#2b91af;">  </span><span style="color:#2b91af;">  </span>
    <span style="color:#2b91af;">Assert</span>.IsFalse(customer1 == customer2);

    <span style="color:green;">// If we change a property of customer1... </span>
    customer1.Email = <span style="color:#a31515;">"xxx@yyy.zzz"</span>;

    <span style="color:green;">// ... then, which instance should be valid? </span><span style="color:#2b91af;"> </span>
    <span style="color:#2b91af;">Assert</span>.AreNotEqual(customer1.Email, customer2.Email);
}</pre>
</div>
<p>In this example, <em>customer1</em> and <em>customer2</em> both contain separate copies of the data for the same customer. If we change the data in <em>customer1</em>, the change has no effect on <em>customer2</em>. If we make changes to both and then save them back to the database, one just overwrites the changes of the other. That’s because our persistence framework <span style="text-decoration:underline;">just doesn’t know</span> that <em>customer1</em> and <em>customer2</em> both contain data for the same logical entity.</p>
<p><em>Conclusion</em>: <strong><em><span style="text-decoration:underline;">multiple objects containing data for the same entity, lead to concurrency problems when it’s time to save data</span>.</em></strong></p>
<h3>How does Entity Framework approach the Identity Map pattern?</h3>
<p>Now let’s have a look at the Identity Map way! In the unit test below, we have some Entity Framework code in which three different object queries are executed in order to get data for the same customer:</p>
<div style="font-size:1.2em;">
<pre class="code">[<span style="color:#2b91af;">TestMethod</span>]
<span style="color:blue;">public void </span>EF_IdentityMap_Solution_Provides_References_To_The_Same_Instance_Of_Customer()
{
    <span style="color:blue;">using </span>(<span style="color:#2b91af;">EFContext </span>context = <span style="color:blue;">new </span><span style="color:#2b91af;">EFContext</span>())
    {
        <span style="color:#2b91af;">Customer </span>customer1 = context.Customers.Single(c =&gt; c.CustomerId == <span style="color:#a31515;">"dsantarelli"</span>);
        <span style="color:#2b91af;">Customer </span>customer2 = context.Customers.Single(c =&gt; c.Email == <span style="color:#a31515;">"dario@santarelli.com"</span>);
        <span style="color:#2b91af;">Customer </span>customer3 = context.Customers.First(c =&gt; c.ContactName == <span style="color:#a31515;">"Dario Santarelli"</span>);

        <span style="color:green;">// The three queries above should return the same customer. </span><span style="color:green;"> </span>
        <span style="color:green;"> // So, </span><span style="color:green;">customer 1,2 and 3 are references to the same instance of Customer. </span>
       <span style="color:#2b91af;"> Assert</span>.IsTrue(customer1 == customer2);
        <span style="color:#2b91af;">Assert</span>.IsTrue(customer2 == customer3);

        <span style="color:green;">// Now if we change a property of customer1... </span>
        customer1.Email = <span style="color:#a31515;">"xxx@yyy.zzz"</span>;

        <span style="color:green;">// ... then customer 1,2 and 3 still remain valid references to the same instance of Customer. </span>
        <span style="color:#2b91af;"> Assert</span>.AreEqual(customer1.Email, customer2.Email);
        <span style="color:#2b91af;">Assert</span>.AreEqual(customer2.Email, customer3.Email);
    }
}</pre>
</div>
<p>How you can see, now all 3 customers are equal. Moreover, when we change a property on <em>customer1</em>, we get that same change on <em>customer2 </em>and<em> customer3</em>. In fact, they’re all references to a single object that is managed by the EF’s <strong><a href="http://msdn.microsoft.com/en-us/library/system.data.objects.objectcontext.aspx" target="_blank">ObjectContext</a></strong>. Behind the scenes EF ensures that only one entity object is created and the multiple entities that we try to load are just multiple references to that one object, regardless of how many times or how many different are the ways we load an entity. This is a behavior compliant with the Identity Map pattern!</p>
<p><strong>The key is EntityKey</strong></p>
<p>So how does this work?  First of all, every entity type has a key that uniquely identifies that entity.</p>
<p>If your Customer entity inherits from <a href="http://msdn.microsoft.com/en-us/library/system.data.objects.dataclasses.entityobject.aspx">EntityObject</a> (which is the base class for all data classes generated by the <a href="http://msdn.microsoft.com/en-us/library/ee382825.aspx" target="_blank">Entity Data Model</a> tools) or simply implements the <a href="http://msdn.microsoft.com/en-us/library/system.data.objects.dataclasses.ientitywithkey.aspx">IEntityWithKey</a> interface, in the debugger you&#8217;ll notice that Customer has a property that EF created for you named <a href="http://msdn.microsoft.com/en-us/library/dd283139.aspx" target="_blank">EntityKey</a> (which corresponds to the primary key in the database). EntityKey contains data about all the information ObjectContext needs in order to maintain an Identity Map. You could think of the map as a “cache” that contains only one instance of each object identified by its EntityKey.</p>
<p>REMEMBER: <em>Entity Framework v4 does not require you to implement <strong>IEntityWithKey</strong> in a custom data class especially if you use </em><a href="http://msdn.microsoft.com/en-us/library/dd456853.aspx" target="_blank"><em>POCO entities</em></a><em>.</em></p>
<p>In the previous example, when we get <em>customer1</em> from our context, by default EF runs the query, creates an instance of Customer (uniquely identified by its key <em>CustomerId</em>), stores that object in the cache, and gives us back a reference to it. When we get <em>customer2</em> from the context, the context <span style="text-decoration:underline;">does run the query again</span> and pulls data from our database, but then it sees that it already has a customer entity with the same EntityKey in the cache so it throws out the data and returns a reference to the entity that’s already in cache. The same thing happens for <em>customer3</em>.</p>
<p>So how many database queries EF will perform if we write something like this?</p>
<div style="font-size:1.2em;">
<pre class="code"><span style="color:#2b91af;">Customer </span>customer1 = context.Customers.Single(c =&gt; c.CustomerId == <span style="color:#a31515;">"dsantarelli"</span>);
<span style="color:#2b91af;">Customer </span>customer2 = context.Customers.Single(c =&gt; c.CustomerId == <span style="color:#a31515;">"dsantarelli"</span>);
<span style="color:#2b91af;">Customer </span>customer3 = context.Customers.Single(c =&gt; c.CustomerId == <span style="color:#a31515;">"dsantarelli"</span>);</pre>
</div>
<p>The answer is: three.</p>
<p>Wait&#8230; if there’s a cache, why is it performing three queries? The second part of Martin Fowler’s definition of Identity map says “&#8230; l<em>ooks up objects using the map when referring to them</em>”. An obvious question is: if I’m loading an object that already exists in my cache, and EF is just going to return a reference to that cached object and throw away any changes it gets from the database query, can’t I just get the object directly from my cache and skip the database query altogether? That could really reduce database load.</p>
<p>The answer is: <span style="text-decoration:underline;">you could explicitly get an entity directly from the cache without hitting the database</span>, but only if you use a special method to get the entity by its EntityKey. Here an example:</p>
<div style="font-size:1.2em;">
<pre class="code"><span style="color:#2b91af;">EntityKey </span>entityKey = <span style="color:blue;">new </span><span style="color:#2b91af;">EntityKey</span>(<span style="color:#a31515;">"EFContext.Customers"</span>, <span style="color:#a31515;">"CustomerId"</span>, <span style="color:#a31515;">"dsantarelli"</span>);</pre>
<pre class="code"><span style="color:blue;">object </span>customerObj;
<span style="color:blue;">if </span>(context.TryGetObjectByKey(entityKey, <span style="color:blue;">out </span>customerObj))
{
    <span style="color:green;">// the customer has been found in the cache </span><span style="color:#2b91af;">Customer </span>customer = (<span style="color:#2b91af;">Customer</span>)customerObj;
}</pre>
</div>
<p>What about if we don’t know the actual value of an EntityKey? Well, we can’t use this feature.</p>
<p>In fact, having to use the EntityKey is a big limitation since most of the time you want to look up data by some other field and not by a primary key which could be a Guid or another data type impossible to know.</p>
<h3>Identity Map and MergeOptions</h3>
<p>Now two interesting questions:</p>
<p>Can I customize the strategy that EF uses to compare the datasource values and the cache entities values?<br />
What happens to cached entities when the underlying database rows change?</p>
<p>Suppose to have the following code:</p>
<div style="font-size:1.2em;">
<pre class="code"><span style="color:#2b91af;">Customer </span>customer1 = context.Customers.Single(c =&gt; c.CustomerId == <span style="color:#a31515;">"dsantarelli"</span>);
<span style="color:green;">// Now someone changes the customer1 record in the DB!!! </span>
<span style="color:#2b91af;">Customer </span>customer2 = context.Customers.Single(c =&gt; c.CustomerId == <span style="color:#a31515;">"dsantarelli"</span>);</pre>
</div>
<p>After <em>customer1</em> is loaded, someone changes the record in the DB. Will <em>customer2</em> have the original values, or the new values? Remember that <em>customer1</em> and <em>customer2</em> are references to the same entity object in the cache, and our first db hit when we got <em>customer1</em> did pull the original value, but then the query for <em>customer2</em> also hit the database and pulled data. How does EF handle that? The answer is: it depends on the <a href="http://msdn.microsoft.com/en-us/library/system.data.objects.mergeoption.aspx" target="_blank">MergeOption</a> enumeration. The possible options are:</p>
<p><strong>AppendOnly</strong> (default) : It simply throws the new data out. If an object is already in the context, the current and original values of object&#8217;s properties in the entry are not overwritten with data source values. The state of the object&#8217;s entry and state of properties of the object in the entry do not change and Identity Map is guaranteed. Here&#8217;s a test example:</p>
<div style="font-size:1.2em;">
<pre class="code">[<span style="color:#2b91af;">TestMethod</span>]
<span style="color:blue;">public void </span>EF_AppendOnly_MergeOption_Throws_NewData_Away()
{
    <span style="color:blue;">using </span>(<span style="color:#2b91af;">EFContext </span>context = <span style="color:blue;">new </span><span style="color:#2b91af;">EFContext</span>())
    {
        <strong>context.Customers.MergeOption = <span style="color:#2b91af;">MergeOption</span>.AppendOnly;</strong>

        <span style="color:#2b91af;">Customer </span>customer1 = context.Customers.Single(c =&gt; c.CustomerId == <span style="color:#a31515;">"dsantarelli"</span>);
        <span style="color:#2b91af;">Assert</span>.AreEqual(customer1.ContactName, <span style="color:#a31515;">"Dario Santarelli"</span>);

        <span style="color:green;">// Now someone changes the customer1 record in the DB</span>
        <span style="color:green;">// by setting ContactName = "Luigi Santarelli" !!!</span>
        ChangeDBRecord(<span style="color:#a31515;">"dsantarelli" , <span style="color:#a31515;">"Luigi Santarelli"</span></span>);

        <span style="color:#2b91af;">Customer </span>customer2 = context.Customers.Single(c =&gt; c.CustomerId == <span style="color:#a31515;">"dsantarelli"</span>);

        <span style="color:#2b91af;">Assert</span>.IsTrue(customer1 == customer2); <span style="color:green;">// They are references to the same Customer instance (Identity Map)</span>
        <span style="color:#2b91af;">Assert</span>.AreEqual(customer2.ContactName, <span style="color:#a31515;">"Dario Santarelli"</span>);  <span style="color:green;">// Original values win! </span>}
}</pre>
</div>
<p><strong>OverwriteChanges:</strong> Unlike the <em>AppendOnly</em> option, it applies new data. If an object is already in the context, the current and original values of object&#8217;s properties in the entry are overwritten with data source values, ignoring every changes we make in the meanwhile. Identity Map principle is still preserved.</p>
<div style="font-size:1.2em;">
<pre class="code">[<span style="color:#2b91af;">TestMethod</span>]
<span style="color:blue;">public void </span>EF_OverwriteChanges_MergeOption_Applies_NewData()
{
    <span style="color:blue;">using </span>(<span style="color:#2b91af;">EFContext </span>context = <span style="color:blue;">new </span><span style="color:#2b91af;">EFContext</span>())
    {
       <strong> context.Customers.MergeOption = <span style="color:#2b91af;">MergeOption</span>.OverwriteChanges;</strong>

        <span style="color:#2b91af;">Customer </span>customer1 = context.Customers.Single(c =&gt; c.CustomerId == <span style="color:#a31515;">"dsantarelli"</span>);
        <span style="color:#2b91af;">Assert</span>.AreEqual(customer1.ContactName, <span style="color:#a31515;">"Dario Santarelli"</span>);

        <span style="color:green;">// Now someone changes the customer1 record in the DB </span>
        <span style="color:green;">// by setting ContactName = "Luigi Santarelli" !!! </span>
        ChangeDBRecord(<span style="color:#a31515;">"dsantarelli" , <span style="color:#a31515;">"Luigi Santarelli"</span></span>);

        customer2 = context.Customers.Single(c =&gt; c.CustomerId == <span style="color:#a31515;">"dsantarelli"</span>);

        <span style="color:#2b91af;">Assert</span>.IsTrue(customer1 == customer2); <span style="color:green;">// They are references to the same instance (Identity Map) </span>
        <span style="color:#2b91af;">Assert</span>.AreEqual(customer2.ContactName, <span style="color:#a31515;">"Luigi Santarelli"</span>); <span style="color:green;">// New values win </span>}
}</pre>
</div>
<p><strong>NoTracking</strong> : In this scenario, objects are not tracked in the <a href="http://msdn.microsoft.com/en-us/library/system.data.objects.objectstatemanager.aspx">ObjectStateManager</a>. Each time we hit the DB for getting a customer, the EF provides a new instance of the Customer class. So, in this case, the Identity Map principle is broken (we can find some analogies with the non-Identity Map solution presented at the beginning of this post).</p>
<div style="font-size:1.2em;">
<pre class="code">[<span style="color:#2b91af;">TestMethod</span>]
<span style="color:blue;">public void </span>EF_NoTracking_MergeOption_Applies_NewData_And_Provides_Different_Copies_Of_The_Same_Customer()
{
    <span style="color:blue;">using </span>(<span style="color:#2b91af;">EFContext </span>context = <span style="color:blue;">new </span><span style="color:#2b91af;">EFContext</span>())
    {
        <strong>context.Customers.MergeOption = <span style="color:#2b91af;">MergeOption</span>.NoTracking;</strong>

        <span style="color:#2b91af;">Customer </span>customer1 = context.Customers.Single(c =&gt; c.CustomerId == <span style="color:#a31515;">"dsantarelli"</span>);
        <span style="color:#2b91af;">Assert</span>.AreEqual(customer1.ContactName, <span style="color:#a31515;">"Dario Santarelli"</span>);

        <span style="color:green;">// Now someone changes the customer1 record in the DB</span>
        <span style="color:green;">// by setting ContactName = "Luigi Santarelli" !!!</span>
        ChangeDBRecord(<span style="color:#a31515;">"dsantarelli"</span>, <span style="color:#a31515;">"Luigi Santarelli"</span>);

        <span style="color:#2b91af;">Customer </span>customer2 = context.Customers.Single(c =&gt; c.CustomerId == <span style="color:#a31515;">"dsantarelli"</span>);

        <span style="color:#2b91af;">Assert</span>.<strong>IsFalse</strong>(customer1 == customer2); <span style="color:green;">// They are NOT references to the same instance (NO Identity Map) </span>
        <span style="color:#2b91af;">Assert</span>.AreEqual(customer1.ContactName, <span style="color:#a31515;">"Dario Santarelli"</span>); <span style="color:green;">// customer1 has original values </span>
        <span style="color:#2b91af;">Assert</span>.AreEqual(customer2.ContactName, <span style="color:#a31515;">"Luigi Santarelli"</span>); <span style="color:green;">// customer2 has new values </span>}
}</pre>
</div>
<p><strong>PreserveChanges</strong> : this option is quite a compromise between the <em>AppendOnly</em> and the <em>OverwriteChanges</em> options.</p>
<ul>
<li>If we don’t change any property of our entity (i.e. the state of the entity is <a href="http://msdn.microsoft.com/en-us/library/system.data.entitystate.aspx">Unchanged</a>), the current and original values in the entry are overwritten with data source values. The state of the entity remains <a href="http://msdn.microsoft.com/en-us/library/system.data.entitystate.aspx">Unchanged</a> and no properties are marked as modified.</li>
<li>If we change a property of our entity (i.e. the state of the entity is <a href="http://msdn.microsoft.com/en-us/library/system.data.entitystate.aspx">Modified</a>), the current values of modified properties are not overwritten with data source values. The original values of unmodified properties are overwritten with the values from the data source.</li>
<li>Entity Framework v4 compares the current values of unmodified properties with the values that were returned from the data source. If the values are not the same, the property is marked as modified.</li>
</ul>
<p>So, let’s see this behavior in a test&#8230;</p>
<div style="font-size:1.2em;">
<pre class="code">[<span style="color:#2b91af;">TestMethod</span>]
<span style="color:blue;">public void </span>EF_PreserveChanges_MergeOption_Preserves_Client_Changes()
{
    <span style="color:blue;">using </span>(<span style="color:#2b91af;">EFContext </span>context = <span style="color:blue;">new </span><span style="color:#2b91af;">EFContext</span>())
    {
        <strong>context.Customers.MergeOption = <span style="color:#2b91af;">MergeOption</span>.PreserveChanges;</strong>

        <span style="color:#2b91af;">Customer </span>customer1 = context.Customers.Single(c =&gt; c.CustomerId == <span style="color:#a31515;">"dsantarelli"</span>);
        <span style="color:#2b91af;">Assert</span>.AreEqual(customer1.ContactName, <span style="color:#a31515;">"Dario Santarelli"</span>);

        customer1.ContactName = <span style="color:#a31515;">"Carlo Santarelli"</span>; <span style="color:green;">// We change the ContactName in memory</span>

        <span style="color:green;">// Now someone changes the customer1 record in the DB</span>
        <span style="color:green;">// by setting ContactName = "Luigi Santarelli" !!!</span>
        ChangeDBRecord(<span style="color:#a31515;">"dsantarelli"</span>, <span style="color:#a31515;">"Luigi Santarelli"</span>);

        <span style="color:#2b91af;">Customer </span>customer2 = context.Customers.Single(c =&gt; c.CustomerId == <span style="color:#a31515;">"dsantarelli"</span>);

        <span style="color:#2b91af;">Assert</span>.IsTrue(customer1 == customer2); <span style="color:green;">// They are references to the same instance (Identity Map) </span>
        <span style="color:#2b91af;">Assert</span>.AreEqual(customer2.ContactName, <span style="color:#a31515;">"Carlo Santarelli"</span>); <span style="color:green;">// Our changes are preserved! </span>}
}</pre>
</div>
<p>HTH</p>
<br />Filed under: <a href='http://dariosantarelli.wordpress.com/category/entity-framework/'>Entity Framework</a> Tagged: <a href='http://dariosantarelli.wordpress.com/tag/entityframework/'>EntityFramework</a>, <a href='http://dariosantarelli.wordpress.com/tag/identitymap/'>IdentityMap</a>, <a href='http://dariosantarelli.wordpress.com/tag/pattern/'>Pattern</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/dariosantarelli.wordpress.com/271/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/dariosantarelli.wordpress.com/271/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/dariosantarelli.wordpress.com/271/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/dariosantarelli.wordpress.com/271/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/dariosantarelli.wordpress.com/271/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/dariosantarelli.wordpress.com/271/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/dariosantarelli.wordpress.com/271/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/dariosantarelli.wordpress.com/271/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/dariosantarelli.wordpress.com/271/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/dariosantarelli.wordpress.com/271/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/dariosantarelli.wordpress.com/271/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/dariosantarelli.wordpress.com/271/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/dariosantarelli.wordpress.com/271/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/dariosantarelli.wordpress.com/271/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dariosantarelli.wordpress.com&amp;blog=502627&amp;post=271&amp;subd=dariosantarelli&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://dariosantarelli.wordpress.com/2011/11/26/entity-framework-v4-identity-map-pattern/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/3805217b57ce1cc8bacbb870a2e3479f?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">dariosantarelli</media:title>
		</media:content>
	</item>
		<item>
		<title>[WPF] Registering the “pack://” scheme in unit tests</title>
		<link>http://dariosantarelli.wordpress.com/2011/08/26/wpf-registering-the-%e2%80%9cpack%e2%80%9d-scheme-in-unit-tests/</link>
		<comments>http://dariosantarelli.wordpress.com/2011/08/26/wpf-registering-the-%e2%80%9cpack%e2%80%9d-scheme-in-unit-tests/#comments</comments>
		<pubDate>Fri, 26 Aug 2011 17:26:43 +0000</pubDate>
		<dc:creator>dariosantarelli</dc:creator>
				<category><![CDATA[.NET Framework]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://dariosantarelli.wordpress.com/?p=250</guid>
		<description><![CDATA[A while ago I tried to test some WPF resources stored in an assembly (BAML). When I tried to execute the following code in a unit test… [TestMethod] public void MyStyle_Should_Be_Loaded() {              ResourceDictionary dictionary = new ResourceDictionary();   dictionary.Source = new Uri(&#8220;pack://application:,,,/TestClassLibrary;component/ResourceDictionary.xaml&#8221;,                               [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dariosantarelli.wordpress.com&amp;blog=502627&amp;post=250&amp;subd=dariosantarelli&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>A while ago I tried to test some WPF resources stored in an assembly (BAML). When I tried to execute the following code in a unit test…</p>
<p style="line-height:normal;background:white;margin:0;"><span style="font-family:'Courier New';"><span style="color:#000000;"><span style="font-size:8.5pt;">[</span></span><span style="font-size:8.5pt;"><span style="color:#2b91af;">TestMethod</span></span><span style="font-size:8.5pt;color:#000000;">]</span></span></p>
<p style="line-height:normal;background:white;margin:0;"><span style="font-family:'Courier New';"><span style="font-size:8.5pt;"><span style="color:#0000ff;">public</span><span style="color:#000000;"> </span><span style="color:#0000ff;">void</span></span><span style="font-size:8.5pt;color:#000000;"> MyStyle_Should_Be_Loaded()</span></span></p>
<p style="line-height:normal;background:white;margin:0;"><span style="font-family:'Courier New';"><span style="font-size:8.5pt;color:#000000;">{            </span></span></p>
<p style="line-height:normal;background:white;margin:0;"><span style="font-family:'Courier New';"><span style="color:#000000;"><span style="font-size:8.5pt;">  </span></span><span style="font-size:8.5pt;"><span style="color:#2b91af;">ResourceDictionary</span><span style="color:#000000;"> dictionary = </span><span style="color:#0000ff;">new</span><span style="color:#000000;"> </span><span style="color:#2b91af;">ResourceDictionary</span></span><span style="font-size:8.5pt;color:#000000;">();</span></span></p>
<p style="line-height:normal;background:white;margin:0;"><span style="font-family:'Courier New';"><span style="color:#000000;"><span style="font-size:8.5pt;">  dictionary.Source = </span></span><span style="font-size:8.5pt;"><span style="color:#0000ff;">new</span><span style="color:#000000;"> </span><span style="color:#2b91af;">Uri</span><span style="color:#000000;">(</span><span style="color:#a31515;">&#8220;pack://application:,,,/TestClassLibrary;component/ResourceDictionary.xaml&#8221;</span><span style="color:#000000;">,</span></span></span></p>
<p style="line-height:normal;background:white;margin:0;"><span style="font-family:'Courier New';"><span style="font-size:8.5pt;"><span style="color:#2b91af;">                              UriKind</span></span><span style="font-size:8.5pt;color:#000000;">.RelativeOrAbsolute);</span></span></p>
<p style="line-height:normal;background:white;margin:0;"><span style="font-family:'Courier New';"><span style="color:#000000;"><span style="font-size:8.5pt;">  </span></span><span style="font-size:8.5pt;"><span style="color:#0000ff;">object</span><span style="color:#000000;"> style = dictionary[</span><span style="color:#a31515;">"myStyle"</span></span><span style="font-size:8.5pt;color:#000000;">];</span></span></p>
<p style="line-height:normal;background:white;margin:0;"><span style="font-family:'Courier New';"><span style="font-size:8.5pt;color:#000000;"> </span></span></p>
<p style="line-height:normal;background:white;margin:0;"><span style="font-family:'Courier New';"><span style="color:#000000;"><span style="font-size:8.5pt;">  </span></span><span style="font-size:8.5pt;"><span style="color:#2b91af;">Assert</span></span><span style="font-size:8.5pt;color:#000000;">.IsNotNull(style);</span></span></p>
<p style="line-height:normal;background:white;margin:0;"><span style="font-family:'Courier New';"><span style="color:#000000;"><span style="font-size:8.5pt;">  </span></span><span style="font-size:8.5pt;"><span style="color:#2b91af;">Assert</span><span style="color:#000000;">.IsTrue(style </span><span style="color:#0000ff;">is</span><span style="color:#000000;"> </span><span style="color:#2b91af;">Style</span></span><span style="font-size:8.5pt;color:#000000;">);<br />
&#8230;</span></span></p>
<p style="line-height:normal;background:white;margin:0;"><span style="font-family:'Courier New';"><span style="color:#000000;"><span style="font-size:8.5pt;">}<br />
</span></span></span></p>
<p>… I received the following strange error while trying to instantiate the <em>Uri</em> class…</p>
<p><em>System.UriFormatException: Invalid URI: Invalid port specified.</em></p>
<p>But why?<br />
The answer is not so obviuos. That&#8217;s because I was executing that code while the <em>pack://</em> scheme wasn’t yet registered. In fact, this scheme is registered when the <a href="http://msdn.microsoft.com/en-us/library/system.windows.application.aspx" target="_blank">Application</a> object is created. The very simple solution is to execute the following code just before executing the test…</p>
<pre style="background:white;margin:0;"><span style="color:#000000;"><span style="font-size:8.5pt;">[</span></span><span style="color:#2b91af;font-family:Courier New;font-size:11px;line-height:19px;white-space:normal;">TestInitialize</span><span style="font-size:11px;">] </span></pre>
<pre style="background:white;margin:0;"><span style="font-family:Courier New;font-size:13px;line-height:19px;white-space:normal;"><span style="font-size:8.5pt;"><span style="color:#0000ff;">public</span><span style="color:#000000;"> </span><span style="color:#0000ff;">void</span></span></span><span style="font-family:Courier New;font-size:13px;line-height:19px;white-space:normal;"><span style="font-family:Consolas, Monaco, monospace;font-size:11px;line-height:18px;white-space:pre;background-color:#ffffff;"> OnTestInitialize() </span></span></pre>
<pre style="background:white;margin:0;"><span style="font-size:11px;">{ </span><span style="font-family:Courier New;font-size:11px;line-height:19px;white-space:normal;"><span style="color:#0000ff;">  </span></span></pre>
<pre style="background:white;margin:0;"><span style="font-family:Courier New;font-size:11px;line-height:19px;white-space:normal;"><span style="color:#0000ff;">   if</span><span style="color:#000000;"> (!</span><span style="color:#2b91af;">UriParser</span><span style="color:#000000;">.IsKnownScheme(</span><span style="color:#a31515;">"pack"</span><span style="color:#000000;">)) </span><span style="color:#0000ff;">new</span><span style="color:#000000;"> System.Windows.</span><span style="color:#2b91af;">Application</span></span><span style="font-size:11px;">();</span></pre>
<pre style="background:white;margin:0;"><span style="font-family:Courier New;font-size:11px;line-height:19px;white-space:normal;">} </span></pre>
<pre style="background:white;margin:0;"><span style="font-family:Courier New;font-size:11px;line-height:19px;white-space:normal;"> </span></pre>
<p>HTH</p>
<br />Filed under: <a href='http://dariosantarelli.wordpress.com/category/net-framework/'>.NET Framework</a>, <a href='http://dariosantarelli.wordpress.com/category/wpf/'>WPF</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/dariosantarelli.wordpress.com/250/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/dariosantarelli.wordpress.com/250/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/dariosantarelli.wordpress.com/250/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/dariosantarelli.wordpress.com/250/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/dariosantarelli.wordpress.com/250/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/dariosantarelli.wordpress.com/250/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/dariosantarelli.wordpress.com/250/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/dariosantarelli.wordpress.com/250/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/dariosantarelli.wordpress.com/250/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/dariosantarelli.wordpress.com/250/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/dariosantarelli.wordpress.com/250/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/dariosantarelli.wordpress.com/250/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/dariosantarelli.wordpress.com/250/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/dariosantarelli.wordpress.com/250/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dariosantarelli.wordpress.com&amp;blog=502627&amp;post=250&amp;subd=dariosantarelli&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://dariosantarelli.wordpress.com/2011/08/26/wpf-registering-the-%e2%80%9cpack%e2%80%9d-scheme-in-unit-tests/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/3805217b57ce1cc8bacbb870a2e3479f?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">dariosantarelli</media:title>
		</media:content>
	</item>
		<item>
		<title>[WPF] Inheritance and DataTemplates</title>
		<link>http://dariosantarelli.wordpress.com/2011/07/28/wpf-inheritance-and-datatemplates/</link>
		<comments>http://dariosantarelli.wordpress.com/2011/07/28/wpf-inheritance-and-datatemplates/#comments</comments>
		<pubDate>Thu, 28 Jul 2011 20:15:37 +0000</pubDate>
		<dc:creator>dariosantarelli</dc:creator>
				<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://dariosantarelli.wordpress.com/?p=168</guid>
		<description><![CDATA[In this post I will show how different DataTemplates related to a hierarchy of classes can be nested and, therefore, reused. The concept is very simple, but its applications in a real scenario could be not so trivial! Let’s assume to have a base ViewModel useful for editing and saving an object of your model. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dariosantarelli.wordpress.com&amp;blog=502627&amp;post=168&amp;subd=dariosantarelli&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>In this post I will show how different DataTemplates related to a hierarchy of classes can be nested and, therefore, reused. The concept is very simple, but its applications in a real scenario could be not so trivial!</p>
<p>Let’s assume to have a base ViewModel useful for editing and saving an object of your model. If the object class is subject to some derivations, maybe you’d like to derive your base ViewModel too in order to fulfill the model inheritance hierarchy. Moreover, most probably you have to define different editing views taking into account the whole inheritance hierarchy. In that case, maybe you&#8217;d like to reuse more XAML as possible.</p>
<p>So, let&#8217;s assume to have a base abstract<em> Customer</em> class and some concrete specializations, like <em>EducationCustomer</em> and <em>GovernmentCustomer</em> (see image below).   <a href="http://dariosantarelli.files.wordpress.com/2011/07/customers.jpg"><img class="size-medium wp-image-169 aligncenter" style="display:block;float:none;margin:5px auto;" title="Customer class hierarchy" src="http://dariosantarelli.files.wordpress.com/2011/07/immagine.jpg?w=400" alt="Customer class hierarchy" /></a>Then, we design ViewModels in order to edit concrete instances of Customer class. In the class diagram below you can see a base <em>ItemEditViewModel&lt;T&gt;</em> which consists in a simple generic ViewModel which exposes a generic <em>Item</em> to be modified and a <em>SaveCommand</em> to persist it somewhere. The class also defines an abstract method <em>OnCanSaveItem()</em> which a concrete implementation must override in order to specify its own validation rules.   <img class="size-medium wp-image-170 aligncenter" style="display:block;float:none;margin:5px auto;" title="ViewModel hierarchy" src="http://dariosantarelli.files.wordpress.com/2011/07/immagine1.jpg?w=700" alt="ViewModel hierarchy" /></p>
<pre style="font-size:8.5pt;"><span style="color:#0000ff;">public</span><span style="color:#0000ff;"> abstract </span><span style="color:#0000ff;">class</span><span style="color:#2b91af;"> CustomerEditViewModel</span><span style="color:#000000;">&lt;T&gt; : </span><span style="color:#2b91af;">ItemEditViewModel</span><span style="color:#000000;">&lt;T&gt; </span><span style="color:#0000ff;">where</span><span style="color:#000000;"> T : </span><span style="color:#2b91af;">Customer</span>
<span style="color:#000000;">{</span>
<span style="color:#0000ff;"> public</span><span style="color:#000000;"> CustomerEditViewModel(T customer) : </span><span style="color:#0000ff;">base</span><span style="color:#000000;">(customer) { }</span>
<span style="color:#0000ff;"> public</span><span style="color:#000000;"> CustomerEditViewModel() { } </span>

<span style="color:#0000ff;"> protected</span><span style="color:#0000ff;"> override </span><span style="color:#0000ff;">bool</span><span style="color:#000000;"> OnCanSaveItem() </span>
<span style="color:#000000;"> {</span>
<span style="color:#0000ff;"> if</span><span style="color:#000000;"> (Item == </span><span style="color:#0000ff;">null</span><span style="color:#000000;">) </span><span style="color:#0000ff;"> return</span><span style="color:#0000ff;"> false</span><span style="color:#000000;">; </span>
<span style="color:#0000ff;"> return</span><span style="color:#000000;"> (!</span><span style="color:#0000ff;">string</span><span style="color:#000000;">.IsNullOrWhiteSpace(Item.Name) &amp;&amp; !</span><span style="color:#0000ff;">string</span><span style="color:#000000;">.IsNullOrWhiteSpace(Item.Email)); </span>
<span style="color:#000000;"> } </span>
<span style="color:#000000;">}</span>

<span style="color:#0000ff;">public </span><span style="color:#0000ff;">class </span><span style="color:#2b91af;">EducationCustomerEditViewModel</span><span style="color:#000000;"> : </span><span style="color:#2b91af;">CustomerEditViewModel</span><span style="color:#000000;">&lt;</span><span style="color:#2b91af;">EducationCustomer</span><span style="color:#000000;">&gt; </span>
<span style="color:#000000;">{</span>
<span style="color:#0000ff;"> public</span><span style="color:#000000;"> EducationCustomerEditViewModel() : </span><span style="color:#0000ff;">base</span><span style="color:#000000;">() { }</span>
<span style="color:#0000ff;"> public</span><span style="color:#000000;"> EducationCustomerEditViewModel(</span><span style="color:#2b91af;">EducationCustomer</span><span style="color:#000000;"> customer) : </span><span style="color:#0000ff;">base</span><span style="color:#000000;">(customer) { }</span> 

 <span style="color:#0000ff;">protected </span><span style="color:#0000ff;">override </span><span style="color:#0000ff;">bool</span><span style="color:#000000;"> OnCanSaveItem() </span>
<span style="color:#000000;"> {</span>
<span style="color:#0000ff;"> if</span><span style="color:#000000;"> (!</span><span style="color:#0000ff;">base</span><span style="color:#000000;">.OnCanSaveItem()) </span><span style="color:#0000ff;">return</span><span style="color:#0000ff;"> false</span><span style="color:#000000;">;</span>
<span style="color:#0000ff;"> else </span><span style="color:#0000ff;">return</span><span style="color:#000000;"> (!</span><span style="color:#0000ff;">string</span><span style="color:#000000;">.IsNullOrWhiteSpace(Item.SchoolName)); </span>
<span style="color:#000000;"> } </span>
<span style="color:#000000;">} </span> 

<span style="color:#0000ff;">public </span><span style="color:#0000ff;">class </span><span style="color:#2b91af;">GovernmentCustomerEditViewModel</span><span style="color:#000000;"> : </span><span style="color:#2b91af;">CustomerEditViewModel</span><span style="color:#000000;">&lt;</span><span style="color:#2b91af;">GovernmentCustomer</span><span style="color:#000000;">&gt;</span>
<span style="color:#000000;">{</span>
  <span style="color:#0000ff;">public</span><span style="color:#000000;"> GovernmentCustomerEditViewModel() : </span><span style="color:#0000ff;">base</span><span style="color:#000000;">() { }</span>
  <span style="color:#0000ff;">public</span><span style="color:#000000;"> GovernmentCustomerEditViewModel(</span><span style="color:#2b91af;">GovernmentCustomer</span><span style="color:#000000;"> customer) : </span><span style="color:#0000ff;">base</span><span style="color:#000000;">(customer) { }</span>

  <span style="color:#0000ff;">protected</span><span style="color:#0000ff;"> override </span><span style="color:#0000ff;">bool</span><span style="color:#000000;"> OnCanSaveItem()</span>
  <span style="color:#000000;">{</span>
   <span style="color:#0000ff;">if</span><span style="color:#000000;"> (!</span><span style="color:#0000ff;">base</span><span style="color:#000000;">.OnCanSaveItem()) </span><span style="color:#0000ff;">return</span><span style="color:#0000ff;"> false</span><span style="color:#000000;">;</span>
   <span style="color:#0000ff;">else</span><span style="color:#0000ff;"> return</span><span style="color:#000000;"> (!</span><span style="color:#0000ff;">string</span><span style="color:#000000;">.IsNullOrWhiteSpace(Item.AgencyName));</span>
  <span style="color:#000000;">}</span>
<span style="color:#000000;">}</span></pre>
<p>Ok, we have just defined model and viewmodels. Now the interesting part! Our datatemplates could share some portions of XAML (e.g. the edit DataTemplate of the <em>GovernmentCustomer</em> is quite identical to the DataTemplate of the <em>EducationCustomer</em>, but it differs from the former just for a field). So, how can we reuse DataTemplates? First, we can define the edit DataTemplate for the base Customer class…</p>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;margin:0;"><span style="color:#0000ff;">&lt;</span><span style="color:#800000;">DataTemplate</span> <span style="color:#ff0000;">x</span>:<span style="color:#ff0000;">Key</span>=<span style="color:#0000ff;">"customerEditTemplate"</span> <span style="color:#ff0000;">DataType</span>=<span style="color:#0000ff;">"{x:Type m:Customer}"</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;margin:0;">  <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">Grid</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;margin:0;">    ...</pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;margin:0;">    <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">TextBlock</span> <span style="color:#ff0000;">Text</span>=<span style="color:#0000ff;">"Name"</span> ... <span style="color:#0000ff;">/&gt;</span></pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;margin:0;">    <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">TextBox</span> <span style="color:#ff0000;">Text</span>=<span style="color:#0000ff;">"{Binding Path=Name, Mode=TwoWay}"</span> ... <span style="color:#ff0000;">Background</span>=<span style="color:#0000ff;">"AliceBlue"</span> <span style="color:#0000ff;">/&gt;</span></pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;margin:0;">    <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">TextBlock</span> <span style="color:#ff0000;">Text</span>=<span style="color:#0000ff;">"Email"</span> ... <span style="color:#0000ff;">/&gt;</span></pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;margin:0;">    <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">TextBox</span> <span style="color:#ff0000;">Text</span>=<span style="color:#0000ff;">"{Binding Path=Email, Mode=TwoWay}"</span> ... <span style="color:#ff0000;">Background</span>=<span style="color:#0000ff;">"AliceBlue"</span> <span style="color:#0000ff;">/&gt;</span></pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;margin:0;">  <span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">Grid</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;margin:0;"><span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">DataTemplate</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;margin:0;"></pre>
<p>&nbsp;<br />
and then, we can reuse the XAML above in the edit DataTemplate for the <em>GovernmentCustomer</em> and the <em>EducationCustomer.</em></p>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;margin:0;"><span style="color:#0000ff;">&lt;</span><span style="color:#800000;">DataTemplate</span> <span style="color:#ff0000;">x</span>:<span style="color:#ff0000;">Key</span>=<span style="color:#0000ff;">"governmentCustomerEditTemplate"</span> <span style="color:#ff0000;">DataType</span>=<span style="color:#0000ff;">"{x:Type m:GovernmentCustomer}"</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;margin:0;">  <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">StackPanel</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;margin:0;">    <strong><span style="color:#0000ff;">&lt;</span><span style="color:#800000;">ContentPresenter</span> <span style="color:#ff0000;">ContentTemplate</span>=<span style="color:#0000ff;">"{StaticResource customerEditTemplate}"</span> <span style="color:#0000ff;">/&gt;</span></strong></pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;margin:0;">    <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">Grid</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;margin:0;">    ...</pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;margin:0;">     <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">TextBlock</span> <span style="color:#ff0000;">Text</span>=<span style="color:#0000ff;">"Agency"</span> ... <span style="color:#0000ff;">/&gt;</span></pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;margin:0;">     <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">TextBox</span> <span style="color:#ff0000;">Text</span>=<span style="color:#0000ff;">"{Binding Path=AgencyName, Mode=TwoWay}"</span> ... <span style="color:#ff0000;">Background</span>=<span style="color:#0000ff;">"LightPink"</span> <span style="color:#0000ff;">/&gt;</span></pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;margin:0;">   <span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">Grid</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;margin:0;">  <span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">StackPanel</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;margin:0;"><span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">DataTemplate</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;margin:0;"></pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;margin:0;"><span style="color:#0000ff;">&lt;</span><span style="color:#800000;">DataTemplate</span> <span style="color:#ff0000;">x</span>:<span style="color:#ff0000;">Key</span>=<span style="color:#0000ff;">"educationCustomerEditTemplate"</span> <span style="color:#ff0000;">DataType</span>=<span style="color:#0000ff;">"{x:Type m:EducationCustomer}"</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;margin:0;">  <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">StackPanel</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;margin:0;">    <strong><span style="color:#0000ff;">&lt;</span><span style="color:#800000;">ContentPresenter</span> <span style="color:#ff0000;">ContentTemplate</span>=<span style="color:#0000ff;">"{StaticResource customerEditTemplate}"</span> <span style="color:#0000ff;">/&gt;</span></strong></pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;margin:0;">    <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">Grid</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;margin:0;">      ...</pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;margin:0;">      <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">TextBlock</span> <span style="color:#ff0000;">Text</span>=<span style="color:#0000ff;">"School"</span> ... <span style="color:#0000ff;">/&gt;</span></pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;margin:0;">      <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">TextBox</span> <span style="color:#ff0000;">Text</span>=<span style="color:#0000ff;">"{Binding Path=SchoolName, Mode=TwoWay}"</span> ... <span style="color:#ff0000;">Background</span>=<span style="color:#0000ff;">"Yellow"</span> <span style="color:#0000ff;">/&gt;</span></pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;margin:0;">    <span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">Grid</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;margin:0;">  <span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">StackPanel</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;margin:0;"><span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">DataTemplate</span><span style="color:#0000ff;">&gt;</span></pre>
<p>&nbsp;<br />
OK, that&#8217;s all. Finally, a simple view can be implemented as below&#8230;</p>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;margin:0;"><span style="color:#0000ff;">&lt;</span><span style="color:#800000;">Window</span> ...<span style="color:#0000ff;">&gt;</span></pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;margin:0;">  <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">Window.Resources</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;margin:0;">    <span style="color:#0000ff;">&lt;</span><span style="color:#c71585;">m</span>:<span style="color:#800000;">GovernmentCustomer</span> <span style="color:#ff0000;">x</span>:<span style="color:#ff0000;">Key</span>=<span style="color:#0000ff;">"governmentCustomer"</span> <span style="color:#0000ff;">/&gt;</span></pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;margin:0;">    <span style="color:#0000ff;">&lt;</span><span style="color:#c71585;">m</span>:<span style="color:#800000;">EducationCustomer</span> <span style="color:#ff0000;">x</span>:<span style="color:#ff0000;">Key</span>=<span style="color:#0000ff;">"educationCustomer"</span> <span style="color:#0000ff;">/&gt;</span></pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;margin:0;">    <span style="color:#0000ff;">&lt;</span><span style="color:#c71585;">vm</span>:<span style="color:#800000;">GovernmentCustomerEditViewModel</span> <span style="color:#ff0000;">x</span>:<span style="color:#ff0000;">Key</span>=<span style="color:#0000ff;">"governmentCustomerEditVM"</span> <span style="color:#ff0000;">Item</span>=<span style="color:#0000ff;">"{StaticResource governmentCustomer}"</span> <span style="color:#0000ff;">/&gt;</span></pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;margin:0;">    <span style="color:#0000ff;">&lt;</span><span style="color:#c71585;">vm</span>:<span style="color:#800000;">EducationCustomerEditViewModel</span> <span style="color:#ff0000;">x</span>:<span style="color:#ff0000;">Key</span>=<span style="color:#0000ff;">"educationCustomerEditVM"</span> <span style="color:#ff0000;">Item</span>=<span style="color:#0000ff;">"{StaticResource educationCustomer}"</span> <span style="color:#0000ff;">/&gt;</span></pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;margin:0;">  <span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">Window.Resources</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;margin:0;"></pre>
<p>&nbsp;</p>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;margin:0;">  <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">StackPanel</span> <span style="color:#ff0000;">DataContext</span>=<span style="color:#0000ff;">"{StaticResource governmentCustomerEditVM}"</span> <span style="color:#ff0000;">Margin</span>=<span style="color:#0000ff;">"10"</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;margin:0;">    <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">ContentPresenter</span> <span style="color:#ff0000;">Content</span>=<span style="color:#0000ff;">"{Binding Path=Item}"</span> <span style="color:#ff0000;">ContentTemplate</span>=<span style="color:#0000ff;">"{StaticResource governmentCustomerEditTemplate}"</span> <span style="color:#0000ff;">/&gt;</span></pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;margin:0;">    <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">Button</span> <span style="color:#ff0000;">Content</span>=<span style="color:#0000ff;">"Save"</span><span style="color:#ff0000;">Command</span>=<span style="color:#0000ff;">"{Binding Path=SaveItemCommand}"</span>... <span style="color:#0000ff;">/&gt;</span></pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;margin:0;">  <span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">StackPanel</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;margin:0;"><span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">Window</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="background-color:#ffffff;width:100%;font-family:consolas,'Courier New',courier,monospace;font-size:12px;margin:0;"></pre>
<p>&nbsp;<br />
As you can see, in this example the edit DataTemplate is referenced by key, but in a real scenario you can define your own mechanism to bind the right ViewModel to a DataTemplate for the Item to be edited and saved. In this example, the output result is the following</p>
<p><a href="http://dariosantarelli.files.wordpress.com/2011/07/immagine11.jpg"><img class="alignnone size-medium wp-image-177" title="EducationCustomer edit Window" src="http://dariosantarelli.files.wordpress.com/2011/07/immagine11.jpg?w=300" alt="EducationCustomer edit Window" /></a><a href="http://dariosantarelli.files.wordpress.com/2011/07/immagine21.jpg"><img class="alignnone size-medium wp-image-180" title="GovernmentCustomer edit Window" src="http://dariosantarelli.files.wordpress.com/2011/07/immagine21.jpg?w=300" alt="GovernmentCustomer edit Window" /></a></p>
<p>HTH</p>
<br />Filed under: <a href='http://dariosantarelli.wordpress.com/category/wpf/'>WPF</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/dariosantarelli.wordpress.com/168/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/dariosantarelli.wordpress.com/168/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/dariosantarelli.wordpress.com/168/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/dariosantarelli.wordpress.com/168/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/dariosantarelli.wordpress.com/168/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/dariosantarelli.wordpress.com/168/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/dariosantarelli.wordpress.com/168/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/dariosantarelli.wordpress.com/168/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/dariosantarelli.wordpress.com/168/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/dariosantarelli.wordpress.com/168/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/dariosantarelli.wordpress.com/168/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/dariosantarelli.wordpress.com/168/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/dariosantarelli.wordpress.com/168/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/dariosantarelli.wordpress.com/168/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dariosantarelli.wordpress.com&amp;blog=502627&amp;post=168&amp;subd=dariosantarelli&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://dariosantarelli.wordpress.com/2011/07/28/wpf-inheritance-and-datatemplates/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/3805217b57ce1cc8bacbb870a2e3479f?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">dariosantarelli</media:title>
		</media:content>

		<media:content url="http://dariosantarelli.files.wordpress.com/2011/07/immagine.jpg?w=400" medium="image">
			<media:title type="html">Customer class hierarchy</media:title>
		</media:content>

		<media:content url="http://dariosantarelli.files.wordpress.com/2011/07/immagine1.jpg?w=700" medium="image">
			<media:title type="html">ViewModel hierarchy</media:title>
		</media:content>

		<media:content url="http://dariosantarelli.files.wordpress.com/2011/07/immagine11.jpg?w=300" medium="image">
			<media:title type="html">EducationCustomer edit Window</media:title>
		</media:content>

		<media:content url="http://dariosantarelli.files.wordpress.com/2011/07/immagine21.jpg?w=300" medium="image">
			<media:title type="html">GovernmentCustomer edit Window</media:title>
		</media:content>
	</item>
		<item>
		<title>[.NET Compact Framework] Working with Point-to-Point Message Queues</title>
		<link>http://dariosantarelli.wordpress.com/2011/06/11/net-compact-framework-working-with-point-to-point-message-queues/</link>
		<comments>http://dariosantarelli.wordpress.com/2011/06/11/net-compact-framework-working-with-point-to-point-message-queues/#comments</comments>
		<pubDate>Sat, 11 Jun 2011 14:45:56 +0000</pubDate>
		<dc:creator>dariosantarelli</dc:creator>
				<category><![CDATA[.NET Framework]]></category>
		<category><![CDATA[Compact Framework]]></category>

		<guid isPermaLink="false">http://dariosantarelli.wordpress.com/?p=132</guid>
		<description><![CDATA[While working on a project of mine, I’ve got to face an interprocess communication (IPC) on a Windows CE device. In my scenario, the device vendor uses the Message Queue Point-To-Point infrastructure so that native processes can communicate with managed processes through IPC. On other Windows platforms, IPC can be achieved through named pipes (native) or remoting [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dariosantarelli.wordpress.com&amp;blog=502627&amp;post=132&amp;subd=dariosantarelli&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>While working on a project of mine, I’ve got to face an interprocess communication (IPC) on a Windows CE device. In my scenario, the device vendor uses the Message Queue Point-To-Point infrastructure so that native processes can communicate with managed processes through IPC. On other Windows platforms, IPC can be achieved through named pipes (native) or remoting (managed), but none of these options are available to Windows CE. Point-to-point represents a little-known IPC mechanism that is efficient, flexible, and unique to Windows CE version 4.0 and later. Moreover it can interact with the operating system, for example, for getting power information.</p>
<p>If you don’t know this feature of Windows CE, first of all you should read this MSDN article:</p>
<p><a href="http://msdn.microsoft.com/en-us/library/aa446556.aspx">Point-to-Point Message Queues with the .NET Compact Framework</a>.</p>
<p>After analyzing the managed wrapper proposed by the article, I’ve started to refactor the source code in order to make it more suitable for my needs. So, I’d like to share my design and implementation <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
<p><a href="http://dariosantarelli.files.wordpress.com/2011/06/cd.jpg"><img class="alignnone size-medium wp-image-135" title="Class Diagram" src="http://dariosantarelli.files.wordpress.com/2011/06/cd.jpg?w=300&#038;h=136" alt="" width="300" height="136" /></a></p>
<p>Let’s explain some key concepts:</p>
<ul>
<li>A message queue can be addressed by a name or, generally, by an handle. The handle is the only data you can refer to if the queue has no name (NULL). Note that the empty string is considered a valid, non-null name.</li>
<li>A message queue can be <em>read-only</em> or <em>write-only</em>: it means that a process can get an handle to a message queue just for reading or writing messages. If you want to read from and write to the same queue, you should create two handles pointing to the same queue.</li>
<li>Message queues are FIFO. Writers can write messages in a queue until it’s full and readers can read messages from a queue until it’s empty. When a reader process invokes a read operation on a message queue, the first unread message is removed from the queue and delivered to the reading process.</li>
</ul>
<p>As you can see in the class diagram above, I’ve defined an abstract <em>MessageQueue</em> class which holds the queue info ( e.g. the max length, the max message length, the current readers/writers count and so on&#8230; ) and exposes a generic factory method for creating concrete implementations.</p>
<p><span style="color:blue;line-height:115%;font-family:'Courier New';font-size:8pt;" lang="EN-US">public</span><span style="line-height:115%;font-family:'Courier New';font-size:8pt;" lang="EN-US"> <span style="color:blue;">static </span>T Create&lt;T&gt;(<span style="color:blue;">string</span> name) <span style="color:blue;">where</span> T : <span style="color:#2b91af;">MessageQueue</span></span><span style="line-height:115%;font-family:'Courier New';font-size:8pt;" lang="EN-US"><span style="color:#2b91af;"><br />
</span></span><span style="color:blue;line-height:115%;font-family:'Courier New';font-size:8pt;" lang="EN-US">public</span><span style="line-height:115%;font-family:'Courier New';font-size:8pt;" lang="EN-US"> <span style="color:blue;">static </span>T Create&lt;T&gt;(<span style="color:blue;">string</span> name, <span style="color:blue;">int</span> length) <span style="color:blue;">where</span> T : <span style="color:#2b91af;">MessageQueue</span></span><span style="line-height:115%;font-family:'Courier New';font-size:8pt;" lang="EN-US"><span style="color:#2b91af;"><br />
</span></span><span style="color:blue;line-height:115%;font-family:'Courier New';font-size:8pt;" lang="EN-US">public</span><span style="line-height:115%;font-family:'Courier New';font-size:8pt;" lang="EN-US"> <span style="color:blue;">static </span>T Create&lt;T&gt;(<span style="color:blue;">string</span> name, <span style="color:blue;">int</span> length, <span style="color:blue;">int</span> maxMessageLength) <span style="color:blue;">where</span> T : <span style="color:#2b91af;">MessageQueue</span></span><span style="line-height:115%;font-family:'Courier New';font-size:8pt;" lang="EN-US"><span style="color:#2b91af;"><br />
</span></span></p>
<p>Let&#8217;s have a look to them:</p>
<h3><strong>WriteOnlyMessageQueue</strong></h3>
<p>It&#8217;s a concrete class for writing messages in a queue. The class exposes some overloads of the <em>Write()</em> method in order to write a message in a queue and choose whether to block the calling thread until the message is written in the queue (that is the queue is not full).</p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:8pt;font-family:'Courier New';color:green;" lang="EN-US">// Create or open an infinite user-defined write-only MessageQueue<br />
</span><span style="font-size:8pt;font-family:'Courier New';color:#2b91af;" lang="EN-US">WriteOnlyMessageQueue</span><span style="font-size:8pt;font-family:'Courier New';" lang="EN-US"> writeOnlyMessageQueue = <span style="color:#2b91af;">MessageQueue</span>.Create&lt;<span style="color:#2b91af;">WriteOnlyMessageQueue</span>&gt;(<span style="color:#a31515;">&#8220;MyQueueName&#8221;</span>);</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:8pt;font-family:'Courier New';color:#2b91af;" lang="EN-US">Message</span><span style="font-size:8pt;font-family:'Courier New';" lang="EN-US"> message = <span style="color:blue;">new</span> <span style="color:#2b91af;">Message</span>(<span style="color:#2b91af;">UTF8Encoding</span>.UTF8.GetBytes(<span style="color:#a31515;">&#8220;Hello world!&#8221;</span>));</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:8pt;font-family:'Courier New';" lang="EN-US"><br />
</span><span style="font-size:8pt;font-family:'Courier New';color:green;" lang="EN-US">// WRITE OPTIONS<br />
</span><span style="font-size:8pt;font-family:'Courier New';color:green;" lang="EN-US">// 1. block the current thread until a message can be written into the queue (that is the queue is not full).<br />
</span><span style="font-size:8pt;font-family:'Courier New';" lang="EN-US">writeOnlyMessageQueue.Write(message, <span style="color:blue;">true</span>);</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:8pt;font-family:'Courier New';color:green;" lang="EN-US"><br />
// 2. block the current thread for a max of 200 ms. If the message can&#8217;t be written during this interval, throw an exception.<br />
</span><span style="font-size:8pt;font-family:'Courier New';color:blue;" lang="EN-US">try</span><span style="font-size:8pt;font-family:'Courier New';" lang="EN-US"> { writeOnlyMessageQueue.Write(message, 200); } <span style="color:blue;">catch </span>(<span style="color:#2b91af;">Exception</span> ex) { }</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:8pt;font-family:'Courier New';color:green;" lang="EN-US"><br />
// 3. don&#8217;t block the current thread. If a message can&#8217;t be written immediately, throw an exception.<br />
</span><span style="font-size:8pt;font-family:'Courier New';color:blue;" lang="EN-US">try</span><span style="font-size:8pt;font-family:'Courier New';" lang="EN-US"> { writeOnlyMessageQueue.Write(message, <span style="color:blue;">false</span>); } <span style="color:blue;">catch</span> (<span style="color:#2b91af;">Exception</span> ex) { }</span></p>
<h3><strong><strong>ReadOnlyMessageQueue</strong></strong></h3>
<p>It&#8217;s a concrete class for reading messages from a queue. The class exposes some overloads of the <em>Read()</em> method in order read a message from a queue and choose whether to block the calling thread until a message can be read from the queue (that is the queue is not empty).</p>
<p><span style="font-size:8pt;font-family:'Courier New';color:green;" lang="EN-US">// Create or open an infinite user-defined read-only MessageQueue<br />
</span><span style="font-size:8pt;font-family:'Courier New';color:#2b91af;" lang="EN-US">ReadOnlyMessageQueue</span><span style="font-size:8pt;font-family:'Courier New';" lang="EN-US"> readOnlyMessageQueue = <span style="color:#2b91af;">MessageQueue</span>.Create&lt;<span style="color:#2b91af;">ReadOnlyMessageQueue</span>&gt;(<span style="color:#a31515;">&#8220;MyQueueName&#8221;</span>);<br />
</span><span style="font-size:8pt;font-family:'Courier New';color:#2b91af;" lang="EN-US">Message</span><span style="font-size:8pt;font-family:'Courier New';" lang="EN-US"> message = <span style="color:blue;">null</span>;</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:8pt;font-family:'Courier New';color:green;" lang="EN-US">// READ OPTIONS<br />
</span><span style="font-size:8pt;font-family:'Courier New';color:green;" lang="EN-US">// 1. block the current thread until a message can be read from the queue.<br />
</span><span style="font-size:8pt;font-family:'Courier New';" lang="EN-US">message = readOnlyMessageQueue.Read(<span style="color:blue;">true</span>);</span></p>
<p class="MsoNormal" style="margin-bottom:.0001pt;line-height:normal;"><span style="font-size:8pt;font-family:'Courier New';color:green;" lang="EN-US">// 2. block the current thread for a max of 200 ms. If no message has been read during this interval, throw an exception.<br />
</span><span style="font-size:8pt;font-family:'Courier New';color:blue;" lang="EN-US">try</span><span style="font-size:8pt;font-family:'Courier New';" lang="EN-US"> { message = readOnlyMessageQueue.Read(200); } <span style="color:blue;">catch</span> (<span style="color:#2b91af;">Exception</span> ex) { &#8230; }</span></p>
<p class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:green;font-family:'Courier New';font-size:8pt;" lang="EN-US">// 3. don&#8217;t block the current thread. If a message can&#8217;t be read immediately, throw an exception.<br />
</span><span style="color:blue;font-family:'Courier New';font-size:8pt;" lang="EN-US">try</span><span style="font-family:'Courier New';font-size:8pt;" lang="EN-US"> { message = readOnlyMessageQueue.Read(<span style="color:blue;">false</span>); } <span style="color:blue;">catch</span> (<span style="color:#2b91af;">Exception</span> ex) { &#8230; }</span><span style="color:black;font-family:'Segoe UI',sans-serif;font-size:9.5pt;" lang="EN-US"><br />
</span></p>
<h3><strong>AutoReadOnlyMessageQueue</strong></h3>
<p>It&#8217;s a concrete class derived from <em>ReadOnlyMessageQueue.</em> It uses a monitoring thread useful to automatically read messages after they are written in the queue. So, the class exposes a <em>MessageRead</em> event which is fired for each message read from the queue.</p>
<p class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-size:8pt;font-family:'Courier New';color:green;" lang="EN-US">// Create or open an infinite user-defined read-only MessageQueue<br />
</span><span style="font-size:8pt;font-family:'Courier New';color:#2b91af;" lang="EN-US">AutoReadOnlyMessageQueue</span><span style="font-size:8pt;font-family:'Courier New';" lang="EN-US"> autoReadOnlyMessageQueue = <span style="color:#2b91af;">MessageQueue</span>.Create&lt;<span style="color:#2b91af;">AutoReadOnlyMessageQueue</span>&gt;(<span style="color:#a31515;">&#8220;MyQueueName&#8221;</span>);</span></p>
<p class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-size:8pt;font-family:'Courier New';color:green;" lang="EN-US">// Starts monitoring the queue.<br />
</span><span style="font-size:8pt;font-family:'Courier New';color:green;" lang="EN-US">// New messages will be read automatically and notified through the MessageRead event.<br />
</span><span style="font-size:8pt;font-family:'Courier New';" lang="EN-US">autoReadOnlyMessageQueue.Start();<br />
</span><span style="font-size:8pt;font-family:'Courier New';" lang="EN-US">autoReadOnlyMessageQueue.MessageRead += (s, e) =&gt; </span><span style="font-size:8pt;font-family:'Courier New';" lang="EN-US">{</span><span style="font-size:8pt;font-family:'Courier New';" lang="EN-US"> <span style="color:blue;">byte</span>[] messageBytes = e.Message.Bytes; };  </span></p>
<p class="MsoNormal">
<p class="MsoNormal">Another static method exposed by the <em>MessageQueue</em> class is <em>OpenByHandle()</em>.  It allows you to open a  MessageQueue by using its handle. This method is suitable to open unnamed queues.</p>
<p><span style="font-size:8pt;line-height:115%;font-family:'Courier New';color:blue;" lang="EN-US">public</span><span style="font-size:8pt;line-height:115%;font-family:'Courier New';" lang="EN-US"> <span style="color:blue;">static </span>T OpenByHandle&lt;T&gt;(<span style="color:#2b91af;">IntPtr</span> queueHandle) <span style="color:blue;">where</span> T : <span style="color:#2b91af;">MessageQueue<br />
</span><span style="color:blue;">public</span> <span style="color:blue;">static </span>T OpenByHandle&lt;T&gt;(<span style="color:#2b91af;">IntPtr</span> queueHandle, <span style="color:#2b91af;">IntPtr</span> processHandle) <span style="color:blue;">where </span>T : <span style="color:#2b91af;">MessageQueue</span></span></p>
<p>As you can see, in this case you need an handle to a source process that owns the message queue, while the queue handle is the same returned by the <em>Create()</em> method.<br />
Finally I&#8217;ve created a simple smart device project for testing this class library (Visual Studio 2008 / Framework 2.0 or 3.5) just refactoring the example proposed by the author of the MSDN article mentioned above.</p>
<p>You can <a href="http://cid-fd34d9886368eda3.office.live.com/self.aspx/Pubblica/MessageQueueP2P.zip" target="_blank">download source code here</a>.</p>
<p>Here’s a screenshot:</p>
<p class="MsoNormal"><a href="http://dariosantarelli.files.wordpress.com/2011/06/ss1.jpg"><img class="alignnone size-medium wp-image-136" title="Message Queue Point-To-Point Test Application" src="http://dariosantarelli.files.wordpress.com/2011/06/ss1.jpg?w=300&#038;h=169" alt="" width="300" height="169" /></a></p>
<p class="MsoNormal">HTH</p>
<br />Filed under: <a href='http://dariosantarelli.wordpress.com/category/net-framework/'>.NET Framework</a>, <a href='http://dariosantarelli.wordpress.com/category/net-framework/compact-framework/'>Compact Framework</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/dariosantarelli.wordpress.com/132/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/dariosantarelli.wordpress.com/132/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/dariosantarelli.wordpress.com/132/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/dariosantarelli.wordpress.com/132/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/dariosantarelli.wordpress.com/132/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/dariosantarelli.wordpress.com/132/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/dariosantarelli.wordpress.com/132/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/dariosantarelli.wordpress.com/132/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/dariosantarelli.wordpress.com/132/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/dariosantarelli.wordpress.com/132/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/dariosantarelli.wordpress.com/132/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/dariosantarelli.wordpress.com/132/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/dariosantarelli.wordpress.com/132/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/dariosantarelli.wordpress.com/132/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dariosantarelli.wordpress.com&amp;blog=502627&amp;post=132&amp;subd=dariosantarelli&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://dariosantarelli.wordpress.com/2011/06/11/net-compact-framework-working-with-point-to-point-message-queues/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/3805217b57ce1cc8bacbb870a2e3479f?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">dariosantarelli</media:title>
		</media:content>

		<media:content url="http://dariosantarelli.files.wordpress.com/2011/06/cd.jpg?w=300" medium="image">
			<media:title type="html">Class Diagram</media:title>
		</media:content>

		<media:content url="http://dariosantarelli.files.wordpress.com/2011/06/ss1.jpg?w=300" medium="image">
			<media:title type="html">Message Queue Point-To-Point Test Application</media:title>
		</media:content>
	</item>
		<item>
		<title>[ASP.NET MVC 2] Splitting DateTime in drop-down lists and model binding</title>
		<link>http://dariosantarelli.wordpress.com/2010/12/26/asp-net-mvc-2-splitting-datetime-in-drop-down-lists-and-model-binding/</link>
		<comments>http://dariosantarelli.wordpress.com/2010/12/26/asp-net-mvc-2-splitting-datetime-in-drop-down-lists-and-model-binding/#comments</comments>
		<pubDate>Sun, 26 Dec 2010 16:33:27 +0000</pubDate>
		<dc:creator>dariosantarelli</dc:creator>
				<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[Binding]]></category>
		<category><![CDATA[MVC]]></category>

		<guid isPermaLink="false">http://dariosantarelli.wordpress.com/?p=112</guid>
		<description><![CDATA[OK this is not the classic DateTime picker bound to a textbox… with a jQuery calendar . If you need a custom datetime editor template that splits the datetime parts in drop-down lists like this… &#60;%= Html.EditorFor(model =&#62; model.BirthDate, &#8220;Date&#8221;) %&#62; …or like this… &#160; &#60;%= Html.EditorFor(model =&#62; model.EventDateTime, &#8220;DateTime&#8221;) %&#62; …then this post may [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dariosantarelli.wordpress.com&amp;blog=502627&amp;post=112&amp;subd=dariosantarelli&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>OK this is not the classic DateTime picker bound to a textbox… with a jQuery calendar <img src='http://s1.wp.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> .<br />
If you need a custom datetime editor template that splits the datetime parts in drop-down lists like this…</p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-family:Consolas;"><span style="background-color:#ffff00;color:#000000;"><span style="font-size:10pt;">&lt;%</span></span><span style="font-size:10pt;"><span style="color:#0000ff;">=</span></span><span style="font-size:10pt;"><span style="color:#000000;"> Html.EditorFor(model =&gt; model.BirthDate, </span><span style="color:#a31515;">&#8220;Date&#8221;</span><span style="color:#000000;">) </span></span><span style="font-size:10pt;background-color:#ffff00;color:#000000;">%&gt;</span></span><br />
<img style="margin:5px;" src="http://img269.imageshack.us/img269/9956/immaginewv.jpg" alt="" /></p>
<p>…or like this…</p>
<p class="MsoNormal" style="line-height:normal;margin:0;">&nbsp;</p>
<p class="MsoNormal" style="line-height:normal;margin:0;"><span style="font-family:Consolas;"><span style="background-color:#ffff00;color:#000000;"><span style="font-size:10pt;">&lt;%</span></span><span style="font-size:10pt;"><span style="color:#0000ff;">=</span></span><span style="font-size:10pt;"><span style="color:#000000;"> Html.EditorFor(model =&gt; model.EventDateTime, </span><span style="color:#a31515;">&#8220;DateTime&#8221;</span><span style="color:#000000;">) </span></span><span style="font-size:10pt;background-color:#ffff00;color:#000000;">%&gt;</span></span></p>
<p><img style="margin:5px;" src="http://img547.imageshack.us/img547/5513/immagine1fj.jpg" alt="" /></p>
<p>…then this post may help you. As you should know, in ASP.NET MVC 2 the default model binder has some difficulties to combine splitted datetime parts in the View. So, if you need to define a DateTime property in your model and make a custom editor template that splits the DateTime parts in different controls (e.g. TextBox and/or DropDownList), first <span style="text-decoration:underline;">you should read </span><a href="http://www.hanselman.com/blog/SplittingDateTimeUnitTestingASPNETMVCCustomModelBinders.aspx"><span style="text-decoration:underline;">this smart solution</span></a><span style="text-decoration:underline;"> by </span><a href="http://www.hanselman.com/"><span style="text-decoration:underline;">Scott Hanselman</span></a>. The idea is to separate the way we render the month field, the day field, the year field etc. from the mechanism that will assemble them back in a DateTime structure for model binding.<br />
Starting from the <strong>Global.asax, </strong>the first thing to do is to register the Scott’s Custom Model Binder and then specify all the available options (the strings there are the suffixes of the fields in your View that will be holding the Date, the Time, the Day etc.)</p>
<pre><span style="color:#2b91af;font-family:Consolas;">ModelBinders</span><span style="font-family:Consolas;"><span style="color:#000000;">.Binders[</span><span style="color:#0000ff;">typeof</span><span style="color:#000000;">(</span><span style="color:#2b91af;">DateTime</span><span style="color:#000000;">)]  = </span><span style="color:#0000ff;">new</span><span style="color:#000000;"> </span><span style="color:#2b91af;">DateTimeModelBinder</span></span><span style="font-family:Consolas;"><span style="color:#000000;">()
{
  Date = </span><span style="color:#a31515;">"Date"</span></span><span style="font-family:Consolas;"><span style="color:#000000;">, </span></span><span style="font-family:Consolas;"><span style="color:#008000;">// Date parts are not splitted in the View
                 // (e.g. the whole date is held by a TextBox  with id “xxx_Date”)</span></span><span style="font-family:Consolas;"><span style="color:#000000;">
  Time = </span><span style="color:#a31515;">"Time"</span></span><span style="font-family:Consolas;"><span style="color:#000000;">, <span style="font-family:Consolas;"><span style="color:#008000;">// Time parts are not  splitted in the View
                 // (e.g. the whole time  is held by a TextBox with id “xxx_Time”)</span></span>
 <strong> Day = </strong></span><span style="color:#a31515;"><strong>"Day"</strong></span></span><span style="font-family:Consolas;"><strong><span style="color:#000000;">, <span style="font-family:Consolas;"><span style="color:#008000;"> </span></span>
  Month = </span><span style="color:#a31515;">"Month"</span></strong></span><span style="font-family:Consolas;"><strong><span style="color:#000000;">,
  Year = </span><span style="color:#a31515;">"Year"</span></strong></span><span style="font-family:Consolas;"><strong><span style="color:#000000;">,
  Hour = </span><span style="color:#a31515;">"Hour"</span></strong></span><span style="font-family:Consolas;"><strong><span style="color:#000000;">,
  Minute = </span><span style="color:#a31515;">"Minute"</span></strong></span><span style="font-family:Consolas;"><strong><span style="color:#000000;">,
  Second = </span><span style="color:#a31515;">"Second"</span>
</strong><span style="color:#000000;">};</span></span></pre>
<p><span style="font-family:Consolas;"><span style="color:#000000;"><br />
</span></span></p>
<p>Now, let’s have a look to template editors. In <strong>Views\Shared\EditorTemplates</strong> directory we can put two simple templates: <em><strong>Date.ascx</strong></em> and <em><strong>DateTime.ascx</strong></em>. The former renders only the drop-down lists for the date part of the DateTime structure (Month, Day, Year), while the latter renders the time part too. Here the code for <em>Date.ascx</em>:</p>
<pre style="margin:0;"><span style="font-family:Consolas;"><span style="background-color:#ffff00;color:#000000;"><span style="font-size:10pt;">&lt;%</span></span><span style="font-size:10pt;"><span style="color:#0000ff;">@</span></span></span><span style="font-size:10pt;"><span style="font-family:Consolas;"><span style="color:#000000;"> </span><span style="color:#800000;">Control</span><span style="color:#000000;"> </span><span style="color:#ff0000;">Language</span><span style="color:#0000ff;">="C#"</span><span style="color:#000000;"> </span><span style="color:#ff0000;">Inherits</span><span style="color:#0000ff;">="System.Web.Mvc.ViewUserControl&lt;System.DateTime&gt;"</span></span><span style="font-family:Consolas;"><span style="color:#000000;"> <span style="background-color:#ffff00;">%&gt;</span>
<span style="background-color:#ffff00;">&lt;%</span></span><span style="color:#0000ff;">@</span><span style="color:#000000;"> </span><span style="color:#800000;">Import</span><span style="color:#000000;"> </span><span style="color:#ff0000;">Namespace</span><span style="color:#0000ff;">="System.Threading"</span></span><span style="font-family:Consolas;"><span style="color:#000000;"> <span style="background-color:#ffff00;">%&gt;</span></span></span></span></pre>
<pre style="margin:0;"><span style="font-size:10pt;"><span style="font-family:Consolas;"><span style="color:#000000;"><span style="background-color:#ffff00;">
</span></span></span></span></pre>
<pre style="margin:0;"><span style="font-size:10pt;"><span style="font-family:Consolas;"><span style="color:#000000;"><span style="background-color:#ffff00;">&lt;%</span></span><span style="color:#0000ff;">=</span></span><span style="font-family:Consolas;"><span style="color:#000000;"> Html.DropDownListFor(dateTime =&gt; dateTime.Month, </span><span style="color:#2b91af;">Enumerable</span><span style="color:#000000;">.Range(1, 12).Select(i =&gt; </span><span style="color:#0000ff;">new</span><span style="color:#000000;"> </span><span style="color:#2b91af;">SelectListItem</span></span><span style="font-family:Consolas;"><span style="color:#000000;">                          
{                              
  Value = i.ToString(),                              
  Text = </span><span style="color:#2b91af;">Thread</span></span><span style="color:#000000;font-family:Consolas;">.CurrentThread.CurrentUICulture.DateTimeFormat.GetMonthName(i),                             
  Selected = (i == Model.Month &amp;&amp; Model != </span><span style="color:#2b91af;font-family:Consolas;">DateTime</span></span><span style="font-size:10pt;"><span style="font-family:Consolas;"><span style="color:#000000;">.MinValue &amp;&amp; Model != </span><span style="color:#2b91af;">DateTime</span></span><span style="font-family:Consolas;"><span style="color:#000000;">.MaxValue)                         
}),</span><span style="color:#a31515;">"-- Month --"</span></span><span style="font-family:Consolas;"><span style="color:#000000;">)<span style="background-color:#ffff00;">%&gt;</span><span style="background-color:#ffffff;"> </span> /</span></span></span></pre>
<pre style="margin:0;"><span style="font-size:10pt;"><span style="font-family:Consolas;"><span style="color:#000000;">
<span style="background-color:#ffff00;">&lt;%</span></span><span style="color:#0000ff;">=</span></span><span style="font-family:Consolas;"><span style="color:#000000;"> Html.DropDownListFor(dateTime =&gt; dateTime.Day, </span><span style="color:#2b91af;">Enumerable</span><span style="color:#000000;">.Range(1, 31).Select(i =&gt; </span><span style="color:#0000ff;">new</span><span style="color:#000000;"> </span><span style="color:#2b91af;">SelectListItem</span></span><span style="font-family:Consolas;"><span style="color:#000000;">                          
{                              
  Value = i.ToString(),                              
  Text = i.ToString(),                             
  Selected = (i == Model.Day &amp;&amp; Model != </span><span style="color:#2b91af;">DateTime</span><span style="color:#000000;">.MinValue &amp;&amp; Model != </span><span style="color:#2b91af;">DateTime</span></span><span style="font-family:Consolas;"><span style="color:#000000;">.MaxValue)                         
}), </span><span style="color:#a31515;">"-- Day --"</span></span><span style="font-family:Consolas;"><span style="color:#000000;">)<span style="background-color:#ffff00;">%&gt;</span> /</span></span></span></pre>
<pre style="margin:0;"><span style="font-size:10pt;"><span style="font-family:Consolas;"><span style="color:#000000;">
<span style="background-color:#ffff00;">&lt;%</span></span><span style="color:#0000ff;">=</span></span><span style="font-family:Consolas;"><span style="color:#000000;"> Html.DropDownListFor(dateTime =&gt; dateTime.Year, </span><span style="color:#2b91af;">Enumerable</span><span style="color:#000000;">.Range(</span><span style="color:#2b91af;">DateTime</span><span style="color:#000000;">.Now.Year-110, 110).Select(i =&gt; </span><span style="color:#0000ff;">new</span><span style="color:#000000;"> </span><span style="color:#2b91af;">SelectListItem</span></span><span style="font-family:Consolas;"><span style="color:#000000;">                           
{                                                             
  Value = i.ToString(),                               
  Text = i.ToString(),                              
  Selected = (i == Model.Year &amp;&amp; Model != </span><span style="color:#2b91af;">DateTime</span><span style="color:#000000;">.MinValue &amp;&amp; Model != </span><span style="color:#2b91af;">DateTime</span></span><span style="font-family:Consolas;"><span style="color:#000000;">.MaxValue)                          
}), </span><span style="color:#a31515;">"-- Year --"</span></span><span style="font-family:Consolas;"><span style="color:#000000;">)<span style="background-color:#ffff00;">%&gt;</span></span></span></span></pre>
<pre style="margin:0;"><span style="font-size:10pt;"><span style="font-family:Consolas;"><span style="color:#000000;"><span style="background-color:#ffff00;">
</span></span></span></span></pre>
<pre style="margin:0;"><span style="font-size:10pt;"><span style="font-family:Consolas;"><span style="color:#000000;"><span style="background-color:#ffff00;">&lt;%</span></span><span style="color:#0000ff;">=</span></span><span style="font-family:Consolas;"><span style="color:#000000;"> Html.HiddenFor(dateTime =&gt; dateTime.Hour)<span style="background-color:#ffff00;">%&gt;</span>
<span style="background-color:#ffff00;">&lt;%</span></span><span style="color:#0000ff;">=</span></span></span><span style="font-family:Consolas;"><span style="font-size:10pt;"><span style="color:#000000;"> Html.HiddenFor(dateTime =&gt; dateTime.Minute)<span style="background-color:#ffff00;">%&gt;</span>
<span style="background-color:#ffff00;">&lt;%</span></span><span style="color:#0000ff;">=</span><span style="color:#000000;"> Html.HiddenFor(dateTime =&gt; dateTime.Second)</span></span><span style="font-size:10pt;background-color:#ffff00;color:#000000;">%&gt;</span></span></pre>
<pre style="margin:0;"><span style="font-family:Consolas;"><span style="font-size:10pt;background-color:#ffff00;color:#000000;">
</span></span></pre>
<p>That’s all!<br />
Note that in the template editor above, the Hour, Minute and Second parts are rendered as HTML hidden fileds, because the Scott’s <em>DateTimeModelBinder</em> configured in the Global.asax expects a value for all the six parts of the splitted DateTime structure. It’s just a clean workaround to make the Scott’s model binder work without any change to the original code. In a real implementation hidden fields should be not required <img src='http://s1.wp.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> .</p>
<p>Now, what about validation? Well, both client-side and server-side validations are quite trivial: the server-side validation can be obtained through a custom <a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.validationattribute.aspx">ValidationAttribute</a><em> </em>that checks if the DateTime value is correct <em>(e.g.</em> the value should be not equal to <em>DateTime.MinValue</em> or <em>DateTime.MaxValue).</em></p>
<pre style="margin:0;"><span style="font-family:Consolas;"><span style="color:#000000;"><span style="font-size:10pt;">[</span></span><span style="font-size:10pt;"><span style="color:#2b91af;">AttributeUsage</span><span style="color:#000000;">(</span><span style="color:#2b91af;">AttributeTargets</span><span style="color:#000000;">.Field | </span><span style="color:#2b91af;">AttributeTargets</span><span style="color:#000000;">.Property, AllowMultiple = </span><span style="color:#0000ff;">false</span><span style="color:#000000;">, Inherited = </span><span style="color:#0000ff;">true</span></span></span><span style="font-size:10pt;"><span style="font-family:Consolas;"><span style="color:#000000;">)]
</span><span style="color:#0000ff;">public</span><span style="color:#000000;"> </span><span style="color:#0000ff;">sealed</span><span style="color:#000000;"> </span><span style="color:#0000ff;">class</span><span style="color:#000000;"> </span><span style="color:#2b91af;">DateRequiredAttribute</span><span style="color:#000000;"> : </span><span style="color:#2b91af;">ValidationAttribute</span>
</span><span style="font-family:Consolas;"><span style="color:#000000;">{       
</span><span style="color:#0000ff;">   public</span><span style="color:#000000;"> DateRequiredAttribute() : </span><span style="color:#0000ff;">base</span></span><span style="font-family:Consolas;"><span style="color:#000000;">() { }
</span><span style="color:#0000ff;">
   public</span><span style="color:#000000;"> </span><span style="color:#0000ff;">override</span><span style="color:#000000;"> </span><span style="color:#0000ff;">string</span><span style="color:#000000;"> FormatErrorMessage(</span><span style="color:#0000ff;">string</span></span><span style="font-family:Consolas;"><span style="color:#000000;"> name)
   {
     </span><span style="color:#0000ff;">return</span><span style="color:#000000;"> </span><span style="color:#0000ff;">string</span><span style="color:#000000;">.Format(</span><span style="color:#2b91af;">CultureInfo</span></span><span style="font-family:Consolas;"><span style="color:#000000;">.CurrentUICulture, ErrorMessageString, name);
   }
  </span><span style="color:#0000ff;">public</span><span style="color:#000000;"> </span><span style="color:#0000ff;">override</span><span style="color:#000000;"> </span><span style="color:#0000ff;">bool</span><span style="color:#000000;"> IsValid(</span><span style="color:#0000ff;">object</span></span><span style="font-family:Consolas;"><span style="color:#000000;"> value)
   {
</span><span style="color:#2b91af;">     DateTime</span><span style="color:#000000;"> dateTime = (</span><span style="color:#2b91af;">DateTime</span></span><span style="font-family:Consolas;"><span style="color:#000000;">)value;
     </span><span style="color:#0000ff;">return</span><span style="color:#000000;"> (dateTime != </span></span></span><span style="font-size:10pt;"><span style="color:#2b91af;font-family:Consolas;">DateTime</span></span><span style="font-family:Consolas;"><span style="font-size:10pt;"><span style="color:#000000;">.MinValue &amp;&amp; dateTime != </span><span style="color:#2b91af;">DateTime</span><span style="color:#000000;">.MaxValue);           
   }
}</span></span></span></pre>
<pre style="margin:0;"><span style="font-family:Consolas;"><span style="font-size:10pt;"><span style="color:#000000;">
</span></span></span></pre>
<p>The corresponding client-side validation adapter can be implemented by deriving the <a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.dataannotationsmodelvalidator(VS.98).aspx">DataAnnotationsModelValidator</a> class. It allows us to specify a remote validation rule from the client. In this scenario, the part of the DateTime structure that could be validated is the Date part.<br />
So, we can create a <em>SplittedDateRequiredValidator </em>in order to check if each drop-down is holding a valid value. To accomplish this requirement, a simple solution is to make the client-side validator aware of the IDs of the &lt;<em>select&gt;</em> elements holding the DateTime’s Month, Day and Year values.</p>
<pre style="margin:0;"><span style="color:#0000ff;font-family:Consolas;"><span style="font-size:10pt;">public</span></span><span style="font-size:10pt;"><span style="font-family:Consolas;"><span style="color:#000000;"> </span><span style="color:#0000ff;">sealed</span><span style="color:#000000;"> </span><span style="color:#0000ff;">class</span><span style="color:#000000;"> </span><span style="color:#2b91af;">SplittedDateRequiredValidator</span><span style="color:#000000;"> : </span><span style="color:#2b91af;">DataAnnotationsModelValidator</span><span style="color:#000000;">&lt;</span><span style="color:#2b91af;">DateRequiredAttribute</span></span><span style="font-family:Consolas;"><span style="color:#000000;">&gt;
{
</span><span style="color:#0000ff;">   private</span><span style="color:#000000;"> </span><span style="color:#0000ff;">string</span></span><span style="font-family:Consolas;"><span style="color:#000000;"> _message;
   </span><span style="color:#0000ff;">private</span><span style="color:#000000;"> </span><span style="color:#0000ff;">string</span></span><span style="font-family:Consolas;"><span style="color:#000000;"> _dayField;
   </span><span style="color:#0000ff;">private</span><span style="color:#000000;"> </span><span style="color:#0000ff;">string</span></span><span style="font-family:Consolas;"><span style="color:#000000;"> _monthField;
   </span><span style="color:#0000ff;">private</span><span style="color:#000000;"> </span><span style="color:#0000ff;">string</span></span><span style="font-family:Consolas;"><span style="color:#000000;"> _yearField;</span></span></span><span style="font-size:10pt;"><span style="font-family:Consolas;"><span style="color:#000000;">

</span></span></span></pre>
<pre style="margin:0;"><span style="font-size:10pt;"><span style="font-family:Consolas;"><span style="color:#000000;">   </span><span style="color:#0000ff;">public</span><span style="color:#000000;"> SplittedDateRequiredValidator(</span><span style="color:#2b91af;">ModelMetadata</span><span style="color:#000000;"> metadata, </span><span style="color:#2b91af;">ControllerContext</span><span style="color:#000000;"> context, </span><span style="color:#2b91af;">DateRequiredAttribute</span></span><span style="font-family:Consolas;"><span style="color:#000000;"> attribute)
                                        : </span><span style="color:#0000ff;">base</span></span><span style="font-family:Consolas;"><span style="color:#000000;">(metadata, context, attribute)
   {
      _message = attribute.ErrorMessage;
       _dayField = metadata.PropertyName + </span><span style="color:#a31515;">"_Day"</span></span><span style="font-family:Consolas;"><span style="color:#000000;">;
       _monthField = metadata.PropertyName + </span><span style="color:#a31515;">"_Month"</span></span><span style="font-family:Consolas;"><span style="color:#000000;">;
       _yearField = metadata.PropertyName + </span><span style="color:#a31515;">"_Year"</span></span><span style="font-family:Consolas;"><span style="color:#000000;">;           
   }

   </span><span style="color:#0000ff;">public</span><span style="color:#000000;"> </span><span style="color:#0000ff;">override</span><span style="color:#000000;"> </span><span style="color:#2b91af;">IEnumerable</span><span style="color:#000000;">&lt;</span><span style="color:#2b91af;">ModelClientValidationRule</span></span><span style="font-family:Consolas;"><span style="color:#000000;">&gt; GetClientValidationRules()
   {
      </span><span style="color:#2b91af;">ModelClientValidationRule</span><span style="color:#000000;"> rule = </span><span style="color:#0000ff;">new</span><span style="color:#000000;"> </span><span style="color:#2b91af;">ModelClientValidationRule
      </span></span><span style="font-family:Consolas;"><span style="color:#000000;">{
         ErrorMessage = _message,
         ValidationType = </span><span style="color:#a31515;">"splittedDateRequiredValidator"</span></span><span style="font-family:Consolas;"><span style="color:#000000;">               
      };

      rule.ValidationParameters.Add(</span><span style="color:#a31515;">"dayFieldId"</span></span><span style="font-family:Consolas;"><span style="color:#000000;">, _dayField);
      rule.ValidationParameters.Add(</span><span style="color:#a31515;">"monthFieldId"</span></span><span style="font-family:Consolas;"><span style="color:#000000;">, _monthField);
      rule.ValidationParameters.Add(</span><span style="color:#a31515;">"yearFieldId"</span></span></span><span style="font-family:Consolas;"><span style="font-size:10pt;"><span style="color:#000000;">, _yearField);

      </span><span style="color:#0000ff;">return</span><span style="color:#000000;"> </span><span style="color:#0000ff;">new</span><span style="color:#000000;">[] { rule };
   }
}</span></span></span></pre>
<pre style="margin:0;"><span style="font-family:Consolas;"><span style="font-size:10pt;"><span style="color:#000000;">
</span></span></span></pre>
<p>Before looking at javascript validator code, let’s register the <em>SplittedDateRequiredValidator</em> as the client-side validation adapter for all model properties decorated with the <em>DateRequiredAttribute</em>. To accomplish that, we have to put the following line of code in the Global.asax…</p>
<pre style="margin:0;"><span style="font-family:Consolas;"><span style="color:#2b91af;"><span style="font-size:10pt;">DataAnnotationsModelValidatorProvider</span></span><span style="font-size:10pt;"><span style="color:#000000;">.RegisterAdapter(</span><span style="color:#0000ff;">typeof</span><span style="color:#000000;">(</span><span style="color:#2b91af;">DateRequiredAttribute</span><span style="color:#000000;">), </span><span style="color:#0000ff;">typeof</span><span style="color:#000000;">(</span><span style="color:#2b91af;">SplittedDateRequiredValidator</span><span style="color:#000000;">));</span></span></span></pre>
<p>&nbsp;</p>
<p>Finally, the client-side validator will evaluate the selected index of the drop-down lists in order to ensure that the user has selected a valid date (note that the <em>isValidDate</em> function simply checks if the users has specified an existing date).</p>
<pre style="margin:0;"><span style="font-family:Consolas;"><span style="color:#000000;"><span style="font-size:10pt;">Sys.Mvc.ValidatorRegistry.validators.splittedDateRequiredValidator = </span></span><span style="font-size:10pt;"><span style="color:#0000ff;">function</span></span></span><span style="font-size:10pt;"><span style="font-family:Consolas;"><span style="color:#000000;"> (rule) {        
</span><span style="color:#0000ff;">  var</span></span><span style="font-family:Consolas;"><span style="color:#000000;"> dayFieldId = rule.ValidationParameters.dayFieldId;    
</span><span style="color:#0000ff;">  var</span></span><span style="font-family:Consolas;"><span style="color:#000000;"> monthFieldId = rule.ValidationParameters.monthFieldId;    
</span><span style="color:#0000ff;">  var</span></span><span style="font-family:Consolas;"><span style="color:#000000;"> yearFieldId = rule.ValidationParameters.yearFieldId;    
</span><span style="color:#0000ff;">  return</span><span style="color:#000000;"> </span><span style="color:#0000ff;">function</span></span><span style="font-family:Consolas;"><span style="color:#000000;"> (value, context) {                
</span><span style="color:#0000ff;">    var</span></span><span style="font-family:Consolas;"><span style="color:#000000;"> dayIdx = $get(dayFieldId).selectedIndex;        
</span><span style="color:#0000ff;">    var</span></span><span style="font-family:Consolas;"><span style="color:#000000;"> monthIdx = $get(monthFieldId).selectedIndex;        
</span><span style="color:#0000ff;">    var</span></span><span style="font-family:Consolas;"><span style="color:#000000;"> yearIdx = $get(yearFieldId).selectedIndex;        
</span><span style="color:#0000ff;">    if</span><span style="color:#000000;"> (dayIdx === 0 || monthIdx === 0 || yearIdx === 0) </span><span style="color:#0000ff;">return</span><span style="color:#000000;"> </span><span style="color:#0000ff;">false</span></span><span style="font-family:Consolas;"><span style="color:#000000;">;        
</span><span style="color:#0000ff;">    else</span><span style="color:#000000;"> </span><span style="color:#0000ff;">return</span></span><span style="font-family:Consolas;"><span style="color:#000000;"> isValidDate(parseInt($get(yearFieldId).value), monthIdx, dayIdx);     </span></span></span></pre>
<pre style="margin:0;"><span style="font-size:10pt;"><span style="font-family:Consolas;"><span style="color:#000000;">  }</span></span></span><span style="font-size:10pt;"><span style="font-family:Consolas;"><span style="color:#000000;">;
</span></span></span><span style="font-family:Consolas;line-height:19px;white-space:normal;font-size:13px;">};</span></pre>
<p><span style="font-size:10pt;"><span style="font-family:Consolas;"><span style="color:#000000;"><br />
</span><span style="color:#0000ff;">function</span></span><span style="font-family:Consolas;"><span style="color:#000000;"> isValidDate(y, m, d) {<br />
</span><span style="color:#0000ff;"> var</span><span style="color:#000000;"> date = </span><span style="color:#0000ff;">new</span></span><span style="font-family:Consolas;"><span style="color:#000000;"> Date(y, m &#8211; 1, d);<br />
</span><span style="color:#0000ff;"> var</span><span style="color:#000000;"> convertedDate = </span><span style="color:#800000;">&#8220;&#8221;</span></span><span style="font-family:Consolas;"><span style="color:#000000;"> + date.getFullYear() + (date.getMonth() + 1) + date.getDate();<br />
</span><span style="color:#0000ff;"> var</span><span style="color:#000000;"> givenDate = </span><span style="color:#800000;">&#8220;&#8221;</span></span></span><span style="font-family:Consolas;"><span style="font-size:10pt;"><span style="color:#000000;"> + y + m + d;<br />
</span><span style="color:#0000ff;"> return</span><span style="color:#000000;"> (givenDate == convertedDate);<br />
}<br />
</span></span></span></p>
<p>Ok let’s put everything together!<br />
Assuming that our model defines a property ”BirthDate” like this…</p>
<p><span style="font-family:Consolas;"><span style="color:#000000;"><span style="font-size:10pt;">[</span></span><span style="font-size:10pt;"><span style="color:#2b91af;">DateRequired</span><span style="color:#000000;">(ErrorMessage = </span><span style="color:#a31515;">"Invalid date. Please specify valid values!"</span></span></span><span style="font-size:10pt;"><span style="color:#000000;font-family:Consolas;">)]<br />
</span></span><span style="font-size:10pt;"><span style="font-family:Consolas;"><span style="color:#000000;">[</span><span style="color:#2b91af;">DataType</span><span style="color:#000000;">(</span><span style="color:#2b91af;">DataType</span></span><span style="font-family:Consolas;"><span style="color:#000000;">.Date)]<br />
[</span><span style="color:#2b91af;">DisplayName</span><span style="color:#000000;">(</span><span style="color:#a31515;">"Birthdate"</span></span></span><span style="font-family:Consolas;"><span style="font-size:10pt;"><span style="color:#000000;">)]<br />
</span><span style="color:#0000ff;">public</span><span style="color:#000000;"> </span><span style="color:#2b91af;">DateTime</span><span style="color:#000000;"> BirthDate { </span><span style="color:#0000ff;">get</span><span style="color:#000000;">; </span><span style="color:#0000ff;">set</span><span style="color:#000000;">; }</span></span></span></p>
<p>… if we put the following code in our View…</p>
<pre style="margin:0;"><span style="font-family:Consolas;"><span style="color:#000000;"><span style="background-color:#ffff00;"><span style="font-size:10pt;">&lt;%</span></span><span style="font-size:10pt;"> Html.EnableClientValidation(); </span><span style="font-size:10pt;background-color:#ffff00;">%&gt;</span></span></span></pre>
<p>&#8230;<br />
<span style="font-family:Consolas;"><span style="background-color:#ffff00;color:#000000;"><span style="font-size:10pt;">&lt;%</span></span><span style="font-size:10pt;"><span style="color:#0000ff;">=</span></span></span><span style="font-family:Consolas;"><span style="font-size:10pt;"><span style="color:#000000;"> Html.EditorFor(m =&gt; m.BirthDate, </span><span style="color:#a31515;">&#8220;Date&#8221;</span><span style="color:#000000;">) <span style="background-color:#ffff00;">%&gt;</span></span><span style="color:#0000ff;">&lt;</span><span style="color:#800000;">br</span><span style="color:#000000;"> </span><span style="color:#0000ff;">/&gt;</span><br />
<span style="color:#000000;"><span style="background-color:#ffff00;">&lt;%</span></span><span style="color:#0000ff;">=</span><span style="color:#000000;"> Html.ValidationMessageFor(m =&gt; m.BirthDate) </span></span><span style="font-size:10pt;background-color:#ffff00;color:#000000;">%&gt;</span></span></p>
<p>…the output would be, for example, the following…</p>
<p><img style="margin:5px;" src="http://img140.imageshack.us/img140/9660/immagineguc.jpg" alt="" /></p>
<p>HTH</p>
<br />Filed under: <a href='http://dariosantarelli.wordpress.com/category/asp-net/asp-net-mvc/'>ASP.NET MVC</a> Tagged: <a href='http://dariosantarelli.wordpress.com/tag/asp-net/'>ASP.NET</a>, <a href='http://dariosantarelli.wordpress.com/tag/binding/'>Binding</a>, <a href='http://dariosantarelli.wordpress.com/tag/mvc/'>MVC</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/dariosantarelli.wordpress.com/112/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/dariosantarelli.wordpress.com/112/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/dariosantarelli.wordpress.com/112/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/dariosantarelli.wordpress.com/112/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/dariosantarelli.wordpress.com/112/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/dariosantarelli.wordpress.com/112/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/dariosantarelli.wordpress.com/112/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/dariosantarelli.wordpress.com/112/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/dariosantarelli.wordpress.com/112/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/dariosantarelli.wordpress.com/112/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/dariosantarelli.wordpress.com/112/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/dariosantarelli.wordpress.com/112/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/dariosantarelli.wordpress.com/112/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/dariosantarelli.wordpress.com/112/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dariosantarelli.wordpress.com&amp;blog=502627&amp;post=112&amp;subd=dariosantarelli&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://dariosantarelli.wordpress.com/2010/12/26/asp-net-mvc-2-splitting-datetime-in-drop-down-lists-and-model-binding/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/3805217b57ce1cc8bacbb870a2e3479f?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">dariosantarelli</media:title>
		</media:content>

		<media:content url="http://img269.imageshack.us/img269/9956/immaginewv.jpg" medium="image" />

		<media:content url="http://img547.imageshack.us/img547/5513/immagine1fj.jpg" medium="image" />

		<media:content url="http://img140.imageshack.us/img140/9660/immagineguc.jpg" medium="image" />
	</item>
		<item>
		<title>[WPF] Binding multiple command parameters using MultiBinding</title>
		<link>http://dariosantarelli.wordpress.com/2010/11/07/wpf-binding-multiple-command-parameters-using-multibinding/</link>
		<comments>http://dariosantarelli.wordpress.com/2010/11/07/wpf-binding-multiple-command-parameters-using-multibinding/#comments</comments>
		<pubDate>Sun, 07 Nov 2010 13:59:51 +0000</pubDate>
		<dc:creator>dariosantarelli</dc:creator>
				<category><![CDATA[WPF]]></category>
		<category><![CDATA[Binding]]></category>
		<category><![CDATA[IMultiValueConverter]]></category>

		<guid isPermaLink="false">http://dariosantarelli.wordpress.com/2010/11/07/wpf-binding-multiple-command-parameters-using-multibinding/</guid>
		<description><![CDATA[In this post, I’d like to show how we can pass multiple values into an ICommand implementation by using the CommandParameter property of an input control. This is useful especially in MVVM architectures, so that the View can interact with the ViewModel in a clean way, facing the fact that the Execute method of the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dariosantarelli.wordpress.com&amp;blog=502627&amp;post=91&amp;subd=dariosantarelli&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div>In this post, I’d like to show how we can pass multiple values into an ICommand implementation by using the <a href="http://msdn.microsoft.com/en-us/library/system.windows.input.icommandsource.commandparameter.aspx">CommandParameter</a> property of an input control. This is useful especially in MVVM architectures, so that the View can interact with the ViewModel in a clean way, facing the fact that the Execute method of the ICommand interface allows only a single object.<br />
A solution is to use the <a href="http://msdn.microsoft.com/en-us/library/system.windows.data.multibinding.aspx">MultiBinding</a> class which allows us to define a collection of <a href="http://msdn.microsoft.com/en-us/library/system.windows.data.binding.aspx">Binding</a> objects attached to the target <em>CommandParameter</em> property of our input control. In a concrete example, let’s consider a simple search box like the picture below: the OK button is bound to a custom FindCommand defined in the ViewModel.</div>
<div><img style="margin:5px;" src="http://img571.imageshack.us/img571/8008/immaginexfr.jpg" alt="" /></div>
<div>
<div>When the user clicks on the OK button, two parameters must be passed to the command: the string to be searched and the “ignorecase” option. But how can we bind these two parameters to the Button’s CommandParameter?<br />
Well, first we have to create a class to hold the parameters.</div>
<pre style="margin:0;"><span style="color:#0000ff;font-family:Consolas;">
</span></pre>
<pre style="margin:0;"><span style="color:#0000ff;font-family:Consolas;">public</span><span style="font-family:Consolas;"><span style="color:#000000;"> </span><span style="color:#0000ff;">class</span><span style="color:#000000;"> </span><span style="color:#2b91af;">FindCommandParameters
</span></span><span style="font-family:Consolas;"><span style="color:#000000;">{</span><span style="color:#0000ff;">
  public</span><span style="color:#000000;"> </span><span style="color:#0000ff;">string</span><span style="color:#000000;"> Text { </span><span style="color:#0000ff;">get</span><span style="color:#000000;">; </span><span style="color:#0000ff;">set</span></span><span style="font-family:Consolas;"><span style="color:#000000;">; }</span><span style="color:#0000ff;">
  public</span><span style="color:#000000;"> </span><span style="color:#0000ff;">bool</span><span style="color:#000000;"> IgnoreCase { </span><span style="color:#0000ff;">get</span><span style="color:#000000;">; </span><span style="color:#0000ff;">set</span><span style="color:#000000;">; }
}</span></span></pre>
<pre style="margin:0;"><span style="font-family:Consolas;"><span style="color:#000000;">
</span></span></pre>
<div>After that, we have to create a class that implements the <a href="http://msdn.microsoft.com/en-us/library/system.windows.data.imultivalueconverter.aspx">IMultiValueConverter</a> interface. This simply converts our multiple parameters into the class that we have defined before.</div>
<pre style="margin:0;"><span style="font-family:Consolas;"><span style="color:#0000ff;">
public</span><span style="color:#000000;"> </span><span style="color:#0000ff;">class</span><span style="color:#000000;"> </span><span style="color:#2b91af;">FindCommandParametersConverter</span><span style="color:#000000;"> : </span><span style="color:#2b91af;">IMultiValueConverter</span></span><span style="font-family:Consolas;"><span style="color:#000000;">
{ </span><span style="color:#0000ff;">
  public</span><span style="color:#000000;"> </span><span style="color:#0000ff;">object</span><span style="color:#000000;"> Convert(</span><span style="color:#0000ff;">object</span><span style="color:#000000;">[] values, </span><span style="color:#2b91af;">Type</span><span style="color:#000000;"> targetType, </span><span style="color:#0000ff;">object</span><span style="color:#000000;"> parameter, System.Globalization.</span><span style="color:#2b91af;">CultureInfo</span></span><span style="font-family:Consolas;"><span style="color:#000000;"> culture)
  {</span><span style="color:#2b91af;">
    FindCommandParameters</span><span style="color:#000000;"> parameters = </span><span style="color:#0000ff;">new</span><span style="color:#000000;"> </span><span style="color:#2b91af;">FindCommandParameters</span></span><span style="font-family:Consolas;"><span style="color:#000000;">(); </span><span style="color:#0000ff;">
    foreach</span><span style="color:#000000;"> (</span><span style="color:#0000ff;">var</span><span style="color:#000000;"> obj </span><span style="color:#0000ff;">in</span></span><span style="font-family:Consolas;"><span style="color:#000000;"> values)
    { </span><span style="color:#0000ff;">
       if</span><span style="color:#000000;"> (obj </span><span style="color:#0000ff;">is</span><span style="color:#000000;"> </span><span style="color:#0000ff;">string</span><span style="color:#000000;">) parameters.Text = (</span><span style="color:#0000ff;">string</span></span><span style="font-family:Consolas;"><span style="color:#000000;">)obj;                </span><span style="color:#0000ff;">
       else if</span><span style="color:#000000;"> (obj </span><span style="color:#0000ff;">is</span><span style="color:#000000;"> </span><span style="color:#0000ff;">bool</span><span style="color:#000000;">) parameters.IgnoreCase = (</span><span style="color:#0000ff;">bool</span></span><span style="font-family:Consolas;"><span style="color:#000000;">)obj;
    } </span><span style="color:#0000ff;">
    return</span></span><span style="font-family:Consolas;"><span style="color:#000000;"> parameters;
  }
</span><span style="color:#0000ff;">  public</span><span style="color:#000000;"> </span><span style="color:#0000ff;">object</span><span style="color:#000000;">[] ConvertBack(</span><span style="color:#0000ff;">object</span><span style="color:#000000;"> value, </span><span style="color:#2b91af;">Type</span><span style="color:#000000;">[] targetTypes, </span><span style="color:#0000ff;">object</span><span style="color:#000000;"> parameter, System.Globalization.</span><span style="color:#2b91af;">CultureInfo</span></span><span style="font-family:Consolas;"><span style="color:#000000;"> culture)
  {</span><span style="color:#0000ff;">
    throw</span><span style="color:#000000;"> </span><span style="color:#0000ff;">new</span><span style="color:#000000;"> </span><span style="color:#2b91af;">NotImplementedException</span><span style="color:#000000;">();
  }
}
</span></span></pre>
<pre style="margin:0;"><span style="font-family:Consolas;"><span style="color:#000000;">
</span></span></pre>
<div>As you can see in the code above, we can iterate through the list of input values of the <em>Convert</em> method, checking their type, and then correctly assign the properties of the parameter class. Obviously you can implement different solutions (you always know the order of the parameters set in the XAML), but the most important thing at this point is that the return value of the <em>Convert</em> method is what will be passed as argument to the <em>Execute</em> method of our FindCommand.</div>
<div>To wire up the XAML to take advantage of this class, we have to include the <em>&lt;Button.CommandParameter&gt;</em> element.  This contains the <em>&lt;MultiBinding&gt;</em> element, which has the “Converter” attribute. In the code below, the converter type is added as a resource to the button to make this post easier to read, but convention usually dictates resources are added at the Window level to allow reuse and readability.<br />
Under the <em>MultiBinding.Bindings</em> element, we add a <em>&lt;Binding&gt;</em> element for each parameter that we need to pass into the command.</div>
<pre style="margin:0;"><span style="font-family:Consolas;"><span style="color:#0000ff;">
</span></span></pre>
<pre style="margin:0;"><span style="font-family:Consolas;"><span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">TextBox</span><span style="color:#ff0000;"> x</span><span style="color:#0000ff;">:</span><span style="color:#ff0000;">Name</span><span style="color:#0000ff;">="txtFind"</span><span style="color:#0000ff;"> /&gt;</span></span></pre>
<pre style="margin:0;"><span style="font-family:Consolas;"><span style="font-size:x-small;"><span style="line-height:13pt;white-space:normal;" lang="EN-US"><span style="color:#0000ff;">&lt;</span></span><span style="line-height:13pt;white-space:normal;" lang="EN-US"><span style="color:#a31515;">CheckBox</span></span><span style="line-height:13pt;white-space:normal;" lang="EN-US"><span style="color:#ff0000;"> x</span></span><span style="line-height:13pt;white-space:normal;" lang="EN-US"><span style="color:#0000ff;">:</span></span><span style="line-height:13pt;white-space:normal;" lang="EN-US"><span style="color:#ff0000;">Name</span></span><span style="line-height:13pt;white-space:normal;" lang="EN-US"><span style="color:#0000ff;">="chkFindIgnoreCase"</span></span><span style="line-height:13pt;white-space:normal;" lang="EN-US"><span style="color:#ff0000;"> Content</span></span><span style="line-height:13pt;white-space:normal;" lang="EN-US"><span style="color:#0000ff;">="Ignore case" /&gt;</span></span></span></span></pre>
<pre style="margin:0;"><span style="font-family:Consolas;"><span style="font-size:x-small;"><span style="line-height:13pt;white-space:normal;" lang="EN-US"><span style="color:#0000ff;"> </span></span></span></span><span style="font-family:Consolas;"><span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">Button</span><span style="color:#ff0000;"> Command</span><span style="color:#0000ff;">="{</span><span style="color:#a31515;">Binding</span><span style="color:#ff0000;"> </span><span style="color:#0000ff;">FindCommand}"</span><span style="color:#ff0000;"> Content</span><span style="color:#0000ff;">="OK"&gt;</span><span style="color:#0000ff;">
  &lt;</span><span style="color:#a31515;">Button.Resources</span><span style="color:#0000ff;">&gt;</span><span style="color:#0000ff;">
    &lt;</span><span style="color:#a31515;">ui</span><span style="color:#0000ff;">:</span><span style="color:#a31515;">FindCommandParametersConverter</span><span style="color:#ff0000;"> x</span><span style="color:#0000ff;">:</span><span style="color:#ff0000;">Key</span><span style="color:#0000ff;">="findCommandParametersConverter" /&gt;</span><span style="color:#0000ff;">
  &lt;/</span><span style="color:#a31515;">Button.Resources</span><span style="color:#0000ff;">&gt;</span><span style="color:#0000ff;">
  &lt;</span><span style="color:#a31515;">Button.CommandParameter</span><span style="color:#0000ff;">&gt;</span><span style="color:#0000ff;">
    &lt;</span><span style="color:#a31515;">MultiBinding</span><span style="color:#ff0000;"> Converter</span><span style="color:#0000ff;">="{</span><span style="color:#a31515;">StaticResource</span><span style="color:#ff0000;"> findCommandParametersConverter</span><span style="color:#0000ff;">}"&gt;</span><span style="color:#0000ff;">
      &lt;</span><span style="color:#a31515;">MultiBinding.Bindings</span><span style="color:#0000ff;">&gt;</span><span style="color:#0000ff;">
        &lt;</span><span style="color:#a31515;">Binding</span><span style="color:#ff0000;"> ElementName</span><span style="color:#0000ff;">="txtFind"</span><span style="color:#ff0000;"> Path</span><span style="color:#0000ff;">="Text" /&gt;</span><span style="color:#0000ff;">
        &lt;</span><span style="color:#a31515;">Binding</span><span style="color:#ff0000;"> ElementName</span><span style="color:#0000ff;">="chkFindIgnoreCase"</span><span style="color:#ff0000;"> Path</span><span style="color:#0000ff;">="IsChecked" /&gt;</span><span style="color:#0000ff;">
      &lt;/</span><span style="color:#a31515;">MultiBinding.Bindings</span><span style="color:#0000ff;">&gt;</span><span style="color:#0000ff;">
    &lt;/</span><span style="color:#a31515;">MultiBinding</span><span style="color:#0000ff;">&gt;</span><span style="color:#0000ff;">
  &lt;/</span><span style="color:#a31515;">Button.CommandParameter</span><span style="color:#0000ff;">&gt;
</span><span style="color:#0000ff;">&lt;/</span><span style="color:#a31515;">Button</span><span style="color:#0000ff;">&gt;</span></span></pre>
<pre style="margin:0;"><span style="font-family:Consolas;"><span style="color:#0000ff;">
</span></span></pre>
<div>
<div>The final step is to consume the <em>FindCommandParameters</em> object instance in our FindCommand’s <em>CanExecute</em> and <em>Execute</em> methods.</p>
</div>
</div>
</div>
<br />Filed under: <a href='http://dariosantarelli.wordpress.com/category/wpf/'>WPF</a> Tagged: <a href='http://dariosantarelli.wordpress.com/tag/binding/'>Binding</a>, <a href='http://dariosantarelli.wordpress.com/tag/imultivalueconverter/'>IMultiValueConverter</a>, <a href='http://dariosantarelli.wordpress.com/tag/wpf/'>WPF</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/dariosantarelli.wordpress.com/91/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/dariosantarelli.wordpress.com/91/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/dariosantarelli.wordpress.com/91/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/dariosantarelli.wordpress.com/91/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/dariosantarelli.wordpress.com/91/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/dariosantarelli.wordpress.com/91/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/dariosantarelli.wordpress.com/91/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/dariosantarelli.wordpress.com/91/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/dariosantarelli.wordpress.com/91/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/dariosantarelli.wordpress.com/91/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/dariosantarelli.wordpress.com/91/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/dariosantarelli.wordpress.com/91/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/dariosantarelli.wordpress.com/91/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/dariosantarelli.wordpress.com/91/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dariosantarelli.wordpress.com&amp;blog=502627&amp;post=91&amp;subd=dariosantarelli&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://dariosantarelli.wordpress.com/2010/11/07/wpf-binding-multiple-command-parameters-using-multibinding/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/3805217b57ce1cc8bacbb870a2e3479f?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">dariosantarelli</media:title>
		</media:content>

		<media:content url="http://img571.imageshack.us/img571/8008/immaginexfr.jpg" medium="image" />
	</item>
		<item>
		<title>[C#] How to programmatically find a COM port by friendly name</title>
		<link>http://dariosantarelli.wordpress.com/2010/10/18/c-how-to-programmatically-find-a-com-port-by-friendly-name/</link>
		<comments>http://dariosantarelli.wordpress.com/2010/10/18/c-how-to-programmatically-find-a-com-port-by-friendly-name/#comments</comments>
		<pubDate>Mon, 18 Oct 2010 20:16:19 +0000</pubDate>
		<dc:creator>dariosantarelli</dc:creator>
				<category><![CDATA[.NET Framework]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[WMI]]></category>

		<guid isPermaLink="false">http://dariosantarelli.wordpress.com/2010/10/18/c-how-to-programmatically-find-a-com-port-by-friendly-name/</guid>
		<description><![CDATA[When using the SerialPort.GetPortNames() method, you are querying the current computer for a list of valid serial port names. For example, you can use this method to determine whether “COM1” and “COM2” are valid serial ports in your computer. The port names are obtained from the system registry (if the registry contains stale or otherwise [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dariosantarelli.wordpress.com&amp;blog=502627&amp;post=87&amp;subd=dariosantarelli&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>When using the <em><a href="http://msdn.microsoft.com/en-US/library/system.io.ports.serialport.getportnames.aspx" target="_blank">SerialPort.GetPortNames()</a></em> method, you are querying the current computer for a list of valid serial port names. For example, you can use this method to determine whether “COM1” and “COM2” are valid serial ports in your computer. The port names are obtained from the system registry (if the registry contains stale or otherwise incorrect data then this method will return incorrect data). The limit of this approach is that you get just an array of port names (e.g. { “COM1”,”COM2” … }) and nothing else! If the com ports are physical, there’s no problem but what about virtual ports connected for example through an USB adapter? Well, you can determine if a port is valid but you don&#8217;t know exactly which COM number was assigned to your device. So you need more information! In the system Device Manager, you can see the COM port friendly name under the &quot;Ports (COM &amp; LPT)&quot; heading. This means that the right COM port number can be found by using <a href="http://msdn.microsoft.com/en-us/library/aa394582(VS.85).aspx" target="_blank">WMI</a> <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />      <br />A solution to this need comes from <a href="http://www.microsoft.com/downloads/en/details.aspx?FamilyID=2cc30a64-ea15-4661-8da4-55bbc145c30e&amp;displaylang=en" target="_blank">WMI Code Creator tool</a> which allows you to generate VBScript, C#, and VB .NET code that uses WMI to complete a management task such as querying for management data, executing a method from a WMI class, or receiving event notifications using WMI.     <br />A suitable WMI query is “<em>SELECT * FROM Win32_PnPEntity WHERE ConfigManagerErrorCode = 0</em>”.     <br />Here is a code example showing how to enumerate the information of the COM ports currently available on your system (including the friendly name of course) by executing the query above.</p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><font face="Courier New"><span style="line-height:9pt;"><font color="#0000ff"><font>using</font></font></span><span style="line-height:9pt;"><font color="#000000"> System.Management;          </p>
<p></font></span></font><span lang="EN-US"><font face="Courier New"><font><span><font color="#0000ff">internal</font></span><font color="#000000"> </font><span><font color="#0000ff">class</font></span><font color="#000000"> </font></font><span><font color="#2b91af">ProcessConnection </font></span></font></span><span lang="EN-US"><font face="Courier New"><font color="#000000"><font>{           <br /></font></font></font></span></p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><span lang="EN-US"><font face="Courier New"><span><font color="#000000"><font>&#160;&#160; </font></font></span><font><span><font color="#0000ff">public</font></span><font color="#000000"> </font><span><font color="#0000ff">static</font></span><font color="#000000"> </font><span><font color="#2b91af">ConnectionOptions</font></span><font color="#000000"> ProcessConnectionOptions()</font></font></font></span></p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><span lang="EN-US"><font face="Courier New"><font color="#000000"><span><font>&#160;&#160; </font></span><font>{</font></font></font></span></p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><span lang="EN-US"><font face="Courier New"><span><font color="#000000"><font>&#160;&#160;&#160;&#160; </font></font></span><font><span><font color="#2b91af">ConnectionOptions</font></span><font color="#000000"> options = </font><span><font color="#0000ff">new</font></span><font color="#000000"> </font><span><font color="#2b91af">ConnectionOptions</font></span><font color="#000000">();</font></font></font></span></p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><span lang="EN-US"><font face="Courier New"><font color="#000000"><span><font>&#160;&#160;&#160;&#160; </font></span><font>options.Impersonation = </font></font><font><span><font color="#2b91af">ImpersonationLevel</font></span><font color="#000000">.Impersonate;</font></font></font></span></p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><span lang="EN-US"><font face="Courier New"><font color="#000000"><span><font>&#160;&#160;&#160;&#160; </font></span><font>options.Authentication = </font></font><font><span><font color="#2b91af">AuthenticationLevel</font></span><font color="#000000">.Default;</font></font></font></span></p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><span lang="EN-US"><font face="Courier New"><font color="#000000"><span><font>&#160;&#160;&#160;&#160; </font></span><font>options.EnablePrivileges = </font></font><font><span><font color="#0000ff">true</font></span><font color="#000000">;</font></font></font></span></p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><span lang="EN-US"><font face="Courier New"><span><font color="#000000"><font>&#160;&#160;&#160;&#160; </font></font></span><font><span><font color="#0000ff">return</font></span><font color="#000000"> options;</font></font></font></span></p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><span lang="EN-US"><font face="Courier New"><font color="#000000"><span><font>&#160;&#160; </font></span><font>}</font></font></font></span></p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><span lang="EN-US"><font face="Courier New"><font color="#000000">&#160;</font></font></span></p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><span lang="EN-US"><font face="Courier New"><span><font color="#000000"><font>&#160;&#160; </font></font></span><font><span><font color="#0000ff">public</font></span><font color="#000000"> </font><span><font color="#0000ff">static</font></span><font color="#000000"> </font><span><font color="#2b91af">ManagementScope</font></span><font color="#000000"> ConnectionScope(</font><span><font color="#0000ff">string</font></span><font color="#000000"> machineName, </font><span><font color="#2b91af">ConnectionOptions</font></span><font color="#000000"> options, </font><span><font color="#0000ff">string</font></span><font color="#000000"> path)</font></font></font></span></p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><span lang="EN-US"><font face="Courier New"><font color="#000000"><span><font>&#160;&#160; </font></span><font>{</font></font></font></span></p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><span lang="EN-US"><font face="Courier New"><span><font color="#000000"><font>&#160;&#160;&#160;&#160; </font></font></span><font><span><font color="#2b91af">ManagementScope</font></span><font color="#000000"> connectScope = </font><span><font color="#0000ff">new</font></span><font color="#000000"> </font><span><font color="#2b91af">ManagementScope</font></span><font color="#000000">();</font></font></font></span></p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><span lang="EN-US"><font face="Courier New"><font color="#000000"><span><font>&#160;&#160;&#160;&#160; </font></span><font>connectScope.Path = </font></font><font><span><font color="#0000ff">new</font></span><font color="#000000"> </font><span><font color="#2b91af">ManagementPath</font></span><font color="#000000">(</font><span><font color="#a31515">@&quot;\\&quot;</font></span><font color="#000000"> + machineName + path);</font></font></font></span></p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><span lang="EN-US"><font face="Courier New"><font color="#000000"><span><font>&#160;&#160;&#160;&#160; </font></span><font>connectScope.Options = options;</font></font></font></span></p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><span lang="EN-US"><font face="Courier New"><font color="#000000"><span><font>&#160;&#160;&#160;&#160; </font></span><font>connectScope.Connect();</font></font></font></span></p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><span lang="EN-US"><font face="Courier New"><span><font color="#000000"><font>&#160;&#160;&#160;&#160; </font></font></span><font><span><font color="#0000ff">return</font></span><font color="#000000"> connectScope;</font></font></font></span></p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><span lang="EN-US"><font face="Courier New"><font color="#000000"><span><font>&#160;&#160; </font></span><font>}</font></font></font></span></p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><span lang="EN-US"><font face="Courier New"><font color="#000000"><font>}</font></font></font></span></p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><span lang="EN-US"><font face="Courier New"><font color="#000000">&#160;</font></font></span></p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><span lang="EN-US"><font face="Courier New"><font><span><font color="#0000ff">public</font></span><font color="#000000"> </font><span><font color="#0000ff">class</font></span><font color="#000000"> </font></font><span><font color="#2b91af">COMPortInfo</font></span></font></span></p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><span lang="EN-US"><font face="Courier New"><font color="#000000"><font>{</font></font></font></span></p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><span lang="EN-US"><font face="Courier New"><span><font color="#000000"><font>&#160;&#160; </font></font></span><font><span><font color="#0000ff">public</font></span><font color="#000000"> </font><span><font color="#0000ff">string</font></span><font color="#000000"> Name { </font><span><font color="#0000ff">get</font></span><font color="#000000">; </font><span><font color="#0000ff">set</font></span><font color="#000000">; }</font></font></font></span></p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><span lang="EN-US"><font face="Courier New"><span><font color="#000000"><font>&#160;&#160; </font></font></span><font><span><font color="#0000ff">public</font></span><font color="#000000"> </font><span><font color="#0000ff">string</font></span><font color="#000000"> Description { </font><span><font color="#0000ff">get</font></span><font color="#000000">; </font><span><font color="#0000ff">set</font></span><font color="#000000">; }</font></font></font></span></p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><span lang="EN-US"><font face="Courier New"><font color="#000000">&#160;</font></font></span></p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><span lang="EN-US"><font face="Courier New"><span><font color="#000000"><font>&#160;&#160; </font></font></span><font><span><font color="#0000ff">public</font></span><font color="#000000"> COMPortInfo() { }</font></font><span><font color="#000000">&#160;&#160;&#160;&#160;&#160; </font></span></font></span></p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><span lang="EN-US"><font face="Courier New"><font color="#000000">&#160;</font></font></span></p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><span lang="EN-US"><font face="Courier New"><span><font color="#000000"><font>&#160;&#160; </font></font></span><font><span><font color="#0000ff">public</font></span><font color="#000000"> </font><span><font color="#0000ff">static</font></span><font color="#000000"> </font><span><font color="#2b91af">List</font></span><font color="#000000">&lt;</font><span><font color="#2b91af">COMPortInfo</font></span><font color="#000000">&gt; GetCOMPortsInfo()</font></font></font></span></p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><span lang="EN-US"><font face="Courier New"><font color="#000000"><span><font>&#160;&#160; </font></span><font>{</font></font></font></span></p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><span lang="EN-US"><font face="Courier New"><span><font color="#000000"><font>&#160;&#160;&#160;&#160; </font></font></span><font><span><font color="#2b91af">List</font></span><font color="#000000">&lt;</font><span><font color="#2b91af">COMPortInfo</font></span><font color="#000000">&gt; comPortInfoList = </font><span><font color="#0000ff">new</font></span><font color="#000000"> </font><span><font color="#2b91af">List</font></span><font color="#000000">&lt;</font><span><font color="#2b91af">COMPortInfo</font></span><font color="#000000">&gt;();</font></font></font></span></p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><span lang="EN-US"><font face="Courier New"><font color="#000000">&#160;</font></font></span></p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><span lang="EN-US"><font face="Courier New"><span><font color="#000000"><font>&#160;&#160;&#160;&#160; </font></font></span><font><span><font color="#2b91af">ConnectionOptions</font></span><font color="#000000"> options = </font><span><font color="#2b91af">ProcessConnection</font></span><font color="#000000">.ProcessConnectionOptions();</font></font></font></span></p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><span lang="EN-US"><font face="Courier New"><span><font color="#000000"><font>&#160;&#160;&#160;&#160; </font></font></span><font><span><font color="#2b91af">ManagementScope</font></span><font color="#000000"> connectionScope = </font><span><font color="#2b91af">ProcessConnection</font></span><font color="#000000">.ConnectionScope(</font><span><font color="#2b91af">Environment</font></span><font color="#000000">.MachineName, options, </font><span><font color="#a31515">@&quot;\root\CIMV2&quot;</font></span><font color="#000000">);</font></font></font></span></p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><span lang="EN-US"><font face="Courier New"><font color="#000000">&#160;</font></font></span></p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><span lang="EN-US"><font face="Courier New"><span><font color="#000000"><font>&#160;&#160;&#160;&#160; </font></font></span><font><span><font color="#2b91af">ObjectQuery</font></span><font color="#000000"> objectQuery = </font><span><font color="#0000ff">new</font></span><font color="#000000"> </font><span><font color="#2b91af">ObjectQuery</font></span><font color="#000000">(</font><span><font color="#a31515">&quot;SELECT * FROM Win32_PnPEntity WHERE ConfigManagerErrorCode = 0&quot;</font></span><font color="#000000">);</font></font></font></span></p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><span lang="EN-US"><font face="Courier New"><span><font color="#000000"><font>&#160;&#160;&#160;&#160; </font></font></span><font><span><font color="#2b91af">ManagementObjectSearcher</font></span><font color="#000000"> comPortSearcher = </font><span><font color="#0000ff">new</font></span><font color="#000000"> </font><span><font color="#2b91af">ManagementObjectSearcher</font></span><font color="#000000">(connectionScope, objectQuery);</font></font></font></span></p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><span lang="EN-US"><font face="Courier New"><font color="#000000">&#160;</font></font></span></p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><span lang="EN-US"><font face="Courier New"><span><font color="#000000"><font>&#160;&#160;&#160;&#160; </font></font></span><font><span><font color="#0000ff">using</font></span><font color="#000000"> (comPortSearcher)</font></font></font></span></p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><span lang="EN-US"><font face="Courier New"><font color="#000000"><span><font>&#160;&#160;&#160;&#160; </font></span><font>{</font></font></font></span></p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><span lang="EN-US"><font face="Courier New"><span><font color="#000000"><font>&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span><font><span><font color="#0000ff">string</font></span><font color="#000000"> caption = </font><span><font color="#0000ff">null</font></span><font color="#000000">;</font></font></font></span></p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><span lang="EN-US"><font face="Courier New"><span><font color="#000000"><font>&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span><font><span><font color="#0000ff">foreach</font></span><font color="#000000"> (</font><span><font color="#2b91af">ManagementObject</font></span><font color="#000000"> obj </font><span><font color="#0000ff">in</font></span><font color="#000000"> comPortSearcher.Get())</font></font></font></span></p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><span lang="EN-US"><font face="Courier New"><font color="#000000"><span><font>&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>{</font></font></font></span></p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><span lang="EN-US"><font face="Courier New"><font color="#000000"><span><font>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span></font><font><span><font color="#0000ff">if</font></span><font color="#000000"> (obj != </font><span><font color="#0000ff">null</font></span><font color="#000000">)</font></font></font></span></p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><span lang="EN-US"><font face="Courier New"><font color="#000000"><span><font>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>{</font></font></font></span></p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><span lang="EN-US"><font face="Courier New"><span><font color="#000000"><font>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span><font><span><font color="#0000ff">object</font></span><font color="#000000"> captionObj = obj[</font><span><font color="#a31515">&quot;Caption&quot;</font></span><font color="#000000">];</font></font></font></span></p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><span lang="EN-US"><font face="Courier New"><span><font color="#000000"><font>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span><font><span><font color="#0000ff">if</font></span><font color="#000000"> (captionObj != </font><span><font color="#0000ff">null</font></span><font color="#000000">)</font></font></font></span></p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><span lang="EN-US"><font face="Courier New"><font color="#000000"><span><font>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>{</font></font></font></span></p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><span lang="EN-US"><font face="Courier New"><font color="#000000"><span><font>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>caption = captionObj.ToString();</font></font></font></span></p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><span lang="EN-US"><font face="Courier New"><font color="#000000"><span><font>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span></font><font><span><font color="#0000ff">if</font></span><font color="#000000"> (caption.Contains(</font><span><font color="#a31515">&quot;(COM&quot;</font></span><font color="#000000">))</font></font></font></span></p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><span lang="EN-US"><font face="Courier New"><font color="#000000"><span><font>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>{</font></font></font></span></p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><span lang="EN-US"><font face="Courier New"><span><font color="#000000"><font>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span><font><span><font color="#2b91af">COMPortInfo</font></span><font color="#000000"> comPortInfo = </font><span><font color="#0000ff">new</font></span><font color="#000000"> </font><span><font color="#2b91af">COMPortInfo</font></span><font color="#000000">();</font></font></font></span></p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><span lang="EN-US"><font face="Courier New"><font color="#000000"><span><font>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>comPortInfo.Name = caption.Substring(caption.LastIndexOf(</font></font><font><span><font color="#a31515">&quot;(COM&quot;</font></span><font color="#000000">)).Replace(</font><span><font color="#a31515">&quot;(&quot;</font></span><font color="#000000">, </font><span><font color="#0000ff">string</font></span><font color="#000000">.Empty).Replace(</font><span><font color="#a31515">&quot;)&quot;</font></span><font color="#000000">,</font></font></font></span></p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><span lang="EN-US"><font face="Courier New"><font><font color="#000000">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font><span><font color="#0000ff">string</font></span><font color="#000000">.Empty);</font></font></font></span></p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><font face="Courier New"><font color="#000000"><span lang="EN-US"><span><font>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span></span><span><font>comPortInfo.Description = caption;</font></span></font></font></p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><span><font face="Courier New"><font color="#000000"><span><font>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>comPortInfoList.Add(comPortInfo);</font></font></font></span></p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><span><font face="Courier New"><font color="#000000"><span><font>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>}</font></font></font></span></p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><span><font face="Courier New"><font color="#000000"><span><font>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>}</font></font></font></span></p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><span><font face="Courier New"><font color="#000000"><span><font>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>}</font></font></font></span></p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><span><font face="Courier New"><font color="#000000"><span><font>&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>}</font></font></font></span></p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><span><font face="Courier New"><font color="#000000"><span><font>&#160;&#160;&#160;&#160; </font></span><font>}</font></font></font></span><span><font face="Courier New"><font color="#000000">&#160;</font></font></span></p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><span><font face="Courier New"><span><font color="#000000"><font>&#160;&#160;&#160;&#160; </font></font></span></font></span><span><font face="Courier New"><font><span><font color="#0000ff">return</font></span><font color="#000000"> comPortInfoList;</font></font></font></span></p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><span><font face="Courier New"><font color="#000000"><span><font>&#160;&#160; </font></span><font>}            <br /></font></font></font></span><span><font face="Courier New"><font color="#000000"><font>}</font></font></font></span></p>
<p>Finally you can easily get the com port list in this way…</p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><span><font face="Courier New"><font><span><font color="#0000ff">foreach</font></span><font color="#000000"> (</font><span><font color="#2b91af">COMPortInfo</font></span><font color="#000000"> comPort </font><span><font color="#0000ff">in</font></span><font color="#000000"> </font><span><font color="#2b91af">COMPortInfo</font></span><font color="#000000">.GetCOMPortsInfo())</font></font></font></span></p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><span><font face="Courier New"><font color="#000000"><font>{</font></font></font></span></p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><span><font face="Courier New"><font><span><font color="#2b91af"><font color="#000000">&#160; </font>Console</font></span><font color="#000000">.WriteLine(</font><span><font color="#0000ff">string</font></span><font color="#000000">.Format(</font><span><font color="#a31515">&quot;{0} &#8211; {1}&quot;</font></span><font color="#000000">, comPort.Name, comPort.Description));</font></font></font></span></p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><span><font face="Courier New"><font color="#000000"><font>}</font></font></font></span></p>
<p>Other solutions?</p>
<ul>
<li>A first alternative is <a href="http://msdn.microsoft.com/en-us/library/cc185682(VS.85).aspx" target="_blank">SetupAPI</a>. You can find a complete example <a href="http://stackoverflow.com/questions/2937585/how-to-open-a-serial-port-by-friendly-name" target="_blank">here</a>. </li>
<li>Secondly, you can try to use <a href="http://support.microsoft.com/kb/311272" target="_blank">DevCon</a> (a Microsoft tool that allows &quot;device management&quot; from the command line): you could use the
<p><a href="http://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx" target="_blank">System.Diagnostics.Process</a> class to parse the standard output of the command line “<font face="Courier New">&gt;devcon find =ports”</font>.</p>
</li>
</ul>
<br />Filed under: <a href='http://dariosantarelli.wordpress.com/category/net-framework/'>.NET Framework</a>, <a href='http://dariosantarelli.wordpress.com/category/c/'>C#</a> Tagged: <a href='http://dariosantarelli.wordpress.com/tag/wmi/'>WMI</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/dariosantarelli.wordpress.com/87/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/dariosantarelli.wordpress.com/87/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/dariosantarelli.wordpress.com/87/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/dariosantarelli.wordpress.com/87/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/dariosantarelli.wordpress.com/87/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/dariosantarelli.wordpress.com/87/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/dariosantarelli.wordpress.com/87/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/dariosantarelli.wordpress.com/87/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/dariosantarelli.wordpress.com/87/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/dariosantarelli.wordpress.com/87/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/dariosantarelli.wordpress.com/87/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/dariosantarelli.wordpress.com/87/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/dariosantarelli.wordpress.com/87/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/dariosantarelli.wordpress.com/87/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dariosantarelli.wordpress.com&amp;blog=502627&amp;post=87&amp;subd=dariosantarelli&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://dariosantarelli.wordpress.com/2010/10/18/c-how-to-programmatically-find-a-com-port-by-friendly-name/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/3805217b57ce1cc8bacbb870a2e3479f?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">dariosantarelli</media:title>
		</media:content>
	</item>
		<item>
		<title>[C#] Byte Array to Hex string</title>
		<link>http://dariosantarelli.wordpress.com/2010/10/16/c-byte-array-to-hex-string/</link>
		<comments>http://dariosantarelli.wordpress.com/2010/10/16/c-byte-array-to-hex-string/#comments</comments>
		<pubDate>Sat, 16 Oct 2010 15:09:39 +0000</pubDate>
		<dc:creator>dariosantarelli</dc:creator>
				<category><![CDATA[.NET Framework]]></category>
		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://dariosantarelli.wordpress.com/2010/10/16/c-byte-array-to-hex-string/</guid>
		<description><![CDATA[There are a lot of ways of converting a byte array to the corresponding hexadecimal string. I usually adopt the BitConverter class in order to optimize the readibility of code, but starting from the .NET Framework 3.0 the same task can be obtained using a single line of code through extensions methods: [TestMethod] public void [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dariosantarelli.wordpress.com&amp;blog=502627&amp;post=86&amp;subd=dariosantarelli&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>There are a lot of ways of converting a byte array to the corresponding hexadecimal string. I usually adopt the <a href="http://msdn.microsoft.com/en-us/library/system.bitconverter.aspx" target="_blank">BitConverter</a> class in order to optimize the readibility of code, but starting from the .NET Framework 3.0 the same task can be obtained using a single line of code through extensions methods:</p>
<p><span lang="EN-US"><font face="Courier New"><font color="#000000"><font>[</font></font><font><span><font color="#2b91af">TestMethod</font></span><font color="#000000">]            <br /></font></font></font></span><span lang="EN-US"><font face="Courier New"><font><span><font color="#0000ff">public</font></span><font color="#000000"> </font><span><font color="#0000ff">void</font></span><font color="#000000"> BitConverterVsStringConcatAndExtensionMethod()            <br /></font></font></font></span><span lang="EN-US"><font face="Courier New"><font color="#000000"><font>{            <br />&#160; </font></font></font></span><span lang="EN-US"><font face="Courier New"><font><span><font color="#0000ff">byte</font></span><font color="#000000">[] bytes = </font><span><font color="#0000ff">new</font></span><font color="#000000"> </font><span><font color="#0000ff">byte</font></span><font color="#000000">[] { 0&#215;00,0xAA,0xB0,0xC8,0&#215;99,0&#215;11,0&#215;01,0&#215;02 &#8230; };            <br />&#160; </font></font></font></span><span lang="EN-US"><font face="Courier New"><font><span><font color="#0000ff">string</font></span><font color="#000000"> expectedResult = </font><span><font color="#a31515">&quot;00AAB0C899110102&#8230;&quot;</font></span><font color="#000000">;            <br />&#160; <br /><strong>&#160; </strong></font></font></font></span><span lang="EN-US"><font face="Courier New"><font><strong><span><font color="#0000ff">string</font></span><font color="#000000"> result1 = </font><span><font color="#2b91af">BitConverter</font></span><font color="#000000">.ToString(bytes).Replace(</font><span><font color="#a31515">&quot;-&quot;</font></span><font color="#000000">,</font><span><font color="#0000ff">string</font></span></strong><font color="#000000"><strong>.Empty);              <br />&#160; <br />&#160; </strong></font></font></font></span><span lang="EN-US"><font face="Courier New"><font><strong><span><font color="#0000ff">string</font></span><font color="#000000"> result2 = </font><span><font color="#0000ff">string</font></span><font color="#000000">.Concat(bytes.Select(b =&gt; b.ToString(</font><span><font color="#a31515">&quot;X2&quot;</font></span></strong><font color="#000000"><strong>)));              <br /></strong>            <br />&#160; </font></font></font></span><span lang="EN-US"><font face="Courier New"><font><span><font color="#2b91af">Assert</font></span><font color="#000000">.AreEqual(expectedResult, result1);            <br />&#160; </font></font></font></span><span lang="EN-US"><font face="Courier New"><font><span><font color="#2b91af">Assert</font></span><font color="#000000">.AreEqual(expectedResult, result2);            <br /></font></font></font></span><font face="Courier New"><font color="#000000"><span><font>}</font></span></font></font></p>
<p>OK no performance issue has been discussed. Aren’t you satisfied? Follow this <a href="http://social.msdn.microsoft.com/forums/en-US/csharpgeneral/thread/3928b8cb-3703-4672-8ccd-33718148d1e3/" target="_blank">thread</a> !!! (4 years of discussion <img src='http://s0.wp.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> )</p>
<br />Filed under: <a href='http://dariosantarelli.wordpress.com/category/net-framework/'>.NET Framework</a>, <a href='http://dariosantarelli.wordpress.com/category/c/'>C#</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/dariosantarelli.wordpress.com/86/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/dariosantarelli.wordpress.com/86/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/dariosantarelli.wordpress.com/86/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/dariosantarelli.wordpress.com/86/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/dariosantarelli.wordpress.com/86/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/dariosantarelli.wordpress.com/86/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/dariosantarelli.wordpress.com/86/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/dariosantarelli.wordpress.com/86/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/dariosantarelli.wordpress.com/86/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/dariosantarelli.wordpress.com/86/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/dariosantarelli.wordpress.com/86/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/dariosantarelli.wordpress.com/86/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/dariosantarelli.wordpress.com/86/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/dariosantarelli.wordpress.com/86/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dariosantarelli.wordpress.com&amp;blog=502627&amp;post=86&amp;subd=dariosantarelli&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://dariosantarelli.wordpress.com/2010/10/16/c-byte-array-to-hex-string/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/3805217b57ce1cc8bacbb870a2e3479f?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">dariosantarelli</media:title>
		</media:content>
	</item>
		<item>
		<title>[ASP.NET MVC 2] Handling timeouts in asynchronous controllers</title>
		<link>http://dariosantarelli.wordpress.com/2010/10/16/asp-net-mvc-2-handling-timeouts-in-asynchronous-controllers/</link>
		<comments>http://dariosantarelli.wordpress.com/2010/10/16/asp-net-mvc-2-handling-timeouts-in-asynchronous-controllers/#comments</comments>
		<pubDate>Sat, 16 Oct 2010 13:41:45 +0000</pubDate>
		<dc:creator>dariosantarelli</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[MVC]]></category>

		<guid isPermaLink="false">http://dariosantarelli.wordpress.com/2010/10/16/asp-net-mvc-2-handling-timeouts-in-asynchronous-controllers/</guid>
		<description><![CDATA[An important feature of the ASP.NET MVC framework is the possibility of creating asynchronous controllers. As in Asynchronous Pages in ASP.NET 2.0, the aim is to avoid a “thread starvation” in your web application, preventing web clients to receive a bad 503 status code (Server too busy). In fact, when the Web Server receives a [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dariosantarelli.wordpress.com&amp;blog=502627&amp;post=85&amp;subd=dariosantarelli&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>An important feature of the ASP.NET MVC framework is the possibility of creating asynchronous controllers. As in <a href="http://msdn.microsoft.com/en-us/magazine/cc163725.aspx" target="_blank">Asynchronous Pages in ASP.NET 2.0</a>, the aim is to avoid a “thread starvation” in your web application, preventing web clients to receive a bad 503 status code (Server too busy). In fact, when the Web Server receives a request, a thread is taken from the application threadpool mantained by the .NET Framework. In a synchronous scenario, this thread lives (and can’t be reused) until all the operations complete. Well, asynchronous pipeline is better when the logic creates bottlenecks waiting for network-bound or I/O-bound operations. Considering that an asynchronous request takes the same amount of time to process as a synchronous request, minimizing the number of threads waiting for blocking operations is a good practice, particularly appreciated by your Web server when it’s bombarded by hundreds of concurrent requests. Now, have a look to this simple asynchronous controller:</p>
<p> <font face="Courier New">
<p style="line-height:normal;margin:0;" class="MsoNormal"><font face="Courier New"><span style="font-family:courier new;" lang="EN-US"><font color="#0000ff"><font>public</font></font></span><span style="font-family:courier new;" lang="EN-US"><font> <span><font color="#0000ff">class</font></span> <span><font color="#2b91af">CustomersController</font></span> : </font><span><font color="#2b91af">AsyncController</font></span></span></font></p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><span lang="EN-US"><font face="Courier New"><font>{</font></font></span></p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><span lang="EN-US"><font face="Courier New"><span><font>&#160;&#160; </font></span></font></span><span lang="EN-US"><font face="Courier New"><font>[<span><font color="#2b91af">AsyncTimeout</font></span>(10000)]</font></font></span></p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><span lang="EN-US"><font face="Courier New"><span><font>&#160;&#160; </font></span><font><span><font color="#0000ff">public</font></span> <span><font color="#0000ff">void</font></span> ListAsync()</font></font></span></p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><span lang="EN-US"><font face="Courier New"><span><font>&#160;&#160; </font></span><font>{</font></font></span></p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><span lang="EN-US"><font face="Courier New"><span><font>&#160;&#160;&#160;&#160; </font></span><font>AsyncManager.OutstandingOperations.Increment();</font></font></span></p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><span lang="EN-US"><font face="Courier New"><span><font>&#160;&#160;&#160;&#160; </font></span><font><span><font color="#2b91af">Task</font></span>.Factory.StartNew(() =&gt;</font></font></span></p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><span lang="EN-US"><font face="Courier New"><span><font>&#160;&#160;&#160;&#160; </font></span><font>{</font></font></span></p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><span lang="EN-US"><font face="Courier New"><span><font>&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font><span><font color="#0000ff">try</font></span> { AsyncManager.Parameters[<span><font color="#a31515">&quot;result&quot;</font></span>] = <span><font color="#0000ff">new</font></span> <span><font color="#2b91af">MyServiceClient</font></span>().GetCustomers(); }</font></font></span></p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><span lang="EN-US"><font face="Courier New"><span><font>&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font><span><font color="#0000ff">catch</font></span> (<span><font color="#2b91af">Exception</font></span> ex) { &#8230; }</font></font></span></p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><span lang="EN-US"><font face="Courier New"><span><font>&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font><span><font color="#0000ff">finally</font></span> { AsyncManager.OutstandingOperations.Decrement(); }</font></font></span></p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><span lang="EN-US"><font face="Courier New"><span><font>&#160;&#160;&#160;&#160; </font></span><font>);</font></font></span></p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><span lang="EN-US"><font face="Courier New"><span><font>&#160;&#160; </font></span><font>}</font></font></span></p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><span lang="EN-US"><font face="Courier New"><font>&#160;</font></font></span></p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><span lang="EN-US"><font face="Courier New"><span><font>&#160;&#160; </font></span><font><span><font color="#0000ff">public</font></span> <span><font color="#2b91af">ActionResult</font></span> ListCompleted(<span><font color="#2b91af">List</font></span>&lt;<span><font color="#2b91af">Customer</font></span>&gt; result)</font></font></span></p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><span lang="EN-US"><font face="Courier New"><span><font>&#160;&#160; </font></span><font>{</font></font></span></p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><span lang="EN-US"><font face="Courier New"><span><font>&#160;&#160;&#160;&#160; </font></span><font><span><font color="#0000ff">return</font></span> View(<span><font color="#a31515">&quot;List&quot;</font></span>, result);</font></font></span></p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><span lang="EN-US"><font face="Courier New"><span><font>&#160;&#160; </font></span><font>}            </p>
<p>&#160;&#160; &#8230;</font></font></span></p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><span lang="EN-US"><span><font face="Courier New"><font>&#160;&#160;&#160;&#160;&#160;&#160; </font></font></span></span></p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><span lang="EN-US"><font face="Courier New"><span><font>&#160;&#160; </font></span><font><span><font color="#0000ff">protected</font></span> <span><font color="#0000ff">override</font></span> <span><font color="#0000ff">void</font></span> OnException(<span><font color="#2b91af">ExceptionContext</font></span> filterContext)</font></font></span></p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><span lang="EN-US"><font face="Courier New"><span><font>&#160;&#160; </font></span><font>{</font></font></span></p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><span lang="EN-US"><font face="Courier New"><span><font>&#160;&#160;&#160;&#160; </font></span><font><span><font color="#0000ff">if</font></span> (filterContext.Exception <span><font color="#0000ff">is</font></span> <span><font color="#2b91af">TimeoutException</font></span>)</font></font></span></p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><span lang="EN-US"><font face="Courier New"><span><font>&#160;&#160;&#160;&#160; </font></span><font>{</font></font></span></p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><span lang="EN-US"><font face="Courier New"><span><font>&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>filterContext.Result = RedirectToAction(<span><font color="#a31515">&quot;TryAgainLater&quot;</font></span>);</font></font></span></p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><span lang="EN-US"><font face="Courier New"><span><font>&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>filterContext.ExceptionHandled = <span><font color="#0000ff">true</font></span>;</font></font></span></p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><span lang="EN-US"><font face="Courier New"><span><font>&#160;&#160;&#160;&#160; </font></span><font>}</font><span><font>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span></font></span></p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><span lang="EN-US"><font face="Courier New"><span><font>&#160;&#160;&#160;&#160; </font></span><font><span><font color="#0000ff">base</font></span>.OnException(filterContext);</font></font></span></p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><span lang="EN-US"><font face="Courier New"><span><font>&#160;&#160; </font></span><font>}</font></font></span><span lang="EN-US"><span><font face="Courier New"><font>&#160;&#160;&#160; </font></font></span></span></p>
<p style="line-height:normal;margin:0;" class="MsoNormal"><font face="Courier New"><span><font>}</font></span></font></p>
<p> </font>
<p>By default, ASP.NET MVC won’t call the <em>ListCompleted</em> method until the <a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.async.asyncmanager.aspx" target="_blank">AsyncManager</a> associated with the request says that there is no outstanding asynchronous operations. But it’s possible that one or more asynchronous operations might never complete!!! Moreover, if the callback for one of your asynchronous operations throws an exception before it calls the <em>AsyncManager.OutstandingOperations.Decrement() </em>method,<em> </em>the request will keep waiting a decrement until it times out! So, putting the <em>AsyncManager.OutstandingOperations.Decrement()</em> call inside a <em>finally</em> block would be fine <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .    <br />The AsyncManager object has a built-in default timeout set to 45 seconds, so if the count of outstanding operations doesn’t reach zero after this long, the framework will throw a <em>System.TimeOutException</em> to abort the request. If you want to set a different timeout you can use the <a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.asynctimeoutattribute.aspx" target="_blank">AsyncTimeout</a> filter for specifying a different duration. If you want to allow asynchronous operations to run for an unlimited period, then use the <a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.noasynctimeoutattribute.aspx" target="_blank">NoAsyncTimeout</a> filter instead.</p>
<p>Finally, we have to say that most applications will have an ASP.NET global exception handler that will deal with timeout exceptions in the same way as other unhandled exceptions. But if you want to treat timeouts in a custom way, providing a different feedback to the user, you can create your own exception filter or you can override the controller’s <em><a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.controller.onexception.aspx" target="_blank">OnException()</a></em> method (e.g. to redirect users to a special “Try again later” page).</p>
<br />Filed under: <a href='http://dariosantarelli.wordpress.com/category/asp-net/'>ASP.NET</a> Tagged: <a href='http://dariosantarelli.wordpress.com/tag/asp-net/'>ASP.NET</a>, <a href='http://dariosantarelli.wordpress.com/tag/mvc/'>MVC</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/dariosantarelli.wordpress.com/85/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/dariosantarelli.wordpress.com/85/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/dariosantarelli.wordpress.com/85/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/dariosantarelli.wordpress.com/85/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/dariosantarelli.wordpress.com/85/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/dariosantarelli.wordpress.com/85/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/dariosantarelli.wordpress.com/85/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/dariosantarelli.wordpress.com/85/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/dariosantarelli.wordpress.com/85/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/dariosantarelli.wordpress.com/85/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/dariosantarelli.wordpress.com/85/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/dariosantarelli.wordpress.com/85/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/dariosantarelli.wordpress.com/85/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/dariosantarelli.wordpress.com/85/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dariosantarelli.wordpress.com&amp;blog=502627&amp;post=85&amp;subd=dariosantarelli&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://dariosantarelli.wordpress.com/2010/10/16/asp-net-mvc-2-handling-timeouts-in-asynchronous-controllers/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/3805217b57ce1cc8bacbb870a2e3479f?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">dariosantarelli</media:title>
		</media:content>
	</item>
		<item>
		<title>Web Development Helper</title>
		<link>http://dariosantarelli.wordpress.com/2008/06/23/web-development-helper/</link>
		<comments>http://dariosantarelli.wordpress.com/2008/06/23/web-development-helper/#comments</comments>
		<pubDate>Mon, 23 Jun 2008 13:22:52 +0000</pubDate>
		<dc:creator>dariosantarelli</dc:creator>
				<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://dariosantarelli.wordpress.com/2008/06/23/web-development-helper/</guid>
		<description><![CDATA[Web Development Helper is a free browser extension for Internet Explorer that provides a set of tools and utilities for the Web developer, esp. Ajax and ASP.NET developers. The tool provides features such as a DOM inspector, an HTTP tracing tool, and script diagnostics and immediate window. Web Development Helper works against IE6+, and requires [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dariosantarelli.wordpress.com&amp;blog=502627&amp;post=72&amp;subd=dariosantarelli&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<blockquote><p><a href="http://projects.nikhilk.net/WebDevHelper/Default.aspx" target="_blank"><strong>Web Development Helper</strong></a> is a free browser extension for Internet Explorer that provides a set of tools and utilities for the Web developer, esp. Ajax and ASP.NET developers. The tool provides features such as a <strong>DOM inspector</strong>, an <strong>HTTP tracing tool</strong>, and <strong>script diagnostics</strong> and <strong>immediate window</strong>. Web Development Helper works against <strong>IE6+</strong>, and <strong>requires the .NET Framework 2.0 or greater</strong> to be installed on the machine.</p>
</blockquote>
<p>Very cool <img src='http://s1.wp.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/dariosantarelli.wordpress.com/72/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/dariosantarelli.wordpress.com/72/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/dariosantarelli.wordpress.com/72/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/dariosantarelli.wordpress.com/72/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/dariosantarelli.wordpress.com/72/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/dariosantarelli.wordpress.com/72/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/dariosantarelli.wordpress.com/72/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/dariosantarelli.wordpress.com/72/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/dariosantarelli.wordpress.com/72/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/dariosantarelli.wordpress.com/72/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/dariosantarelli.wordpress.com/72/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/dariosantarelli.wordpress.com/72/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/dariosantarelli.wordpress.com/72/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/dariosantarelli.wordpress.com/72/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/dariosantarelli.wordpress.com/72/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/dariosantarelli.wordpress.com/72/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dariosantarelli.wordpress.com&amp;blog=502627&amp;post=72&amp;subd=dariosantarelli&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://dariosantarelli.wordpress.com/2008/06/23/web-development-helper/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/3805217b57ce1cc8bacbb870a2e3479f?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">dariosantarelli</media:title>
		</media:content>
	</item>
		<item>
		<title>Easyfrom Database Convert</title>
		<link>http://dariosantarelli.wordpress.com/2008/06/22/easyfrom-database-convert/</link>
		<comments>http://dariosantarelli.wordpress.com/2008/06/22/easyfrom-database-convert/#comments</comments>
		<pubDate>Sun, 22 Jun 2008 18:10:04 +0000</pubDate>
		<dc:creator>dariosantarelli</dc:creator>
				<category><![CDATA[Blogroll]]></category>

		<guid isPermaLink="false">http://dariosantarelli.wordpress.com/2008/06/22/easyfrom-database-convert/</guid>
		<description><![CDATA[If you are migrating your database across different platforms or applications, then you know that it can not be done by simple copy-and-paste operations. To forget about difficulties associated with database conversion, you should try ESF Database Convert. This wizard-based tool addresses almost any database conversion need. Advanced converting mechanisms of the tool provide smooth [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dariosantarelli.wordpress.com&amp;blog=502627&amp;post=71&amp;subd=dariosantarelli&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<blockquote><p>If you are <strong>migrating your database across different platforms or applications</strong>, then you know that it can not be done by simple copy-and-paste operations. To forget about difficulties associated with database conversion, you should try <strong><a href="http://www.easyfrom.net/" target="_blank">ESF Database Convert</a></strong>. This wizard-based tool addresses almost any database conversion need. Advanced converting mechanisms of the tool provide smooth conversion directly from/to any of the following database formats: <strong>Oracle, MySQL, SQL Server, PostgreSQL, Visual Foxpro, FireBird, InterBase, Access, Excel, Paradox, Lotus, dBase, Text and others(e.g.: Access to Oracle, Oracle to SQL Server, SQL Server to MySQL, MySQL to PostgreSQL&#8230;)</strong>. Also you can convert any format of a database with ODBC DSN. <br /><em>ESF Database Convert</em> includes the support of table CLOB/BLOB, Primary/Foreign Keys, Indexes, Auto-ID and maps table and field names/types in converting. It provides all the required conversion options, taking into account the peculiarities of both input and output database formats. You can convert data exactly the way you want it. <br />The tool comes with the batch conversion mode that can enhance productivity by speeding up the entire conversion process. Our users regularly convert multi-million records databases using our software.</p>
</blockquote>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/dariosantarelli.wordpress.com/71/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/dariosantarelli.wordpress.com/71/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/dariosantarelli.wordpress.com/71/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/dariosantarelli.wordpress.com/71/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/dariosantarelli.wordpress.com/71/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/dariosantarelli.wordpress.com/71/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/dariosantarelli.wordpress.com/71/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/dariosantarelli.wordpress.com/71/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/dariosantarelli.wordpress.com/71/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/dariosantarelli.wordpress.com/71/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/dariosantarelli.wordpress.com/71/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/dariosantarelli.wordpress.com/71/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/dariosantarelli.wordpress.com/71/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/dariosantarelli.wordpress.com/71/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/dariosantarelli.wordpress.com/71/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/dariosantarelli.wordpress.com/71/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dariosantarelli.wordpress.com&amp;blog=502627&amp;post=71&amp;subd=dariosantarelli&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://dariosantarelli.wordpress.com/2008/06/22/easyfrom-database-convert/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/3805217b57ce1cc8bacbb870a2e3479f?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">dariosantarelli</media:title>
		</media:content>
	</item>
		<item>
		<title>Programming languages history</title>
		<link>http://dariosantarelli.wordpress.com/2008/06/22/programming-languages-history/</link>
		<comments>http://dariosantarelli.wordpress.com/2008/06/22/programming-languages-history/#comments</comments>
		<pubDate>Sun, 22 Jun 2008 17:55:39 +0000</pubDate>
		<dc:creator>dariosantarelli</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://dariosantarelli.wordpress.com/2008/06/22/programming-languages-history/</guid>
		<description><![CDATA[Here you can find an interesting diagram of programming languages history. Years go by, but surprisely you can see how apparently incompatible paths (OO and functional programming) are slowly fusing in time.&#160; For about 50 years, computer programmers have been writing code. New technologies continue to emerge, develop, and mature. Now there are more than [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dariosantarelli.wordpress.com&amp;blog=502627&amp;post=70&amp;subd=dariosantarelli&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><a href="http://merd.sourceforge.net/pixel/language-study/diagram.html" target="_blank"><strong>Here</strong></a> you can find an interesting diagram of <strong>programming languages history</strong>. Years go by, but surprisely you can see how apparently incompatible paths (OO and functional programming) are slowly fusing in time.&nbsp; For about 50 years, computer programmers have been writing code. New technologies continue to emerge, develop, and mature. <strong>Now there are more than 2,500 documented programming languages!</strong> </p>
<p>Here a preview <img src='http://s1.wp.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p><a href="http://people.mandriva.com/~prigaux/language-study/diagram.png"><img alt="diagram" src="http://people.mandriva.com/~prigaux/language-study/diagram-thumbnail.png"></a></p>
<p>Moreover, O&#8217;Reilly has produced a poster called <a href="http://www.oreilly.com/news/graphics/prog_lang_poster.pdf">History of Programming Languages</a>&nbsp; which plots over 50 programming languages on a multi-layered, color-coded timeline. </p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/dariosantarelli.wordpress.com/70/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/dariosantarelli.wordpress.com/70/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/dariosantarelli.wordpress.com/70/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/dariosantarelli.wordpress.com/70/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/dariosantarelli.wordpress.com/70/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/dariosantarelli.wordpress.com/70/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/dariosantarelli.wordpress.com/70/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/dariosantarelli.wordpress.com/70/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/dariosantarelli.wordpress.com/70/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/dariosantarelli.wordpress.com/70/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/dariosantarelli.wordpress.com/70/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/dariosantarelli.wordpress.com/70/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/dariosantarelli.wordpress.com/70/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/dariosantarelli.wordpress.com/70/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/dariosantarelli.wordpress.com/70/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/dariosantarelli.wordpress.com/70/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dariosantarelli.wordpress.com&amp;blog=502627&amp;post=70&amp;subd=dariosantarelli&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://dariosantarelli.wordpress.com/2008/06/22/programming-languages-history/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/3805217b57ce1cc8bacbb870a2e3479f?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">dariosantarelli</media:title>
		</media:content>

		<media:content url="http://people.mandriva.com/~prigaux/language-study/diagram-thumbnail.png" medium="image">
			<media:title type="html">diagram</media:title>
		</media:content>
	</item>
		<item>
		<title>Silverlight Spy</title>
		<link>http://dariosantarelli.wordpress.com/2008/05/09/silverlight-spy/</link>
		<comments>http://dariosantarelli.wordpress.com/2008/05/09/silverlight-spy/#comments</comments>
		<pubDate>Fri, 09 May 2008 08:06:15 +0000</pubDate>
		<dc:creator>dariosantarelli</dc:creator>
				<category><![CDATA[Silverlight]]></category>

		<guid isPermaLink="false">http://dariosantarelli.wordpress.com/2008/05/09/silverlight-spy/</guid>
		<description><![CDATA[Silverlight Spy provides detailed XAML inspection of any Silverlight application. It allows you to explore the UI element tree, monitor events, extract XAML, get and set object properties, view statistics and more. Nowadays it supports Silverlight 2.0 (Beta 1).You can download it here.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dariosantarelli.wordpress.com&amp;blog=502627&amp;post=69&amp;subd=dariosantarelli&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><a href="http://firstfloorsoftware.com/SilverlightSpy/" target="_blank">Silverlight Spy</a> provides detailed XAML inspection of any Silverlight application. It allows you to explore the UI element tree, monitor events, extract XAML, get and set object properties, view statistics and more. Nowadays it supports <strong>Silverlight 2.0 (Beta 1)</strong>.<br />You can download it <a href="http://firstfloorsoftware.com/silverlightspy/download-silverlight-spy/" target="_blank"><strong>here</strong></a>.</p>
<p><a href="http://firstfloorsoftware.com/silverlightspy/" target="_blank"><img height="172" src="http://lh6.ggpht.com/NowSense/SBRpBVnQ7mI/AAAAAAAAAaY/xhiEzCx-XJQ/s1600/SilverlightSpy22.jpg" width="280"></a></p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/dariosantarelli.wordpress.com/69/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/dariosantarelli.wordpress.com/69/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/dariosantarelli.wordpress.com/69/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/dariosantarelli.wordpress.com/69/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/dariosantarelli.wordpress.com/69/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/dariosantarelli.wordpress.com/69/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/dariosantarelli.wordpress.com/69/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/dariosantarelli.wordpress.com/69/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/dariosantarelli.wordpress.com/69/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/dariosantarelli.wordpress.com/69/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/dariosantarelli.wordpress.com/69/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/dariosantarelli.wordpress.com/69/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/dariosantarelli.wordpress.com/69/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/dariosantarelli.wordpress.com/69/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/dariosantarelli.wordpress.com/69/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/dariosantarelli.wordpress.com/69/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dariosantarelli.wordpress.com&amp;blog=502627&amp;post=69&amp;subd=dariosantarelli&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://dariosantarelli.wordpress.com/2008/05/09/silverlight-spy/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/3805217b57ce1cc8bacbb870a2e3479f?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">dariosantarelli</media:title>
		</media:content>

		<media:content url="http://lh6.ggpht.com/NowSense/SBRpBVnQ7mI/AAAAAAAAAaY/xhiEzCx-XJQ/s1600/SilverlightSpy22.jpg" medium="image" />
	</item>
		<item>
		<title>.NET 3.5 Enhancements Training Kit</title>
		<link>http://dariosantarelli.wordpress.com/2008/04/16/net-35-enhancements-training-kit/</link>
		<comments>http://dariosantarelli.wordpress.com/2008/04/16/net-35-enhancements-training-kit/#comments</comments>
		<pubDate>Wed, 16 Apr 2008 18:49:01 +0000</pubDate>
		<dc:creator>dariosantarelli</dc:creator>
				<category><![CDATA[.NET Framework]]></category>
		<category><![CDATA[AJAX]]></category>
		<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[Microsoft Technology]]></category>
		<category><![CDATA[Web 2.0]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://dariosantarelli.wordpress.com/2008/04/16/net-35-enhancements-training-kit/</guid>
		<description><![CDATA[I was looking for some resources in order to learning something more about the .NET 3.5 extensions&#8230; after a little search I&#8217;ve discovered that just few days ago Microsoft has published the .NET 3.5 Enhancements Training Kit: The .NET Framework 3.5 Enhancements Training Kit includes presentations, hands-on labs, and demos. This content is designed to [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dariosantarelli.wordpress.com&amp;blog=502627&amp;post=68&amp;subd=dariosantarelli&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I was looking for some resources in order to learning something more about the .NET 3.5 extensions&#8230; after a little search I&#8217;ve discovered that just few days ago Microsoft has published the <strong>.NET 3.5 Enhancements Training Kit:</strong></p>
<blockquote><p>The .NET Framework 3.5 Enhancements Training Kit includes presentations, hands-on labs, and demos. This content is designed to help you learn how to utilize the .NET 3.5 Enhancement features including: ASP.NET MVC, ASP.NET Dynamic Data, ASP.NET AJAX History, ASP.NET Silverlight controls, ADO.NET Data Services and ADO.NET Entity Framework.</p>
</blockquote>
<p>What can I say&#8230; Happy <a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=355c80e9-fde0-4812-98b5-8a03f5874e96&amp;displaylang=en" target="_blank"><strong>download</strong></a></p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/dariosantarelli.wordpress.com/68/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/dariosantarelli.wordpress.com/68/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/dariosantarelli.wordpress.com/68/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/dariosantarelli.wordpress.com/68/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/dariosantarelli.wordpress.com/68/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/dariosantarelli.wordpress.com/68/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/dariosantarelli.wordpress.com/68/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/dariosantarelli.wordpress.com/68/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/dariosantarelli.wordpress.com/68/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/dariosantarelli.wordpress.com/68/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/dariosantarelli.wordpress.com/68/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/dariosantarelli.wordpress.com/68/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/dariosantarelli.wordpress.com/68/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/dariosantarelli.wordpress.com/68/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/dariosantarelli.wordpress.com/68/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/dariosantarelli.wordpress.com/68/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dariosantarelli.wordpress.com&amp;blog=502627&amp;post=68&amp;subd=dariosantarelli&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://dariosantarelli.wordpress.com/2008/04/16/net-35-enhancements-training-kit/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/3805217b57ce1cc8bacbb870a2e3479f?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">dariosantarelli</media:title>
		</media:content>
	</item>
		<item>
		<title>FluidKit for WPF</title>
		<link>http://dariosantarelli.wordpress.com/2008/03/17/fluidkit-for-wpf/</link>
		<comments>http://dariosantarelli.wordpress.com/2008/03/17/fluidkit-for-wpf/#comments</comments>
		<pubDate>Mon, 17 Mar 2008 15:40:51 +0000</pubDate>
		<dc:creator>dariosantarelli</dc:creator>
				<category><![CDATA[.NET Framework]]></category>
		<category><![CDATA[Microsoft Technology]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://dariosantarelli.wordpress.com/2008/03/17/fluidkit-for-wpf/</guid>
		<description><![CDATA[On CodePlex you can find FluidKit, a WPF library containing a powerhouse of controls, frameworks, helpers, tools, etc. for productive WPF development. Here is the introductory blog post.Available controls: ImageButton DragDropManager GlassWindow BalloonDecorator ItemSkimmingPanel + SkimmingContextAdorner PennerDoubleAnimation ElementFlow ( Very&#160; powerful ): allows you to display your items in different carousel like modes<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dariosantarelli.wordpress.com&amp;blog=502627&amp;post=67&amp;subd=dariosantarelli&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>On CodePlex you can find <a href="http://www.codeplex.com/fluidkit" target="_blank">FluidKit</a>, a WPF library containing a powerhouse of controls, frameworks, helpers, tools, etc. for productive WPF development. Here is the introductory <a href="http://blog.pixelingene.com/?p=137">blog post</a>.<br />Available controls:</p>
<ul>
<li>ImageButton </li>
<li>DragDropManager
<li>GlassWindow
<li>BalloonDecorator
<li>ItemSkimmingPanel + SkimmingContextAdorner
<li>PennerDoubleAnimation
<li><b>ElementFlow</b> <em>( Very&nbsp; powerful ): </em>allows you to display your items in different carousel like modes</li>
</ul>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/dariosantarelli.wordpress.com/67/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/dariosantarelli.wordpress.com/67/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/dariosantarelli.wordpress.com/67/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/dariosantarelli.wordpress.com/67/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/dariosantarelli.wordpress.com/67/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/dariosantarelli.wordpress.com/67/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/dariosantarelli.wordpress.com/67/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/dariosantarelli.wordpress.com/67/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/dariosantarelli.wordpress.com/67/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/dariosantarelli.wordpress.com/67/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/dariosantarelli.wordpress.com/67/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/dariosantarelli.wordpress.com/67/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/dariosantarelli.wordpress.com/67/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/dariosantarelli.wordpress.com/67/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/dariosantarelli.wordpress.com/67/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/dariosantarelli.wordpress.com/67/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dariosantarelli.wordpress.com&amp;blog=502627&amp;post=67&amp;subd=dariosantarelli&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://dariosantarelli.wordpress.com/2008/03/17/fluidkit-for-wpf/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/3805217b57ce1cc8bacbb870a2e3479f?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">dariosantarelli</media:title>
		</media:content>
	</item>
		<item>
		<title>Installing Enterprise Library 3.1 in VS2008</title>
		<link>http://dariosantarelli.wordpress.com/2008/03/14/installing-enterprise-library-31-in-vs2008/</link>
		<comments>http://dariosantarelli.wordpress.com/2008/03/14/installing-enterprise-library-31-in-vs2008/#comments</comments>
		<pubDate>Fri, 14 Mar 2008 19:27:34 +0000</pubDate>
		<dc:creator>dariosantarelli</dc:creator>
				<category><![CDATA[.NET Framework]]></category>
		<category><![CDATA[Microsoft Technology]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://dariosantarelli.wordpress.com/2008/03/14/installing-enterprise-library-31-in-vs2008/</guid>
		<description><![CDATA[The patterns &#38; practices Enterprise Library 3.1 is a library of application blocks published for Visual Studio 2005 and designed to assist developers with common enterprise development challenges. Application blocks are a type of guidance, provided as source code that can be used &#8220;as is,&#8221; extended, or modified by developers to use on enterprise development [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dariosantarelli.wordpress.com&amp;blog=502627&amp;post=66&amp;subd=dariosantarelli&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><a name="Description"></a>The <a href="http://www.codeplex.com/entlib" target="_blank"><strong>patterns &amp; practices Enterprise Library 3.1</strong></a> is a library of application blocks published for Visual Studio 2005 and designed to assist developers with common enterprise development challenges. Application blocks are a type of guidance, provided as source code that can be used &#8220;as is,&#8221; extended, or modified by developers to use on enterprise development projects. This release of Enterprise Library includes application blocks for <strong>Caching</strong>, <strong>Cryptography</strong>, <strong>Data Access</strong>, <strong>Exception Handling</strong>, <strong>Logging</strong>, <strong>Policy Injection</strong>, <strong>Security</strong> and <strong>Validation</strong>.<br />In <a href="http://blogs.msdn.com/scottdensmore/archive/2008/03/13/how-to-get-enterprise-library-3-1-working-in-vs-2008.aspx" target="_blank">this post</a> you can find how to to get the integrated tool to work in Visual Studio 2008. Simply you have to run a registry script that will change the keys where VS looks to load the integrated tool package. After you run the script, you will need to run <em>devenv /setup</em> from the Visual Studio 2008 command prompt. You can download the file from the <a href="http://www.codeplex.com/Release/ProjectReleases.aspx?ProjectName=entlibcontrib&amp;ReleaseId=11669" target="_blank">EntLibContrib project</a> on CodePlex.
<p>P.S.: If you already have the Enterprise Library 3.0 installed, you must uninstall it before installing the Enterprise Library 3.1. </p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/dariosantarelli.wordpress.com/66/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/dariosantarelli.wordpress.com/66/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/dariosantarelli.wordpress.com/66/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/dariosantarelli.wordpress.com/66/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/dariosantarelli.wordpress.com/66/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/dariosantarelli.wordpress.com/66/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/dariosantarelli.wordpress.com/66/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/dariosantarelli.wordpress.com/66/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/dariosantarelli.wordpress.com/66/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/dariosantarelli.wordpress.com/66/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/dariosantarelli.wordpress.com/66/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/dariosantarelli.wordpress.com/66/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/dariosantarelli.wordpress.com/66/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/dariosantarelli.wordpress.com/66/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/dariosantarelli.wordpress.com/66/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/dariosantarelli.wordpress.com/66/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dariosantarelli.wordpress.com&amp;blog=502627&amp;post=66&amp;subd=dariosantarelli&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://dariosantarelli.wordpress.com/2008/03/14/installing-enterprise-library-31-in-vs2008/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/3805217b57ce1cc8bacbb870a2e3479f?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">dariosantarelli</media:title>
		</media:content>
	</item>
		<item>
		<title>LINQ to Objects in .NET 2.0/3.0 projects</title>
		<link>http://dariosantarelli.wordpress.com/2008/03/01/linq-to-objects-in-net-2030-projects/</link>
		<comments>http://dariosantarelli.wordpress.com/2008/03/01/linq-to-objects-in-net-2030-projects/#comments</comments>
		<pubDate>Sat, 01 Mar 2008 11:25:52 +0000</pubDate>
		<dc:creator>dariosantarelli</dc:creator>
				<category><![CDATA[.NET Framework]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Microsoft Technology]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://dariosantarelli.wordpress.com/2008/03/01/linq-to-objects-in-net-2030-projects/</guid>
		<description><![CDATA[LINQ&#8217;s query operators are implemented in .NET Framework 3.5. And here lies a difficulty: you might be unsuccessful in demanding that all your customers install Framework 3.5 right away. So what does this mean if you want to code in C# 3.0 and write LINQ queries?You can use LINQBridge, a reimplementation of all the standard [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dariosantarelli.wordpress.com&amp;blog=502627&amp;post=65&amp;subd=dariosantarelli&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>LINQ&#8217;s query operators are implemented in .NET Framework 3.5. And here lies a difficulty: you might be unsuccessful in demanding that all your customers install Framework 3.5 right away. So what does this mean if you want to code in C# 3.0 and write LINQ queries?<br />You can use<strong> </strong><a href="http://www.albahari.com/nutshell/linqbridge.html" target="_blank"><strong>LINQBridge</strong></a>, a reimplementation of all the standard query operators in Framework 3.5&#8242;s Enumerable class by <a href="http://www.albahari.com/">Joseph Albahari</a>. </p>
<ul>
<li>LINQBridge is designed to work with the C# 3.0 compiler, as used by Visual Studio 2008. </li>
<li>LINQBridge comprises a &#8220;LINQ to Objects&#8221; API for running local queries. (It doesn&#8217;t include an implementation of LINQ to SQL, nor LINQ to XML). </li>
<li>LINQBridge also includes Framework 3.5&#8242;s generic Func and Action delegates, as well as ExtensionAttribute, allowing you to use C# 3.0&#8242;s extension methods in Framework 2.0. In fact </li>
<li>LINQBridge lets you use nearly all of the features in C# 3.0 with Framework 2.0—including extension methods, lambda functions and query comprehensions. The only feature it does not support is compiling lambdas to expression trees (i.e., Expression&lt;TDelegate&gt;). </li>
</ul>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/dariosantarelli.wordpress.com/65/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/dariosantarelli.wordpress.com/65/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/dariosantarelli.wordpress.com/65/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/dariosantarelli.wordpress.com/65/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/dariosantarelli.wordpress.com/65/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/dariosantarelli.wordpress.com/65/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/dariosantarelli.wordpress.com/65/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/dariosantarelli.wordpress.com/65/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/dariosantarelli.wordpress.com/65/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/dariosantarelli.wordpress.com/65/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/dariosantarelli.wordpress.com/65/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/dariosantarelli.wordpress.com/65/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/dariosantarelli.wordpress.com/65/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/dariosantarelli.wordpress.com/65/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/dariosantarelli.wordpress.com/65/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/dariosantarelli.wordpress.com/65/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dariosantarelli.wordpress.com&amp;blog=502627&amp;post=65&amp;subd=dariosantarelli&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://dariosantarelli.wordpress.com/2008/03/01/linq-to-objects-in-net-2030-projects/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/3805217b57ce1cc8bacbb870a2e3479f?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">dariosantarelli</media:title>
		</media:content>
	</item>
		<item>
		<title>.NET Reference Source project</title>
		<link>http://dariosantarelli.wordpress.com/2008/01/20/net-reference-source-project/</link>
		<comments>http://dariosantarelli.wordpress.com/2008/01/20/net-reference-source-project/#comments</comments>
		<pubDate>Sun, 20 Jan 2008 19:30:42 +0000</pubDate>
		<dc:creator>dariosantarelli</dc:creator>
				<category><![CDATA[.NET Framework]]></category>
		<category><![CDATA[Microsoft Technology]]></category>

		<guid isPermaLink="false">http://dariosantarelli.wordpress.com/2008/01/20/net-reference-source-project/</guid>
		<description><![CDATA[In his blog, Scott Guthrie announces the ability for .NET developers to download and browse the source code of the .NET Framework libraries, and to easily enable debugging support in them. Specifically, now we can browse and debug the source code for the following libraries: .NET Base Class Libraries (including System, System.CodeDom, System.Collections, System.ComponentModel, System.Diagnostics, [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dariosantarelli.wordpress.com&amp;blog=502627&amp;post=64&amp;subd=dariosantarelli&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>In his blog, <a href="http://weblogs.asp.net/scottgu" target="_blank">Scott Guthrie</a> announces the ability for .NET developers to download and browse the source code of the .NET Framework libraries, and to easily enable debugging support in them. Specifically, now we can browse and debug the source code for the following libraries:</p>
<ul>
<li>.NET Base Class Libraries (including System, System.CodeDom, System.Collections, System.ComponentModel, System.Diagnostics, System.Drawing, System.Globalization, System.IO, System.Net, System.Reflection, System.Runtime, System.Security, System.Text, System.Threading, etc). </li>
<li>ASP.NET (System.Web, System.Web.Extensions) </li>
<li>Windows Forms (System.Windows.Forms) </li>
<li>Windows Presentation Foundation (System.Windows) </li>
<li>ADO.NET and XML (System.Data and System.Xml) </li>
</ul>
<p>Moreover, in this <a href="http://blogs.msdn.com/sburke/archive/2008/01/16/configuring-visual-studio-to-debug-net-framework-source-code.aspx" target="_blank">great post</a>, Shawn Burke shows us how to configure Visual Studio 2008 to Debug .NET Framework Source Code&#8230;. VERY INTERESTING!</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/dariosantarelli.wordpress.com/64/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/dariosantarelli.wordpress.com/64/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/dariosantarelli.wordpress.com/64/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/dariosantarelli.wordpress.com/64/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/dariosantarelli.wordpress.com/64/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/dariosantarelli.wordpress.com/64/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/dariosantarelli.wordpress.com/64/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/dariosantarelli.wordpress.com/64/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/dariosantarelli.wordpress.com/64/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/dariosantarelli.wordpress.com/64/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/dariosantarelli.wordpress.com/64/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/dariosantarelli.wordpress.com/64/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/dariosantarelli.wordpress.com/64/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/dariosantarelli.wordpress.com/64/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/dariosantarelli.wordpress.com/64/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/dariosantarelli.wordpress.com/64/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dariosantarelli.wordpress.com&amp;blog=502627&amp;post=64&amp;subd=dariosantarelli&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://dariosantarelli.wordpress.com/2008/01/20/net-reference-source-project/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/3805217b57ce1cc8bacbb870a2e3479f?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">dariosantarelli</media:title>
		</media:content>
	</item>
		<item>
		<title>Vista/XP Virtual Desktop Manager</title>
		<link>http://dariosantarelli.wordpress.com/2008/01/19/vistaxp-virtual-desktop-manager/</link>
		<comments>http://dariosantarelli.wordpress.com/2008/01/19/vistaxp-virtual-desktop-manager/#comments</comments>
		<pubDate>Sat, 19 Jan 2008 13:55:45 +0000</pubDate>
		<dc:creator>dariosantarelli</dc:creator>
				<category><![CDATA[Microsoft Technology]]></category>

		<guid isPermaLink="false">http://dariosantarelli.wordpress.com/2008/01/19/vistaxp-virtual-desktop-manager/</guid>
		<description><![CDATA[Vista provides developers with the new DWM based aero interface. A new set of thumbnail API&#8217;s can be used to access some of this new technology. On codeplex we can find a virtual desktop program takes advantage of this new API and uses some tricks of its own to provide a powerful virtual desktop manager [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dariosantarelli.wordpress.com&amp;blog=502627&amp;post=63&amp;subd=dariosantarelli&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Vista provides developers with the new DWM based aero interface. A new set of thumbnail API&#8217;s can be used to access some of this new technology. On codeplex we can find a virtual desktop program takes advantage of this new API and uses some tricks of its own to provide a powerful virtual desktop manager with a full screen thumbnail based preview. You can have as many desktops as you want and can seamlessly switch between them.<br /><strong>IMPORTANT</strong>: Vista/XP Virtual Desktops provides support for XP as well, although window previews are not live since XP doesn&#8217;t have DWM.<br />Here some cool features:
<li>Full screen desktop/window manager/preview with full drag and drop managing
<li>Desktop switch indicator
<li>An infinite number of desktops
<li>Watch the windows move in real time as you drag them around in the window manager
<li>Multiple monitor support
<li>Window menus
<li>Tray icons for each desktop
<li>Per-desktop backgrounds
<li>Configurable colors, fade speeds, hotkeys, etc.
<li>Uses Vista&#8217;s live thumbnails
<li>XP support
<li>And much more!
<p><a href="http://www.codeplex.com/vdm" target="_blank"><strong>Here</strong></a> the codeplex project!</p>
<p><img src="http://www.codeplex.com/Project/Download/FileDownload.aspx?ProjectName=vdm&amp;DownloadId=7838"> </p>
</li>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/dariosantarelli.wordpress.com/63/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/dariosantarelli.wordpress.com/63/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/dariosantarelli.wordpress.com/63/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/dariosantarelli.wordpress.com/63/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/dariosantarelli.wordpress.com/63/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/dariosantarelli.wordpress.com/63/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/dariosantarelli.wordpress.com/63/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/dariosantarelli.wordpress.com/63/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/dariosantarelli.wordpress.com/63/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/dariosantarelli.wordpress.com/63/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/dariosantarelli.wordpress.com/63/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/dariosantarelli.wordpress.com/63/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/dariosantarelli.wordpress.com/63/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/dariosantarelli.wordpress.com/63/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/dariosantarelli.wordpress.com/63/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/dariosantarelli.wordpress.com/63/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dariosantarelli.wordpress.com&amp;blog=502627&amp;post=63&amp;subd=dariosantarelli&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://dariosantarelli.wordpress.com/2008/01/19/vistaxp-virtual-desktop-manager/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/3805217b57ce1cc8bacbb870a2e3479f?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">dariosantarelli</media:title>
		</media:content>

		<media:content url="http://www.codeplex.com/Project/Download/FileDownload.aspx?ProjectName=vdm&#38;DownloadId=7838" medium="image" />
	</item>
	</channel>
</rss>
