Global MVC Helpers


This evening while I was helping an ex colleague of mine, who was venturing onto the web platform for the first time, an interesting thing happened, he taught me a thing or two!

Neil had been researching web forms and MVC, given it was a green field project he choose MVC, WebForms would have sufficed but it is not as elegant and efficient as the next generation of web development tools such as MVC, jQuery and Razor. As part of his research he come across a pluralsite video that mentioned a magic folder for something… naturally this aroused my curiosity, I don’t believe in magic! Surely the presenter was mistaking?

The video

The video is available here it’s Scott Allen giving an intro to MVC. Sure enough he mentions a magic folder.

Q. What is this magic folder?
A. Just the standard App_Code asp folder,

Q. What is so special with in MVC?
A. It allows you to define global MVC helpers

Q. What’s a MVC Helper?
A. Read my other post here

Q. Why is this so magical?
A. Because the dev team ran out of time Smile

Q. How do I know this?
A. Because this months edition of DevProConnections magazine contains an article “Fine-Tune Your ASP.NET MVC Skills” that explains it.


From the magazine i’ve learned that according to both the Razor online documentation and Scott Guthrie’s blog, global helpers are supported by placing a page in the ~/Views/Helpers folder. Any helpers that are defined in this folder should be available in any part of the application. However, because of time constraints, this feature wasn’t implemented. The workaround is first to create an App_Code folder and then add to the folder a new helper created as a .cshtml (or .vbhtml). You can then access the the new helper by using <file name>.<helper name> convention.

Q. Did I learn anything else new?
A. Yes while helping him add scripts to a view I discovered MVC3 ajax helpers, I’m not sure if I’m buying into them fully yet as I’ve gotten used to the jQuery syntax on it’s own, but I’ll be doing some more investigation.

So tnx Neil for letting me help you, I learned a lot! Laughing out loudRolling on the floor laughingNyah-Nyah

Microsoft Synchronization Services, WCF OData, Sql Azure, WPF, iPhone

Part1 – Setting up your database

I did some work with an interesting piece of tech lately, Microsoft Syncronization services 4.0 CTP. This post aims to give an overview of where to start, but firstly, let me describe the how all this plugs together and what it buys me.


The OData + Sync protocol uses the OData format for the data payload that can be consumed by clients that are running on any platform. Clients and the service use the protocol to perform synchronization, where a full synchronization is performed the first time and incremental synchronization is performed subsequent times. The protocol is designed with the goal to make it easy to implement the client-side of the protocol, and most (if not all) of the synchronization logic will be running on the service side. OData + Sync is intended to be used to enable synchronization for a variety of sources including, but not limited to, relational databases and file systems.


The CTP release includes server components that make it easy for you to build a sync Web service that exposes data to be synchronized via the OData + Sync protocol. For example, you can easily expose data in a SQL Server database or a SQL Azure database via a WCF sync endpoint using these server components. In our case our server in sitting in the Azure Cloud providing an OData endpoint, the data is also hosted in the Cloud in Sq1 Azure.


We have 3 clients, (well 4 actually but our Silverlight version has lagged behind and in not yet publically available).

  1. WP7 – Local data stored in Isolated Storage, DataVisualizationToolkit for charting, SL3
  2. iPhone – Connecting directly to the server via OData/Json
  3. ASP.MVC Ajax – A powerful web interface written using MVC3 and jQuery
  4. Silverlight – Initially used RIA Services to access the server but synchronization model is nearly identical to the WP7 SL3 approach with isolated storage now.

How to create the server.

  1. Create you database schema in SSMS (SqlServer managment studio)
  2. Now that your database has been created you’ll need to provision it, think of this much in the same way that you would provision an existing sql server database for asp membership tables and stored procedures. The syncronization framework will provide you with a tool called SyncSvcUtilHelper.exe this basically is a GUI for the command line version.


before provisioning you’ll need to create a Sync configuration, so choose option on, select a filename in step 1 and select your database in step2



Select the list of tables that you are interested in syncing


That’s pretty much it. Now you’ll need to choose option2 to provision the database.
Select the configuration file you created as part of step 1 and choose the provisioning option.


That’s all that’s involved, your Database is now ready to be synchronized, of course there are come considerations, unique id’s etc, but you’ll find all this in the documentation.


My next post will cover creating the WCF service for exposing the database, after that I’ll run through creating the clients.

jQuery–Preventing default link behaviour


This sample shows you how to hijack the default behaviour of the anchor tag and do something different.
The interesting part is that we use the event arg in the click function, once we have this actual arg we can call preventDefault(); on it to stop the navigation if necessary.

In this sample I just toggle the visibility of my div with a default animation (now you see it now you don’t)

 <!DOCTYPE html>
 <html lang="en">
   <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
   <script src="" type="text/javascript"></script>
   <script type="text/javascript">
       $(function () {
           $("a").click(function (event) {
 <style type="text/css">
    div.test { width:362px; height:20; background-color:Red; }
   <a href="">jQuery</a>
     <div  class="test">
     <p>this is a test</p>

MVC Scaffolding and EF4CodeFirst–A data driven website in under a minute!



Here’s a run through on how to create a data driven web application in under a minute. I’m going to create a simple part tracking system as to manage the vast amount of helicopter parts lying about my garage. Some parts I’ll never use, some parts are missing little comprising bits, the idea is to deploy the application as part of the club website allowing members to quickly find a part in a hurry for the big upcoming competition

Lets get started:

1) Install MVC Scaffolding if you’ve not done so before, go to the package console and type Install-Package MvcScaffolding


  • Create a class for the part



Now type Scaffold Controller PartModel



Here you see that all the views get created, hey and they are even DRY

If you want to make unit testing easier or suport Ioc etc you can use the –Repository flag.
If you need to rescaffold then use –Force.


Ok so we’re 50 seconds into this at this stage, and we could just run the application now provided we’ve got sqlexpress installed. However if you won’t we can use SqlCompact version.

Drop back into the Package Manager Consoler and

Install-Package EFCodeFirst.SqlServerCompact


That’s it, Q.E.D.

“Knock, knock.” -- “Who’s there?” -- long wait... -- “Java.”


So whose faster? Well C# or Java if you are talking “RAD”, but who executes faster?

IMO, C# and Java can be just as fast or faster because the JIT compiler (JIT or just in time is a compiler that compiles your IL the first time it's executed). JIT compiler can make optimizations that a C++ compiled program cannot because it can query the machine. It can determine if the machine is Intel or AMD; Pentium 4, Core Solo, or Core Duo; or if supports SSE4, etc.

A C++ program has to be compiled beforehand usually with mixed optimizations so that it runs decently well on all machines, but is not optimized as much as it could be for a single configuration (i.e. processor, instruction set, other hardware).

Additionally certain language features allow the compiler in C# and Java to make assumptions about your code that allows it to optimize certain parts away that just aren't safe for the C/C++ compiler to do. When you have access to pointers there's a lot of optimizations that just aren't safe.

Also Java and C# can do heap allocations more efficiently than C++ because the layer of abstraction between the garbage collector and your code allows it to do all of its heap compression at once (a fairly expensive operation).

Now I can't speak for Java on this next point, but I know that C# for example will actually remove methods and method calls when it knows the body of the method is empty. And it will use this kind of logic throughout your code.

So as you can see, there are lots of reasons why certain C# or Java implementations will be faster.

Now this all said, specific optimizations can be made in C++ that will blow away anything that you could do with C#, especially in the graphics realm and anytime you're close to the hardware. Pointers do wonders here.

Of course, C# (or Java, or VB) is usually faster to produce viable and robust solution than is C++ (if only because C++ has complex semantics, and C++ standard library, while interesting and powerful, is quite poor when compared with the full scope of the standard library from .NET or Java), so usually, the difference between C++ and .NET or Java JIT won't be visible to most users, and for those binaries that are critical, well, you can still call C++ processing from C# or Java (even if this kind of native calls can be quite costly in themselves).

Keyboard window shortcuts Win7


If like me you prefer to learn keyboard shortcuts and sell your soul in order to avoid moving the mouse, then you’ll like these shortcuts that I’ve stumbled upon this evening.

  • Win]+[Left Arrow] : Dock the active window to the left of the screen
  • [Win]+[Right Arrow] : Dock the active window to the right of the screen
  • [Win]+[Up Arrow] : Maximize the active window
  • [Win]+[Down Arrow] : Restore from the Maximized Position, or Minimize as an active window

And for the multi monitor people out there………

[Win]+[Shift} +[Right or Left Arrow] : Move the active window from once screen to the other

.NET 4.0 Code Contracts


Yesterday I read an interesting article on a flight home from Amsterdam and though I would share it with you.

One of the generally excepted approaches of writing functions is to validate you input arguments before using them, If-Then-Throw pattern.

Now .Net 4.0 supports Design by Contract approach to software design .NET 4 code contracts. Lets dive straight in (as always Smile)


   1:  using System.Diagnostics.Contracts;
   3:  public class Calculator
   4:  {   
   5:      public Int32 Sum(Int32 x, Int32 y)
   6:      {        
   7:          Contract.Requires<ArgumentOutOfRangeException>(x >= 0 && y >= 0); 
   8:          Contract.Ensures(Contract.Result<Int32>() >= 0);         
  10:          if (x == y)            
  11:          return 2 * x;         
  12:          return x + y;    
  13:      }  
  15:      public Int32 Divide(Int32 x, Int32 y)    
  16:      {
  17:          Contract.Requires<ArgumentOutOfRangeException>(x >= 0 && y >= 0);       
  18:          Contract.Requires<ArgumentOutOfRangeException>(y > 0);
  19:          Contract.Ensures(Contract.Result<Int32>() >= 0);      
  20:          return x / y;    
  21:      }
  22:  }

Here we see just how powerful contracts can be, have a look at Dino Esposito’s article above for more information.

jQuery Template Example


Straight to screenshot


Ok so I’m here in a hotel in Germany, been stuck in java all day so I needed .net like a drug this evening.

Let me describe what you see above.

The screenshot is from WebMatrix it shows the default webpage where I’m presenting some JSON data to the user. The data itself is just a list of model helicopter types and ratings out of 10 for each (subjective of course). The JSON in this case is hardcoded client side but could of course come from aywhere e.g. a server side MVC action called with jQuery.ajax etc.

The real good stuff comes from the following lines

<script id="modelHeliCoptersTemplate" type="text/x-jquery-tmpl">
        <b>${Name}</b> (${OutOfTen})


Here we define the template that is basically some html littered with prop Names e.g. $(Name}, note the script type that is used for jQuery templates.

The load function is quite simple, it basically says: select the helicopter jQuery template and apply the helis JSON to it and put the result into the “results” unordered list.

Have a look at the jQuery template documentation for some more samples and have fun Smile

The Ultimate Top 25 Chuck Norris “The Programmer” Jokes.


1. When Chuck Norris throws exceptions, it’s across the room.
2. All arrays Chuck Norris declares are of infinite size, because Chuck Norris knows no bounds.
3. Chuck Norris doesn’t have disk latency because the hard drive knows to hurry the hell up.
4. Chuck Norris writes code that optimizes itself.
5. Chuck Norris can’t test for equality because he has no equal.
6. Chuck Norris doesn’t need garbage collection because he doesn’t call .Dispose(), he calls .DropKick().
7. Chuck Norris’s first program was kill -9.
8. Chuck Norris burst the dot com bubble.
9. All browsers support the hex definitions #chuck and #norris for the colors black and blue.
10. MySpace actually isn’t your space, it’s Chuck’s (he just lets you use it).
11. Chuck Norris can write infinite recursion functions…and have them return.
12. Chuck Norris can solve the Towers of Hanoi in one move.
13. The only pattern Chuck Norris knows is God Object.
14. Chuck Norris finished World of Warcraft.
15. Project managers never ask Chuck Norris for estimations…ever.
16. Chuck Norris doesn’t use web standards as the web will conform to him.
17. “It works on my machine” always holds true for Chuck Norris.
18. Whiteboards are white because Chuck Norris scared them that way.
19. Chuck Norris doesn’t do Burn Down charts, he does Smack Down charts.
20. Chuck Norris can delete the Recycling Bin.
21. Chuck Norris’s beard can type 140 wpm.
22. Chuck Norris can unit test entire applications with a single assert.
23. Chuck Norris doesn’t bug hunt as that signifies a probability of failure, he goes bug killing.
24. Chuck Norris’s keyboard doesn’t have a Ctrl key because nothing controls Chuck Norris.
25. When Chuck Norris is web surfing websites get the message “Warning: Internet Explorer has deemed this user to be malicious or dangerous. Proceed?”.