AnyAct 0.0.9

There is a newer version of this package available.
See the version list below for details.
dotnet add package AnyAct --version 0.0.9                
NuGet\Install-Package AnyAct -Version 0.0.9                
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="AnyAct" Version="0.0.9" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add AnyAct --version 0.0.9                
#r "nuget: AnyAct, 0.0.9"                
#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 AnyAct as a Cake Addin
#addin nuget:?package=AnyAct&version=0.0.9

// Install AnyAct as a Cake Tool
#tool nuget:?package=AnyAct&version=0.0.9                

AnyAct

AnyAct is a flexible, lightweight library for .NET designed to simplify the process of handling different types of actions in a dynamic way at runtime. Modeled after the Mediator pattern, it allows your application to dispatch requests to the correct handler without the need to know the request type at compile time.

Key Features

  • Runtime Type Resolution: No need to know the type at compile time. AnyAct allows you to determine the type of request at runtime, providing flexibility in managing your application's workflow.
  • Flexible Handler Dispatch: Your request handlers are dynamically dispatched based on the runtime type. Handle a variety of scenarios with ease.
  • Easy Integration: AnyAct works smoothly with your existing dependency injection setup, ensuring you can integrate it into your projects without hassle.
  • Extensible Design: Built with extensibility in mind, AnyAct allows you to easily extend its functionality to meet your specific application needs.

Differences with MediatR

While MediatR is a great library for implementing the Mediator pattern, it requires you to know the type of requests at compile time. AnyAct offers a more flexible approach by allowing you to determine the type of request at runtime. This flexibility makes AnyAct a powerful tool for handling a variety of scenarios where the request type cannot be determined at compile time. For example:

public async Task<ExecutorResult> ExecuteAction(MyAction action, CancellationToken ct)
{
    // Some preprocessing...
    
    var genericActionType = typeof(MyAction<>).MakeGenericType(modelType);
    var genericAction = Activator.CreateInstance(genericActionType, action)!;

    return await _actionExecutor.Execute<ExecutorResult>(genericAction, ct);
}

Getting Started

To start using AnyAct, you need to install the package and register it in your application's startup configuration.

Installation

You can install AnyAct via NuGet package manager. Run the following command:

dotnet add package AnyAct

Registering AnyAct

In your application's startup configuration, you need to add AnyAct services. You can do this in your Startup.cs or wherever you configure your services:

public void ConfigureServices(IServiceCollection services)
{
    services.AddAnyAct<Startup>();
}

The generic type parameter <TAssemblyMarker> should be a type that is part of the assembly where your action handlers are located.

Implementing Handlers

Each handler should implement the IActionHandler<TValue, TResult> interface. Here's an example:

public class MyActionHandler : IActionHandler<MyAction, MyResult>
{
    public async Task<MyResult> Handle(MyAction action, CancellationToken ct)
    {
        // Handle the action and return the result...
    }
}

Executing Actions

To execute an action, inject IActionExecutor into your class and call the Execute<TResult> method:

public class MyClass
{
    private readonly IActionExecutor _actionExecutor;

    public MyClass(IActionExecutor actionExecutor)
    {
        _actionExecutor = actionExecutor;
    }

    public async Task DoSomething()
    {
        var action = new MyAction();
        var result = await _actionExecutor.Execute<MyResult>(action);
        
        // Use the result...
    }
}
Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  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. 
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
0.0.10 192 8/3/2023
0.0.9 182 7/29/2023
0.0.8 189 7/21/2023
0.0.7 179 7/21/2023
0.0.6 184 7/21/2023
0.0.5 180 7/21/2023
0.0.4 188 7/21/2023
0.0.3 185 7/21/2023
0.0.2 183 7/21/2023
0.0.1 187 7/20/2023
0.0.0 189 7/20/2023