Constructing Representations

This is an introduction to core five classes required to constructing a representation from the controller, this case the root of the API because it is the simplest. This pattern is the mainstay that variation emanate from and the files tend to live across two projects. The tutorial provides various views:

  • IDE/code structure of classes
  • Block diagram
  • Sequence diagram
  • Actual code
  • curl request/response

image of the localhost:5000

Goal

  • Return an unauthenticated resource on the root/home of the API (simple GET)
  • Learn the code layering of the solution
  • Learn about creating link relations and their URIs

Note: this code assumes that the MVC pipeline is already setup

Anatomy of code

Below, you see on overview of the code, then you see the implementation, finally we’ll draw it again in a way to help you start to remember it! Remember, this is essentially the pattern of code you’ll use all the time with adding representations to the domain. No more, no less.

Logical Resource through a controller action

For the more visual, below is a diagram that layers between the Representation and the Model where:

  • Representations are serialised in and out across the request and response, respectively
  • FromRepresentation is an incoming representation mapped to a domain model (that may be validated and persisted, ie apply business process/rules)
  • ToRepresentation is an outgoing representation constructed from a domain with link relations created through a UriFactory that maps resource identities to routes (ie back into the controller actions)
  • Model itself are the in-memory domain objects from persistence

Note: each of the boundaries denoted by box lines indicate potential impedance mismatch that should be understandable in the code

This diagram is generalisable to most MVC frameworks and suggest that perhaps we might need to find variations of the pattern to meet hypermedia designed APIs

logical resource

GET: sequencing request to response

Below is a way to understand the interaction of the pattern of representation request/response:

Get request/response

The code

Domain

Controller

Factories/Helpers

Request in curl

Note: if running the todo API itself, more links will be returned.

Unlock the next chapter
How should we version an API?
We shouldn't as well known authors tell us
In the URI
With a header
As a field in the root as a known place because really we don't need versioning that much