DotnetCQRS.Extensions.FluentAssertions 1.1.0

dotnet add package DotnetCQRS.Extensions.FluentAssertions --version 1.1.0
NuGet\Install-Package DotnetCQRS.Extensions.FluentAssertions -Version 1.1.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="DotnetCQRS.Extensions.FluentAssertions" Version="1.1.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add DotnetCQRS.Extensions.FluentAssertions --version 1.1.0
#r "nuget: DotnetCQRS.Extensions.FluentAssertions, 1.1.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.
// Install DotnetCQRS.Extensions.FluentAssertions as a Cake Addin
#addin nuget:?package=DotnetCQRS.Extensions.FluentAssertions&version=1.1.0

// Install DotnetCQRS.Extensions.FluentAssertions as a Cake Tool
#tool nuget:?package=DotnetCQRS.Extensions.FluentAssertions&version=1.1.0

.Net CQRS

A simple CQRS library for .net

Installing

You should install .Net CQRS with Nuget

Install-Package DotnetCQRS

or using the .Net core command line

dotnet add package DotnetCQRS

Usage

To define a Command or Query, create a class and inherit from either ICommand or IQuery. For Queries, you will need to define a return result.


public class CreateUserCommand : ICommand
{
  public string Name { get; set; }
}

public class FetchUsersQuery : IQuery<FetchUsersResult>
{

}

public class FetchUsersResult
{
  public IReadOnlyList<Users> Users { get; set; }

  public class Users
  {
    public int Id { get; set; }
    public string Name { get; set; }
  }
}

Then define the handlers:

public class CreateUserHandler : ICommandHandler<CreateUserCommand>
{
  public async Task<Result> HandleAsync(CreateUserCommand command, CancellationToken cancellationToken)
  {
    // Code here for validating and creating a user
    
    return Result.Success();
  }
}

public class FetchUsersHandler : IQueryHandler<FetchUsersQuery, FetchUsersResult>
{
  public async Task<Result> HandleAsync(CreateUserCommand FetchUsersQuery, CancellationToken cancellationToken)
  {
    var usersList = // Some code to pull users from some kind of data store
    return Result.Success(usersList);
  }
}

You will then need to register the Query and Command Handlers with some kind of dependency injection so that you can call the dispatchers. The project includes some DI setup for both Microsoft.Extensions.DependencyInjection and Autofac. (documentation coming soon).

Once registered you will need to instantiate the IQueryDispatcher or ICommandDispatcher to run the above commands and queries.

// For query
var dispatcher = serviceProvider.GetService<IQueryDispatcher>();
var result = dispatcher.Run(new FetchUsersQuery(), CancellationTokenSource.Token);

if (result.IsFailure)
{
  // Handle an instance where the result was a failure
}
var users = result.Value; // This is where the result of the query lives

// For command
var dispatcher = serviceProvider.GetService<IQueryDispatcher>();
var result = dispatcher.Run(new CreateUserCommand()
{
  Name = "Foobar",
}, CancellationTokenSource.Token);

if (result.IsSuccess)
{
  // Good times!
}
else
{
  // Handle an instance where the result was a failure
}

FAQ

What is CQRS?

CQRS (Command and Query Responsibility Segregation) is a pattern for structuring applications. For more information on this pattern check the Microsoft documentation

Unlike the above implementation, this one is used for doing one Command or Query Handler per file rather than being able to do many in a single file.

For more information check out the basics or the ApiExample

What About Dependency Injection?

Check the /src folder to find two Dependency Injection implementations for the Microsoft.Extensions.DependencyInjection and Autofac libraries. If you want other implementations let me know through the Issues tab in Github.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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.1.0 427 4/2/2022
1.0.0 384 3/29/2022

Initial Release