Dzidek.Net.Infrastructure.MadiatR.AttributeBasedInfrastructure 1.0.0

dotnet add package Dzidek.Net.Infrastructure.MadiatR.AttributeBasedInfrastructure --version 1.0.0                
NuGet\Install-Package Dzidek.Net.Infrastructure.MadiatR.AttributeBasedInfrastructure -Version 1.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Dzidek.Net.Infrastructure.MadiatR.AttributeBasedInfrastructure" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Dzidek.Net.Infrastructure.MadiatR.AttributeBasedInfrastructure --version 1.0.0                
#r "nuget: Dzidek.Net.Infrastructure.MadiatR.AttributeBasedInfrastructure, 1.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Dzidek.Net.Infrastructure.MadiatR.AttributeBasedInfrastructure as a Cake Addin
#addin nuget:?package=Dzidek.Net.Infrastructure.MadiatR.AttributeBasedInfrastructure&version=1.0.0

// Install Dzidek.Net.Infrastructure.MadiatR.AttributeBasedInfrastructure as a Cake Tool
#tool nuget:?package=Dzidek.Net.Infrastructure.MadiatR.AttributeBasedInfrastructure&version=1.0.0                

Extendable attribute-based MediatR infrastructure framework

An easy way to focus on what is most important and critical - business requirements

Main concept

The framework provides an extensible and configurable way to implement infrastructure code such as RetryPolicy, Circuit breaker, Timeout and another infrastructure

How to start

Install the required package

To install a Dzidek.Net.Infrastructure.MadiatR.AttributeBasedInfrastructure module into project, Nuget Package Manager Console can be used:

Install-Package Dzidek.Net.Infrastructure.MadiatR.AttributeBasedInfrastructure -ProjectName <ProjectName>
Create your own infrastructure attribute

Create your own infrastructure attribute

using Dzidek.Net.Infrastructure.MadiatR.AttributeBasedInfrastructure;
...
public class MyAttribute1Attribute : BaseInfrastructureAttribute
{
  public override Task<TResponse> Handle<TRequest, TResponse>(Func<TRequest, CancellationToken, Task<TResponse>> action, TRequest request, CancellationToken cancellationToken)
  {
  }
}

For example RetryPolicy using Polly.Net

using Dzidek.Net.Infrastructure.MadiatR.AttributeBasedInfrastructure;
using Polly;
using Polly.Contrib.WaitAndRetry;
...
internal sealed class RetryPolicyAttribute : BaseInfrastructureAttribute
{
  private ILogger<MyAttribute1Attribute>? _logger;
  public override Task<TResponse> Handle<TRequest, TResponse>(Func<TRequest, CancellationToken, Task<TResponse>> action, TRequest request, CancellationToken cancellationToken)
  {
    _logger?.LogInformation("RetryPolicyAttribute has been called");
    var delay = Backoff.DecorrelatedJitterBackoffV2(medianFirstRetryDelay: TimeSpan.FromSeconds(1), retryCount: 5);
    return Policy
      .Handle<Exception>()
      .WaitAndRetryAsync(delay)
      .ExecuteAsync(async () => await action(request, cancellationToken));
  }
  public override void RegisterServices(IServiceProvider contextServiceProvider)
  {
    _logger = contextServiceProvider.GetService<ILogger<MyAttribute1Attribute>>();
    base.RegisterServices(contextServiceProvider);
  }
}
Get services in your attribute if you need them

To get service instance in your attribute override method RegisterServices.

  public override void RegisterServices(IServiceProvider contextServiceProvider)
  {
    //Get services right here
    //var service = contextServiceProvider.GetRequiredService<IService>();
    base.RegisterServices(contextServiceProvider);
  }
Register the attributes in the IoC container

Register MediatR attribute based infrastructure in IoC

using Dzidek.Net.Infrastructure.AttributeBasedInfrastructure.Middlewares;
...
builder.Services
  .AddMediatRAttributeBasedInfrastructure();
Decorate query with your attribute
[MyAttribute1]
[RetryPolicy]
internal sealed class AttributedImplementationQuery : IRequest<int> { }
Call sequence

Attributes are called from top to bottom. The sequence of calls is similar to the DotNet middleware. For more information please go to https://github.com/DzidekDotNet/Dzidek.Net.Infrastructure.MadiatR.AttributeBasedInfrastructure

Working example

Working example you can find in project repo https://github.com/DzidekDotNet/Dzidek.Net.Infrastructure.MadiatR.AttributeBasedInfrastructure

Changelog

  • 1.0.0
    • Working example
    • Abstract attribute
    • IoC registration

Nuget packages

Dzidek.Net.Infrastructure.MadiatR.AttributeBasedInfrastructure

Authors

@DzidekDotNet

License

MIT

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 263 11/1/2023
1.0.0-rc1 105 11/1/2023