DPS907 WSA500 Lab 1

Your first small assignment, known as Lab 1. Enables you to get started with web services.

Read/skim all of this document before you begin work.

.

DPS907 WSA500 Lab 1 – Due Mon Sep 21

Due date: Monday, September 21, 2015, at 8:00am ET

Grade value: 4% of your final course grade (partially graded in-class on Mon Sep 14)

If you wish to submit the lab before the due date and time, you can do that.

.

Objective(s)

Get started with web service coding and testing.

.

Introduction to the problem to be solved

We need a simple web service that will deliver data about smartphones. The web service will support fetch all, fetch one, and add new scenarios.

.

Specifications and work plan

Here’s a brief list of specifications that you must implement:

  • Follows best practices
  • Implements the recommended system design guidance
  • Uses Entity Framework and Code First technology
  • Supports HTTP GET, for get all and get one use cases
  • Supports HTTP POST, for the add new use case
  • Includes a Fiddler log file that shows complete coverage of tests

.

Getting started

We suggest that you build the app’s structure, by creating a number of source code files. That way you know the “big picture”, and can fill in the details by editing the source code files.

The name of your project will be “Lab1”.

Create a project, using the “Web API” template. “Individual User Accounts” authentication. No hosting. No Web Forms or unit tests.

As you perform each step, test your work, to ensure that you’re making error-free progress:

  • Build/compile
  • Where appropriate, use Fiddler

.

This project will include the components needed to use a database, and Entity Framework Code First technology:

  • Its data context class is ApplicationDbContext, in the IdentityModels.cs source code file
  • You must add your own DbSet<TEntity> properties to this class

.

Update the project

We are introducing a new habit: Updating your projects with the latest bits. How?

After loading the project in Visual Studio, open the NuGet Package Manager.

In the left-side navigator, choose Updates > nuget.org. The center panel will fill with a list of packages that need to be updated. Click the Update All button.

npm-update1

.

While it updates, it may ask you to confirm replacements, overwrites, etc. At the end of the process, it will display the list, with green-circled check marks that show the updated packages.

Depending upon your Visual Studio version, it may tell you that the project must be reloaded. Yes, you want to do that.

npm-update2

.

Add AutoMapper

Using the NuGet Package Manager, install the latest version of AutoMapper.

.

Add the source code files that define the app’s structure

Add the source code files that define the app’s structure. As suggested above, build/compile after you add new items and perform edits.

In the Models folder, add a source code file to hold your design model classes, named DesignModelClasses.cs.

In the Controllers folder, add a source code file to hold your data operations management methods, named Manager.cs.

In the Controllers folder, add a source code file to hold your resource model (i.e. view model) classes. Its name will be Smartphone_vm.cs.

In the Controllers folder, add a controller. Its type will be “Web API 2 Controller with read/write actions”. Its name will be SmartphonesController.cs.

.

Doing the work

Now that you have the app’s structure configured, you can circle back and do the programming work.

.

Design model classes

In the DesignModelClasses.cs source code file, create an entity class to describe a smartphone. The class name should be “Smartphone”. Write five or more properties (your choice, but use common sense). Use four or more different data types. For example:

  • Id (identifier)
  • Manufacturer name
  • Smartphone model name
  • Release date (date & time)
  • Screen size (double)
  • Sell price (integer)

Remember to use a default constructor whenever you have a DateTime property in a class.

.

Data context and data storage (aka facade services)

The project will include an IdentityModels.cs source code file. You will remember that from your web apps course.

The ApplicationDbContext class is the app’s data context class. (It ultimately inherits from DbContext.)

Add a DbSet<TEntity> property for the app’s entity class.

.

Data operations management

In the Controllers folder, the Manager.cs source code file holds the Manager class. As you remember from your web apps course, its purpose is to contain the methods that perform data service operations. Controllers will call into this manager class.

Like all manager classes, it needs a field for the facade reference (the data context). Then, it will need (for this lab) methods for:

  • Fetch all objects in a collection (should they be ordered?)
  • Fetch a specific object based on its unique identifier (what happens if not found?)
  • Add a new object to the collection

Remember that all Manager class methods must accept and deliver view model objects or collections. The Manager class must not leak design model class objects or collections.

