DPS907 WSA500 Lab 9

Updated content, with some hints and helpful information, has been added, in blue text.

DPS907 WSA500 Lab 9 – Due Mon Dec 7

Due date: Monday, December 7, 2015, at 8:00am ET

Grade value: 10% of your final course grade

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

Yes, your project can earn part marks if a feature is missing.

Objective(s)

Create a legacy SOAP XML web service. Create a client that works with the web service.

Introduction to the problem to be solved

In the USA and Canada, Black Friday has captured the attention of many in our society. Therefore, we need a web service that models this curious aspect of pop culture.

The web service will be based on legacy SOAP XML technologies. It will enable a caller to create a sales order, among other tasks (e.g. get a list of available products, and display an existing sales order).

A client app, as a separate project, will interact with the web service.

Your work will be hosted publicly on Microsoft Azure Services.

Do you need to implement security?

No.

Specifications and work plan

The following specifications apply to all of your work:

Follows best practices

Implements the recommended system design guidance

Uses Entity Framework and Code First technology

Deployed to Microsoft Azure Services

For this assignment, here’s what we’re looking for:

Web service app, based on SOAP XML technology

Supports these operations:

Get a product list,

create a sales order, and

display a completed (saved) sales order

Client app, based on ASP.NET MVC web app technology

Supports these operations:

Create a sales order

List all sales orders

Display one completed (saved) sales order

The in-class grading on Monday, November 30, will look at these web service components:

Entity classes

Resource model classes

Interface definition

Getting started

Create a new project, using the “Web API project v3” template, named Lab9Service.

Create a separate new project, using the familiar ASP.NET MVC web app template, named Lab9Client. The “Authentication” setting doesn’t matter.

Lab9Service design model classes

We will use a simple design model. The objectives of this assignment are to ensure that you can create and use a legacy SOAP XML web service, so this assignment is not robust or full-featured enough for real-world production use. Therefore:

We will have a simple Product class.

A separate class will hold a SalesOrder, which is related (to-many) with an OrderItem class. An OrderItem object has a required association with a SalesOrder object.

The design model looks like this:

Lab9Service resource model classes

Next, create resource model classes. They must support these use cases:

Get a collection of Product objects

Get a collection of SalesOrder objects

Get one SalesOrder object, with its associated collection of OrderItem objects

Add new SalesOrder object, which includes a collection of OrderItem objects

Lab9Service – add a WCF Service

Add a WCF Service named “OrderService” to the project.

As you have learned, its interface defines the supported or expected operations.

In the interface source code file, add method declarations for the operations that will support the use cases. Pay attention to parameters and return types. Continue to follow the rules that you have learned: Accept and deliver resource model objects and collections. Do not leak information about the design model to callers.

Remember to test your work, incrementally

As you work on the remaining tasks, test your work frequently, using the WCF Test Client.

Complete the entity-related tasks

As you know, there’s a set of entity-related tasks that must be done after adding new entity classes to the app’s design model. Plan on completing them now, by following this list of reminders:

Add DbSet property to the data context class for each entity

Write/create the AutoMapper maps in the WebApiApplication class

Add a new repository to manage each new entity (as required)

Modify the Worker class, so that includes a property for each just-added repository class

Add some starter data for Products

Programmatically, add some starter data for the Product entity.

Ensure that this process runs only once. There are many ways of doing that. Ask if you need a reminder.

Think of something product-like in your life that you like. Create ten (10) products. Please set the initial value of the UnitsInStock property to a number in the hundreds, so that there are enough Product items available as you test your app’s logic.

Use case implementation –
Get a collection of Product objects

The service will deliver a collection of Product objects. Well, actually it will deliver a collection of resource model objects.

Like a controller, the service will call a repository method, which will actually do the work. Therefore, complete and test the code that enables the delivery of a collection of product objects.

Hint: While you are writing code in the product repository, add another method that will get one product by its identifier. This method will be needed later, when adding a new sales order, and validating the existence of a product.

Use case implementation –
Get a collection of SalesOrder objects

The service will deliver a collection of SalesOrder objects, using a resource model class.

Should the resource model class include a collection of OrderItem objects? It doesn’t matter (for grading purposes). If your resource model does include the collection, then it can be used for this “get all” use case, and the “get one” use case that follows.

Then, you can still decide whether you fetch the associated OrderItem objects when you fetch the SalesOrder objects. If you do, they come along. If not, the collection’s value will be null for each item in the “get all” use case. That’s OK. Remember, we’re focusing on SOAP XML web service competence in this assignment, and we’re not trying to precisely model the real world with a production-ready app.

Use case implementation –
Get one SalesOrder object, with its associated collection of OrderItem objects

The service will deliver a specific SalesOrder object, using a resource model class.

Include its collection of OrderItem objects (obviously using a resource model class).

Use case implementation –
Add new SalesOrder object, which includes a collection of OrderItem objects

The service will accept a package of data, and create some objects in the data store.

What does the package of data look like? Well, think about any experience that you have had in a retail store. For example, Peter visits the Canadian Tire store, and selects two hammers and one screwdriver. That’s the data package – the sales order customer is “Peter”, and the two order items are hammers (quantity 2) and screwdriver (quantity 1). While processing the transaction, all the missing pieces of data are filled in (date, price, etc.).

