<?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/"
	>

<channel>
	<title>Sam Mueller &#187; Visual Studio 2010</title>
	<atom:link href="http://samuelmueller.com/tag/visual-studio-2010/feed/" rel="self" type="application/rss+xml" />
	<link>http://samuelmueller.com</link>
	<description>APPLICATION DEVELOPER EXTRAORDINAIRE</description>
	<lastBuildDate>Thu, 24 Nov 2011 04:23:09 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Silverlight 4 RC Released!</title>
		<link>http://samuelmueller.com/2010/03/silverlight-4-rc-released/</link>
		<comments>http://samuelmueller.com/2010/03/silverlight-4-rc-released/#comments</comments>
		<pubDate>Mon, 15 Mar 2010 19:23:04 +0000</pubDate>
		<dc:creator>Sam Mueller</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Silverlight 4]]></category>
		<category><![CDATA[Visual Studio 2010]]></category>

		<guid isPermaLink="false">http://samuelmueller.com/?p=177</guid>
		<description><![CDATA[The long awaited RC for Silverlight 4 has been released! Tim Heuer has posted a blog detailing what has changed from Beta to RC. You can download the tools here.]]></description>
			<content:encoded><![CDATA[<p>The long awaited RC for Silverlight 4 has been released!  Tim Heuer has <a href="http://timheuer.com/blog/archive/2010/03/15/whats-new-in-silverlight-4-rc-mix10.aspx">posted a blog detailing what has changed from Beta to RC</a>.</p>
<p>You can download the tools <a href="http://go.microsoft.com/fwlink/?LinkID=141284">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://samuelmueller.com/2010/03/silverlight-4-rc-released/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Using Google Closure Templates with ASP.NET MVC in Visual Studio 2010</title>
		<link>http://samuelmueller.com/2009/12/using-google-closure-templates-with-asp-net-mvc-in-visual-studio-2010/</link>
		<comments>http://samuelmueller.com/2009/12/using-google-closure-templates-with-asp-net-mvc-in-visual-studio-2010/#comments</comments>
		<pubDate>Wed, 16 Dec 2009 18:40:30 +0000</pubDate>
		<dc:creator>Sam Mueller</dc:creator>
				<category><![CDATA[Client Templates]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Visual Studio 2010]]></category>
		<category><![CDATA[Google Closure Templates]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://samuelmueller.com/?p=119</guid>
		<description><![CDATA[Client-side templates have become a vital component of AJAX-driven websites. The web is undoubtedly trending towards sites that load content dynamically after the page loads, rather than all at once in the initial page request. This pattern allows web pages &#8230; <a href="http://samuelmueller.com/2009/12/using-google-closure-templates-with-asp-net-mvc-in-visual-studio-2010/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Client-side templates have become a vital component of AJAX-driven websites.  The web is undoubtedly trending towards sites that load content dynamically <em>after</em> the page loads, rather than all at once in the initial page request.  This pattern allows web pages to become lighter and more responsive, which translates to a better experience for the user.  However, the conventional server side templating and databinding techniques that web developers typically use aren&#8217;t as effective anymore.  That is why there are so many JavaScript template solutions that have popped up in recent years:</p>
<ul>
<li><a href="http://aspnet.codeplex.com/wikipage?title=AJAX&amp;referringTitle=Home">ASP.NET Ajax 4</a></li>
<li><a href="http://www.extjs.com/deploy/dev/docs/">ExtJs and XTemplate</a></li>
<li>jQuery Plugins like <a href="http://github.com/raid-ox/chain.js">Chain.js</a>, <a href="http://jtemplates.tpython.com/">jTemplates</a>, <a href="http://arashkarimzadeh.com/index.php/jquery/11-jbind-jquery-bind-template.html">jBind</a>, etc.</li>
<li><a href="http://code.google.com/p/closure-templates/">Google Closure Templates</a></li>
</ul>
<p>Google&#8217;s Closure Templates is the new kid on the block, and the subject of this article.  One might wonder: Why do we need yet another JavaScript templating solution?  The main advantage that sets Closure Templates apart from the other libraries is the included compiler.  Other templating solutions either parse a string of special template syntax, or traverse through actual html elements with special markup or extra classes.  When making light use of client-side templates, these solutions can work very well.  But, as you start to build applications with extremely large datasets and complex templating, then performance starts to become an issue.  These scenarios are where Closure templates begin to shine.  Instead of directly using the template that you write, you run it through the compiler to output JavaScript functions that you can use in your code.  This process is advantageous on two levels; the first is that the JavaScript functions outputted from the compiler are going to be optimized and extremely fast.  Secondly, the compiler can also create server side compatible templates as well, so that you can write your templates once and be able to use them either in JavaScript or in server-side code.  Unfortunately, the current version of the compiler can only generate Java code, and there is no option for .NET languages such as C#.  This is a bummer for .NET developers, but even without C# templates, there is still great value in lightning-fast, compiled JavaScript templates.</p>
<h2>Creating your first Closure Template in Visual Studio 2010</h2>
<p>Let&#8217;s start with a new ASP.NET MVC Project:</p>
<p><img class="alignnone size-full wp-image-131" title="vs2010-new-mvc-project" src="http://samuelmueller.com/wp-content/uploads/2009/12/vs2010-new-mvc-project.png" alt="vs2010-new-mvc-project" width="816" height="483" /></p>
<p>You can create a Web Application project if you are more comfortable with that type of project structure.  Because we will be dealing with just JavaScript, it shouldn&#8217;t matter.</p>
<p>After creating a project, download the <a href="http://code.google.com/p/closure-templates/">compiler and JavaScript utility library</a>.  Extract the zip file, and copy both SoyToJsSrcCompiler.jar and soyutils.js to your templates directory:</p>
<p><img class="alignnone size-full wp-image-137" title="soy-compiler" src="http://samuelmueller.com/wp-content/uploads/2009/12/soy-compiler.png" alt="soy-compiler" width="327" height="522" /></p>
<p>You will see in the screenshot above that I have changed the Build Action of SoyToJsSrcCompiler.jar to &#8220;None&#8221;, and Copy to Output Directory is set to &#8220;Do not copy&#8221;.  This ensures that the compiler is not compiled into the dll, and the file is not copied to the output folder.  The latter is especially useful when using Visual Studio&#8217;s publish feature, because the compiler is not necessary when deploying your website.</p>
<p>Now that we have the required files in place, let&#8217;s go ahead and create a soy template file, example.soy.  This is the file that will contain one or more templates using Closure&#8217;s template syntax.  After creating a template in this file, we will then use the compiler to generate a JavaScript representation of the template that we will reference in our html page.  Every soy file should have the three following components, in the following order:</p>
<ul>
<li>A namespace declaration.</li>
<li>One or more template definitions.</li>
<li>A newline at the end of the file.</li>
</ul>
<p>Go ahead and enter the following example template in your example.soy file:<br />
<code><br />
{namespace closure.examples}</code></p>
<p><code> </code></p>
<p><code>/**<br />
* Says hello to a person.<br />
* @param name The name of the person to say hello to.<br />
*/<br />
{template .helloName}<br />
Hello {$name}!<br />
{/template}</p>
<p></code></p>
<p>Make sure that your soy file is encoded as ANSI, rather than UTF-8.  Even though Google says <a href="http://code.google.com/closure/templates/docs/concepts.html">UTF-8 should be supported</a>, in Windows 7 x64 (and maybe other Windows operating systems and versions) this is currently not the case.  And if you create a file within Visual Studio, it will encode the file as UTF-8 and you will get the following exception when compiling a template:</p>
<p><code>Exception in thread "main" com.google.template.soy.base.SoySyntaxException: In file example.soy: Tag 'namespace' not at start of line.</code></p>
<p><img class="alignnone size-full wp-image-155" title="closure-compiler-encoding-exception" src="http://samuelmueller.com/wp-content/uploads/2009/12/closure-compiler-encoding-exception.png" alt="closure-compiler-encoding-exception" width="858" height="196" /></p>
<p>If you are unsure that your file is saved with the proper encoding, just open it up in notepad, and select File &gt; Save As to see or change the encoding.</p>
<p>After we&#8217;ve included the necessary files in our project, it&#8217;s time to make compiling templates a little more user-friendly.  The jar file is pretty annoying to call from the command line and manually change the parameters for each of your different soy files.  Visual Studio has a perfect solution for this, and it&#8217;s called <em>External Tools</em>.  This feature allows you to set up a VS menu item and seamlessly run commands from the IDE with just the press of a button.  To do this, first click on Tools &gt; External Tools from the menu:</p>
<p><img class="alignnone size-full wp-image-141" title="external-tools" src="http://samuelmueller.com/wp-content/uploads/2009/12/external-tools.png" alt="external-tools" width="348" height="374" /><br />
<code><br />
From here, you can create a new menu item with the following parameters:<br />
Title: <strong>Compile Closure Template</strong><br />
Command: <strong>C:Program Files (x86)Javajre6binjava.exe</strong><br />
Arguments: <strong>-jar "$(ItemDir)SoyToJsSrcCompiler.jar" --outputPathFormat $(ItemFileName).js $(ItemFileName)$(ItemExt)</strong><br />
Initial Directory: <strong>$(ItemDir)</strong><br />
</code><br />
(Thanks to Tj Stewart for this tip)</p>
<p>Note that your Command entry may differ, depending on where your java installation resides on your computer.  Here is what it looks like:</p>
<p><img class="alignnone size-full wp-image-150" title="closure-template-tool" src="http://samuelmueller.com/wp-content/uploads/2009/12/closure-template-tool.png" alt="closure-template-tool" width="471" height="460" /></p>
<p>Once you have this setup, compiling your soy templates is as simple as selecting the soy file in solution explorer and pressing the &#8220;Compile Closure Template&#8221; button in the Tools menu:</p>
<p><img class="alignnone size-full wp-image-145" title="invoke-closure-template-tool" src="http://samuelmueller.com/wp-content/uploads/2009/12/invoke-closure-template-tool.png" alt="invoke-closure-template-tool" width="348" height="349" /></p>
<p>When you attempt to compile a template, make sure to have your output window open.  If there are any exceptions that occur during the compilation, this is where they will be displayed.  If the compilation was successful, you should now have a new example.js file in the Templates directory.  Note that it won&#8217;t be added to the project automatically, so you&#8217;ll have to add it yourself.  Subsequent compiles for the same soy file will only require this step once.</p>
<p>Now let&#8217;s create a page to utilize the new template, and call it example.html.  We could create an MVC view, but it really isn&#8217;t necessary since we are only dealing with JavaScript:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #00bbdd;">&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;html</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/1999/xhtml&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;head<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;title<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;script</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/script<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;script</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;Scripts/Templates/soyutils.js&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/script<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;script</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;Scripts/Templates/example.js&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/script<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;script</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text/javascript&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
		$(function () {
			$('#wrapper').html(closure.examples.helloName({ name: 'Sam' }));
		});
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/script<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/head<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;body<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;wrapper&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/body<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/html<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>I&#8217;m using a little jQuery to make my life easier here, and notice that I am passing JSON with the Closure template parameters as the properties of the object.  Your result should look something like this:</p>
<p><img src="http://samuelmueller.com/wp-content/uploads/2009/12/closure-result.png" alt="closure-result" title="closure-result" width="638" height="236" class="alignnone size-full wp-image-164" /></p>
<p>In summary, Google Closure Templates provide a scalable solution to client side templating by providing an understandable syntax in the form of soy files, with the ability to compile those templates into fast and efficient JavaScript functions that you can use in your pages.  Even though there is no current support for reusing Closure templates in C#, there is still value in utilizing this solution in a Visual Studio project, especially when the External Tools feature makes compilations so easy and convenient.</p>
<p>I hope this article serves as a starting point to getting up and running in Visual Studio 2010.  In future articles, I will be delving into more complex examples and explanations of Closure Templates in real-world scenarios.</p>
]]></content:encoded>
			<wfw:commentRss>http://samuelmueller.com/2009/12/using-google-closure-templates-with-asp-net-mvc-in-visual-studio-2010/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Working with Projections and DTOs in WCF Data Services</title>
		<link>http://samuelmueller.com/2009/11/working-with-projections-and-dtos-in-wcf-data-services/</link>
		<comments>http://samuelmueller.com/2009/11/working-with-projections-and-dtos-in-wcf-data-services/#comments</comments>
		<pubDate>Mon, 23 Nov 2009 10:31:29 +0000</pubDate>
		<dc:creator>Sam Mueller</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Visual Studio 2010]]></category>
		<category><![CDATA[WCF]]></category>
		<category><![CDATA[ADO.NET Data Services]]></category>
		<category><![CDATA[Entity Framework 4]]></category>
		<category><![CDATA[POCO]]></category>
		<category><![CDATA[WCF Data Services]]></category>

		<guid isPermaLink="false">http://blog.samuelmueller.com/?p=64</guid>
		<description><![CDATA[For those of you who haven&#8217;t been following &#8220;Project Astoria&#8221;, you&#8217;ve been missing out on some pretty exciting technology. WCF Data Services (formerly known as ADO.NET Data Services) is a stack on top of WCF that enables the creation and &#8230; <a href="http://samuelmueller.com/2009/11/working-with-projections-and-dtos-in-wcf-data-services/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>For those of you who haven&#8217;t been following &#8220;Project Astoria&#8221;, you&#8217;ve been missing out on some pretty exciting technology.  <a href="http://msdn.microsoft.com/en-us/data/bb931106.aspx">WCF Data Services</a> (formerly known as ADO.NET Data Services) is a stack on top of WCF that enables the creation and consumption of REST-based data services for the web.   There are several intriguing features in the upcoming release that coincides with .NET Framework 4, including:</p>
<ul>
<li><strong>Projections</strong>:  Since CTP2, the Data Services URI API supports the ability to work with a subset of properties on an Entity.</li>
<li><strong>Data Binding</strong>:  The client library now supports two-way data binding for applications built with Silverlight or WPF technologies.</li>
<li><strong>Row Coun</strong>t:  You can now retrieve the total number of entities in a set, without having to fetch all of the entities within that set.</li>
</ul>
<p>The ability to create projections and shape your data directly on the URL can be quite useful. Oftentimes, only a few properties of an entity are necessary, and WCF Data Services makes it easy to achieve an efficient query that returns precisely the data you need.  For an example, take the following Entity Framework data model:</p>
<p><img src="http://samuelmueller.com/wp-content/uploads/2009/11/example-entity-model.png" alt="Example Entity Model" title="Example Entity Model" width="566" height="499" class="size-full wp-image-86" /></p>
<p>Let&#8217;s say you only wanted to return a list of Employee Names, along with the City they live in.  If exposed via a WCF Data Service, you could could get exactly this data by using the $select parameter in your querystring:<br />
<code></p>
<p>http://domain/data.svc/Employees?$select=Id,FirstName,LastName,Address/Id,Address/City&#038;$expand=Address</p>
<p></code></p>
<p>This request will only return the Employee&#8217;s Id, FirstName, and LastName and the Address&#8217;s Id, and City.  The $expand query parameter with the Address value is telling the service to eager load the Address object, which is necessary be able to return the projected properties that we need from it. Any developer who has been creating AJAX-intensive websites will tell you that ability to achieve this granularity without any extra work is extremely useful.</p>
<h2>More Complex Scenarios</h2>
<p>The projections and expansion features are extremely useful, but at some point you will undoubtedly run into a scenario that isn&#8217;t supported via the querystring API.  WCF Data Services allows you to expose custom operations on your service to facilitate these scenarios.  Using example data model above, let&#8217;s say we would want the Employee&#8217;s Id, Name, and the Count of the OptionsApprovals from the StockOptionApproval relationship.  Since there is currently no way to project the count of a child relationship in the querytstring API (don&#8217;t misunderstand the $count parameter as I first did; it will not help here), you would need to expose a custom service operation.  Intuitively, you might code something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&#91;</span>WebGet<span style="color: #008000;">&#93;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> IEnumerable<span style="color: #008000;">&amp;</span>lt<span style="color: #008000;">;</span>EmployeeOptionApprovalCountDTO<span style="color: #008000;">&amp;</span>gt<span style="color: #008000;">;</span> EmployeesWithOptionApprovalCount<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	var employees <span style="color: #008000;">=</span> CurrentDataSource<span style="color: #008000;">.</span><span style="color: #0000FF;">Employees</span>
		<span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">Select</span><span style="color: #008000;">&#40;</span>x <span style="color: #008000;">=&amp;</span>gt<span style="color: #008000;">;</span> <span style="color: #008000;">new</span> EmployeeOptionApprovalCountDTO <span style="color: #008000;">&#123;</span>
			x<span style="color: #008000;">.</span><span style="color: #0000FF;">ID</span>,
			x<span style="color: #008000;">.</span><span style="color: #0000FF;">Name</span>,
			OptionsApprovalCount <span style="color: #008000;">=</span> x<span style="color: #008000;">.</span><span style="color: #0000FF;">Children</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Count</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
		<span style="color: #008000;">&#125;</span><span style="color: #008000;">&#41;</span>
		<span style="color: #008000;">.</span><span style="color: #0000FF;">ToList</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #0600FF; font-weight: bold;">return</span> employees<span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Unfortunately, this will not work.  At least not when your WCFService inherits from an Entity Framework ObjectContext:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> EmployeeService <span style="color: #008000;">:</span> DataService<span style="color: #008000;">&amp;</span>lt<span style="color: #008000;">;</span>EntityContext<span style="color: #008000;">&amp;</span>gt<span style="color: #008000;">;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> InitializeService<span style="color: #008000;">&#40;</span>DataServiceConfiguration config<span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		config<span style="color: #008000;">.</span><span style="color: #0000FF;">UseVerboseErrors</span> <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span>
		<span style="color: #008080; font-style: italic;">// more config options here...</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>When your service inherits from an ObjectContext, WCF Data Services will use the ObjectContextDataProvider.  A current limitation is that you can only return entities from the custom service operations that you expose.  In other words, if you try to return a data transfer object (regular CLR class), your service will fault with:</p>
<pre>
'Unable to load metadata for return type 'System.Collections.Generic.IEnumerable`1[EmployeeOptionApprovalCountDTO]' of method 'System.Collections.Generic.IEnumerable`1[EmployeeOptionApprovalCountDTO] GetEntityWithCount(Int32)'
</pre>
<p>Disappointingly, you can&#8217;t even use the config&#8217;s RegisterKnownType method during InitializeService, because it isn&#8217;t used when ObjectContextDataProvider is chosen as the Provider for your service.  So what other options do you have for this seemingly straightforward use case?</p>
<p>The next thing I tried was to create an Entity in the model that isn&#8217;t mapped to a table in the database. This was a dead end, because when using the unmapped entity in the entity data model as the DTO to return from the service, I received this exception:</p>
<pre>
The server encountered an error processing the request. The exception message is 'Service operation 'UnmappedEntityDTO' produces instances of type 'UnmappedEntityDTO', but there are no visible entity sets for that type. The service operation should be hidden or a resource set for type 'UnmappedEntityDTO' should be made visible.'.
</pre>
<p>So what are we left with to try? Fortunately, the entity framework has the concept of complex types, which technically are to be used as properties of entities.  However, the ObjectContextDataProvider will allow you to return a complex type in the EntityDataModel from your service operation.  You can create them in the Model Browser:</p>
<p><img src="http://samuelmueller.com/wp-content/uploads/2009/11/example-ef-complex-type.png" alt="example-ef-complex-type" title="example-ef-complex-type" width="377" height="412" class="alignnone size-full wp-image-96" /></p>
<p>As if jumping through all those hoops weren&#8217;t enough, there is one more gotcha to consider.  When building a Linq to Entities query, you cannot use a complex type as part of your where clause!  You will receive the following exception:</p>
<pre>
The entity or complex type 'ComplexTypeAsDTO' cannot be constructed in a LINQ to Entities query.
</pre>
<p>So instead, you must first use an anonymous type, invoke ToList() to ensure your query gets executed, and then finally transform your anonymous type to your complex type so that your service is able to return your objects:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">return</span> CurrentDataSource<span style="color: #008000;">.</span><span style="color: #0000FF;">Employees</span>
	<span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">Select</span><span style="color: #008000;">&#40;</span>x <span style="color: #008000;">=&amp;</span>gt<span style="color: #008000;">;</span> <span style="color: #008000;">new</span><span style="color: #008000;">&#123;</span>
		Id <span style="color: #008000;">=</span> x<span style="color: #008000;">.</span><span style="color: #0000FF;">Id</span>,
		Name <span style="color: #008000;">=</span> x<span style="color: #008000;">.</span><span style="color: #0000FF;">Name</span>,
		OptionsApprovalCount <span style="color: #008000;">=</span> x<span style="color: #008000;">.</span><span style="color: #0000FF;">OptionsApprovals</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Count</span>
	<span style="color: #008000;">&#125;</span><span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">.</span><span style="color: #0000FF;">ToList</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">Select</span><span style="color: #008000;">&#40;</span>x <span style="color: #008000;">=&amp;</span>gt<span style="color: #008000;">;</span> <span style="color: #008000;">new</span> EmployeeWithOptionsApprovalCount <span style="color: #008000;">&#123;</span>
		Id <span style="color: #008000;">=</span> x<span style="color: #008000;">.</span><span style="color: #0000FF;">Id</span>,
		Name <span style="color: #008000;">=</span> x<span style="color: #008000;">.</span><span style="color: #0000FF;">Name</span>,
		OptionsApprovalCount <span style="color: #008000;">=</span> x<span style="color: #008000;">.</span><span style="color: #0000FF;">OptionsApprovalCount</span>
	<span style="color: #008000;">&#125;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<h2>Summary</h2>
<p>WCF Data Services enables rapid development and the ability to easily expose your entity model.  And when combined with the entity framework in conjunction with it&#8217;s new POCO support, you don&#8217;t have to sacrifice your n-tier architecture and proper separation of concerns.  However, there are still some counterintuitive practices that must be used in order to handle seemingly-basic scenarios.  Hopefully, by the time the final product is delivered, there will be better support for these situations.  But for now, I&#8217;m happy that I found a decent workaround!</p>
]]></content:encoded>
			<wfw:commentRss>http://samuelmueller.com/2009/11/working-with-projections-and-dtos-in-wcf-data-services/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
	</channel>
</rss>

