Xpandables.Net 7.0.0

.NET 7.0
dotnet add package Xpandables.Net --version 7.0.0
NuGet\Install-Package Xpandables.Net -Version 7.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="Xpandables.Net" Version="7.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Xpandables.Net --version 7.0.0
#r "nuget: Xpandables.Net, 7.0.0"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install Xpandables.Net as a Cake Addin
#addin nuget:?package=Xpandables.Net&version=7.0.0

// Install Xpandables.Net as a Cake Tool
#tool nuget:?package=Xpandables.Net&version=7.0.0

Xpandables.Net

Provides with useful interfaces contracts in .Net 7.0 and some implementations mostly following the spirit of SOLID principles, CQRS... The library is strongly-typed, which means it should be hard to make invalid requests and it also makes it easy to discover available methods and properties though IntelliSense.

Feel free to fork this project, make your own changes and create a pull request.

Read the Xpandables.Net.Samples for a minimal Web Api implementation using multi-tenancy with aggregates. You may need to create a PostgreSql database using the migration definitions.

Features

Usually, when registering types, we are forced to reference the libraries concerned and we end up with a very coupled set. To avoid this, you can register these types by calling an export extension method, which uses MEF: Managed Extensibility Framework.

In your api startup class


// AddXServiceExport(IConfiguration, Action{ExportServiceOptions}) adds and configures registration of services using 
// the IAddServiceExport interface implementation found in the target libraries according to the export options.
// You can use configuration file to set up the libraries to be scanned.

public class Startup
{
    ....
    services
        .AddXServices()
            .AddXServiceExport(Configuration, options => options.SearchPattern = "your-search-pattern-dll")
        .Build();
    ...
}

In the library you want types to be registered


[Export(typeof(IAddServiceExport))]
public sealed class RegisterServiceExport : IAddServiceExport
{
    public void AddServices(IServiceCollection services, IConfiguration configuration)
    {
        services
            .AddXServices()
                .AddXCommandDispatcher()
                ...
            .Build();
        ....
    }
}

Decorator pattern

You can use the extension methods to apply the decorator pattern to your types.


// This method and its extensions ensure that the supplied TDecorator" decorator is returned, wrapping the original 
// registered "TService", by injecting that service type into the constructor of the supplied "TDecorator". 
// Multiple decorators may be applied to the same "TService". By default, a new "TDecorator" instance 
// will be returned on each request, 
// independently of the lifestyle of the wrapped service. Multiple decorators can be applied to the same service type. 
// The order in which they are registered is the order they get applied in. This means that the decorator 
// that gets registered first, gets applied first, which means that the next registered decorator, 
// will wrap the first decorator, which wraps the original service type.

 services
    .AddXServices()
        .XTryDecorate<TService, TDecorator>()
    .Build();
   

Suppose you want to add logging for the AddPersonCommand ...


// The AddPersonCommand decorator for logging

public sealed class AddPersonCommandHandlerLoggingDecorator : 
    ICommandHandler<AddPersonCommand>
{
    private readonly ICommandHandler<AddPersonCommand> _decoratee;
    private readonly ILogger<AddPersonCommandHandler> _logger;
    
    public AddPersonCommandHandlerLoggingDecorator(
        ILogger<AddPersonCommandHandler> logger,
        ICommandHandler<AddPersonCommand> decoratee)
        => (_logger, _decoratee) = (logger, decoratee);

    public async ValueTask<OperationResult> HandleAsync(
        AddPersonCommand command, CancellationToken cancellationToken = default)
    {
        _logger.Information(...);
        
        var response = await _decoratee.HandleAsync(command, cancellationToken).configureAwait(false);
        
        _logger.Information(...)
        
        return response;
    }
}

// Register

services
    .AddXServices()
        .XTryDecorate<AddPersonCommandHandler, AddPersonCommandHandlerLoggingDecorator>()
    .Build();

 // or

services.AddXServices()
    .AddXHandlers(
        options =>
        {
            options.UseValidatorDecorator(); // this option will add the command decorator registration
        },
        typeof(AddPersonCommandHandler).Assembly)
    .Build()

// or you can define the generic model, for all commands that implement ICommand 
// interface or something else.

public sealed class CommandLoggingDecorator<TCommand> : ICommandHandler<TCommand>
    where TCommand : notnull, ICommand // you can add more constraints
{
    private readonly ICommandHandler<TCommand> _ decoratee;
    private readonly ILogger<TCommand> _logger;
    
    public CommandLoggingDecorator(ILogger<TCommand> logger, ICommandHandler<TCommand> decoratee)
        => (_logger, _ decoratee) = (logger, decoratee);

    public async ValueTask<OperationResult> HandleAsync(
         TCommand command, CancellationToken cancellationToken = default)
    {
        _logger.Information(...);
        
        var response = await _decoratee.HandleAsync(command, cancellationToken).configureAwait(false);
        
        _logger.Information(...)
        
        return response;
    }
}

// and for registration

// The CommandLoggingDecorator will be applied to all command handlers whose commands meet 
// the decorator's constraints : 
// To be a notnull and implement ICommand interface

services
    .AddXServices()
        .XTryDecorate(typeof(ICommandHandler<>), typeof(CommandLoggingDecorator<>))
    .Build();

Aggregate{TAggregateId}

Libraries also provide with DDD model implementation 'Aggregate{TAggregateId}' using event sourcing and out-box pattern.

Product Versions
.NET net7.0 net7.0-android net7.0-ios net7.0-maccatalyst net7.0-macos net7.0-tvos net7.0-windows
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (10)