This is not a complex task. Do NOT make it complex. It’s just arithmetic, that processes a simple, very common, and well-understood human activity. Create a coding plan, step-by-step, before you write code. Ask if you need help. (Duh, you’re not going to find a solution “out there” by using Google search.)

The result of this task will be to return a fully-configured sales order object (that includes a collection of order items).

Hint: As noted above, create a coding plan, step-by-step, before you write code. The following is the professor’s coding plan for his sample solution:

Create a sales order object, based on the incoming data. When the data comes in, it will get mapped on to a SalesOrder object. Some properties will have data (Customer, OrderItems), but many properties will be empty, until they get filled in. Click the image to see the SalesOrder object at this point in time.

Fill in the date-and-time value. Create an “order total” accumulator, which gets updated when processing each order item.

For each item in the “order items” collection…

Look up the product, and ensure that it exists

Update the product object, by adjusting the “units in stock” amount

Update the “item total” value

Make the association between the order item and the new sales order object

Update the “order total” accumulator value

After processing all the “order items”, update the sales order’s “order total” value

Add the new sales order object to the entity collection, and save changes

Return a mapped version of the sales order to the caller

Work on the client app

BEFORE starting the work on the client app, ensure that the web service works correctly. All four use cases. Use the WCF Test Client to ensure that it’s working.

Then, you can begin work on the client app.

Add service reference

One of the first tasks is to add a service reference to the project.

Names are important. The “Namespace” name will be used in your code to enclose the generated proxy classes, and enable access to the service class. Therefore, make it meaningful and clear.

In the “Add Service Reference” dialog, open the Advanced configuration dialog, and clear (un-check) the “Reuse types in referenced assemblies” checkbox.

Visual Studio will generate some code. It creates classes that act as a local proxy to the remote web service.

It also creates classes for the objects that will get passed to and from the web service. We can use these classes directly, or you can create your own resource model classes. (For this assignment, it is acceptable to use the classes directly, and consider them equivalent to resource model classes.)

Add the service layer code

As you have learned when creating ASP.NET MVC web apps, you should create a Manager class (in the Controllers folder). The Manager code will know how to contact the web service, and handle the request and response messages.

In a method that calls the web service, the typical pattern will:

Create an instance of the web service client class

Call a method (while paying attention to arguments and return types)

Add a controller and the necessary views

In the client app, add a standard controller, probably named SalesOrdersController.cs. The use cases are dominated by sales order processing, so make a decision about what kind of methods you need. You probably need these:

Display a list of sales orders

Display the details for a single/selected sales order

A pair of methods to handle a typical “add new” sales order scenario

Keep it simple. Use view scaffolding when possible. In the views, ensure that Bootstrap classes are used for all HTML Form elements.

Hint: Are you looking for some help with the “add new” sales order task?

Look at the Appendix to this Lab 9 specification, below.

Making changes in the web service

If you make a change to the web service’s methods or resource models, the changes affect how client apps can use the web service.

Therefore, if you do change the web service, you must “update” the service reference in the client app. Locate the service references folder in Solution Explorer, right-click the service, and choose “Update Service Reference”.

Deploy your work to Microsoft Azure Services

Having problems deploying to or running on Azure? Check this document.

Preview of the deployment process

At a minimum, your Lab9Service will be deployed to Azure.

Then, in your client app, you must update the service reference, by pointing to the Azure-deployed web service. Then, test your client app again, to ensure that it still works.

If that works well, then you can deploy your client app to Azure.

Azure web app and database preparation for your Lab 9 service

Create a web app, with a database, for your Lab 9 service. The suggested name is (substitute your Seneca account name for the “astudent” placeholder below):

astudent-ws2015-lab9.azurewebsites.net

Deploy your Lab 9 service to this endpoint. Before continuing, use the WCF Test Client to interact with the service.

Update your client app

As noted above, update the service reference, now that the web service is on Azure. Right-click the service reference, and choose “Configure Service Reference”. Notice the localhost address:

Change the address to the Azure-deployed address:

Test the client app, to ensure that it works with the Azure-deployed web service.

If it does…

Azure web app (no database) for your Lab 9 client app

Create a web app, with no database, for your Lab 9 client app. The suggested name is (substitute your Seneca account name for the “astudent” placeholder below):

astudent-ws2015-lab9client.azurewebsites.net

You can now deploy your Lab 9 client app to this endpoint.

Testing your work

Use the WCF Test Client, and your client app.

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

At this point in time, you are familiar with the submission process:

Copy both projects – Lab9Service, and Lab9Client

Remove the packages, bin, and obj folders

Zip and upload to the designated location on My.Seneca/Blackboard, before the due date

If you need more details, look at the info in assignments 1 through 4.

Appendix – Elegantly handling the “add new” sales order task

It would be nice to create an HTML Form that was easy and safe to use.

If you need help with this task, your professor can help by providing some guidance from his sample solution.

Requirement – add some new view models

We start with some view model classes. We need:

SalesOrder – represents the data posted by the HTML Form

OrderItem – represents one order item; a collection of these is in a sales order

Fetches the list of products (from the web service), and configures it in the sales order add form

Finally, it passes the form to the view

“Create new sales order” view

Create the view to support the “add new” task. Use scaffolding, and specify the sales order add form class, so that it generates the view code.

At this point in time, it will add code only for the “Customer” property. Remember (from your web apps programming course experience) that collections are NOT automatically code-generated. We must do that part ourselves.

You can add the following view code, between the “Customer” text box, and the “Create” (submit) button: