liveBook · Manning

Cross-cutting concern

As explained in ASP.NET Core in Action, 3e

A cross-cutting concern is application behavior that applies across many or all request paths and features rather than one specific resource.

A cross-cutting concern is application behavior needed across many request paths rather than one resource. Logging, error handling, security, user association, and request language are common examples. In ASP.NET Core, middleware is the preferred place to configure and handle these behaviors because every request passes through the middleware pipeline. section 4.1 section 4.0

Middleware handles a cross-cutting concern by processing the request before passing it onward, then participating again as the response returns.

  • Authentication can associate a request with a user; authorization can then check permissions for that request. section 4.1
  • A component can short-circuit the pipeline and return a response directly, becoming terminal middleware. section 4.1
  • Middleware applies broadly, while filters suit behavior that needs MVC constructs or only selected controllers, pages, or actions. section 21.3

A request pipeline

A request can pass through logging middleware, security-related middleware, authentication, authorization, and endpoint middleware. Authorization either forwards an allowed request so the endpoint can generate a response or stops a forbidden request before the endpoint runs. section 4.1

Figure 4.3 You can also think of middleware as being a series of nested components; a request is sent deeper into the middleware, and the response resurfaces from it. Each middleware component can execute logic before passing the response on to the next middleware component and can execute logic after the response has been created, on the way back out of the stack.
CH04_F03_Lock3

The nested-pipeline view shows how middleware runs on the way in and can run again on the way out, around response creation. section 4.1

How ASP.NET Core in Action, 3e covers it

How deep it goes
Explains pipeline flow, request modification, response handling, short-circuiting, and middleware-versus-filter scope.
Examples
Uses logging, security headers, user association, request language, exception handling, and permission checks as examples.
What it assumes
Assumes request and response flow while introducing middleware pipelines, short-circuiting, and filters through application examples.
What you can do after
Evaluate whether middleware or filters better fit a cross-cutting behavior’s required scope and framework access.
How it connects
Connects middleware with the middleware pipeline, authentication, authorization, endpoint processing, exception handling, logging, and MVC filters.

Also explained in

Compare how 2 books explain cross-cutting concern →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.