Skip to content

Advanced Mediator Pipelines and Domain-Driven Enhancements for Modern .NET Applications.

License

Notifications You must be signed in to change notification settings

Moongazing/PipelineEnhancer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

AuthorizationBehavior

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

public class MySecuredRequest : IRequest, ISecuredRequest { public string[] Roles => new[] { "Admin", "Editor" }; }

CachingBehavior

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

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

CacheRemovingBehavior

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

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

LoggingBehavior

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

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

PerformanceBehavior

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

public class MyPerformanceRequest : IRequest, IIntervalRequest { public int Interval => 5; // Log warning if execution exceeds 5 seconds }

TransactionScopeBehavior

Ensure atomicity across operations by implementing the ITransactionalRequest interface.

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

ValidationBehavior

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

public class MyRequestValidator : AbstractValidator { 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(); cacheMock.Setup(x => x.GetAsync("my-cache-key", It.IsAny())) .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() );

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 Handle(TRequest request, RequestHandlerDelegate 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 [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.

About

Advanced Mediator Pipelines and Domain-Driven Enhancements for Modern .NET Applications.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Sponsor this project

 

Packages

No packages published

Languages