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
CachingBehaviorto improve performance. - Invalidate cached data on updates with
CacheRemovingBehavior.
Logging
- Automatically log request and response information using
LoggingBehaviorfor improved debugging and monitoring.
Performance Tracking
- Track and monitor the execution time of your request handlers with
PerformanceBehavior.
Transaction Management
- Handle atomic operations with
TransactionScopeBehaviorto 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
- AuthorizationBehavior
Protect your requests by implementing the ISecuredRequest interface. Specify the roles that can access the request.
public class MySecuredRequest : IRequest
- CachingBehavior
Leverage distributed caching by implementing ICachableRequest. Define a unique cache key, sliding expiration, and optional grouping for cache invalidation.
public class MyCachableRequest : IRequest
- CacheRemovingBehavior
Automatically remove or invalidate cached data after updates by implementing ICacheRemoverRequest.
public class MyCacheInvalidationRequest : IRequest
- LoggingBehavior
To enable logging for a request, simply implement the ILoggableRequest interface. No additional configuration is required.
public class MyLoggableRequest : IRequest
- PerformanceBehavior
Monitor performance by specifying a threshold for request execution time using IIntervalRequest.
public class MyPerformanceRequest : IRequest
- TransactionScopeBehavior
Ensure atomicity across operations by implementing the ITransactionalRequest interface.
public class MyTransactionalRequest : IRequest
- ValidationBehavior
Define rules for your requests using FluentValidation. These rules will be applied automatically before the request handler is executed.
public class MyRequestValidator : AbstractValidator
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
var loggerMock = new Mock<ILogger<CachingBehavior<MyRequest, MyResponse>>>();
var cachingBehavior = new CachingBehavior<MyRequest, MyResponse>(
cacheMock.Object,
loggerMock.Object,
Mock.Of
Example: Mocking LoggingBehavior
var loggerMock = new Mock<ILogger<LoggingBehavior<MyRequest, MyResponse>>>();
var httpContextAccessorMock = new Mock
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
Support
If you encounter any issues or have questions, please open an issue on GitHub or contact us at [email protected]
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.
Packages
| Package | Version | Downloads |
|---|---|---|
| Moongazing.PipelineEnhancer | 1.0.0 | 237 |