Interlink 1.2.1
dotnet add package Interlink --version 1.2.1
NuGet\Install-Package Interlink -Version 1.2.1
<PackageReference Include="Interlink" Version="1.2.1" />
<PackageVersion Include="Interlink" Version="1.2.1" />
<PackageReference Include="Interlink" />
paket add Interlink --version 1.2.1
#r "nuget: Interlink, 1.2.1"
#addin nuget:?package=Interlink&version=1.2.1
#tool nuget:?package=Interlink&version=1.2.1
Interlink
Interlink is a lightweight and modern mediator library for .NET, designed to decouple your code through request/response and notification patterns. Built with simplicity and performance in mind, it helps streamline communication between components while maintaining a clean architecture.
โจ Features
- ๐งฉ Simple mediator pattern for request/response
- ๐ Publish/Subscribe notification system
- ๐ง Pipeline behaviors (logging, validation, etc.)
- ๐ง Clean separation of concerns via handlers
- ๐ช Dependency injection support out of the box
- ๐ Decouples logic using handlers
- ๐งฉ Easy registration with
AddInterlink()
- ๐ Lightweight, fast, and no external dependencies
- ๐ Pre and Post Processors for enhanced lifecycle control
- โ Compatible with .NET 8 and .NET 9
๐ก Why Interlink?
- Clean, intuitive API
- No bloat โ just powerful mediation
- Perfect for CQRS, Clean Architecture, Modular Design
- Highly extensible with behaviors and notifications
๐ฆ Installation
Install Interlink via NuGet:
dotnet add package Interlink
โ๏ธ Setup
Register Interlink in your Startup.cs
or Program.cs
builder.Services.AddInterlink();
You can optionally pass an Assembly:
builder.Services.AddInterlink(typeof(MyHandler).Assembly);
๐จ Request/Response Pattern
1. Define a request:
public class GetAllPets
{
public record Query : IRequest<List<string>>;
public class Handler : IRequestHandler<Query, List<string>>
{
public Task<List<string>> Handle(Query request, CancellationToken cancellationToken)
{
var pets = new List<string> { "Dog", "Cat", "Fish" };
return Task.FromResult(pets);
}
}
}
2. Inject and use ISender:
[ApiController]
[Route("api/[controller]")]
public class PetController(ISender sender) : ControllerBase
{
[HttpGet]
public async Task<IActionResult> GetAllPets(CancellationToken cancellationToken)
{
var pets = await sender.Send(new GetAllPets.Query(), cancellationToken);
return Ok(pets);
}
}
๐ฃ Notifications (One-to-Many)
1. Define a notification:
public class UserCreated(string userName) : INotification
{
public string UserName { get; } = userName;
}
2. Create one or more handlers:
public class SendWelcomeEmail : INotificationHandler<UserCreated>
{
public Task Handle(UserCreated notification, CancellationToken cancellationToken)
{
Console.WriteLine($"Welcome email sent to {notification.UserName}");
return Task.CompletedTask;
}
}
3. Publish with IPublisher:
public class AccountService(IPublisher publisher)
{
public async Task RegisterUser(string username)
{
// Save to DB...
await publisher.Publish(new UserCreated(username));
}
}
๐งฌ Pipeline Behaviors
Useful for logging, validation, performance monitoring, etc.
1. Define a behavior:
public class LoggingBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
where TRequest : IRequest<TResponse>
{
public async Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate<TResponse> next)
{
Console.WriteLine($"Handling {typeof(TRequest).Name}");
var response = await next();
Console.WriteLine($"Handled {typeof(TRequest).Name}");
return response;
}
}
Pipeline behaviors can be manually registered like this:
builder.Services.AddInterlink();
builder.Services.AddScoped(typeof(IPipelineBehavior<,>), typeof(LoggingBehavior<,>));
๐ Pre and Post Processor
1. Define a Pre and Post Processors:
public class MyRequestPreProcessor : IRequestPreProcessor<GetAllPets.Query>
{
public Task Process(GetAllPets.Query request, CancellationToken cancellationToken)
{
Console.WriteLine("[PreProcessor] Processing GetAllPets request...");
return Task.CompletedTask;
}
}
public class MyRequestPostProcessor : IRequestPostProcessor<GetAllPets.Query, List<Pet>>
{
public Task Process(GetAllPets.Query request, List<Pet> response, CancellationToken cancellationToken)
{
Console.WriteLine("[PostProcessor] Processing GetAllPets response...");
return Task.CompletedTask;
}
}
Pre and Post Processors are automatically registered when you call AddInterlink().
๐ฆ API Overview
IRequest<TResponse>
public interface IRequest<TResponse> { }
IRequestHandler<TRequest, TResponse>
public interface IRequestHandler<TRequest, TResponse>
where TRequest : IRequest<TResponse>
{
Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken);
}
INotification
public interface INotification { }
INotificationHandler<TNotification>
public interface INotificationHandler<TNotification>
where TNotification : INotification
{
Task Handle(TNotification notification, CancellationToken cancellationToken);
}
ISender
public interface ISender
{
Task<TResponse> Send<TResponse>(IRequest<TResponse> request, CancellationToken cancellationToken = default);
}
IPublisher
public interface IPublisher
{
Task Publish<TNotification>(TNotification notification, CancellationToken cancellationToken = default)
where TNotification : INotification;
}
IPipelineBehavior<TRequest, TResponse>
public delegate Task<TResponse> RequestHandlerDelegate<TResponse>();
public interface IPipelineBehavior<TRequest, TResponse>
where TRequest : IRequest<TResponse>
{
Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate<TResponse> next);
}
IRequestPreProcessor<TRequest>
public interface IRequestPreProcessor<TRequest>
{
Task Process(TRequest request, CancellationToken cancellationToken);
}
IRequestPostProcessor<TRequest, TResponse>
public interface IRequestPostProcessor<TRequest, TResponse>
{
Task Process(TRequest request, TResponse response, CancellationToken cancellationToken);
}
๐ Example Use Case
- CQRS: Use
IRequest<TResponse>
for Queries and Commands - Event-Driven architecture: Use
INotification
for broadcasting domain events - Middleware-style behaviors: Add
IPipelineBehavior
for logging, validation, caching, etc.
๐ Roadmap
โ v1.0.0 โ Core Mediator Basics (Released)
- Basic
IRequest<TResponse>
andIRequestHandler<TRequest, TResponse>
ISender
for sending requestsAddInterlink()
for automatic DI registration- Clean, lightweight design
- Only .NET 9 support
โ v1.0.1 โ Core Mediator Basics (Released)
- Basic
IRequest<TResponse>
andIRequestHandler<TRequest, TResponse>
ISender
for sending requestsAddInterlink()
for automatic DI registration- Clean, lightweight design
- .NET 8+ support
โ v1.1.0 โ Notifications & Pipelines (Released)
INotification
andINotificationHandler<TNotification>
IPublisher
for event broadcastingIPipelineBehavior<TRequest, TResponse>
support- Enhanced
AddInterlink()
with scanning and registration for notifications and pipelines - Updated documentation and examples
- .NET 8+ support
โ v1.2.0 โ Pre/Post Processors (Released)
IRequestPreProcessor<TRequest>
interfaceIRequestPostProcessor<TRequest, TResponse>
interface- Pre and post hooks for request lifecycle
- Optional unit-of-work behaviors
โ v1.2.1 โ Fix Critical Bugs (Released)
- Fix critical bugs in
IPipelineBehavior<TRequest, TResponse>
๐ v1.3.0 โ Performance & Customization
- Handler resolution caching (delegate-based)
- Custom service factory injection support
- Pipeline ordering via attributes or configuration
- Assembly scanning filters by namespace or attribute
๐ v1.4.0 โ Extensions
Interlink.Extensions.Logging
โ built-in logging behaviorInterlink.Extensions.Validation
โ integration with FluentValidationInterlink.AspNetCore
โ model binding & filters for ASP.NET Core
๐งช v1.5.0 โ Developer Experience
- Source generator / Roslyn analyzer for missing handler detection
- Code snippets and templates for common patterns
- Custom exception types (e.g.,
HandlerNotFoundException
)
๐ Future Ideas
- Request cancellation and timeout behaviors
- Metrics collection and tracing support
- Dynamic or externalized pipeline config (e.g., JSON-based)
Stay tuned for more updates! Contributions and suggestions are welcome. โจ
๐ License
MIT License ยฉ ManuHub
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. 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. 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. |
-
net8.0
-
net9.0
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.