<?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>Elegant Code &#187; Chris Brandsma</title>
	<atom:link href="http://elegantcode.com/author/brandsma/feed/" rel="self" type="application/rss+xml" />
	<link>http://elegantcode.com</link>
	<description></description>
	<lastBuildDate>Sat, 11 Feb 2012 04:39:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.5</generator>
		<item>
		<title>Marines vs. Boy Scouts</title>
		<link>http://elegantcode.com/2012/01/16/marines-vs-boy-scouts/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=marines-vs-boy-scouts</link>
		<comments>http://elegantcode.com/2012/01/16/marines-vs-boy-scouts/#comments</comments>
		<pubDate>Mon, 16 Jan 2012 17:08:00 +0000</pubDate>
		<dc:creator>Chris Brandsma</dc:creator>
				<category><![CDATA[Esoterica]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2012/01/16/marines-vs-boy-scouts/</guid>
		<description><![CDATA[What I’m talking about here are two styles of development.&#160; And this isn’t an “us vs. them”, this is a “me, myself, I and all of yall”.&#160; We all use both of these patterns in developing code, and will even switch patterns in the same product from feature to feature.&#160; Anyway, here are your definitions: [...]]]></description>
			<content:encoded><![CDATA[<p>What I’m talking about here are two styles of development.&#160; And this isn’t an “us vs. them”, this is a “me, myself, I and all of yall”.&#160; We all use both of these patterns in developing code, and will even switch patterns in the same product from feature to feature.&#160; Anyway, here are your definitions:</p>
<p><strong>Boy Scout</strong>:&#160; “Leave thing better than you found them”</p>
<p><strong>Marine</strong>: “Get in, get out, leave no trace behind.”</p>
<p>Sounds simple right?&#160; So where does this apply?&#160; Starting with the Marine.&#160; I more frequently use this when dealing with someone else’s code, or something I just need to hack out real quick.&#160; Often you don’t plan on maintaining the code, probably won’t own it, or you are just following the given pattern that is already established in the code – no matter how bad it is.&#160; Typically you are just trying to get in and out without your name being attached.&#160; You will also hear the adage “if it ain’t broke don’t fix it” commonly among staunch practitioners.&#160; You can maintain code like this for a while, but if used too often it will come back to haunt you.&#160; Each feature becomes harder to add, unexpected behaviors crop up, and performance problems creep in.</p>
<p>Now the Boy Scout.&#160; You are adding a feature and find a mess.&#160; The code is working, but is very unmaintainable.&#160; Instead of just adding your feature, you tear the code apart and refactor the daylights out of the code.&#160; Once complete and all is in order, then you add your feature.&#160; It took you three times longer than expected to get the feature added, but you might be saving time in the future, assuming you will need to work with the code again.&#160; Unfortunately, it is also very possible to get over your head in a hurry.&#160; It would be great to have some unit tests to fall back on in this situation … but I haven’t seen that yet.&#160; A good thing to do before you start a boy scout trip is to warn people where you are going, how long you’ll be there, and when to send for help.</p>
<h2>Pointers</h2>
<p>Now, lets not get too far ahead of ourselves here, there is absolutely a time and place for each approach.&#160; It should also not be too hard to understand which is correct – but I’ll give some guidance anyway.&#160; Here are some points.</p>
<ol>
<li>If the code in question will be rarely used by one guy in the basement and doesn’t own a gun, marine mission till the cows come home.</li>
<li>If the code is non-critical, proof of concept, spike, or the like and can get everything done in less than 4 hours, marine mission.</li>
<li>If you are a consultant brought in to add one feature and supplement a team with a strange style but is currently working for them, marine mission.&#160; Otherwise you will be “that guy”, meaning the one that didn’t fit in.&#160; Sometime it pays to just go with the flow.</li>
<li>If the code is critical path, a essential web page seen by every customer you have: Boy Scout, and continue to Boy Scout.</li>
<li>If the code is already a buggy mess, the customers hate you, your product, and your car – role up your sleeves and get to work.</li>
</ol>
<p>End result, either approach can get you in trouble when used inappropriately.&#160; You need sound judgment to figure this out.&#160; But then again, if software development was easy everyone would do it.</p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2012/01/16/marines-vs-boy-scouts/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Thoughts on MicroOrms</title>
		<link>http://elegantcode.com/2011/08/02/thoughts-on-microorms/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=thoughts-on-microorms</link>
		<comments>http://elegantcode.com/2011/08/02/thoughts-on-microorms/#comments</comments>
		<pubDate>Wed, 03 Aug 2011 04:21:29 +0000</pubDate>
		<dc:creator>Chris Brandsma</dc:creator>
				<category><![CDATA[Esoterica]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2011/08/02/thoughts-on-microorms/</guid>
		<description><![CDATA[I’ve been spending some time getting my head wrapped around the idea of MicroOrms lately.&#160; A lot of this has to do with some of the scalability/caching issues I’ve been having with NHibernate.&#160; Don’t get me wrong, I still love NHibernate, but it can be a bit of a beast when you are really trying [...]]]></description>
			<content:encoded><![CDATA[<p>I’ve been spending some time getting my head wrapped around the idea of MicroOrms lately.&#160; A lot of this has to do with some of the scalability/caching issues I’ve been having with NHibernate.&#160; Don’t get me wrong, I still love NHibernate, but it can be a bit of a beast when you are really trying to drive performance, there is a lot of overhead in there.&#160; Besides, my general rule has been a good ORM can handle 80-90% of your queries, and then performance tune out as needed.&#160; I’m at the performance tuning part now.</p>
<p>BTW: if you are using Entity Framework you are in the same boat.</p>
<p>So the question is, what to do with that last 10-20% that you need performance on?&#160; Previously I had gone straight to Ado.Net, or used my own <a href="http://fluentado.codeplex.com/">FluentAdo</a> project.&#160; But a few new projects have dropped in on the scene and it was time to take a look.</p>
<p>Let’s start off with some requirements: </p>
<ol>
<li>Don’t be a pain to setup.</li>
<li>Don’t hork up my domain model (I have NHibernate for that).&#160; POCO preferred.</li>
<li>Be able handle multiple result sets (something NHibernate does not do well)</li>
<li>Preference executing raw sql, not an api.&#160; </li>
</ol>
<ol>I don’t need:
<li>A custom query api for constructing sql. </li>
<li>Change tracking</li>
<li>Caching </li>
<li>Lazy Loading </li>
<li>Automatically loading child objects </li>
</ol>
<ol>Basically, be simple, be light, be fast, don’t be another NHibernate.&#160; </ol>
<p>Now the Contenders:</p>
<ul>
<ul>
<li><font style="background-color: #ffffff">Dapper </font></li>
<li>Simple.Data</li>
<li>Massive</li>
<h3>
<ul></ul>
<ul>Dapper</ul>
</h3>
<p><a href="http://code.google.com/p/dapper-dot-net/">Dapper</a> is my current favorite.&#160; I deals with POCO objects, setup is very simple, very little overhead and ceremony.&#160; That said, it is probably the rawest for dealing with sprocs.&#160; But for a bare metal way of loading domain objects, this guy is hard to beat.</p>
<p> Here is a sample query:</ul>
<p>var customer = _connection.Query&lt;Customer&gt;(     <br />&#160;&#160;&#160; &quot;select * from dbo.Customers where CustomerId = @id&quot;,      <br />&#160;&#160;&#160; new {id = 1})      <br />&#160;&#160;&#160; .FirstOrDefault();</p>
<p>Basically, Dapper handles all of my requirements with ease.&#160; I almost forgot to mention, Dapper is compatible with .Net 4.0 and 3.5.&#160;&#160; That is a huge win for me.&#160; Plus, the guys on Stack Overflow are using it, which makes for an easy “sell” to my management team.</p>
<p>&#160;</p>
<h3>Simple.Data</h3>
<ul><a href="https://github.com/markrendle/Simple.Data">Simple.Data</a> is also very interesting.&#160; It makes extensive use of .net 4.0 and the dynamic.&#160; Simple.Data is fairly ORM’ish, in that it seems to scan your database schema and dynamically add methods for retrieving data.&#160; But it still uses POCO classes so I am good with that.</ul>
<p> Here is a sample query:</ul>
<p>var db = Database.Open();   <br />var customer = db.Customers.FindByCustomerId(1);</p>
<p>To get that to work I defined the customer class with a CustomerID…and that is it.&#160; Dapper came up with the FindByCustomerId part.&#160; It also adds stored procs as well, so if I have a sproc called “GetCustomers”, I can call:</p>
<p>db.GetCustomers()</p>
<p>It is just scary what is going on here.&#160; Granted there is a down-side to this system – albiet minor.&#160; There is a bit of overhead while Simple.Data scans your database schema and gets setup.</p>
<p>Verdict: I don’t mind Simple.Data, but the .Net 4.0 only part is a problem, plus it strays a bit into becoming a ORM – a bit too much API, but nothing obnoxious.</p>
<h3>Massive</h3>
<p>I should have known how this one would turn out ahead of time, but I really didn’t want to believe it.&#160; When I was young and foolish I was a SubSonic guy.&#160; Truth is, I read Rob Connery’s blog religiously.&#160; But I moved passed SubSonic and onto NHibernate and never looked back.&#160; Heck, I moved passed Active Record and onto Repository Pattern.&#160; (Since then I’ve moved on again to Query/Command Pattern, but another time for that).</p>
<p>First off, Massive horks up my domain model.&#160; Each entity has to inherit from DynamicModel.&#160; So before my model looked like this:</p>
<p>class Customer   <br />{    <br />&#160;&#160;&#160; public int CustomerId { get; set; }    <br />&#160;&#160;&#160; public string Name { get; set; }    <br />&#160;&#160;&#160; public string Address { get; set; }    <br />}</p>
<p>Now, Massive-fied:</p>
<p>class Customer: DynamicModel   <br />{    <br />&#160;&#160;&#160; public Customer(): base(&quot;Default&quot;)    <br />&#160;&#160;&#160; {    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; TableName = &quot;Customers&quot;;    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; PrimaryKeyField = &quot;CustomerId&quot;;    <br />&#160;&#160;&#160; }</p>
<p>&#160;&#160;&#160; public int CustomerId { get; set; }   <br />&#160;&#160;&#160; public string Name { get; set; }    <br />&#160;&#160;&#160; public string Address { get; set; }    <br />}</p>
<p>And that “Default” in there, that is the name of my connection string.&#160; Wonderful.&#160; Also, because my table names are plural, I have to specify the name of my table for them in EVERY SINGLE CLASS.</p>
<p>Now, how to query:</p>
<p>var tbl = new Customer();   <br />var customer = tbl.Single(1);</p>
<p>Smack-dab, back to Active Record.</p>
<p>Also, I didn’t see anything on how to parse multiple result sets.</p>
<p>Go to <a href="http://blog.wekeroad.com/helpy-stuff/and-i-shall-call-it-massive">Rob’s site and you will see more samples</a>.&#160; Needless to say, it is off my list.&#160; Granted, Massive is still much less overhead than NHibernate and Entity framework, but there is too much API there for me, too much setup, I don’t get my POCOs, etc.</p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2011/08/02/thoughts-on-microorms/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
		<item>
		<title>So what was I thinking anyway?</title>
		<link>http://elegantcode.com/2011/04/01/so-what-was-i-thinking-anyway/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=so-what-was-i-thinking-anyway</link>
		<comments>http://elegantcode.com/2011/04/01/so-what-was-i-thinking-anyway/#comments</comments>
		<pubDate>Sat, 02 Apr 2011 01:26:28 +0000</pubDate>
		<dc:creator>Chris Brandsma</dc:creator>
				<category><![CDATA[Esoterica]]></category>

		<guid isPermaLink="false">http://elegantcode.com/?p=4228</guid>
		<description><![CDATA[Ever start a project just to learn something new?  But to do it, you have to have an app idea to throw around it?  I had one of those.  I&#8217;ve been doing a wee bit of iOS development over the past year, and I found I wanted to learn about iAds, in App Purchasing, talking between [...]]]></description>
			<content:encoded><![CDATA[<p>Ever start a project just to learn something new?  But to do it, you have to have an app idea to throw around it?  I had one of those.  I&#8217;ve been doing a wee bit of iOS development over the past year, and I found I wanted to learn about iAds, in App Purchasing, talking between devices (via BlueTooth), and just get better at coding in general.</p>
<p>I did just that.  I created an IPhone/IPad app called <a href="http://www.diamondbsoftware.com/index.php/agile-cards/">Agile Cards</a>.  You can read up on it if you like.  Agile Cards is your basic card estimation app for the IPhone.  Real original, I know.  I think my competition is about a dozen other apps.  The only thing Agile Cards does that the others do not (that I know of) is use an IPad as a server.  This is optional of course.  So if you have several IPhones and an IPad you can have a digitally dynamic Scrum Estimation.</p>
<p>So what was I thinking?  I was thinking I needed practice.</p>
<p>I knew what I wanted to practice, so I thought up an app to do that with.  Now the great thing about the App Stores (for Apple, Android, and WinPhone 7), is that you can &#8220;easily&#8221; take these little apps that you are writing and sell them.  I&#8217;ve had dozens of apps that I&#8217;ve done for exactly the same purpose (to learn), but never had an easy route for selling them.  And setting up an online store is just a big pain as far as I&#8217;m concerned.</p>
<p>But I put easily in quotes up there.  There are a few caveats to my statement above.  I had a designer that wanted to do some more IPhone designing.  I&#8217;ve helped him with apps as well, so he was happy to help out.  If  you are going to go onto an app store, you will/should spend more time on user experience than you normally would, especially if you were just writing the app for yourself.</p>
<p>There is also the hassle of dealing with the app store requirements.  You need icons, splash screens, verbiage (which I kind of suck at), a web site, screen shots, email addresses, etc.  I even setup a Google Voice account for it.   All this for a free app?  (OK, there are ad, but free none the less).</p>
<p>What was I thinking?  I was thinking I also needed the practice of finishing an app.</p>
<p>There is nothing wrong with starting an app and throwing it away, or just stopping when you don&#8217;t need to go any further.  You end up with a program with a few quirks (bugs) that never get fixed, but you are probably the only person using it anyway.  That is fine.  But every now and then you need to FINISH something.  By FINISH, I mean shipping quality.  Shipping quality is hard btw.  It takes more time.  You need other people looking at things.</p>
<p>This is to remind you that software can be hard.  We deal with details, lots of them.  Selling software to other people means dealing with even more details.  It is good to do every now and then.  On your own.</p>
<p>So what was I thinking?  I thought it sounded like fun.</p>
<p>And by fun, like running a marathon half prepared fun.  BTW: I never did get to the &#8220;In app purchasing&#8221; bit.   I guess that can be next time.  It was to remove the ads.  I never have released an app with every single feature I ever wanted in it.</p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2011/04/01/so-what-was-i-thinking-anyway/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Book Review: Brain Rules</title>
		<link>http://elegantcode.com/2010/11/20/book-review-brain-rules/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=book-review-brain-rules</link>
		<comments>http://elegantcode.com/2010/11/20/book-review-brain-rules/#comments</comments>
		<pubDate>Sun, 21 Nov 2010 00:28:50 +0000</pubDate>
		<dc:creator>Chris Brandsma</dc:creator>
				<category><![CDATA[Esoterica]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2010/11/20/book-review-brain-rules/</guid>
		<description><![CDATA[This is not your usual book review that you would expect on a site like this.&#160; First off, the book does not talk about code, software, or software developers even once.&#160;&#160; But, almost everything in this book is applicable to what we do as software developers, managers, project managers, and leads. The book, Brain Rules [...]]]></description>
			<content:encoded><![CDATA[<p>This is not your usual book review that you would expect on a site like this.&#160; First off, the book does not talk about code, software, or software developers even once.&#160;&#160; But, almost everything in this book is applicable to what we do as software developers, managers, project managers, and leads.</p>
<p>The book, Brain Rules by John Medina, is about the brain and what is actually know about.&#160; I like the author’s criteria for what went in the book.&#160; He called it the “Cranky Scientist Rule.”&#160; Nothing is in the book that hasn’t been published in a scientific journal, peer reviewed, and duplicated by someone else.&#160; I liked that as well.</p>
<p>Each chapter, and there are 12 of them, are about what the brain needs, and how best to give the brain what it needs.&#160; There are chapters specific to Sleep, Stress, Short term memory, long term memory, and gender.&#160; There are few absolutes in the book (except multitasking, he says you can’t do it and wants to ban texting in cars – I’ll grant him that one), but LOTS of pointers.&#160; Also, there are plenty of asides where he talks about actual people with interesting conditions you should really hope you don’t get (but probably wouldn’t notice if you did).</p>
<p>And for a book on the brain, it is not dry at all.&#160; The Author is a VERY effective writer, and judging from his credentials, probably an excellent teacher as well.&#160; If you have a few minutes, I highly recommend checking out his web site: <a href="http://BrainRules.com">BrainRules.com</a> and watch some of his videos.</p>
<p><a href="http://www.amazon.com/Brain-Rules-Principles-Surviving-Thriving/dp/0979777747/ref=sr_1_1?s=books&amp;ie=UTF8&amp;qid=1290299287&amp;sr=1-1">Check out the book as well at amazon</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2010/11/20/book-review-brain-rules/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Asp.Net MVC: My Personal View Rules</title>
		<link>http://elegantcode.com/2010/07/05/asp-net-mvc-my-personal-view-rules/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=asp-net-mvc-my-personal-view-rules</link>
		<comments>http://elegantcode.com/2010/07/05/asp-net-mvc-my-personal-view-rules/#comments</comments>
		<pubDate>Mon, 05 Jul 2010 23:10:02 +0000</pubDate>
		<dc:creator>Chris Brandsma</dc:creator>
				<category><![CDATA[Asp.Net MVC]]></category>

		<guid isPermaLink="false">http://elegantcode.com/?p=3641</guid>
		<description><![CDATA[I&#8217;ve been working with a team of guys on several Asp.Net MVC projects since October of 2009.  While that isn&#8217;t the greatest amount of time, and I&#8217;m still no expert, I thought I&#8217;d jot down a few of the practices that we have developed to help make coding a bit smoother.  Asp.Net MVC, as with [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been working with a team of guys on several Asp.Net MVC projects since October of 2009.  While that isn&#8217;t the greatest amount of time, and I&#8217;m still no expert, I thought I&#8217;d jot down a few of the practices that we have developed to help make coding a bit smoother.  Asp.Net MVC, as with every new technology can be used poorly, and when use poorly you try to identify why that code was bad, how it could have been done better.</p>
<p>First, lets think about what the view should be doing &#8211; in a single responsibility sort of way: turn data into html.  That right there rules out several options.  No retrieving data, no extra data transformations.  Just turn some data into html.  And frankly, that is complicated enough.  So a side goal that I strive for is to create a markup page (the aspx) that is similar to the desired html output.  The main reason for that side goal is to make double checking the output that much easier.  I want to see a &#8216;div&#8217; in my markup, and have a reasonable idea where that &#8216;div&#8217; will show up in the html.</p>
<ol>
<li><strong>Keep as much code out of your views as you can.</strong></li>
</ol>
<p>Don&#8217;t make this rule overly simplistic.  Some code belongs in the view.  A &#8216;for&#8217; loop to create a table, a simple &#8216;if&#8217; block to show administrator functionality, stuff like that.  But you shouldn&#8217;t be having to specify the DateTime format, or string parsing.  That is what the ViewModels should do for you.  Rule of thumb, if you see a block where there is more C# than HTML, you probably did it wrong.</p>
<p>I will also extend this rule to JavaScript as well.  I&#8217;ve talked about the why JavaScript should not be in the views before, so this should not be a shock.  JavaScript belong in separate files.  Period.</p>
<p>2. <strong>Make Views typed.</strong></p>
<p>This is true for all views where you have to pass data from the controller to the view.  Make a View Model for the view and pass data via that model.  This opens up a whole host of better patterns for you, like typed HtmlHelpers.  As a result, it is VERY rare that I will share a view model between views, or even Controller Actions.  I make separate models for GET, POST, and DELETE.  I guess my view is, the more the merrier.</p>
<p>3.   <strong>Make the View Models specific to the needs of the view.</strong></p>
<p>OK, this isn&#8217;t actually a View best practice, but it is highly related.  If you try to keep the model for the view too generic, you end up with a lot of logic in the view to transpose the data into something useful.  The key point is that the data in the model serves the view, so all of the work to get the data into the correct format should be done when putting the data into  the model.  I will often take this to the point where the model will give html elements in the view their CSS classes.  So that means I have more than data from the database in the views.</p>
<p>Side note: when it comes to populating View Models with data specific for the View, <a href="http://automapper.codeplex.com/">AutoMapper</a> rocks!  That is all.</p>
<p>4.  <strong>Custom Html Helpers are wonderful things</strong></p>
<p>It is remarkably simple to create your own Html Helper, and once you get the hang of them they are beautiful.  They are wonderful little ways to encapsulate a small amount of logic so you can get it out of the aspx view.  Use them to encapsulate small amounts of code you need in various place through the project.</p>
<p>Another little &#8220;trick&#8221; I will use from time to time to create custom models just for a html helper (passed in via the view&#8217;s view model).  I have a few places where I need to change the markup because of the browser being used&#8230;so I create a custom helper that can detect the browser.</p>
<p>5. <strong>Standard HTML Helpers are great, but remember html</strong></p>
<p>The key point I&#8217;m trying to make here is to become familiar with the output of the standard HTML helpers.  While the helpers can be great, they have their warts (anything having to do with an attribute name/value is a bit ugly).  Sometimes it is easier to swap them out with the standard html (especially with inputs) to get the exact output you want.  As a bonus, it is easier for the next guy coming in to figure out what you were after.   Currently, I&#8217;d say I use the helpers about 50% of the time over raw HTML.</p>
<p>Now, typing the html, or the helper, still kind of stinks.  You have to type the same code over and over.  Take a look at what <a href="http://code.google.com/p/zen-coding/">Zen-Coding</a> does.  You can do the same thing with ReSharper Templates or Visual Studio snippets&#8230;or just install one of the ReSharper or Visual Studio pluggins.  But beyond that, there is an art to customizing Visual Studio that you should learn.</p>
<p>6. <strong> Wrap all links in Url.Content and Url.Action</strong></p>
<p>You have a web app.  You have to navigate between pages, call web services, link in javascript and CSS.  That is just what we do.  All of those links should be wrapped in Url.Content or Url.Action helpers.   The problem that is easy to run into is you move from development to test and the base url for your application changes.  You were testing at http://localhost:898989/ , and now you are deployed to http://myserver/myapp/  and a whole lot of urls just stopped working.  Url.Content and Url.Action are supposed to fix that.  That is why you use them.</p>
<p>7.  <strong>Get to know Partial Views for Ajax calls.</strong></p>
<p>Partial views are actually just views that don&#8217;t have master pages and the html/body tag sections.  Partials can be executed in a multitude of ways, not just from inside a view on the server, but also from Javascript on the browser. JQuery also has a wonderful little method called $.load that will call a url, take the html that is returned, and slap it into the page.  This can greatly simplify a lot of behaviors.</p>
<p>I subtle little trick I sometimes do with this this is to wrap a section of code that takes a long time to load in a partial.  I will then call that partial into my page AFTER is has loaded on the browser (using the JavaScript function setTimeout to call the $.load ).  Now I get  a page that loads faster, but still has all of the data it needs.</p>
<p>8. <strong>Make the Master page work for you.</strong></p>
<p>It isn&#8217;t that there is something inherently wrong with the existing master page that you get when you create a new Asp.net MVC project.  Typically it is 80% of what you would want.  But, as soon as I know what my general page is supposed to look like, I rip right into the Master Page.   Also keep in mind that you can nest Master Pages as well.</p>
<p>9. <strong>Think about what a designer would want.</strong></p>
<p>Even if you don&#8217;t have one.  This is just a general pattern I try to get into.  I think about a designer as someone that can take raw html, add some css and images and make my work look a LOT better.    This means I use the raw html whenever possible, I write my JavaScript click handler so they will work with buttons or links (hint: always return false &#8212; and why haven&#8217;t I written a JQuery plugin for this yet?).</p>
<p>10.  <strong>Version your css and JavaScript</strong></p>
<p>This is actually getting into my next blog post, but figure out a way to version your JavaScript and CSS.  This really isn&#8217;t MVC specific, you should do this in almost any project.  The key reason is to help you with a browser&#8217;s cache.  You know you have a problem when the first thing someone tells you, when asking for help, is that they cleared their cache already.   My thought on this is your web app&#8217;s dll should have a version number, set the project to auto version, and then pop that onto the end of the css/javascript file call.  So it might look like this:  &#8221;http://myapp/&#8230;/file.css?version=1.0.0.256&#8243;.   In my sample code, when in development I stick a timestamp on the file in the same way.</p>
<p>OK, 10 rules is quite enough (I didn&#8217;t even think I would get that many).   To the american&#8217;s reading this: Happy Independence Day.</p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2010/07/05/asp-net-mvc-my-personal-view-rules/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
		<item>
		<title>Developers still need database skills</title>
		<link>http://elegantcode.com/2010/04/30/developers-still-need-database-skills/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=developers-still-need-database-skills</link>
		<comments>http://elegantcode.com/2010/04/30/developers-still-need-database-skills/#comments</comments>
		<pubDate>Sat, 01 May 2010 00:53:37 +0000</pubDate>
		<dc:creator>Chris Brandsma</dc:creator>
				<category><![CDATA[Esoterica]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2010/04/30/developers-still-need-database-skills/</guid>
		<description><![CDATA[Rant here.&#160; First off, I’m going to be talking about DBAs.&#160; And by DBA, I mean a database developer.&#160; Someone who knows relational theory, optimization, and SQL inside out.&#160; This is an R&#38;D DBA.&#160; There is another type of DBA that mainly deals with uptime and backup strategies.&#160; This is an IT DBA, I’m not [...]]]></description>
			<content:encoded><![CDATA[<p>Rant here.&#160; First off, I’m going to be talking about DBAs.&#160; And by DBA, I mean a database developer.&#160; Someone who knows relational theory, optimization, and SQL inside out.&#160; This is an R&amp;D DBA.&#160; There is another type of DBA that mainly deals with uptime and backup strategies.&#160; This is an IT DBA, I’m not talking about that one at all.</p>
<p>I was going to talk to some of my fellow developers about a couple of database issues.&#160; The response was, “you said the ‘D’ word.&#160; Now, we are an NHibernate/Microsoft SQL Server shop (or, Fluent NHibernate), and we rarely have to think about think about database architecture.&#160; Heck, up till a month ago I don’t think we had to do any native SQL queries at all.&#160; Currently, I don’t think we have any stored procs at all.&#160; In my book, all of that is a good thing.&#160; You can also chalk one up to Microsoft SQL Server in this one, our system has grown a lot and performance has remained pretty darn stable.</p>
<p>And lets be frank, I love this – most of the time.&#160; Linq To NHibernate queries, Fluent NHibernate, and the like all make for a very enjoyable experience for developing against a database.&#160; I would say that the vast majority of our querying needs are met with no further issues.&#160; I certainly don’t want to go back to a stored proc world.&#160; </p>
<p>That said, I don’t like using our ORM as an excuse to not talk about database issues or SQL issues.&#160; If you are using a particular data storage technology, it still behooves you to know as much about it as possible.&#160; Otherwise, when you get to a point where you need to know about what is going on will be too late.&#160; Lets be frank here, no ORM will handle all of you query needs.&#160; At best we are talking 90%, sometimes only 80%, for a typical application.&#160; At some point you either have to go to the metal, or you end up coding things in a very strange way.</p>
<p>So here is the main point, as long as Relational Databases continue to be the “right answer” for business data storage, you need to learn database theory and SQL.&#160; You might not need to know SQL as well as your native C#, Ruby, or other main language.&#160; But that is not an excuse for not being comfortable with left outer joins, third normal form, and indexing.</p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2010/04/30/developers-still-need-database-skills/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Your JavaScript goes WHERE?</title>
		<link>http://elegantcode.com/2010/03/30/your-javascript-goes-where/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=your-javascript-goes-where</link>
		<comments>http://elegantcode.com/2010/03/30/your-javascript-goes-where/#comments</comments>
		<pubDate>Wed, 31 Mar 2010 03:35:37 +0000</pubDate>
		<dc:creator>Chris Brandsma</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[html]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2010/03/30/your-javascript-goes-where/</guid>
		<description><![CDATA[Let me start by saying that I was not trying to be controversial.  I was at Boise Code Camp giving a session about Asp.Net MVC, and I happened to mention that JavaScript was supposed to go at the bottom of your page.  Plus, I said this in a “of course you all know this already” [...]]]></description>
			<content:encoded><![CDATA[<p>Let me start by saying that I was not trying to be controversial.  I was at Boise Code Camp giving a session about Asp.Net MVC, and I happened to mention that JavaScript was supposed to go at the bottom of your page.  Plus, I said this in a “of course you all know this already” type of voice, thinking this needed no further explanation.</p>
<p>Then the hands started raising.  Quickly, I might add.  I had to explain myself.  Yes, JavaScript goes at the bottom of your page.  Yes, I realize that we were all taught, back in the day, that JavaScript’s true home was in the &lt;head&gt; element.  In fact, <a href="http://docs.jquery.com/Tutorials:Getting_Started_with_jQuery">many sample pages still show JavaScript at the top</a>.  But we probably learned that ten years ago, you have to think a few things have changed…except IE6 – that sucker is going to live forever!  Anyway, I’m not the type that like to stand up in a crowd and say “everything you, and everyone you know, is doing is wrong” (I’m not that smart).  And if I do, I want to come with some substantial proof.</p>
<p>To start, <a href="http://www.w3.org/TR/REC-html40/interact/scripts.html#h-18.2.1">where does the w3c say the SCRIPT element is supposed to go?</a>  If you read the document in the link, SCRIPT can live in either the HEAD or the BODY elements. So technically, both places are legal.  I am stating this so that no one come back and tells me I’m breaking any fundamental web rules.  If the w3c says it is ok, then it is ok darn it.</p>
<h2>But why the bottom of the page? </h2>
<p>That one is slightly harder to explain.  But here are the basics:  when a browser loads a document there are only so many thread for processing the document and download all the other crap that you put on the page <a href="http://theoatmeal.com/comics/design_hell">so it pops</a> (so, images and css, not to mention the bagillion font tags that Word likes to put in html).  There is also an unwritten rule that JavaScript should enhance the page after it is loaded.  So any time the browser spends parsing JavaScript is time it could be using to make the page look right.  Also, with some browsers, when the browser is loading JavaScript, ALL OTHER ACTIVITY STOPS.  Nothing else is downloaded, the browser is at a standstill until all the script is parsed – and <a href="http://ejohn.org/blog/javascript-performance-rundown/">you know how fast IE is at JavaScript.</a></p>
<p>Lets be clear about this “rule”.  <a href="http://developer.yahoo.com/performance/rules.html#js_bottom">I did not make this up.</a>  But there are other ways around the performance penalty, you can also <a href="http://code.google.com/speed/page-speed/docs/payload.html#DeferLoadingJS">defer load</a> your JavaScript.  Also, putting all of the JavaScript into a single, external file is a great idea.  With a external file you get the added benefit of the JavaScript file being cached by the browser (Note: great for the user – pain for your tester.  Repeat after me, “CLEAR YOUR CACHE” often when testing).</p>
<p>OK, so let us review.</p>
<p><a href="http://elegantcode.com/wp-content/uploads/2010/03/image17.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" src="http://elegantcode.com/wp-content/uploads/2010/03/image_thumb17.png" border="0" alt="image" width="823" height="594" /></a></p>
<h2>But how bad is this?</h2>
<p>That is a fair question as well.  Truthfully, it depends.  I know that is my stock answer, but I’m talking about how much this will affect the user.  It could be nothing.  But if you have a lot of JavaScript on the page, it could be significant.   Potentially it could be as annoying as a blink tag.  Even worse it might be on par with ESPN and their auto-playing videos.   But, if we are talking about 10-20 lines of script, there could be no change at all in render time. I will say this, by moving the script to the bottom of the page I have taken pages that were not usable for 10 seconds, improved it to moments (the page had a LOT of JavaScript…and it was a very noble cause).</p>
<h2>What else should I be doing that I’m not?</h2>
<p>This one is easy.  Run all of your pages through <a href="http://developer.yahoo.com/yslow/">YSlow</a> and <a href="http://code.google.com/speed/page-speed/">PageSpeed</a> (you need FireFox and <a href="http://getfirebug.com/">Firebug</a> installed for this) and find out.  Also, you don’t have to be perfect as far as these tools are concerned.  Some of the rule are probably not practical either (the “You should be using a CDN for images” rule comes to mind).  But it is good to know where you can improve.  PageSpeed has a number of really nice tools as well.  It will return you properly sized images, and compressed CSS and JavaScript if you ask it.</p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2010/03/30/your-javascript-goes-where/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Asp.Net MVC 2 Areas</title>
		<link>http://elegantcode.com/2010/03/13/asp-net-mvc-2-areas/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=asp-net-mvc-2-areas</link>
		<comments>http://elegantcode.com/2010/03/13/asp-net-mvc-2-areas/#comments</comments>
		<pubDate>Sun, 14 Mar 2010 00:13:19 +0000</pubDate>
		<dc:creator>Chris Brandsma</dc:creator>
				<category><![CDATA[Asp.Net MVC]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2010/03/13/asp-net-mvc-2-areas/</guid>
		<description><![CDATA[I’m looking into the new stuff in Asp.Net MVC 2, trying to figure out what is cool and what is just there.&#160; Areas look like a nice addition.&#160; Areas allow you to separate your Asp.Net MVC application into more distinct partitions.&#160; So all of the Controllers, Models, Views, and even routes belong to one directory [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://elegantcode.com/wp-content/uploads/2010/03/image13.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; margin-left: 0px; border-top: 0px; margin-right: 0px; border-right: 0px" title="image" border="0" alt="image" align="right" src="http://elegantcode.com/wp-content/uploads/2010/03/image_thumb13.png" width="119" height="244" /></a>I’m looking into the new stuff in Asp.Net MVC 2, trying to figure out what is cool and what is just there.&#160; Areas look like a nice addition.&#160; Areas allow you to separate your Asp.Net MVC application into more distinct partitions.&#160; So all of the Controllers, Models, Views, and even routes belong to one directory structure.&#160; It is like having a sub-project inside of you MVC project. </p>
<p>Here is the problem, as I see it:&#160; as an MVC project gets large, keeping all of the necessary pieces and parts for a set of controller actions straight can get a bit daunting.&#160; I’ve ended up with duplicate folder structures in views and models, which can make navigation a pain.</p>
<p>To make a new Area, simply right-click anywhere in you MVC project, Add-&gt;Area.&#160; As I said, you can do this from anywhere in the project structure, but the Areas are created in a new “Areas” folder.&#160; When you name the Area, a new folder is created with the Areas name as well.</p>
<p>Once created you should see all the familiar Controllers, Models, and Views folders (all blank).&#160; In addition to that, you will see an &lt;name&gt;AreaRegistration.cs file.&#160; This inherits from AreaRegistration, and this is where any new Routes go.&#160; In the file is a prebuilt route that should look similar to this:</p>
<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
<div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum1">   1:</span> context.MapRoute(</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum2">   2:</span>     <span style="color: #006080">&quot;test1_default&quot;</span>,</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum3">   3:</span>     <span style="color: #006080">&quot;test1/{controller}/{action}/{id}&quot;</span>,</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum4">   4:</span>     <span style="color: #0000ff">new</span> { action = <span style="color: #006080">&quot;Index&quot;</span>, id = UrlParameter.Optional }</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum5">   5:</span> );</pre>
<p><!--CRLF--></div>
</div>
<p>So a url to the area would include /test1/ (name of my Area) in the path.&#160; That would be good if you had multiple controllers with the same name, but if you don’t, just take out the “test1” and you won’t need it in the path.</p>
<p>Also, if you leave Area name in the route, you will have a little more work to, here is what one of mine looked like:&#160; &lt;%=Html.ActionLink(&quot;test&quot;, &quot;Index&quot;, &quot;test1/TestArea&quot;) %&gt;.&#160; But it turns out this is more correct: </p>
<p>&lt;%=Html.ActionLink(&quot;test&quot;, &quot;Index&quot;, &quot;TestArea&quot;, new { area=&quot;Test1&quot;}, new {}) %&gt; </p>
<p>This also mean that any controller actions you want to link to outside if Area needs to include a blank area in the link, like this:</p>
<p>&lt;%=Html.ActionLink(&quot;Home&quot;, &quot;Index&quot;, &quot;Home&quot;, new { area=&quot;&quot;}, new {}) %&gt; </p>
</p>
<p>In case you are wondering, the first object in the area is for route values, the second is for html attributes.</p>
<p>So the downside of using Areas is that you could complicate your routing in a hurry.&#160; Plus it does not look like the Html helpers are there to lend you a hand either.&#160; It is very doable, just annoying.&#160; Of course, the easy fix is to remove the Area name from the route and move on.&#160; That should be ok so long as you do not have two controllers with the same name.</p>
<p>So are Areas a compelling feature? I think so.&#160; Plus, the larger the site, the more compelling Areas can become.&#160; For smaller sites, probably not.&#160; But I would not say no either.</p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2010/03/13/asp-net-mvc-2-areas/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>JQuery Validator Cheat Sheet</title>
		<link>http://elegantcode.com/2010/03/02/jquery-validator-cheat-sheet/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=jquery-validator-cheat-sheet</link>
		<comments>http://elegantcode.com/2010/03/02/jquery-validator-cheat-sheet/#comments</comments>
		<pubDate>Tue, 02 Mar 2010 21:56:18 +0000</pubDate>
		<dc:creator>Chris Brandsma</dc:creator>
				<category><![CDATA[JQuery]]></category>

		<guid isPermaLink="false">http://elegantcode.com/?p=3313</guid>
		<description><![CDATA[I’m getting a session ready for Boise Code Camp 2010 on JQuery Validator. I thought it would be cool to hand out a cheat sheet – but I couldn’t find one. So here is rev. 1 of my Jquery Validator Cheat sheet.]]></description>
			<content:encoded><![CDATA[<p>I’m getting a session ready for <a href="http://BoiseCodeCamp.com">Boise Code Camp 2010</a> on <a href="http://docs.jquery.com/Plugins/Validation">JQuery Validator</a>.  I thought it would be cool to hand out a cheat sheet – but I couldn’t find one.  So here is rev. 1 of my <a href="http://elegantcode.com/wp-content/uploads/2010/03/Jquery-Validator-Cheat-sheet.pdf">Jquery Validator Cheat sheet</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2010/03/02/jquery-validator-cheat-sheet/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Note to self: Asp.Net MVC Controllers are not Code Behind</title>
		<link>http://elegantcode.com/2010/02/23/note-to-self-asp-net-mvc-controllers-are-not-code-behind/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=note-to-self-asp-net-mvc-controllers-are-not-code-behind</link>
		<comments>http://elegantcode.com/2010/02/23/note-to-self-asp-net-mvc-controllers-are-not-code-behind/#comments</comments>
		<pubDate>Tue, 23 Feb 2010 22:28:27 +0000</pubDate>
		<dc:creator>Chris Brandsma</dc:creator>
				<category><![CDATA[Asp.Net MVC]]></category>
		<category><![CDATA[TDD]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2010/02/23/note-to-self-asp-net-mvc-controllers-are-not-code-behind/</guid>
		<description><![CDATA[Sometimes I think things are more complicated than they really are.&#160; I got my head into a bind recently when I had some code that I really wanted under test in a Controller Action. My first thought was “Crap, this is going to be rough”.&#160; Mainly I was still thinking of my time trying to [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes I think things are more complicated than they really are.&#160; I got my head into a bind recently when I had some code that I really wanted under test in a Controller Action.</p>
<p>My first thought was “Crap, this is going to be rough”.&#160; Mainly I was still thinking of my time trying to test Code Behind in Asp.Net WebForms.&#160; I had to smack myself a few times after I remembered that the Controller base class is an abstract type.&#160; Reality is that a Controller doesn’t have to be any harder to test than any other service class.&#160; It isn’t like inheriting from Page (Asp.Net WebForms).</p>
<p>Not that my code did not have some lingering issues.&#160; Like: why did I have something in my Controller Action that would require testing in the first place?&#160; I hate having much logic at all in my Actions.&#160; Get input, send it off for processing (logic), go somewhere.&#160;&#160; That should be about it – unless you have to go to different places depending on what is returned.&#160; Now, I want some tests.</p>
<p>Anyway, now back to your regularly scheduled program.</p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2010/02/23/note-to-self-asp-net-mvc-controllers-are-not-code-behind/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

