DPS907 notes – Fri Sep 20

Supporting all HTTP methods. Associations and navigation properties. Adding objects with associations. AutoMapper tips.

.

Supporting all relevant HTTP methods, continued

Our code example today will help you learn to support all relevant HTTP methods.

Open the repository, week 3, and look for sep20v1.

.

Request > response flow

The following diagram captures the actions and data that flow from a request to a response.

The problem domain continues to be the familiar School of ICT “Programs” and “Subjects” entities. In the diagram, the requestor / user wants to create a new “Subject” object. Click the diagram to see it full-size in a new tab/window.

RequestFlow

.

Associations between objects (navigation properties)

You will often model real-life objects that have associations with other objects.

In our examples, a ‘Program’ has a collection of ‘Subject’ objects, and a ‘Supplier’ has a collection of ‘Product’ objects.

Looking at the other side of the association, a ‘Subject’ object belongs to a ‘Program’, and similarly, a ‘Product’ is made by a ‘Supplier’.

When performing data operations, you can work with either side of the association:

  • When working with a to-one navigation property, you simply assign an object to the property (e.g. blah.Supplier = s;)
  • When working with a collection navigation property, you add a new item with the collection’s Add() method (e.g. blah.Subjects.Add(s);)

.

AutoMapper tips

Including an associated (related) collection

If you have an entity that has an associated (related) collection – for example, a Program has a collection of Subjects, or a Supplier has a collection of Products – you may want to return an object with its associated collection.

To do this, you need to do three things:

1. In a new view model class, add an appropriate navigation property for the collection. The property names must match. (And you will need AutoMapper maps defined in Global.asax.cs, but you probably already have them done.)

2. In the repository, when the code fetches the object, add the Include() extension method to fetch its associated collection.

3. In the controller, add a method that will call the repository method; a typical pattern is to use a query string parameter.

Here’s how these code changes work together. Click to open it in a new tab/window:

AutoMapperAssociatedCollection

.

Including a property from an associated object

If you have an entity that has an associated object – for example, a Subject belongs to a Program, or a Product belongs to a Supplier – you may want to return an object with its associated object.

To do this, you need to do three things:

1. In a new view model class, add an appropriate navigation property for the associated object. The property names must be a composite of the class name and the property name. (And you will need AutoMapper maps defined in Global.asax.cs, but you probably already have them done.)

2. In the repository, when the code fetches the object, add the Include() extension method to fetch its associated object.

3. In the controller, add a method that will call the repository method; a typical pattern is to use a query string parameter.

Here’s how these code changes work together. Click to open it in a new tab/window:

AutoMapperComposite

.

.

.

.

.

.

.

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: