miguelbtcode.MediaHub.Contracts 1.0.0

The owner has unlisted this package. This could mean that the package is deprecated, has security vulnerabilities or shouldn't be used anymore.
dotnet add package miguelbtcode.MediaHub.Contracts --version 1.0.0
                    
NuGet\Install-Package miguelbtcode.MediaHub.Contracts -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="miguelbtcode.MediaHub.Contracts" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="miguelbtcode.MediaHub.Contracts" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="miguelbtcode.MediaHub.Contracts" />
                    
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 miguelbtcode.MediaHub.Contracts --version 1.0.0
                    
#r "nuget: miguelbtcode.MediaHub.Contracts, 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 miguelbtcode.MediaHub.Contracts@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=miguelbtcode.MediaHub.Contracts&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=miguelbtcode.MediaHub.Contracts&version=1.0.0
                    
Install as a Cake Tool

MediaHub

NuGet Build Status License: MIT

MediaHub is a lightweight and efficient implementation of the Mediator pattern for .NET applications, designed to facilitate communication between components while maintaining low coupling.

Features

  • Lightweight and fast implementation of the Mediator pattern
  • Behavior pipeline for intercepting and modifying requests
  • Built-in caching support
  • Easy registration with .NET's service container
  • Compatible with dependency injection
  • Automated validations

Installation

You can install MediaHub via NuGet:

dotnet add package MediaHub

Or using the Visual Studio Package Manager:

Install-Package MediaHub

Basic Usage

Configuration

Register MediaHub in your service container:

services.AddMediaHub(config => config
    .RegisterServicesFromAssemblies(typeof(Startup).Assembly));

Define a Request and Handler

// Define a request
public class GetUserByIdQuery : IRequest<UserDto>
{
    public int UserId { get; set; }
}

// Implement a handler
public class GetUserByIdHandler : IRequestHandler<GetUserByIdQuery, UserDto>
{
    private readonly IUserRepository _userRepository;

    public GetUserByIdHandler(IUserRepository userRepository)
    {
        _userRepository = userRepository;
    }

    public async Task<UserDto> Handle(GetUserByIdQuery request, CancellationToken cancellationToken)
    {
        var user = await _userRepository.GetByIdAsync(request.UserId, cancellationToken);
        return new UserDto { Id = user.Id, Name = user.Name, Email = user.Email };
    }
}

Using the Mediator

public class UserController : ControllerBase
{
    private readonly IMediator _mediator;

    public UserController(IMediator mediator)
    {
        _mediator = mediator;
    }

    [HttpGet("{id}")]
    public async Task<ActionResult<UserDto>> GetUser(int id)
    {
        var query = new GetUserByIdQuery { UserId = id };
        var result = await _mediator.Send(query);
        return Ok(result);
    }
}

Pipeline Behaviors

MediaHub allows you to add pipeline behaviors that execute before and after handlers:

Validation Behavior

public class ValidationBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
    where TRequest : IRequest<TResponse>
{
    private readonly IEnumerable<IValidator<TRequest>> _validators;

    public ValidationBehavior(IEnumerable<IValidator<TRequest>> validators)
    {
        _validators = validators;
    }

    public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next, CancellationToken cancellationToken)
    {
        if (_validators.Any())
        {
            var context = new ValidationContext<TRequest>(request);
            var validationResults = await Task.WhenAll(
                _validators.Select(v => v.ValidateAsync(context, cancellationToken)));
            
            var failures = validationResults
                .SelectMany(r => r.Errors)
                .Where(f => f != null)
                .ToList();

            if (failures.Count != 0)
                throw new ValidationException(failures);
        }

        return await next();
    }
}

Registering Behaviors

services.AddMediaHub(config => config
    .RegisterServicesFromAssemblies(typeof(Startup).Assembly)
    .AddGlobalPipelineBehavior<ValidationBehavior<,>>());

Caching Support

MediaHub includes built-in caching support:

// Mark requests as cacheable
public class GetCachedDataQuery : IRequest<DataResult>, ICacheable
{
    public int Id { get; set; }
    
    public string CacheKey => $"GetCachedData-{Id}";
}

// Register the caching behavior
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(CachingBehavior<,>));

Examples

For more detailed examples, check the /samples folder in the repository.

Contributing

Contributions are welcome. Please feel free to submit pull requests or open issues if you find any problems or have suggestions.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

License

This project is licensed under the MIT License - see the LICENSE file for details.

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.
  • net9.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