Eric lippert - Amazon Web Services
Eric lippert - Amazon Web Services
Eric lippert - Amazon Web Services
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
Media Formatters are the extension points that allow us to plug<br />
in our own Formatter and thus help our Service support more<br />
Content Types.<br />
Defining the Premise<br />
Today we will see how we can build a <strong>Web</strong> Service that returns<br />
Appointments in a rudimentary Timekeeping application that<br />
maintains ‘Timecards’.<br />
On top of the Service, we will have a Single Page application<br />
that uses Knockout JS to render basic CRUD screens for<br />
Timecards. We will also create a Media Formatter that formats<br />
these ‘Timecards’ into the VCalendar format so that they can be<br />
downloaded and imported into our Calendaring tools like<br />
Outlook etc.<br />
Building a ‘Time Card’<br />
Service<br />
We will start off by creating a new MVC 4 project in Visual Studio<br />
and call it TimeKeepr. From the templates list, we select the<br />
‘<strong>Web</strong> API’ template to get started.<br />
First up, let’s build the model and the controller.<br />
The Model and (<strong>Web</strong>) API<br />
Controller<br />
Our Model is very simple. It has the following properties. As we<br />
can see, it’s a bare-bones definition of a Task, it simply maintains<br />
Summary, Description, StartDate and EndDate.<br />
public class TimeCard<br />
{<br />
public int Id { get; set; }<br />
public string Summary { get; set; }<br />
public string Description { get; set; }<br />
public DateTime StartDate { get; set; }<br />
public DateTime EndDate { get; set; }<br />
public string Status { get; set; }<br />
}<br />
Once the model is added, we build the app, and then add a<br />
TimeCardController with the following settings. As you can see,<br />
we are creating API controllers as opposed to MVC Controllers so<br />
no views are going to be generated.<br />
The DBContext that is created by the scaffold pretty much takes<br />
care of the database persistence. Today we will cut some ‘pattern’<br />
corners and leave the DBContext instantiation in the controller<br />
itself. For a production system, it should be a part of the<br />
Repository and we should be injecting repository instances into<br />
the controller.<br />
The View using<br />
Knockout JS<br />
We will build a single page View with Add and Edit functionality.<br />
The view will use the Knockout JS library to implement two way<br />
data-binding. This will give us a nice and responsive UI. We will<br />
post data back to the server using Ajax. We will also use a plugin<br />
by Ryan Niemeyer that will help us control Knockout’s two way<br />
data-binding by giving us options to either commit or roll-back<br />
any changes.<br />
Setting up Dependencies<br />
We’ll update our jQuery and KO dependencies from the Nuget<br />
Package Manager using the following commands<br />
PM> update-package jquery –version 1.9.1<br />
PM> update-package knockoutjs<br />
Next we’ll add a JS called knockout.protectedObservable.js and<br />
add the following script by Ryan Niemeyer.<br />
// Source-Ryan Niemeyer’s JS Fiddle at http://jsfiddle.<br />
net/rniemeyer/X9rRa/<br />
64 | DNCmagazine www.dotnetcurry.com