The Manager class will also need using directives for the app’s Models namespace and the AutoMapper namespace.

.

View models (aka resource models)

You know about “view models” from your web apps course. In web services, these classes are often called “resource models”. They’re the same.

Earlier, you created a source code file named Smartphone_vm.cs. It will hold resource model classes for these use cases:

  • Add (i.e. SmartphoneAdd)
  • Base (i.e. SmartphoneBase)

Write these now.

Obviously, we need program code to map between design model and resource model classes. We will continue to use AutoMapper.

You have learned that the ideal place to locate AutoMapper ‘create map’ statements is in the “application” class. In a web service project, the class you’re looking for is named WebApiApplication. Therefore, add the right number of statements. You need a two maps: One from design model to resource model, and another to enable the ‘add’ use case (resource model to design model).

.

Controller programming

The SmartphoneController class, like all controller classes, it needs a field for the manager class reference.

Then, three of its methods need to be implemented:

  • Get all
  • Get one
  • Add new

You can comment out the Put and Delete methods; we will not use them in this lab.

As a reminder (because it may have been awhile since you worked in web apps), here is pseudocode for the three methods.

.

Get all

Call the manager’s “get all” method
Return an Ok() result

.

Get one

Call the manager’s “get one” method
Test the result…
If null, return a NotFound() result
Otherwise, return an Ok() result

.

Add new

Ensure that there is an object in the entity body, if not, return a BadRequest() result
Ensure that a passed-in object is valid (using model state validation), if not, return a BadRequest() result
Attempt to add the new item
Test the result…
If the attempt was unsuccessful, return a BadRequest() result
Otherwise…
Construct a URI that will point to the new resource
Return the URI and the new object in a Created() result

.

Testing your work

Use Fiddler.

Ensure that it has been configured to save the message bodies in requests and responses. (A default installation does not do this.) If you are using a College computer, this has already been configured. If you installed Fiddler on your own computer, follow the instructions on this document.

Test all scenarios (use cases).

When testing the ‘add new’ scenario, remember:

  • The HTTP method is POST
  • The target is the collection URI
  • The request needs a Content-Type header
  • The message body needs a JSON object that matches the “SmartphoneAdd” resource model

The date (i.e. DateTime type) property needs some explanation. The JSON serializer uses the ISO 8601 standard for dates and times. For example the due date and time of this lab is expressed as a string, “2014-09-16T08:00:00”. In other words the date is in year-month-day format. Then there’s a letter “T”. Finally, the time is in hour-minute-second format.

Use that format when you enter the string for the date in your ‘add new’ requests.

.

Saving – ‘exporting’ – your tests

On the left side list of requests, you can delete items that you don’t want included in the export.

When you’re ready to save, choose File > Export Sessions > All Sessions…

The export format will be “HTTPArchive v1.2″. Click the Next button to choose a save location (your project’s root, in the same folder as the “packages” and “lab1” folder) and specify a filename. Name the file by using the project name (e.g. “lab1.har”).

(You can test whether the export was successful. How? First, close then re-open Fiddler. Choose File > Import Sessions. Select “HTTPArchive” as the import format. Navigate to the folder that holds “lab1.har”, and select it. Finally, browse through the request-response sessions.)

.

Reminder about academic honesty

You must comply with the College’s academic honesty policy. Although you may interact and collaborate with others, you must submit your own work.

.

Submitting your work

Here’s how to submit your work, before the due date and time:

1. Locate the folder that holds your “Lab1″ project files. (It has about three items, a Visual Studio “solution” file, a folder that has your project’s source code, and a “packages” folder.)

2. Make a copy of the folder. This is the data that you will be uploading.

3. Remove the “packages” folder from the copied folder; also, remove the “bin” and “obj” folders.

4. Compress/zip the copied folder. The zip file SHOULD be about 1MB or less in size. If it isn’t, you haven’t followed the instructions properly.

5. Login to My.Seneca. Open the Web Services Architecture course area. Click the “Assignments” link on the left-side navigator. Follow the link for this lab. Submit/upload your zip file. The page will accept three submissions, so if you upload, then decide to fix something and upload again, you can do so.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

Advertisements
%d bloggers like this: