Moongazing.PipelineEnhancer 1.0.0

dotnet add package Moongazing.PipelineEnhancer --version 1.0.0
                    
NuGet\Install-Package Moongazing.PipelineEnhancer -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="Moongazing.PipelineEnhancer" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Moongazing.PipelineEnhancer" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Moongazing.PipelineEnhancer" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Moongazing.PipelineEnhancer --version 1.0.0
                    
#r "nuget: Moongazing.PipelineEnhancer, 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.
#:package Moongazing.PipelineEnhancer@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Moongazing.PipelineEnhancer&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Moongazing.PipelineEnhancer&version=1.0.0
                    
Install as a Cake Tool

Moongazing Pipeline Enhancer

Moongazing Pipeline Enhancer is a robust and extensible library tailored for MediatR-based .NET applications. It provides a suite of prebuilt pipeline behaviors that enhance the development experience by simplifying cross-cutting concerns such as caching, logging, validation, performance tracking, authorization, and transaction management.

This library aims to streamline application development by enforcing clean architecture principles, improving code readability, and fostering reusability. Whether you're building a monolithic application or a distributed microservices system, Moongazing Pipeline Enhancer adapts seamlessly to your requirements.


โœจ Features

๐Ÿ›ก๏ธ Authorization

  • Enforce role-based access control on your requests using AuthorizationBehavior.

๐Ÿ“ฆ Caching

  • Cache request results with CachingBehavior to improve performance.
  • Invalidate cached data on updates with CacheRemovingBehavior.

๐Ÿ–‹๏ธ Logging

  • Automatically log request and response information using LoggingBehavior for improved debugging and monitoring.

๐Ÿš€ Performance Tracking

  • Track and monitor the execution time of your request handlers with PerformanceBehavior.

โš™๏ธ Transaction Management

  • Handle atomic operations with TransactionScopeBehavior to ensure consistency across multiple operations.

โœ… Validation

  • Validate requests using FluentValidation integrated via ValidationBehavior.

๐Ÿ”ง Custom Behavior Support

  • Extend the pipeline by creating your custom behaviors with minimal effort.

๐Ÿ› ๏ธ Installation

1. Install the NuGet Package

Add the library to your project using NuGet:

dotnet add package Moongazing.PipelineEnhancer

  • Add Required Configuration

If you're using caching features, ensure you define CacheSettings in your appsettings.json file:

{ "CacheSettings": { "SlidingExpiration": 30 } }

Register the Pipeline Behaviors

Add the pipeline behaviors to your application by registering them in the Program.cs or Startup.cs file:

var builder = WebApplication.CreateBuilder(args);

// Add MediatR and Moongazing Pipeline Enhancer behaviors builder.Services.AddPipelineBehaviors();

var app = builder.Build(); app.Run();

๐Ÿš€ Usage

  1. AuthorizationBehavior

Protect your requests by implementing the ISecuredRequest interface. Specify the roles that can access the request.

public class MySecuredRequest : IRequest<MyResponse>, ISecuredRequest { public string[] Roles โ‡’ new[] { "Admin", "Editor" }; }

  1. CachingBehavior

Leverage distributed caching by implementing ICachableRequest. Define a unique cache key, sliding expiration, and optional grouping for cache invalidation.

public class MyCachableRequest : IRequest<MyResponse>, ICachableRequest { public string CacheKey โ‡’ "my-unique-cache-key"; public bool BypassCache โ‡’ false; public string? CacheGroupKey โ‡’ "my-cache-group"; public TimeSpan? SlidingExpiration โ‡’ TimeSpan.FromMinutes(15); }

  1. CacheRemovingBehavior

Automatically remove or invalidate cached data after updates by implementing ICacheRemoverRequest.

public class MyCacheInvalidationRequest : IRequest<MyResponse>, ICacheRemoverRequest { public string CacheKey โ‡’ "my-unique-cache-key"; public string? CacheGroupKey โ‡’ "my-cache-group"; public bool BypassCache โ‡’ false; }

  1. LoggingBehavior

To enable logging for a request, simply implement the ILoggableRequest interface. No additional configuration is required.

public class MyLoggableRequest : IRequest<MyResponse>, ILoggableRequest { public string Data { get; set; } }

  1. PerformanceBehavior

Monitor performance by specifying a threshold for request execution time using IIntervalRequest.

public class MyPerformanceRequest : IRequest<MyResponse>, IIntervalRequest { public int Interval โ‡’ 5; // Log warning if execution exceeds 5 seconds }

  1. TransactionScopeBehavior

Ensure atomicity across operations by implementing the ITransactionalRequest interface.

public class MyTransactionalRequest : IRequest<MyResponse>, ITransactionalRequest { public int EntityId { get; set; } }

  1. ValidationBehavior

Define rules for your requests using FluentValidation. These rules will be applied automatically before the request handler is executed.

public class MyRequestValidator : AbstractValidator<MyRequest> { public MyRequestValidator() { RuleFor(x โ‡’ x.Name).NotEmpty().WithMessage("Name is required."); RuleFor(x โ‡’ x.Age).GreaterThan(0).WithMessage("Age must be greater than 0."); } }

๐Ÿงช Testing

Moongazing Pipeline Enhancer is built with testability in mind. You can mock its behaviors and dependencies for unit testing. Example: Mocking CachingBehavior

var cacheMock = new Mock<IDistributedCache>(); cacheMock.Setup(x โ‡’ x.GetAsync("my-cache-key", It.IsAny<CancellationToken>())) .ReturnsAsync(Encoding.UTF8.GetBytes("{"Message":"Cached Response"}"));

var loggerMock = new Mock<ILogger<CachingBehavior<MyRequest, MyResponse>>>();

var cachingBehavior = new CachingBehavior<MyRequest, MyResponse>( cacheMock.Object, loggerMock.Object, Mock.Of<IConfiguration>() );

Example: Mocking LoggingBehavior

var loggerMock = new Mock<ILogger<LoggingBehavior<MyRequest, MyResponse>>>(); var httpContextAccessorMock = new Mock<IHttpContextAccessor>();

var loggingBehavior = new LoggingBehavior<MyRequest, MyResponse>( httpContextAccessorMock.Object, loggerMock.Object );

๐Ÿ“š Advanced Configuration Extending Moongazing Pipeline Enhancer

Add your own custom pipeline behaviors by implementing the IPipelineBehavior<TRequest, TResponse> interface:

public class CustomBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse> { public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next, CancellationToken cancellationToken) { // Pre-processing logic var response = await next(); // Post-processing logic return response; } }

๐Ÿ”ง Support

If you encounter any issues or have questions, please open an issue on GitHub or contact us at tunahan.ali.ozturk@outlook.com

โค๏ธ Acknowledgements

Special thanks to the .NET community for inspiration and support in building this library..

๐Ÿ‘จโ€๐Ÿ’ป About the Author

Moongazing Pipeline Enhancer is developed and maintained by the Tunahan Ali Ozturk, passionate software developer dedicated to creating scalable and maintainable solutions.

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.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 125 11/28/2024