Showing the top 5 NuGet packages that depend on Xpandables.Net:

Package Downloads
Xpandables.Net.DependencyInjection

A utility library in .Net7.0 to easily add dependency injection using the Xpandables.Net library

Xpandables.NetCore

A utility library in .Net5.0 to add AspNetCore behaviors to Xpandables.Net.

Xpandables.Net.Windows

A utility library in .Net5.0 to extend Windows Forms and WPF behaviors.

Xpandables.Net.QrCodes

A utility library in .Net5.0 to create qr-codes.

Xpandables.Net.BlazorExtended

A utility library in .Net6.0 to add helpers to Blazor.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
7.0.0 206 11/9/2022
7.0.0-rc2.0.1 47 10/12/2022
7.0.0-rc1.0.0 53 9/26/2022
6.1.1 434 8/6/2022
6.0.9 478 7/9/2022
6.0.8 454 6/27/2022
6.0.4 478 3/15/2022
6.0.3 478 2/22/2022
6.0.2 285 1/4/2022
6.0.1 332 12/4/2021
6.0.0 426 11/8/2021
6.0.0-rc.4.3 100 11/3/2021
6.0.0-rc.3.1 104 10/15/2021
6.0.0-rc.3 95 10/14/2021
6.0.0-rc.2 93 9/21/2021
6.0.0-preview.5 102 8/26/2021
5.6.1 603 6/30/2021
5.6.0 580 6/9/2021
5.5.1 570 5/26/2021
5.4.4 489 4/12/2021
5.4.0 507 3/11/2021
5.3.14 444 3/2/2021
5.3.13 420 2/25/2021
5.3.12 424 2/21/2021
5.3.11 420 2/18/2021
5.3.10 433 2/18/2021
5.3.9 474 2/11/2021
5.3.8 495 2/10/2021
5.3.7 443 2/7/2021
5.3.6 283 2/7/2021
5.3.5 439 2/7/2021
5.3.4 443 2/7/2021
5.3.3 420 2/5/2021
5.3.2 461 2/2/2021
5.3.1 445 1/31/2021
5.3.0 434 1/31/2021
5.2.14 475 1/26/2021
5.2.13 434 1/25/2021
5.2.12 465 1/22/2021
5.2.11 483 1/19/2021
5.2.10 429 1/16/2021
5.2.9 452 1/13/2021
5.2.8 457 1/8/2021
5.2.7 484 1/6/2021
5.2.6 458 1/6/2021
5.2.5 502 12/17/2020
5.2.4 474 12/12/2020
5.2.3 489 12/8/2020
5.2.2 476 12/7/2020
5.2.1 494 12/7/2020
5.2.0 551 12/6/2020
5.1.1 578 12/6/2020
5.1.0 503 12/5/2020
5.0.6 515 12/5/2020
5.0.5 457 11/23/2020
5.0.4 549 11/22/2020
5.0.3 667 11/20/2020
5.0.2 559 11/19/2020
5.0.1 635 11/16/2020
5.0.0 606 11/12/2020
5.0.0-rc.2.1.4 211 11/6/2020
5.0.0-rc.2.1.3 216 11/1/2020
5.0.0-rc.2.1.2 190 10/31/2020
5.0.0-rc.2.1.0 237 10/24/2020
5.0.0-rc.2.0.2 151 10/22/2020
5.0.0-rc.2.0.1 188 10/17/2020
5.0.0-rc.2.0.0 191 10/17/2020
5.0.0-rc.1.1.5 258 10/11/2020
5.0.0-rc.1.1.4 242 10/11/2020
5.0.0-rc.1.1.3 299 10/10/2020
5.0.0-rc.1.1.2 180 10/4/2020
5.0.0-rc.1.1.1 200 10/2/2020
5.0.0-rc.1.1.0 173 10/1/2020
5.0.0-rc.1.0.9 231 9/29/2020
5.0.0-rc.1.0.8 162 9/28/2020
5.0.0-rc.1.0.7 201 9/28/2020
5.0.0-rc.1.0.6 257 9/26/2020
5.0.0-rc.1.0.5 207 9/25/2020
5.0.0-rc.1.0.4 173 9/24/2020
5.0.0-rc.1.0.3 236 9/23/2020
5.0.0-rc.1.0.2 194 9/23/2020
5.0.0-rc.1.0.1 179 9/21/2020
5.0.0-preview.2.0.0 194 8/16/2020
5.0.0-preview.1.0.8 181 8/10/2020
5.0.0-preview.1.0.7 239 8/10/2020
5.0.0-preview.1.0.6 237 8/5/2020
5.0.0-preview.1.0.5 239 8/3/2020
5.0.0-preview.1.0.4 196 8/3/2020
5.0.0-preview.1.0.3 244 8/2/2020
5.0.0-preview.1.0.2 283 8/2/2020
5.0.0-preview.1.0.1 299 8/2/2020
5.0.0-preview.1.0.0 336 7/25/2020
3.2.1 755 9/17/2020
3.2.0 801 9/15/2020
3.1.9 775 9/13/2020
3.1.8 852 9/13/2020
3.1.7 751 9/12/2020
3.1.6 937 9/5/2020
3.1.5 753 9/4/2020
3.1.4 841 8/29/2020
3.1.3 786 8/28/2020
3.1.2 760 8/27/2020
3.1.1 766 8/24/2020
3.1.0 813 8/19/2020

Add the primitive record class to define primitive types.