Git Product home page Git Product logo

one.more.lib.for.mediatr's Introduction

One More Lib For MediatR

CI NuGet

As the name suggests, a new library that extends MediatR with integrated PipelineBehavior:

  • Performance Logger
  • MemoryCache
  • Validation with FluentValidation
  • Retry Pattern with Polly

Installing One.More.Lib.For.MediatR

Link to the nuget package

We recommend using NuGet:

Install-Package One.More.Lib.For.MediatR

Or via the .NET Core command line interface:

dotnet add package One.More.Lib.For.MediatR

Performance Logger PipelineBehavior

How to configure it?

The simplest way:

builder.Services.AddMediatRExtensions(cfg => cfg.PerformanceLoggerSupport = true);

If you want to configure it:

builder.Services.AddMediatRExtensions(cfg => cfg.AddPerformanceLoggerSupport(500));

How yo use it?

Nothing to do. Requests exceeding th threshold will be logged.

MemoryCache PipelineBehavior

How to configure it?

The simplest way:

builder.Services.AddMediatRExtensions(cfg => cfg.MemoryCacheSupport = true);

If you want a complete configuration:

builder.Services.AddMediatRExtensions(cfg => cfg.AddMemoryCacheSupport(slidingExpiration: TimeSpan.FromMinutes(10), priority: CacheItemPriority.Low));

All the possible options are:

  • DateTimeOffset? AbsoluteExpiration
    
  • TimeSpan? AbsoluteExpirationRelativeToNow
    
  • TimeSpan? SlidingExpiration
    
  • CacheItemPriority Priority
    

You can also create a specific configuration with the same options for a given request:

builder.Services.AddMediatRExtensions(cfg =>
{
  cfg.AddMemoryCacheSupport(slidingExpiration: TimeSpan.FromMinutes(10), priority: CacheItemPriority.Low);
  cfg.AddMemoryCacheSupportOverrideFor<GiveRequest>(priority: CacheItemPriority.High);
}

How yo use it?

Just add [MemoryCache] attribute on requests classes or records.

[MemoryCache]
public class GetUserByName : IRequest<User?>
{
    public string Name { get; set; }
}

Validation with FluentValidation

How to configure it?

There is not simplest way because you need to enter the assemblies to be scanned for FluentValidation:

builder.Services.AddMediatRExtensions(cfg => cfg.AddFluentValidationSupport(new[] { typeof(Program).Assembly }));

How to use it?

Nothing to do, just create Validation rules. If a rule is not respected a ValidationException will be thrown.

Retry Pattern with Polly

How to configure it?

The simplest way:

builder.Services.AddMediatRExtensions(cfg => cfg.RetrySupport = true);

If you want to configure it:

builder.Services.AddMediatRExtensions(cfg => cfg.AddRetrySupport(retryCount: 3, retryDelay: 100));

When to use it?

  • For example, when you call on external resources and the infrastructure can sometimes have temporarey failures.
  • Or when you want to deal with the database and there may be temporary conflicts.

How to use it?

Just add [RetryPolicy] attribute on requests classes or records.

[RetryPolicy]
public class GetProduct : IRequest<Product>
{
    public Guid ProductId { get; set; }
}

Or you can override default values by passing them in the RetryPolicy Attribute

[RetryPolicy(OverrideRetryCount = true, RetryCount = 5)]
public class GetProduct : IRequest<Product>
{
    public Guid ProductId { get; set; }
}
[RetryPolicy(OverrideRetryDelay = true, RetryDelay = 200)]
public class GetProduct : IRequest<Product>
{
    public Guid ProductId { get; set; }
}

one.more.lib.for.mediatr's People

Contributors

pascal-libaud avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.