Some skills never go out of fashion. Managing data and serving it up to a client is one of those skills. Sure, the technologies change and the way we use them, but we’re still gathering and delivering data.
Recently I wrote about how my new course is progressing for Pluralsight. The course, titled “Building Single Page Apps (SPA) with HTML5, ASP.NET Web API, Knockout and jQuery” , is an end to end course. This means I cover the app and how it uses all of the technologies to produce the SPA.
Today I’ll share some insights on the server technologies I am using in Code Camper SPA. The diagram below shows the key ingredients I used to create the server technologies. One of the goals of this architecture is to surface the data in an simple way so I can add new ways to get to the data or even add new data fairly quickly. The main idea was to keep the Web API’s methods simple, which I think was achieved.
I’ve been down this road many times in creating server architectures to serve data so I had a pretty good idea of what I wanted to keep the same and what new technologies and patterns I wanted to sprinkle in. Also, it helps to have good friends like Dan Wahlin and Ward Bell to bounce some ideas off. We three tend to think alike so you will see some similarities in how we address these issues. Ward had a very active hand in helping shape this particular app as my unofficial code reviewer (and often telling me I was crazy).
I started out thinking I would use SQL Server but decided to go with SQL Server CE, just because it makes distributing the database that much simpler. Of course, for production apps I recommended SQL Server over CE. The database is rather simple, but has enough real world scenarios in it to drive the points home. We have sessions, speakers, attendees, tracks, time slots, and many to many joins between sessions and attendees.
Entity Framework Code First (Magical Unicorn Edition)
I chose Entity Framework (EF) Code First (v4.x or v5 will work) for 2 reasons. First, experience matters. It’s a solid ORM that works well with web technologies as it has proven to do the job for me in my experiences. Second, I wanted to write my model separate from the database and use pure POCO (plain old class objects). EF Code First does a great job at staying out of my way when I create my models.
I do wish that EF Code First had a more appropriate name. Its name implies that you have to create your code first, then generate the database. That’s the impression I run into quite often – but its not entirely accurate. EF Code First can generate the database from models, but it doesn’t have to. You can certainly create the database and the models separately. The fluent API (or data annotations) allow you to hook them up where the conventions may be different between the models and database. So while Scott Hanselman dubbed EF the “Magical Unicorn Edition”, I’d settle with a revised name of “EF POCO Loco “
Yes, I like data patterns. And you will find a few in this course including the Repository Pattern (to expose the data in a consistent way), Unit of Work Pattern (to decouple the Web API from the lower layers and to aggregate the repositories), the Factory pattern (for creating repositories), and Single Responsibility Principle (SRP). The SRP is all throughout the client and server, in fact. Simply put, the patterns help make the app easier to debug, scale and maintain.
ASP.NET Web API
I’m very happy with how the app has shaped up on the server side (and overall).
More on the Code Camper SPA
Part 2 - Client Technologies
Part 3 - Server Technologies (the Data Layer)
Part 4 - Serving JSON with ASP.NET Web API
Part 5 - HTML 5 and ASP.NET Web Optimization
Part 7 - MVVM and KnockoutJS
Part 8 - Data Services on the Client
Part 10 - Saving, Change Tracking, and Commanding
Part 11 - Responsive Design and Mobility