DPS907 WSA500 Assignment 1 – AutoMapper help

This document has information that will help you transition your past knowledge and skills to Web API projects.

 

Is your AutoMapper knowledge up-to-date?

If your ASP.NET MVC web apps course began in January 2017 or later, then you already know how to configure and use AutoMapper.

If not, continue reading.

 

Review – program objects

In both web apps and web services, the same chain of program objects is involved in handling requests from users.

Let’s start from the perspective of a user, and assume that a program – a browser, or a some other client app – is being used.

The user makes a request. Depending on the kind of request, a small amount of data is in the request, or a larger amount. The controller is the program object that services the request.

To fulfill the request, the controller method usually calls a method in the app’s manager object, which is a convenient centrally-located object, which handles the app’s data service operations (get, add, edit, delete).

Most requests work with data. The manager method will get or change data that’s in a persistent store (often called a database).

In summary:

User
⇩  ⇧
Controller
⇩  ⇧
Manager
⇩  ⇧
Data store

Review the system design guidance in the web apps course notes.

 

Review – data classes

There are two kinds of classes that you will write when describing data:

1. Design model classes, for data entities located in the persistent store

2. View (or “resource”) model classes, for data entities that support user interaction

 

As you know, our controllers NEVER work directly with the data store. The manager does. And, the manager knows how to work with both design model classes and view model classes. Controllers work only with view model classes.

We use AutoMapper to map (or convert) objects of one kind to another.

When we use AutoMapper in a project, we must do the following:

  1. Add the AutoMapper package to the project
  2. To the manager class, add code that configures and defines the AutoMapper bits that we need
  3. Use AutoMapper mapping/conversion methods (mostly in the manager, sometimes in controllers)

 

Later, when we begin using new project templates (authored by your professor), some of this work will already be done in the template, enabling you to focus on adding the functionality you need for the project’s use cases.

 

Obsolete AutoMapper configuration and usage

In the past, we used an AutoMapperConfig.cs source code file (static AutoMapperConfig class), located in the App_Start folder. It defined and created mappers that were used in the app in a RegisterMappings() static method.

The method was called when the app started, with a statement in the the WebApiApplication class (in the Global.asax.cs source code file), resulting in a global static object, “Mapper”.

Then, a mapping task was done with the (upper-case)  Mapper.Map<T>()  method.)

 

Modern AutoMapper configuration and usage

Now, it’s all done in the Manager.cs source code file (Manager class). An AutoMapper configuration object is defined, and in the class constructor, an instance of the mapper is defined and created.

Usage is much the same as before, except we always use this lower-case  mapper.Map<T>()  to do the mapping task.

We have created a code example project that shows this modern way of configuring and using AutoMapper:

AutoMapperInWebAPI

 

The first and most important change is the definition and creation of the mapper object.

Look at the code in the manager class, beginning on line 23.

It defines some fields used in the class.

Then later, in the constructor, those fields are initialized.

 

The second important change is how the mapper object is used.

Look at the code in the manager class, on line 69.

It does a mapping from design model objects, to resource (view) model objects.

 

Get and use the code example project. Carefully read the code in these source code files:

  • DesignModelClasses.cs
  • Manager.cs
  • CoursesController.cs
  • LoadDataController.cs

Load/run the app. Then, interact with it, by using Fiddler.

 

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

Advertisements
%d bloggers like this: