This post is primarily for my fellow friends of local .net user group who asked for a rough guideline on how to get started with the latest Entity Framework Code First, I will try to keep it short and concise. I assume that it is going to be used in an ASP.NET MVC Application and behind the scene an IoC container would be used to glue everything. Before going into the implementation details there are certain design choice of mine, first persistence ignorance which means the domain model have no clue whether Entity Framework is used or any other ORM (nothing fancy here, out of the box the EF Code First has this support), next, I prefer to use class mapping which I think gives a bit more control comparing to decorating the domain classes with the data annotation attributes. Lets starts with a typical Category, Product, Customer and Order domain.
ASP.NET MVC ViewModel usage and pick your best pattern
In this post, I will start with typical usage of view model in view that you often see in ASP.NET MVC Applications with full of tag soup, then I refactor it incrementally to correct the implementation and finally show you a better implementation which promotes better OOP and encapsulation, makes the view fully logic-less by delegating the presentation logic to a separate class. Lets see the initial implementation, consider a User Profile view which contains the following code:
ASP.NET MVC with PAjax or PushState/ReplaceState and Ajax
In the recent episode of RailsCasts (highly recommended even if you have tiny bit of interest in Rails) Ryan Bates shows a nice little but very elegant trick to load different pages with Ajax request. The fundamental difference of this trick with the vanilla jQuery.ajax is that it keeps the browser history intact, the visitor of the page will see the regular url change in the browser location bar and it would even degrade gracefully if the browser JavaScript is turned off. Behind the scene it uses a jQuery plugin called pajax which does the heavy lifting, by default the plug-in comes with a Sinatra example, there is also a Rails integration gem which is written by the DHH(creator of Rails) himself. In this post I will use the same Sinatra example but ported to ASP.NET MVC.
NHibernate Built-in Code Based Mapping instead of using Fluent NHibernate
This is just a quick post to show how to use the new NHibernate built-in mapping instead of using Fluent NHibernate. It looks like the Fluent Hibernate is not compatible with the latest version of NHibernate that is available in NuGet. Although I am a fan of Fluent NHibernate but I am not sure about it’s future as the core NHibernate now has this similar kind of code based mapping instead of those boring xml files. I will show you the Fluent NHibernate Mapping first then the new mapping. This is extracted from one of my pet project that I am using to play with jQuery Mobile and Spine. Lets say I have a basic User object which was mapped in Fluent NHibernate like the following:
Meet Spine.js – My framework of choice for client side MVC (Part 2)
In the last post, we have used Html5 local storage for persistence, in this post we are going to integrate it with ASP.NET MVC. Before moving forward, let me tell you that frameworks like backbone.js and spine.js expects your server endpoints are RESTful and do utilize the http verbs, for example the same endpoint can perform different things based upon the http verb. Unlike Rails, the default Controller and routes that ASP.NET MVC generates does not have those features. So we need to tweak the ASP.NET MVC to improve its “RESTful-ness”. In case, if you are wondering about the url structures, here is the list of defaults of ASP.NET MVC: