Skip to content

Request Activation

James Crosswell edited this page Apr 3, 2016 · 3 revisions

Overview

The first thing Command Routing does when it processes a request is to create a strongly typed Request Model. It does this by:

  1. Deserializing the message body of the HTTP message
  2. Setting request model properties from any route values

For example, imagine we have the following request type:

public class UpdateWidgetRequest {
    public int Id { get; set; }
    public String Name { get; set; }
}

And the following HTTP Request:

POST /widget/15 HTTP/1.1
{
    name: "Foodles"
}

Finally, we have a command route "POST /widget/{id}" that maps to a set of request handlers for UpdateWidgetRequest, Command Routing will create the following instance of UpdateWidgetRequest:

var requestModel = new UpdateWidgetRequest {
    Id = 15,
    Name = "Foodles"
};

It's a bit more complicated than that, since Command Routing won't actually use the new operator but, conceptually at least, you can pretend that's what happens under the hood... essentially it merges the request body with any route values to create your request model for you.

Setting nested properties from route values

Note that you can also set nested properties of your request handlers by using dot notation in your routing templates. For example:

class Child
{
    public string Name { get; set; }
    public Parent Dad { get; set; } = new Parent();
}

class Parent
{
    public string Name { get; set; }
}

public class Startup
{
        public void Configure(IApplicationBuilder app)
        {
            var commandRoutes = new CommandRouteBuilder(app.ApplicationServices);

            commandRoutes
                .Post("{name:alpha}/{dad.name:alpha}/your/father")
                .As<Child>()
                .RoutesTo<WhosYourDaddy>();

            // other stuff
        }

        // other stuff
}

And you could activate that route with a request like this:

POST /luke/im/your/father HTTP/1.1