<?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>source code bean &#187; C#</title>
	<atom:link href="http://sourcecodebean.com/archives/category/c/feed" rel="self" type="application/rss+xml" />
	<link>http://sourcecodebean.com</link>
	<description>thoughts and ideas from a .net developer</description>
	<lastBuildDate>Fri, 27 Jan 2012 20:36:28 +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>MongoDB, C# and Mono</title>
		<link>http://sourcecodebean.com/archives/mongodb-c-and-mono/1408</link>
		<comments>http://sourcecodebean.com/archives/mongodb-c-and-mono/1408#comments</comments>
		<pubDate>Wed, 11 Jan 2012 18:53:48 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Mono]]></category>
		<category><![CDATA[NoSQL]]></category>

		<guid isPermaLink="false">http://sourcecodebean.com/?p=1408</guid>
		<description><![CDATA[MongoDB is one of the many free open source NoSQL databases that exist today. I wanted to try out how well the official drivers for C# worked when using Mono. On the MongoDB web site they have pre built binaries of MongoDB for almost every platform, i grabbed the 64 bit binary for OSX. No [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.mongodb.org/">MongoDB</a> is one of the many free open source NoSQL databases that exist today. I wanted to try out how well the official drivers for C# worked when using Mono. </p>
<p>On the MongoDB web site they have pre built binaries of MongoDB for almost every platform, i grabbed the 64 bit binary for OSX. No installation is required, just unzip the file and run the &#8220;mongod&#8221; binary from the directory. There is also a binary called &#8220;mongo&#8221;, this is the <a href="http://www.mongodb.org/display/DOCS/Overview+-+The+MongoDB+Interactive+Shell">mongo interactive shell</a> which is very useful. </p>
<p>The C# drivers are available, both in binary form and as source, from the <a href="http://www.mongodb.org/display/DOCS/CSharp+Language+Center">C# language center</a>. To get started I fired up MonoDevelop 2.8 running on Mono 2.10.8:</p>
<p><a href="http://sourcecodebean.com/wp-content/uploads/2012/01/monodevelop.png"><img src="http://sourcecodebean.com/wp-content/uploads/2012/01/monodevelop-300x187.png" alt="" title="monodevelop" width="300" height="187" class="aligncenter size-medium wp-image-1451" /></a><br />
<a href="http://sourcecodebean.com/wp-content/uploads/2012/01/edit_references.png"><img src="http://sourcecodebean.com/wp-content/uploads/2012/01/edit_references-300x187.png" alt="" title="edit_references" width="300" height="187" class="aligncenter size-medium wp-image-1449" /></a><br />
<a href="http://sourcecodebean.com/wp-content/uploads/2012/01/add_references.png"><img src="http://sourcecodebean.com/wp-content/uploads/2012/01/add_references-300x201.png" alt="" title="add_references" width="300" height="201" class="aligncenter size-medium wp-image-1450" /></a></p>
<p>MonoDevelop has matured a lot since I used it the first time. It is still not as good as Visual Studio, but it is getting there. What takes most time to get used to is the totally different set of shortcuts, and the lack of all ReSharper magic. </p>
<p>Let me break down a simple console application example on how to use MongoDB using the official driver from 10gen: <span id="more-1408"></span></p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">using System;</div>
</li>
<li class="li1">
<div class="de1">using System.<span class="me1">Collections</span>.<span class="me1">Generic</span>;</div>
</li>
<li class="li2">
<div class="de2">using System.<span class="me1">Linq</span>;</div>
</li>
<li class="li1">
<div class="de1">using MongoDB.<span class="me1">Bson</span>;</div>
</li>
<li class="li1">
<div class="de1">using MongoDB.<span class="me1">Driver</span>;</div>
</li>
<li class="li1">
<div class="de1">using MongoDB.<span class="me1">Driver</span>.<span class="me1">Builders</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">namespace MongoDbTest</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span> </div>
</li>
<li class="li1">
<div class="de1">&nbsp; public class Article</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; public MongoDB.<span class="me1">Bson</span>.<span class="me1">ObjectId</span> id <span class="br0">&#123;</span> get; set; <span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; public <span class="kw4">string</span> title <span class="br0">&#123;</span> get; set; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; public <span class="kw4">string</span> status <span class="br0">&#123;</span> get; set; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; public <span class="kw4">string</span><span class="br0">&#91;</span><span class="br0">&#93;</span> tags <span class="br0">&#123;</span> get; set; <span class="br0">&#125;</span>&nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; class MainClass</div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; public <span class="kw4">static</span> <span class="kw4">void</span> Main <span class="br0">&#40;</span><span class="kw4">string</span><span class="br0">&#91;</span><span class="br0">&#93;</span> args<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; <span class="co1">// Connect to the database and get a colleciton</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; MongoServer server = MongoServer.<span class="me1">Create</span> <span class="br0">&#40;</span><span class="st0">&quot;mongodb://localhost&quot;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; MongoDatabase db = server.<span class="me1">GetDatabase</span> <span class="br0">&#40;</span><span class="st0">&quot;mongodb-tutorial&quot;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="co1">// Get an article collection</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; MongoCollection&lt;BsonDocument&gt; articles = db.<span class="me1">GetCollection</span>&lt;BsonDocument&gt; <span class="br0">&#40;</span><span class="st0">&quot;articles&quot;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="co1">// Empty the collection (so all runs of this program will result in the same output)</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; articles.<span class="me1">RemoveAll</span> <span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="co1">//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="co1">// Create two articles</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="co1">//</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; articles.<span class="me1">Save</span> <span class="br0">&#40;</span>new BsonDocument</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><span class="st0">&quot;title&quot;</span>, <span class="st0">&quot;A MongoDB article&quot;</span><span class="br0">&#125;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><span class="st0">&quot;status&quot;</span>, <span class="st0">&quot;published&quot;</span><span class="br0">&#125;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><span class="st0">&quot;tags&quot;</span>, new BsonArray <span class="br0">&#123;</span> <span class="st0">&quot;c#&quot;</span>, <span class="st0">&quot;mongodb&quot;</span> <span class="br0">&#125;</span><span class="br0">&#125;</span>&nbsp; &nbsp; &nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; articles.<span class="me1">Save</span> <span class="br0">&#40;</span>new BsonDocument</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><span class="st0">&quot;title&quot;</span>, <span class="st0">&quot;A Mono article&quot;</span><span class="br0">&#125;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><span class="st0">&quot;status&quot;</span>, <span class="st0">&quot;draft&quot;</span><span class="br0">&#125;</span>,</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><span class="st0">&quot;tags&quot;</span>, new BsonArray <span class="br0">&#123;</span> <span class="st0">&quot;c#&quot;</span>, <span class="st0">&quot;mono&quot;</span> <span class="br0">&#125;</span><span class="br0">&#125;</span>&nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><span class="br0">&#41;</span>;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<blockquote><p>If you type this in the MongoDB shell, you will see that the following articles are stored in the database:</p>
<pre>
> db.articles.find()
        {
          "_id" : ObjectId("4f097f8e74b5b343b97f56a7"),
          "title" : "A MongoDB article",
          "status" : "published",
          "tags" : [ "c#", "mongodb" ]
        }

        {
          "_id" : ObjectId("4f097f8e74b5b343b97f56a8"),
          "title" :
          "A Mono article",
          "status" : "draft",
          "tags" : [ "c#", "mono" ]
       }
</pre>
</blockquote>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp;<span class="co1">// Find and print the first article</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; BsonDocument firstArticle = articles.<span class="me1">FindOne</span> <span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; PrintArticle <span class="br0">&#40;</span> <span class="co1">// Access the properties using indexing and then casting using the .As*</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; firstArticle<span class="br0">&#91;</span><span class="st0">&quot;_id&quot;</span><span class="br0">&#93;</span>.<span class="me1">AsObjectId</span>, </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; firstArticle<span class="br0">&#91;</span><span class="st0">&quot;title&quot;</span><span class="br0">&#93;</span>.<span class="me1">AsString</span>, </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; firstArticle<span class="br0">&#91;</span><span class="st0">&quot;status&quot;</span><span class="br0">&#93;</span>.<span class="me1">AsString</span>, </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#40;</span>firstArticle<span class="br0">&#91;</span><span class="st0">&quot;tags&quot;</span><span class="br0">&#93;</span>.<span class="me1">AsBsonArray</span><span class="br0">&#41;</span>.<span class="me1">Select</span> <span class="br0">&#40;</span>x =&gt; x.<span class="me1">AsString</span><span class="br0">&#41;</span><span class="br0">&#41;</span>; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="co1">//</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="co1">// Typed collections&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="co1">//</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="co1">// Using indexing and the .As* operations is not that convenient. A better way</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="co1">// is to use a typed collection. Earlier we defined a Article class, lets use it:</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; MongoCollection&lt;Article&gt; typedArticles = db.<span class="me1">GetCollection</span>&lt;Article&gt; <span class="br0">&#40;</span><span class="st0">&quot;articles&quot;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; Article typedArticle = typedArticles.<span class="me1">FindOneAs</span>&lt;Article&gt; <span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; PrintArticle <span class="br0">&#40;</span>typedArticle.<span class="me1">id</span>, typedArticle.<span class="me1">title</span>, </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; typedArticle.<span class="me1">status</span>, typedArticle.<span class="me1">tags</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="co1">// Save a typed article</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; typedArticles.<span class="me1">Save</span> <span class="br0">&#40;</span>new Article <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; title = <span class="st0">&quot;An article about MonoDevelop&quot;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; status = <span class="st0">&quot;published&quot;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tags = new <span class="br0">&#91;</span><span class="br0">&#93;</span> <span class="br0">&#123;</span><span class="st0">&quot;c#&quot;</span>, <span class="st0">&quot;mono&quot;</span>, <span class="st0">&quot;mono-develop&quot;</span><span class="br0">&#125;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="co1">// </span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="co1">// Querying</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="co1">// </span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="co1">// Find article with status draft</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; Article draftArticle = typedArticles</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; .<span class="me1">FindOneAs</span>&lt;Article&gt; <span class="br0">&#40;</span>Query.<span class="me1">EQ</span> <span class="br0">&#40;</span><span class="st0">&quot;status&quot;</span>, <span class="st0">&quot;draft&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; PrintArticle <span class="br0">&#40;</span>draftArticle.<span class="me1">id</span>, draftArticle.<span class="me1">title</span>, </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; draftArticle.<span class="me1">status</span>, draftArticle.<span class="me1">tags</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="co1">// </span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; <span class="co1">// Updating</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="co1">// </span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="co1">// Lets update the draft article and set status published</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; Console.<span class="me1">WriteLine</span> <span class="br0">&#40;</span><span class="st0">&quot;Nr of published articles before update:&quot;</span> </div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;+ typedArticles.<span class="me1">Find</span> <span class="br0">&#40;</span>Query.<span class="me1">EQ</span> <span class="br0">&#40;</span><span class="st0">&quot;status&quot;</span>, <span class="st0">&quot;published&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>.<span class="me1">Count</span> <span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; typedArticles.<span class="me1">Update</span> <span class="br0">&#40;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; new QueryDocument <span class="br0">&#123;</span> <span class="br0">&#123;</span> <span class="st0">&quot;_id&quot;</span>, draftArticle.<span class="me1">id</span> <span class="br0">&#125;</span> <span class="br0">&#125;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; new UpdateDocument <span class="br0">&#123;</span> <span class="br0">&#123;</span> <span class="st0">&quot;$set&quot;</span>, new BsonDocument <span class="br0">&#40;</span><span class="st0">&quot;status&quot;</span>, <span class="st0">&quot;published&quot;</span><span class="br0">&#41;</span> <span class="br0">&#125;</span> </div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// $set is atomic</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; Console.<span class="me1">WriteLine</span> <span class="br0">&#40;</span><span class="st0">&quot;Nr of pubhlished articles after update:&quot;</span> </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;+ typedArticles.<span class="me1">Find</span> <span class="br0">&#40;</span>Query.<span class="me1">EQ</span> <span class="br0">&#40;</span><span class="st0">&quot;status&quot;</span>, <span class="st0">&quot;published&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>.<span class="me1">Count</span> <span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="co1">// </span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="co1">// MapReduce</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="co1">// </span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="co1">// This will count the number of times a tag has been used by using MapReduce</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; var map =</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&quot;function() {&quot;</span> +</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&quot; &nbsp; &nbsp;this.tags.forEach(function(t) {&quot;</span> + <span class="co1">// Iterate all the tags and </span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&quot; &nbsp; &nbsp; &nbsp; &nbsp;emit(t, { count : 1 }); &quot;</span> + &nbsp; &nbsp;<span class="co1">// emit the tag name and initial count </span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&quot; &nbsp; &nbsp;})&quot;</span> +</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&quot;}&quot;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; var reduce =</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&quot;function(key, emits) {&quot;</span> + <span class="co1">// Reduce by tag name and summarize the result</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&quot; &nbsp; &nbsp;total = 0;&quot;</span> +</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&quot; &nbsp; &nbsp;for (var i in emits) {&quot;</span> +</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&quot; &nbsp; &nbsp; &nbsp; &nbsp;total += emits[i].count;&quot;</span> +</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&quot; &nbsp; &nbsp;}&quot;</span> +</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&quot; &nbsp; &nbsp;return { count : total };&quot;</span> +</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&quot;}&quot;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; Console.<span class="me1">WriteLine</span> <span class="br0">&#40;</span><span class="st0">&quot;MapReduce result:&quot;</span><span class="br0">&#41;</span>;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; var mr = typedArticles.<span class="me1">MapReduce</span> <span class="br0">&#40;</span>map, reduce<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; foreach <span class="br0">&#40;</span>var document in mr.<span class="me1">GetResults</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; Console.<span class="me1">WriteLine</span> <span class="br0">&#40;</span>document.<span class="me1">ToJson</span> <span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; public <span class="kw4">static</span> <span class="kw4">void</span> PrintArticle <span class="br0">&#40;</span>ObjectId id, <span class="kw4">string</span> title, </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; <span class="kw4">string</span> status, IEnumerable&lt;string&gt; tags<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; Console.<span class="me1">WriteLine</span> <span class="br0">&#40;</span><span class="st0">&quot;id: &quot;</span> + id + <span class="st0">&quot;, title: &quot;</span> + title<span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; Console.<span class="me1">WriteLine</span> <span class="br0">&#40;</span><span class="st0">&quot;- tags: &quot;</span> + <span class="kw4">string</span>.<span class="me1">Join</span> <span class="br0">&#40;</span><span class="st0">&quot;, &quot;</span>, tags<span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p>Output from the program:</p>
<blockquote>
<p>id: 4f0989fc74b5b3458ae38105, title: A MongoDB article<br />
- tags: c#, mongodb<br />
id: 4f0989fc74b5b3458ae38105, title: A MongoDB article<br />
- tags: c#, mongodb<br />
id: 4f0989fc74b5b3458ae38106, title: A Mono article<br />
- tags: c#, mono<br />
Nr of pubhlished articles before update:2<br />
Nr of pubhlished articles after update:3<br />
MapReduce result:<br />
{ &#8220;_id&#8221; : &#8220;c#&#8221;, &#8220;value&#8221; : { &#8220;count&#8221; : 3.0 } }<br />
{ &#8220;_id&#8221; : &#8220;mongodb&#8221;, &#8220;value&#8221; : { &#8220;count&#8221; : 1.0 } }<br />
{ &#8220;_id&#8221; : &#8220;mono&#8221;, &#8220;value&#8221; : { &#8220;count&#8221; : 2.0 } }<br />
{ &#8220;_id&#8221; : &#8220;mono-develop&#8221;, &#8220;value&#8221; : { &#8220;count&#8221; : 1.0 } }
</p></blockquote>
<p>Using the C# driver for MongoDB, is as you can see, very straight forward. Usually when I run applications on Mono I make sure to build all libraries using the Mono compiler, to make sure the Microsoft compiler have not used any optimizations that is not supported under Mono (I have run into this issue in the past). However, in this case the .NET binaries on the MongoDB site seems to work just fine under Mono. I also downloaded the MongoDB C# driver source and build them using MonoDevelop without any problems. </p>
<p>One improvement I would like to see is LINQ support in the driver. It would be so much more convenient to be able to use LINQ and strongly typed objects to query the database or create MapReduce-functions.</p>
]]></content:encoded>
			<wfw:commentRss>http://sourcecodebean.com/archives/mongodb-c-and-mono/1408/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>String interning in C#</title>
		<link>http://sourcecodebean.com/archives/string-interning-in-c/1367</link>
		<comments>http://sourcecodebean.com/archives/string-interning-in-c/1367#comments</comments>
		<pubDate>Wed, 07 Dec 2011 11:15:33 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://sourcecodebean.com/?p=1367</guid>
		<description><![CDATA[Last week a coworker sent me an interesting piece of code: &#160; const string a = &#34;&#34;; const string b = &#34;&#34;; const string c = &#34;hello&#34;; const string d = &#34;hello&#34;; &#160; Console.WriteLine&#40;ReferenceEquals&#40;string.Empty, string.Empty&#41; &#160; ? &#34;Not so surprising…&#34; &#160; : &#34;Oh, the humanity!&#34;&#41;; &#160; Console.WriteLine&#40;ReferenceEquals&#40;a, string.Empty&#41; &#160; ? &#34;But I thought string was [...]]]></description>
			<content:encoded><![CDATA[<p>Last week a coworker sent me an interesting piece of code:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw4">const</span> <span class="kw4">string</span> a = <span class="st0">&quot;&quot;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw4">const</span> <span class="kw4">string</span> b = <span class="st0">&quot;&quot;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw4">const</span> <span class="kw4">string</span> c = <span class="st0">&quot;hello&quot;</span>;</div>
</li>
<li class="li2">
<div class="de2"><span class="kw4">const</span> <span class="kw4">string</span> d = <span class="st0">&quot;hello&quot;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">Console.<span class="me1">WriteLine</span><span class="br0">&#40;</span>ReferenceEquals<span class="br0">&#40;</span><span class="kw4">string</span>.<span class="me1">Empty</span>, <span class="kw4">string</span>.<span class="me1">Empty</span><span class="br0">&#41;</span> </div>
</li>
<li class="li1">
<div class="de1">&nbsp; ? <span class="st0">&quot;Not so surprising…&quot;</span> </div>
</li>
<li class="li1">
<div class="de1">&nbsp; : <span class="st0">&quot;Oh, the humanity!&quot;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">Console.<span class="me1">WriteLine</span><span class="br0">&#40;</span>ReferenceEquals<span class="br0">&#40;</span>a, <span class="kw4">string</span>.<span class="me1">Empty</span><span class="br0">&#41;</span> </div>
</li>
<li class="li1">
<div class="de1">&nbsp; ? <span class="st0">&quot;But I thought string was a reference type!&quot;</span> </div>
</li>
<li class="li1">
<div class="de1">&nbsp; : <span class="st0">&quot;Seems fair…&quot;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">Console.<span class="me1">WriteLine</span><span class="br0">&#40;</span>ReferenceEquals<span class="br0">&#40;</span>a, b<span class="br0">&#41;</span> </div>
</li>
<li class="li1">
<div class="de1">&nbsp; ? <span class="st0">&quot; But I thought string was a reference type!&quot;</span> </div>
</li>
<li class="li1">
<div class="de1">&nbsp; : <span class="st0">&quot; Seems fair…&quot;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">Console.<span class="me1">WriteLine</span><span class="br0">&#40;</span>ReferenceEquals<span class="br0">&#40;</span>c, d<span class="br0">&#41;</span> </div>
</li>
<li class="li2">
<div class="de2">&nbsp; ? <span class="st0">&quot; But I thought string was a reference type!&quot;</span> </div>
</li>
<li class="li1">
<div class="de1">&nbsp; : <span class="st0">&quot; Seems fair…&quot;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p>Running the program will, maybe a bit surprising, produce:</p>
<blockquote><p>Not so surprising…<br />
But I thought string was a reference type!<br />
But I thought string was a reference type!<br />
But I thought string was a reference type!</p></blockquote>
<p>The reason to this is what is called <em>String interning</em>. The CLR basically holds a hashmap of all the strings in the program (edit: string literals), one entry per unique string. So two identical strings will always have the same reference even though they are defined in different places. String interning is mainly used to speed up string comparisons (no need to check char by char if they are identical, if they have the same reference they are equal), but also to reduce the memory footprint of the application. </p>
<p>Read more about string interning:<br />
<a href="http://aspadvice.com/blogs/from_net_geeks_desk/archive/2008/12/25/String-Interning-in-C_2300_.aspx">http://aspadvice.com/blogs/from_net_geeks_desk/archive/2008/12/25/String-Interning-in-C_2300_.aspx</a><br />
<a href="http://en.wikipedia.org/wiki/String_interning">http://en.wikipedia.org/wiki/String_interning</a></p>
]]></content:encoded>
			<wfw:commentRss>http://sourcecodebean.com/archives/string-interning-in-c/1367/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Reference types are always passed by value in C#</title>
		<link>http://sourcecodebean.com/archives/reference-types-are-always-passed-by-value-in-c/1123</link>
		<comments>http://sourcecodebean.com/archives/reference-types-are-always-passed-by-value-in-c/1123#comments</comments>
		<pubDate>Mon, 02 May 2011 06:05:00 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://sourcecodebean.com/?p=1123</guid>
		<description><![CDATA[Last week i had a discussion with a friend about C# and passing parameters to methods. My friend asked why it is possible to use the &#8216;ref&#8217; keyword for a reference type in C# when passing it to a method, he was under the impression that reference types are passed by reference to methods. This [...]]]></description>
			<content:encoded><![CDATA[<p>Last week i had a discussion with a friend about C# and passing parameters to methods. My friend asked why it is possible to use the &#8216;ref&#8217; keyword for a reference type in C# when passing it to a method, he was under the impression that reference types are passed by reference to methods. This seems to be a quite common misunderstanding about C#, to make it clear:</p>
<blockquote><p>Reference types are always passed to methods <strong>by value</strong> (unless you use the &#8216;ref&#8217; keyword)</p></blockquote>
<p><span id="more-1123"></span></p>
<p>What happens when you call a method with a reference type as parameter is that the value of the reference (the pointer, an address to pointing to the referenced object) is passed to the method (by value). What this means is that you can not modify the original reference, but you can modify the object it is referencing (pointing to).</p>
<p>By using the &#8216;ref&#8217; keyword you will pass the address of the reference to the method, which means that you can modify its value. </p>
<p>This is a short example illustrating this (remember, in C#, a string is a reference type):</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">class Program</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw4">static</span> <span class="kw4">void</span> Main<span class="br0">&#40;</span><span class="kw4">string</span><span class="br0">&#91;</span><span class="br0">&#93;</span> args<span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">string</span> s = <span class="st0">&quot;orig value&quot;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; Console.<span class="me1">WriteLine</span><span class="br0">&#40;</span><span class="st0">&quot;Orig value: &quot;</span> + s<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; MyFunc1<span class="br0">&#40;</span>s<span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; Console.<span class="me1">WriteLine</span><span class="br0">&#40;</span><span class="st0">&quot;After MyFunc1: &quot;</span> + s<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; MyFunc2<span class="br0">&#40;</span>ref s<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; Console.<span class="me1">WriteLine</span><span class="br0">&#40;</span><span class="st0">&quot;After MyFunc2: &quot;</span> + s<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; public <span class="kw4">static</span> <span class="kw4">void</span> MyFunc1<span class="br0">&#40;</span><span class="kw4">string</span> s<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; s = <span class="st0">&quot;new value 1&quot;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; public <span class="kw4">static</span> <span class="kw4">void</span> MyFunc2<span class="br0">&#40;</span>ref <span class="kw4">string</span> s<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; s = <span class="st0">&quot;new value 2&quot;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p>Output:<br />
Orig value: orig value<br />
After MyFunc1: orig value<br />
After MyFunc2: new value 2</p>
]]></content:encoded>
			<wfw:commentRss>http://sourcecodebean.com/archives/reference-types-are-always-passed-by-value-in-c/1123/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>A simple image sprite generator in C#</title>
		<link>http://sourcecodebean.com/archives/a-simple-image-sprite-generator-in-c-sharp/1105</link>
		<comments>http://sourcecodebean.com/archives/a-simple-image-sprite-generator-in-c-sharp/1105#comments</comments>
		<pubDate>Mon, 18 Apr 2011 20:17:01 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://sourcecodebean.com/?p=1105</guid>
		<description><![CDATA[Last week I posted the source code to a small program I wrote in Java to merge images into a sprite. This week I present the same application but in C#, pretty much translated line by line using System; using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Linq; &#160; namespace NSpriteGenerator &#123; &#160; &#160; class Program [...]]]></description>
			<content:encoded><![CDATA[<p>Last week I <a href="http://sourcecodebean.com/archives/a-simple-image-sprite-generator-in-java/1065">posted the source code to a small program</a> I wrote in Java to merge images into a sprite. This week I present the same application but in C#, pretty much translated line by line <img src='http://sourcecodebean.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">using System;</div>
</li>
<li class="li1">
<div class="de1">using System.<span class="me1">Drawing</span>;</div>
</li>
<li class="li1">
<div class="de1">using System.<span class="me1">Drawing</span>.<span class="me1">Imaging</span>;</div>
</li>
<li class="li1">
<div class="de1">using System.<span class="me1">IO</span>;</div>
</li>
<li class="li2">
<div class="de2">using System.<span class="me1">Linq</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">namespace NSpriteGenerator</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; class Program</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">static</span> <span class="kw4">void</span> Main<span class="br0">&#40;</span><span class="kw4">string</span><span class="br0">&#91;</span><span class="br0">&#93;</span> args<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>args.<span class="me1">Length</span> != <span class="nu0">2</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.<span class="me1">WriteLine</span><span class="br0">&#40;</span><span class="st0">&quot;Usage: NSpriteGenerator {path to images} {output file}&quot;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var imagePath = args<span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var outputFile = args<span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var imageFolder = new DirectoryInfo<span class="br0">&#40;</span>imagePath<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// Read images</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var imageList = imageFolder.<span class="me1">GetFiles</span><span class="br0">&#40;</span><span class="st0">&quot;*.png&quot;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .<span class="me1">Select</span><span class="br0">&#40;</span>file =&gt; Image.<span class="me1">FromFile</span><span class="br0">&#40;</span>file.<span class="me1">FullName</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// Find max width and total height</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var maxWidth = <span class="nu0">0</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var totalHeight = <span class="nu0">0</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach<span class="br0">&#40;</span>var image in imageList<span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; totalHeight += image.<span class="me1">Height</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>image.<span class="me1">Width</span> &gt; maxWidth<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; maxWidth = image.<span class="me1">Width</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.<span class="me1">WriteLine</span><span class="br0">&#40;</span><span class="kw4">string</span>.<span class="me1">Format</span><span class="br0">&#40;</span><span class="st0">&quot;Number of images: {0}, total height: {1}px, width: {2}&quot;</span>, imageList.<span class="me1">Count</span><span class="br0">&#40;</span><span class="br0">&#41;</span>, totalHeight, maxWidth<span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// Create the actual sprite</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var currentY = <span class="nu0">0</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; using <span class="br0">&#40;</span>var bitmap = new Bitmap<span class="br0">&#40;</span>maxWidth, totalHeight<span class="br0">&#41;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; using <span class="br0">&#40;</span>var canvas = Graphics.<span class="me1">FromImage</span><span class="br0">&#40;</span>bitmap<span class="br0">&#41;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach <span class="br0">&#40;</span>var image in imageList<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; canvas.<span class="me1">DrawImage</span><span class="br0">&#40;</span>image, <span class="nu0">0</span>, currentY<span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; currentY += image.<span class="me1">Height</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; canvas.<span class="me1">Save</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.<span class="me1">WriteLine</span><span class="br0">&#40;</span><span class="st0">&quot;Writing sprite: &quot;</span>+ outputFile<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bitmap.<span class="me1">Save</span><span class="br0">&#40;</span>outputFile, ImageFormat.<span class="me1">Png</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p>Output from a run:</p>
<blockquote><p><code><br />
PS C:\dev\NSpriteGenerator> .\NSpriteGenerator.exe C:\temp\gfx c:\temp\sprite.png<br />
Number of images: 10, total height: 640px, width: 34<br />
Writing sprite: c:\temp\sprite.png<br />
</code></p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://sourcecodebean.com/archives/a-simple-image-sprite-generator-in-c-sharp/1105/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>AppHarbor &#8211; Heroku for .NET?</title>
		<link>http://sourcecodebean.com/archives/appharbor-heroku-for-net/987</link>
		<comments>http://sourcecodebean.com/archives/appharbor-heroku-for-net/987#comments</comments>
		<pubDate>Wed, 06 Apr 2011 20:24:31 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://sourcecodebean.com/?p=987</guid>
		<description><![CDATA[I have been reading about this new startup that offers a service called AppHarbor. It seems very much as the same idea as Heroku for Ruby, but for .NET. The basic idea is that you deploy your code by pushing your code onto the server using Git, the server then builds, runs unit tests and [...]]]></description>
			<content:encoded><![CDATA[<p>I have been reading about this new startup that offers a service called AppHarbor. It seems very much as the same idea as <a href="http://heroku.com/">Heroku for Ruby</a>, but for .NET. The basic idea is that you deploy your code by pushing your code onto the server using Git, the server then builds, runs unit tests and deploys your application. Super simple!</p>
<p>When I first read about AppHarbor I got the impression that is was running ontop of Microsoft Azure, which turned out to be all wrong. AppHarbor is running in the Amazon Cloud on EC2 instances. When it comes to databases AppHarbor provides access to MySQL and MSSQL. Interestingly they recommend users to use MySQL:</p>
<blockquote><p><a href="http://blog.appharbor.com/2011/01/12/mysql-support">&#8220;we expect to be able to offer MySQL on faster hardware and with more redundancy than SQL Server at the same price point. If you are planning to build an app on top of AppHarbor and have the option, we recommend you go with MySQL.&#8221;</a></p></blockquote>
<p>Another interesting choice is that they have chosen <a href="http://support.appharbor.com/kb/getting-started/information-about-our-load-balancer">Nginx as their proxy/load balancer</a>.</p>
<p>Ok, let me show you how easy it is to create a simple ASP.NET MVC 3 application and deploy it on AppHarbour <span id="more-987"></span> The first step is to go to <a href="http://appharbor.com">appharbor.com</a> and register an account. Once created you will be able to create your first app:</p>
<p><a href="http://sourcecodebean.com/wp-content/uploads/2011/03/1.png"><img class="aligncenter size-full wp-image-995" title="1" src="http://sourcecodebean.com/wp-content/uploads/2011/03/1.png" alt="" width="600" /></a></p>
<p>Give your application and press create. You have done all the configuring you need to do at AppHarbor.</p>
<p><a href="http://sourcecodebean.com/wp-content/uploads/2011/03/2.png"><img class="aligncenter size-full wp-image-996" title="2" src="http://sourcecodebean.com/wp-content/uploads/2011/03/2.png" alt="" width="600" /></a></p>
<p>Lets create a MVC 3 project with unit tests:</p>
<p><a href="http://sourcecodebean.com/wp-content/uploads/2011/03/3.png"><img class="aligncenter size-full wp-image-997" title="3" src="http://sourcecodebean.com/wp-content/uploads/2011/03/3.png" alt="" width="600" /></a><br />
<a href="http://sourcecodebean.com/wp-content/uploads/2011/03/4.png"><img class="aligncenter size-full wp-image-998" title="4" src="http://sourcecodebean.com/wp-content/uploads/2011/03/4.png" alt="" width="600" /></a></p>
<p>Make sure your project builds and that the tests passes. Also make sure you have Git installed, if not you can fetch it from <a href="http://code.google.com/p/msysgit/downloads/list">here</a>. Now open a shell and cd into the folder where you created your application.</p>
<p>First we need to configure git with your name, the email you used to register at AppHarbor and last the git postBuffer size:</p>
<blockquote><p><code>PS C:\dev\appharbor\MyCoolApp&gt; git config --global user.name SourceCodeBean<br />
PS C:\dev\appharbor\MyCoolApp&gt; git config --global user.email mail@sourcecodebean.com<br />
PS C:\dev\appharbor\MyCoolApp&gt; git config --global http.postBuffer 52428800</code></p></blockquote>
<p>After this we initiate the git repository:</p>
<blockquote><p><code>PS C:\dev\appharbor\MyCoolApp&gt; git init<br />
PS C:\dev\appharbor\MyCoolApp&gt; git add .<br />
PS C:\dev\appharbor\MyCoolApp&gt; git commit -m "initial commit"</code></p></blockquote>
<p>And when this is done we add AppHarbor as a remote repository:</p>
<blockquote><p><code>PS C:\dev\appharbor\MyCoolApp&gt; git remote add appharbor https://moberg@appharbor.com/mycoolapp-1.git</code></p></blockquote>
<p>We are now set to push our code onto AppHarbor. To push it just type:</p>
<blockquote><p><code>PS C:\dev\appharbor\MyCoolApp&gt; git push appharbor master</code></p></blockquote>
<p>We should now be able to see the project building:</p>
<p><a href="http://sourcecodebean.com/wp-content/uploads/2011/03/7.png"><img class="aligncenter size-full wp-image-1012" title="7" src="http://sourcecodebean.com/wp-content/uploads/2011/03/7.png" alt="" width="600" /></a></p>
<p>Hopefully it built successfully. A problem I ran into was that I had ASP.NET MVC 3 RC installed and AppHarbor had the RTM version installed, this caused my first attempt to build my project to fail. After upgrading my local version and fixing the project I pushed a new version that built without problems.</p>
<p><a href="http://sourcecodebean.com/wp-content/uploads/2011/03/8.png"><img class="aligncenter size-full wp-image-1013" title="8" src="http://sourcecodebean.com/wp-content/uploads/2011/03/8.png" alt="" width="600" /></a></p>
<p>We can now browse to the site (yay! that was way easier than setting up IIS manually):</p>
<p><a href="http://sourcecodebean.com/wp-content/uploads/2011/03/9.png"><img class="aligncenter size-full wp-image-1015" title="9" src="http://sourcecodebean.com/wp-content/uploads/2011/03/9.png" alt="" width="600" /></a></p>
<h3><strong>Failing tests</strong></h3>
<p>Ok, lets change something in our application that will make a test fail:</p>
<p><a href="http://sourcecodebean.com/wp-content/uploads/2011/03/10.png"><img class="aligncenter size-full wp-image-1014" title="10" src="http://sourcecodebean.com/wp-content/uploads/2011/03/10.png" alt="" width="553" /></a></p>
<p>Now we are going to be bad and not run our unit tests before commiting and pushing the code</p>
<blockquote><p><code>PS C:\dev\appharbor\MyCoolApp&gt; git commit -a -m "a failing test"<br />
PS C:\dev\appharbor\MyCoolApp&gt; git push appharbor master</code></p></blockquote>
<p>.. and we are building again</p>
<p><a href="http://sourcecodebean.com/wp-content/uploads/2011/03/11.png"><img class="aligncenter size-full wp-image-1016" title="11" src="http://sourcecodebean.com/wp-content/uploads/2011/03/11.png" alt="" width="600" /></a></p>
<p>Woot! The build has failed!</p>
<p><a href="http://sourcecodebean.com/wp-content/uploads/2011/03/12.png"><img class="aligncenter size-full wp-image-1017" title="12" src="http://sourcecodebean.com/wp-content/uploads/2011/03/12.png" alt="" width="600" /></a></p>
<p>We can easily see that it is the test that verifies the text on the start page that has failed</p>
<p><a href="http://sourcecodebean.com/wp-content/uploads/2011/03/13.png"><img class="aligncenter size-full wp-image-1018" title="13" src="http://sourcecodebean.com/wp-content/uploads/2011/03/13.png" alt="" width="600" /></a></p>
<p>Ok, lets fix it!</p>
<p><a href="http://sourcecodebean.com/wp-content/uploads/2011/03/14small.png"><img class="aligncenter size-full wp-image-1020" title="14small" src="http://sourcecodebean.com/wp-content/uploads/2011/03/14small.png" alt="" height="212" /></a></p>
<p>Commit and push it again. And wait for the build to complete.</p>
<p><a href="http://sourcecodebean.com/wp-content/uploads/2011/03/15.png"><img class="aligncenter size-full wp-image-1021" title="15" src="http://sourcecodebean.com/wp-content/uploads/2011/03/15.png" alt="" width="600" /></a></p>
<p>Yay! Our new version built successfully!</p>
<p><a href="http://sourcecodebean.com/wp-content/uploads/2011/03/16.png"><img class="aligncenter size-full wp-image-1022" title="16" src="http://sourcecodebean.com/wp-content/uploads/2011/03/16.png" alt="" width="600" /></a></p>
<p>Lets check it out:</p>
<p><a href="http://sourcecodebean.com/wp-content/uploads/2011/03/17.png"><img class="aligncenter size-full wp-image-1023" title="17" src="http://sourcecodebean.com/wp-content/uploads/2011/03/17.png" alt="" width="600" /></a></p>
<h3><strong>Rolling back to a previous version</strong></h3>
<p>Being able to rollback an update is sometimes necessary and AppHarbor makes it ridiculously simple, all you need to do is to press the &#8220;Deploy&#8221; link next to an old version. Clicking it will build and deploy the selected version.</p>
<p><a href="http://sourcecodebean.com/wp-content/uploads/2011/03/18.png"><img class="aligncenter size-full wp-image-1024" title="18" src="http://sourcecodebean.com/wp-content/uploads/2011/03/18.png" alt="" width="600" /></a></p>
<p>We can now see that it is the first version that is active</p>
<p><a href="http://sourcecodebean.com/wp-content/uploads/2011/03/19.png"><img class="aligncenter size-full wp-image-1025" title="19" src="http://sourcecodebean.com/wp-content/uploads/2011/03/19.png" alt="" width="600" /></a></p>
<p>I love the idea behind AppHarbor, this is really how sites should be deployed! However, using AppHarbor also means giving up some control over your environment. It will be interesting to see how they handle, for example, upgrades between different versions of IIS, installation of service packs, etc. Two features i miss is the ability to run background jobs and a distributed cache to use, but hopefully it will be coming to AppHarbor. AppHarbor still a very young startup, and so far what I have seen is really impressive!</p>
]]></content:encoded>
			<wfw:commentRss>http://sourcecodebean.com/archives/appharbor-heroku-for-net/987/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>MVC 3 RTM Install Error (after upgrading to VS2010 SP1)</title>
		<link>http://sourcecodebean.com/archives/mvc-3-rtm-install-error-after-upgrading-to-vs2010-sp1/977</link>
		<comments>http://sourcecodebean.com/archives/mvc-3-rtm-install-error-after-upgrading-to-vs2010-sp1/977#comments</comments>
		<pubDate>Thu, 24 Mar 2011 18:28:54 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://sourcecodebean.com/?p=977</guid>
		<description><![CDATA[Yesterday I installed Visual Studio 2010 SP1 on my home computer. After the installation I wanted to upgrade MVC3 to the RTM version (I had the RC version installed), so i uninstalled the RC and downloaded the new version. The installation came half way, then stopped, in the log file i found this: Installation failed [...]]]></description>
			<content:encoded><![CDATA[<p>Yesterday I installed Visual Studio 2010 SP1 on my home computer. After the installation I wanted to upgrade MVC3 to the RTM version (I had the RC version installed), so i uninstalled the RC and downloaded the new version. The installation came half way, then stopped, in the log file i found this:</p>
<p><strong><em>Installation failed with error code: (0&#215;80070643)</em></strong></p>
<p>Not very helpful, but at least something to google. Then I found <a href="http://blog.mjjames.co.uk/2010/12/mvc-3-rc2-install-error-0x80070643.html">this</a> blog post. The author describes pretty much the same issue that I had, so I did what he suggested, unpacked the installer using 7-Zip and installed the MSI packages in the following order:</p>
<ol>
<li>1. aspnetwebpages.msi</li>
<li>2. aspnetwebpagesvs2010tools.msi</li>
<li>3. aspnetmvc3.msi</li>
<li>4. aspnetmvc3vs2010tools.msi</li>
<li>5. nuget.msi</li>
</ol>
<p>This happily installed MVC3 without complaining. Thanks Michael! </p>
]]></content:encoded>
			<wfw:commentRss>http://sourcecodebean.com/archives/mvc-3-rtm-install-error-after-upgrading-to-vs2010-sp1/977/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>The must have tools for C# development</title>
		<link>http://sourcecodebean.com/archives/the-must-have-tools-for-c-development/922</link>
		<comments>http://sourcecodebean.com/archives/the-must-have-tools-for-c-development/922#comments</comments>
		<pubDate>Tue, 08 Mar 2011 07:00:01 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://sourcecodebean.com/?p=922</guid>
		<description><![CDATA[Visual Studio 2010 I don&#8217;t think Visual Studio needs an introduction. VS 2010 brought us .NET 4.0 that contains a lot of new goodies! ReSharper ReSharper is the absolut must have for Visual Studio. It provides very improved navigation and search, refactoring, code generation, code cleanup and unit testing. I have gotten so used to [...]]]></description>
			<content:encoded><![CDATA[<h3><a href="http://www.microsoft.com/visualstudio/" rel="nofollow">Visual Studio 2010</a></h3>
<p>I don&#8217;t think Visual Studio needs an introduction. VS 2010 brought us .NET 4.0 that contains a lot of new goodies!  <br/><br/></p>
<h3><a href="http://www.jetbrains.com/resharper/" rel="nofollow">ReSharper</a></h3>
<p>ReSharper is the absolut must have for Visual Studio. It provides very improved navigation and search, refactoring, code generation, code cleanup and unit testing. I have gotten so used to having ReSharper installed so today I have a really hard time using Visual Studio without it.</p>
<p><strong>Some useful shortcuts</strong></p>
<ul>
<li>Ctrl+T : Go to type</li>
<li>Ctrl+Shift-T :  Go to file</li>
<li>Alt-Ins: Generate code (constructors, properties, implementing/overriding members)</li>
<li>Alt+Enter : Show available quick-fixes and context actions</li>
<li>Ctrl+Space : Symbol Code Completion</li>
<li>Ctrl+Alt+Space : Smart Code Completion</li>
<li>Shift+F12 : Find usages</li>
<li>Ctrl+Shift+R : Refactor this</li>
<li>Ctrl+R+R : Rename this</li>
<li>Ctrl+R+M: Extract method</li>
<li>Ctrl+R+V : Introduce variable</li>
<li>Ctrl+R+F : Introduce field </li>
<li>Ctrl+R+P : Introduce parameter</li>
<li>Alt+Shift+L : Locate in Solution Explore</li>
</li>
<p><br/></p>
<h3><a href="http://visualstudiogallery.msdn.microsoft.com/e5f41ad9-4edc-4912-bca3-91147db95b99/" rel="nofollow" >PowerCommands for Visual Studio 2010</a></h3>
<p>PowerCommands 10.0 is a set of useful extensions for the Visual Studio 2010 adding additional functionality to various areas Visual Studio. This is some of the commands I find must useful: </p>
<ul>
<li>Copy References</li>
<li>Paste References</li>
<li>Copy As Project Reference</li>
<li>Edit Project File</li>
<li>Open Containing Folder</li>
<li>Open Command Prompt Here</li>
<li>Unload Projects</li>
<li>Reload Projects</li>
</ul>
<p><br/></p>
<h3><a href="http://">Visual Studio 2010 &#8211; Productivity Power Tools</a></h3>
<p>Productivity Power Tools from the Microsoft Visual Studio team contains a lot of great extensions</p>
<ul>
<li>Very much improved dialog for add reference</li>
<li>Solution Naigator &#8211; a enhanced  Solution Explorer</li>
<li>Improved tab system for editor</li>
<li>Highlight current line (isn&#8217;t it weird that VS is missing this feature?)</li>
</ul>
<p>One annoying &#8216;feature&#8217; is the triple click to navigate to source, never got it to work properly so I disabled it. Some shortcuts also colide with ReSharper so some tweaking is needed to make it work perfectly.</p>
]]></content:encoded>
			<wfw:commentRss>http://sourcecodebean.com/archives/the-must-have-tools-for-c-development/922/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Thoughts on the Nokia / Windows Phone 7 deal</title>
		<link>http://sourcecodebean.com/archives/thoughts-on-the-nokia-windows-phone-7-deal/902</link>
		<comments>http://sourcecodebean.com/archives/thoughts-on-the-nokia-windows-phone-7-deal/902#comments</comments>
		<pubDate>Tue, 01 Mar 2011 07:28:21 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[Mobile]]></category>

		<guid isPermaLink="false">http://sourcecodebean.com/?p=902</guid>
		<description><![CDATA[For the last week the news that Nokia has chosen Windows Phone 7 as their main operating system for all their new smart phones has been all over the news. A lot of people are very upset and say that Nokia should have gone with Android instead, or that they should have invested more in [...]]]></description>
			<content:encoded><![CDATA[<p>For the last week the news that Nokia has chosen Windows Phone 7 as their main operating system for all their new smart phones has been all over the news. A lot of people are very upset and say that Nokia should have gone with Android instead, or that they should have invested more in their own linux os Meego. Personally, looking at the options Nokia had, I think this is might be the best move Nokia could have done. Let me explain why. <span id="more-902"></span></p>
<p>For years Nokia has been struggling keeping Symbian alive. Symbian is an old operating system and has well served it purpose in millions of Nokia phones. However, during the last few years Nokia has totally failed on innovating, pretty much every other vendor now have smart phones running Andriod that offers more features and a better user experience then what Nokia can offer. Nokia needed to make a drastic change and the choices at this time were not that many.</p>
<ul>
<li>Meego</li>
<li>Android</li>
<li>Windows Phone 7</li>
</ul>
<p>Meego is Nokias in house developed linux based operating system. The vision behind Meego is great, but development has been too slow. From what I have seen of Meego it is a great operating system if you want to customize and geek around with your phone, but it is not yet ready for prime time. Another problem with Meego is that it is lacking an ecosystem of applications and developers (such as App store for IPhone or Android Market). Placing the bet on Meego would have been extremely risky and Nokia knows this. </p>
<p>Android seemed like the most obvious choice for Nokia. This is what most other vendors are offering and there is a big ecosystem around Android. The problem with it is Nokias limited way of differentiating themselves, they do not just want to become another &#8220;Android vendor&#8221;. Sure there are several ways they could have differentiated themselves on Android, just like HTC and Samsung are trying to do, but it seems like this was not enough for Nokia. </p>
<p>Windows Phone 7 was released at MWC in 2010 and is a totally new operating system from Microsoft. It is based on Windows Embedded Compact 7 with a new UI layer called Metro. It does not inherit anything from the horrible mess that was Windows Mobile 6.  The deal Nokia made with Microsoft gives Nokia &#8216;special privileges&#8217; to WP7, that other manufactures do not get. It is not clear yet exactly what this means, but what is clear is that this will make Nokia <strong>the main WP7 vendor</strong> on the market, clearly better than being just another Android vendor. </p>
<p>So will it be successful? Time will tell, but Microsoft has put a lot of money into WP7 and will not let it fail. At the same time Nokia has a reach that no other cell phone vendor has (they are still the by far biggest manufacturer of phones in the world), so if they are able to successfully build an application ecosystem around WP7 I think it will be a success and we will get a mobile space with three main players, Apple iOS, Android and WP7. Competition is good and vendors need other choices than Android for their phones (iOS is apple only, so in a way it does not play the same game).</p>
<p>For C# developers, such as my self, i think getting full backing from Nokia is good news. I would love to develop applications on the Windows Phone 7 platform. From what I have seen from the tools it integrates very well with Visual Studio and seems to be a lot of fun to work with. Maybe now is the time to start learning it?</p>
]]></content:encoded>
			<wfw:commentRss>http://sourcecodebean.com/archives/thoughts-on-the-nokia-windows-phone-7-deal/902/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Entity Framework 4 &#8211; Code First</title>
		<link>http://sourcecodebean.com/archives/entity-framework-4-code-first/672</link>
		<comments>http://sourcecodebean.com/archives/entity-framework-4-code-first/672#comments</comments>
		<pubDate>Mon, 04 Oct 2010 21:57:57 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://sourcecodebean.com/?p=672</guid>
		<description><![CDATA[Entity Framework 4 itself is interesting but what really got me hooked was the CTP 4 preview, and what Microsoft call &#8220;Code First&#8221;. Code First allows us to describe our model using regular POCO classes, instead of designing our model using either the EF editor or creating in directly in the database. A very simple [...]]]></description>
			<content:encoded><![CDATA[<p>Entity Framework 4 itself is interesting but what really got me hooked was the CTP 4 preview, and what Microsoft call &#8220;Code First&#8221;. Code First allows us to describe our model using regular POCO classes, instead of designing our model using either the EF editor or creating in directly in the database. </p>
<p><span id="more-672"></span><br />
A very simple example of this would be to create a two classes to store blog entries and comments.</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; public class BlogEntry</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; public <span class="kw4">int</span> Id <span class="br0">&#123;</span> get; set; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; public <span class="kw4">string</span> Title <span class="br0">&#123;</span> get; set; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; public <span class="kw4">string</span> Body <span class="br0">&#123;</span> get; set; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; public virtual ICollection&lt;Comment&gt; Comments <span class="br0">&#123;</span> get; set; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; public class Comment</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; public <span class="kw4">int</span> Id <span class="br0">&#123;</span> get; set; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; public <span class="kw4">string</span> Name <span class="br0">&#123;</span> get; set; <span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; public <span class="kw4">string</span> Text <span class="br0">&#123;</span> get; set; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p>As you can see, this is regular C# classes, no decorators or inheritance involved. Now we need a class that will access the database:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; public class Db : DbContext</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; public DbSet&lt;BlogEntry&gt; BlogEntries <span class="br0">&#123;</span> get; set; <span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; public DbSet&lt;Comment&gt; Comments <span class="br0">&#123;</span> get; set; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p>To try out storing and retreiving some values, i created a simple console application:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">using System;</div>
</li>
<li class="li1">
<div class="de1">using System.<span class="me1">Collections</span>.<span class="me1">ObjectModel</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">namespace ConsoleApplication1</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; class Program</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">static</span> <span class="kw4">void</span> Main<span class="br0">&#40;</span><span class="kw4">string</span><span class="br0">&#91;</span><span class="br0">&#93;</span> args<span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var db = new Db<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// Create a new video</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var blogEntry = new BlogEntry<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span> Title = <span class="st0">&quot;Coding some EF in VS2010&quot;</span>, Body = <span class="st0">&quot;Pretty sweet!&quot;</span> <span class="br0">&#125;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; blogEntry.<span class="me1">Comments</span> = new Collection&lt;Comment&gt;<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; blogEntry.<span class="me1">Comments</span>.<span class="me1">Add</span><span class="br0">&#40;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Comment<span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Name = <span class="st0">&quot;Han Solo&quot;</span>, </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Text = <span class="st0">&quot;Great article, will show it to Chewbacca&quot;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; blogEntry.<span class="me1">Comments</span>.<span class="me1">Add</span><span class="br0">&#40;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Comment<span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Name = <span class="st0">&quot;Darth Wader&quot;</span>, </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Text = <span class="st0">&quot;Good work my son, you are just learning to know the power of the dark side&quot;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; db.<span class="me1">BlogEntries</span>.<span class="me1">Add</span><span class="br0">&#40;</span>blogEntry<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; db.<span class="me1">SaveChanges</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// Loop videos and </span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach <span class="br0">&#40;</span>var v in db.<span class="me1">BlogEntries</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.<span class="me1">WriteLine</span><span class="br0">&#40;</span><span class="st0">&quot;BlogEntry Id: &quot;</span> + v.<span class="me1">Id</span> + <span class="st0">&quot;, Title: &quot;</span> + v.<span class="me1">Title</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.<span class="me1">WriteLine</span><span class="br0">&#40;</span><span class="st0">&quot;Comments:&quot;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach <span class="br0">&#40;</span>var tag in v.<span class="me1">Comments</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.<span class="me1">WriteLine</span><span class="br0">&#40;</span><span class="st0">&quot; &nbsp;Comment Id: &quot;</span> + tag.<span class="me1">Id</span> + <span class="st0">&quot;, Name: &quot;</span> + tag.<span class="me1">Name</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p>Executing the program prints (not very surprising):</p>
<blockquote><p>PS > .\ConsoleApplication1.exe<br />
BlogEntry Id: 1, Title: Coding some EF in VS2010<br />
Comments:<br />
  Comment Id: 1, Name: Han Solo<br />
  Comment Id: 2, Name: Darth Wader
</p></blockquote>
<p>Now to the interesting part, what really happened? </p>
<p>If we start SQL Management Studio and connects to my local SQL server express instance we can see that Entity Framework has automatically mapped the POCO objects to tables and setup the corresponding relations.</p>
<p><a href="http://sourcecodebean.com/wp-content/uploads/2010/10/sqlexpress.png"><img src="http://sourcecodebean.com/wp-content/uploads/2010/10/sqlexpress.png" alt="" title="sqlexpress" width="345" height="278" class="aligncenter size-full wp-image-680" /></a></p>
<p>And we can see that the data got stored:<br />
<a href="http://sourcecodebean.com/wp-content/uploads/2010/10/sqlexpress1.png"><img src="http://sourcecodebean.com/wp-content/uploads/2010/10/sqlexpress1.png" alt="" title="sqlexpress" width="532" height="492" class="aligncenter size-full wp-image-682" /></a></p>
<p>So far so good, now we come to the part where EF code first isn&#8217;t really mature enough yet. Say we want to make a small change to our Comment class, add a new property for example. The application will compile without compiling but the next time we run the application it will throw the following exception:</p>
<blockquote><p>Unhandled Exception: System.InvalidOperationException: The model backing the &#8216;Db&#8217; context has changed since the database<br />
 was created.  Either manually delete/update the database, or call Database.SetInitializer with an IDatabaseInitializer<br />
instance.  For example, the RecreateDatabaseIfModelChanges strategy will automatically delete and recreate the database,<br />
 and optionally seed it with new data.<br />
   at System.Data.Entity.Infrastructure.CreateDatabaseOnlyIfNotExists`1.InitializeDatabase(TContext context)<br />
   at System.Data.Entity.Infrastructure.Database.Initialize()<br />
   at System.Data.Entity.Internal.InternalContext.Initialize()<br />
   at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)<br />
   at System.Data.Entity.Internal.Linq.EfInternalQuery`1.Initialize()<br />
   at System.Data.Entity.Internal.Linq.EfInternalQuery`1.GetEnumerator()<br />
   at System.Data.Entity.Infrastructure.DbQuery`1.System.Collections.Generic.IEnumerable<TEntity>.GetEnumerator()<br />
   at ConsoleApplication1.Program.Main(String[] args) in C:\Dev\ConsoleApplication1\ConsoleApplication1\Program.cs:line<br />
33
</p></blockquote>
<p>Which means that we pretty much has to delete the database and recreate it after every change we make &#8211; definitely not how i would prefer to work. </p>
<p><strong>Conclusion</strong><br />
EF code first looks like a step in the right direction, but in order to make it usefull EF must provide automatic database migrations without loosing data, both for development use and for production use. </p>
]]></content:encoded>
			<wfw:commentRss>http://sourcecodebean.com/archives/entity-framework-4-code-first/672/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>First steps toward EPiServer and ASP.NET MVC</title>
		<link>http://sourcecodebean.com/archives/first-steps-toward-episerver-and-asp-net-mvc/595</link>
		<comments>http://sourcecodebean.com/archives/first-steps-toward-episerver-and-asp-net-mvc/595#comments</comments>
		<pubDate>Mon, 19 Apr 2010 20:33:38 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[EPiServer]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://sourcecodebean.com/?p=595</guid>
		<description><![CDATA[Recently I started a new assignment at a customer where we are going to build their new public web site and their intranet, both of them based on EPiServer CMS 6. The person leading the project is very found of ASP.NET MVC, so my first task was to experiment with EPiServer and ASP.NET MVC, to [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I started a new assignment at a customer where we are going to build their new public web site and their intranet, both of them based on EPiServer CMS 6. The person leading the project is very found of ASP.NET MVC, so my first task was to experiment with EPiServer and ASP.NET MVC,  to try to get a running prototype up. </p>
<p>I did some research online to see what others had done. I found two great posts by <a href="http://joelabrahamsson.com/entry/episerver-and-mvc-retrieving-current-page-using-a-custom-model-binder">Joel Abrahamsson</a> and one from <a href="http://fbrz.wordpress.com/2009/10/10/episerver-and-mvc/">Fabio Fabrizio</a>, who based his solution on Joels experiments. </p>
<p>Joels first attempt was to create a base class from which all Controllers inherited. However, this made the controller do more than it should and made it very hard to test. His second attempt was to create a custom model binder and make every action receive currentPage as a parameter. This approach makes it much easier to create unit tests for the code, since it is now possible to moc the currentPage parameter. </p>
<p>Fabio took a different approach and decided to implement a custom MVC handler and a controller factory. This approach fits very well with MVC and felt like the right way to go, so I decided to create a prototype based on Fabios code. Fabios code was based on EPiServer CMS 5 and ASP.NET MVC 1.0, but I wanted to use EPiServer 6 and ASP.NET MVC 2.0, so I started porting the solution.</p>
<p>Porting the solution to EPiServer 6 and ASP.NET MVC 2.0 required some changes to both code and configuration, which not all of them was trivial. At the moment I have a prototype up running, and it works remarkably well! I will continue my experiments and post my results.</p>
]]></content:encoded>
			<wfw:commentRss>http://sourcecodebean.com/archives/first-steps-toward-episerver-and-asp-net-mvc/595/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic
Page Caching using disk: enhanced

Served from: sourcecodebean.com @ 2012-02-06 06:29:56 -->
