Adam Anderly

Husband, Father, Developer

  • About
  • Blog
  • Archives
  • Plugins
    • WooCommerce MailChimp
    • Wistia Responsive
  • Contact

Copyright © 2025
Adam Anderly ยท Log in

Cross-Cutting Concerns with MediatR Pipeline Behaviors

December 12, 2019 By Adam Leave a Comment

I’ve recently been exploring MediatR Pipeline Behaviors and cross-cutting concerns (logging, validation, security, transactions) but also wanted to tackle some performance and resilience scenarios that may not always apply to every request/command (retry, fallback and caching).

Here are some gists that tackle these concerns:

Caching Behavior (using ASP.NET Core IDistributedCache):

For caching, I’m using a separately defined CachePolicy similar to a FluentValidation Validator class.

Fallback Behavior (using Polly):

For Fallback and Retry behavior, I’m using separate interfaces IFallbackHandler and IRetryableRequest to get the behavior.

Retry Behavior (using Polly):

These can all be combined as well and work based on the order they’re registered in Startup.cs. In my case, I want my main Handle method to retry, then, optionally, fallback to my HandleFallback, then finally cache the result based on the defined CachePolicy.

I’m debating between using separately defined classes/policies (like FluentValidation Validators) vs. making Handlers implement the interfaces to get the behavior. On one hand, if they’re separate, I could move the Cache, Fallback and Retry policies to separate files, folders, etc. and keep my main MediatR handlers very lean and clean. If you want to see them in the same place, you could put them in the same file like many of Jimmy Bogard’s examples with Validators, etc. But if the files get too big and you want to separate them out, you could do that. However, with Retry and Fallback, it’s kind of nice seeing that with the main request handler.

Of course, all this gets wired-up and registered in Startup.cs. I’m using Scrutor to aid in discovery my Caching, Fallback and Retry policies. See below.

What do you think? Which approach do you prefer? Let me know in the comments.

Share this:

  • Twitter
  • LinkedIn
  • Email
  • Print
  • More
  • Reddit

Filed Under: .NET, ASP.NET Core, MediatR Tagged With: ASP.NET Core, Caching, Fallback, MediatR, Polly, Retry, Scrutor

RSS

RSS Feed

Subscribe

Enter your email address to subscribe and receive new posts by email.

Find It Here

Top Posts

  • Cross-Cutting Concerns with MediatR Pipeline Behaviors

Recent Posts

  • Cross-Cutting Concerns with MediatR Pipeline Behaviors
  • Laravel User Timezone Aware Trait
  • Useful Laravel Model Traits: Gravatar
  • Laravel Transformable – An Eloquent Model Trait for Consumable Models
  • Gravity Forms + Microsoft Dynamics CRM

Categories

  • .NET (12)
  • ASP.NET Core (1)
  • ASP.NET MVC (7)
  • Dependency Injection (3)
  • Laravel (3)
  • Laravel Traits (2)
  • MediatR (1)
  • Node.js (2)
  • NuGet (5)
  • PHP (2)
  • REST (4)
  • Silverlight (5)
  • Uncategorized (3)
  • WordPress (7)
  • WordPress Plugins (7)

Tags

.NET APIs ASP.NET Core ASP.NET MVC Azure Caching cli CRM Dependency Injection dns dnsimple Dynamics Dynamics CRM Expansive Fallback Gravity Forms iPhone Laravel Laravel Traits MailChimp MediatR Microsoft Dynamics CRM MobileMe Node.js NuGet PHP Polly REST Retry Rounding Scrutor Silverlight SimpleMembership sublime-text-2 VS2010 Wistia WooCommerce WordPress WordPress Plugins
  • About
  • Blog
  • Archives
  • Plugins
    • WooCommerce MailChimp
    • Wistia Responsive
  • Contact
loading Cancel
Post was not sent - check your email addresses!
Email check failed, please try again
Sorry, your blog cannot share posts by email.