Linkr 1.0.0

dotnet add package Linkr --version 1.0.0
                    
NuGet\Install-Package Linkr -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="Linkr" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Linkr" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Linkr" />
                    
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 Linkr --version 1.0.0
                    
#r "nuget: Linkr, 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 Linkr@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=Linkr&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Linkr&version=1.0.0
                    
Install as a Cake Tool

Linkr

Linkr is a powerful request/response mediation library designed to provide clean separation of concerns, extensibility, and maintainability for complex business applications.

Introduction 👋

Image from URL

Linkr is a modern .NET-based mediation library built for clean, scalable request/response handling. Inspired by Mediator patterns, Linkr enables you to encapsulate business logic into handlers, enforce validation and authorization policies, monitor requests with diagnostics, and orchestrate advanced pipelines via behaviors—all using a modular and pluggable architecture.

With Linkr, developers can focus on domain logic while seamlessly integrating cross-cutting concerns like logging, validation, timeouts, and authorization. Whether you're building microservices, monoliths, or APIs, Linkr empowers you to keep your application maintainable, testable, and aligned with clean architecture principles

Authors

Features

  • Request/Response Pattern
  • Authorization Policies
  • Validation Policies
  • Cancellation Policy
  • Timeout Policy
  • Diagnostics (Logging)
  • Custom Authorization Attributes
  • Pipeline Behavior

Usage/Examples

1- Request/Response Pattern Examples

Query Example

-- Example - Query:

public class GetUserQuery : IRequest<UserDto> 
{
    public string UserId { get; set; }
}

public class GetUserQueryHandler : IRequestHandler<GetUserQuery, UserDto>
{
    public Task<UserDto> Handle(GetUserQuery request, CancellationToken cancellationToken)
    {
        return Task.FromResult(new UserDto { Id = request.UserId, Name = "John Doe" });
    }
}

Command Example

public class CreateUserCommand : IRequest<bool>
{
    public string Email { get; set; }
    public string Name { get; set; }
}

public class CreateUserCommandHandler : IRequestHandler<CreateUserCommand, bool>
{
    public Task<bool> Handle(CreateUserCommand request, CancellationToken cancellationToken)
    {
        // Save user to DB
        return Task.FromResult(true);
    }
}

2- Pipeline Behaviors

Add cross-cutting concerns (logging, validation, etc.)

Example


public class LoggingBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, 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(TResponse).Name}");
        return response;
    }
}

3- Validation Behaviors

Ensure requests are valid before execution.

Example


public class CreateUserValidator : IValidator<CreateUserCommand>
{
    public void Validate(CreateUserCommand request)
    {
        if (string.IsNullOrWhiteSpace(request.Email))
            throw new ValidationException("Email is required");
    }
}

4- Authorization Behaviors

Enforce role or claim-based access to request handlers.

Example


[Authorize(Roles = "Admin")]
public class DeleteUserCommand : IRequest<bool> 
{
    public string UserId { get; set; }
}

5- Custom Authorization Attributes

You can customize security checks using user context.

Example


[OnlySelf]
public class UpdateUserProfileCommand : IRequest<bool>
{
    public string UserId { get; set; }
    public string NewName { get; set; }
}


public class OnlySelfAttribute : Attribute, IAuthorizationRequirement
{
    public bool IsAuthorized(IUserContext userContext, object request)
    {
        if (request is UpdateUserProfileCommand cmd)
            return userContext.UserId == cmd.UserId;

        return false;
    }
}

6- Custom Authorization Attributes

Automatically handle timeout and cancellation tokens in long-running tasks.

Example


public class TimeoutBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
{
    public async Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate<TResponse> next)
    {
        using var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
        cts.CancelAfter(TimeSpan.FromSeconds(10));
        return await next();
    }
}

-- To reigster into DI 
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(TimeoutBehavior<,>));

7- Custom Authorization Attributes

Diagnostics Support.

Example


public class DiagnosticBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
{
    public async Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate<TResponse> next)
    {
        var stopwatch = Stopwatch.StartNew();
        var response = await next();
        stopwatch.Stop();
        Console.WriteLine($"Request {typeof(TRequest).Name} took {stopwatch.ElapsedMilliseconds}ms");
        return response;
    }
}


-- To register into DI 
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(DiagnosticBehavior<,>));

Product 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 was computed.  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.
  • net8.0

    • No dependencies.

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 219 4/22/2025