-
Notifications
You must be signed in to change notification settings - Fork 3
route sets
Command sets provide a way of composing command routes. Rather than registering all of your routes in one place, you can instead group routes into command sets that you register (which may optionally share a route prefix).
Each command set can configure zero or more command routes and can also configure child command sets, resulting in a tree of routes that get's composed recursively.
In this way, the registration of command routes can be delegated from the startup module to command sets in a hierarchical manner.
In order to create a command set, you simply have to create a class that implements the ICommandSet
interface.
public interface ICommandSet
{
void Configure(ICommandRouteBuilder builder);
}
For example:
public class AccountCommands: ICommandSet
{
public void Configure(ICommandRouteBuilder routes)
{
routes
.Post("signin")
.As<EmptyRequest>()
.RoutesTo<SignIn>();
routes
.Delete("signout")
.As<EmptyRequest>()
.RoutesTo<SignOut>();
}
}
This command set might then be registered by the command route builder in the Configure
method of the Startup
class as follows:
var routeBuilder = new CommandRouteBuilder(app.ApplicationServices);
routeBuilder.Map("account").To<AccountCommands>();
app.UseRouter(routeBuilder.Build());
This is the equivalent of doing the following direclty in the Configure
method of the Startup
class:
var routeBuilder = new CommandRouteBuilder(app.ApplicationServices);
routeBuilder
.Post("account/signin")
.As<EmptyRequest>()
.RoutesTo<SignIn>();
routeBuilder
.Delete("account/signout")
.As<EmptyRequest>()
.RoutesTo<SignOut>();
app.UseRouter(routeBuilder.Build());
One of the main advantages of using command sets over plain vanilla command routes is that you an ensure consistent prefixes are applied to groups of related routes (and their child routes).
However it's also much easier to manage routes when they're encapsulated in hierarchical command sets (much as it's easier to mange files when they're grouped into hierarchical folders in a file system).