Here's the recipe I used to create the app:
- MVC 2
- Add jQuery reference to Site.Master
- Add jQuery.Tmpl plugin to Site.Master
- Add json2 library to Site.Master
- Create ADO.NET Entity Data Model
- Add tables and fields as desired
- Publish SQL to your SQL server
- Create a WCF Data Service
- Set your entity access rule to "*" and EntitySetRights.All
- Disable WebDAV for your app
- Build the app
- Client-side code
There's a few neat things you get out of this. One is that your app is RESTful. REST is just a philosophy, but adopting it can make life really easy as it does here - jQuery is able to communicate with the server without any special tools (save the templating plugin, but that's so we're 100% client side). We're just sending JSON back and forth and using HTTP Verbs (that's all REST is, distilled). The particular structuring isn't defined by REST - it's OData. OData best thought of as a REST implementation. It also includes a query format so I can ask for certain kinds of objects, only certain fields, etc. Just to give some background, OData is an open standard that Microsoft is behind. The OData site itself has links to libraries used in many different languages, not just the .NET variety. Netflix uses it as well, and you can query movies in all different kinds of ways using their public OData API.
There seems to be a lot of cool things about OData, but the things that are interesting to me is that there's no work on the server-side to support different formats such as XML and JSON. Ask for what you want in the request type and you get a response back the way you wanted. You can filter what kind of objects you want server-side or even just pulling certain fields back using a query string. Again, there's no work on the server developer's end to support this.
I also don't have to get nitty gritty with MVC and how it handles me moving between pages and the like. I can be a one-page app very easily here.
The data service classes provide hooks for intercepting queries and sets so you can ensure validation, add additional constraints, and strip constraints to prevent DoS attacks with expensive queries. I haven't gotten into the details of the server-side yet.
1) Create MVC 2 Project
This is pretty self-explanatory (:
2) Add jQuery to reference Site.Master
3) Add jQuery.Tmpl plugin to Site.Master
This you'll need to snag from Github. At time of writing, the plugin only works on Firefox, but one of the other forks likely fixes this issue (it's an easy fix, but why not get all of the other community fixes?). nje's fork seems to be the most current/active, so we'll grab that. I like to use IIS to host my apps locally so I can get deployment issues out of the way. It also means I can refer to webservices and the like without having to worry about the port changing on me, or the app turning off because I didn't touch it every few minutes or leave my IDE running.
4) Add json2 library to Site.Master
Same as #3 with regards to IIS. This is the official JSON library. Use it like so:
5) Create ADO.NET Entity Data Model
Others have explored this in detail. We just need to create the edmx. I have to remind myself that this is the container I'm naming, not the table/entity. I named my data model as ODataPrototypeDb.
6) Add tables and fields as desired
The Entity framework calls database fields scalar properties. I'm going to name my entity as User and add the fields FirstName and LastName (strings).
7) Publish SQL to your SQL server
This can be done within Visual Studio pretty easily. Right click on your Entity designer and click "Generate Database from Model". The wizard will help you get your connection string configured. You'll need to create the DB manually (and ensure permissions is necessary). I configured it against my localhost and used Windows Authentication. After you finish the wizard, it'll generate a .sql file. You can execute this from within the IDE itself. Right click on the SQL editor in Visual Studio -> Connection -> Connect. Once connected right click again -> Execute SQL. Now all of your tables and fields should be configured! Be aware that you can update your Entity Data Model and push those changes back to the DB, but doing so as I described above will drop your tables and recreate them. You'll want something more intelligent after you've made it into production (:
8) Create a WCF Data Service
Add a new item to your project. Under the Web category is WCF Data Service. I named mine PrototypeDataService. Generically point the data service to the Entity container type made above (not one of the entity types itself).
9) Set your entity access rule to "*" and EntitySetRights.All
This will allow all entities to be read and written to in any manner. In a real enterprise app we'd constrain this a bit. You can just uncomment line 18 and change it to accomplish this.
10) Disable WebDAV for your app
WebDAV will swallow any HTTP PUT and DELETE verbs. This will prevent you from updating and deleting entities via your RESTful OData service. To disable WebDAV for your app, just add this to your web config under the configuration node:
This will only bite you in IIS, but you have to eventually deploy to IIS anyways. Why not also run in IIS locally in your project settings so you can feel these pains earlier? (:
11) Build the app
Ctrl + Shift + B - After that you're only doing UI. No more build/run as part of your dev cycle. Just save/run.
12) Client-side code
Example of doing CRUD with a WCF Data Service using OData + jQuery (all client-side code):
Add this line to your DataService to get descriptive errors back from the service while you're debugging.
To Get IIS working nicely, I had to set the identity for the default app pool (ASP.NET v4.0) to my logged in account, because that's the owning account for SQL Server.
Big thanks for Scott Hanselman for introducing me to jQuery + OData during this talk.