Instructor.Core 1.0.0

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

.NET Coverage Status Nuget download

icon Instructor

Overview

Instructor is a lightweight, minimalist library focused on one job: dispatching instructions (commands and queries) to their respective handlers.

It integrates seamlessly with your existing IoC container—like Autofac or Microsoft.Extensions.DependencyInjection—without enforcing conventions or adding unnecessary abstractions. All handler resolution is delegated to the container you already use.

Example Usage

A working example can be found in the Instructor GitHub Repository.

Define Commands and Queries (aka Instructions)

Just implement IInstruction<TValue> for each instruction.

public class AddCustomerCommand: IInstruction<None>
{
    public Guid   CustomerID   { get;}
    public string CustomerName { get; } = default!;

    public AddCustomerCommand(CustomerData customerData)

        => (CustomerID, CustomerName) = (customerData.CustomerID, customerData.CustomerName);
}

public class GetCustomerQuery(Guid customerID) : IInstruction<CustomerData>
{
    public Guid CustomerID { get; } = customerID;
}

Define Handlers

Implement IInstructionHandler<TInstruction, TValue> or optionally the marker interfaces ICommandHandler<TInstruction, TValue> or IQueryHandler<TInstruction, TValue>.


public class AddCustomerCommandHandler : ICommandHandler<AddCustomerCommand, None>
{
    public AddCustomerCommandHandler(){ }//TODO: inject database dependencies here

    public async Task<None> Handle(AddCustomerCommand instruction, CancellationToken cancellationToken)
    {
        await Console.Out.WriteLineAsync($"Added customer {instruction.CustomerName} with ID {instruction.CustomerID} to the database (not).");
        return None.Value;
    }
}

public class GetCustomerQueryHandler : IQueryHandler<GetCustomerQuery, CustomerData>
{
    public GetCustomerQueryHandler() { } //TODO: inject dependencies here as usual
    public async Task<CustomerData> Handle(GetCustomerQuery instruction, CancellationToken cancellationToken)
    {
        await Console.Out.WriteLineAsync($"Getting the customer with ID {instruction.CustomerID} from the database (not).");

        return new CustomerData(instruction.CustomerID, "John Doe");
    }
}

Register the InstructionDispatcher and its factory delegate that defers resolution of instruction handlers via the underlying IoC container

Autofcac example:

   /*
       * Scan assemblies or add handlers manually i.e. builder.RegisterType<AddCustomerCommandHandler>().As<IInstructionHandler<AddCustomerCommand, None>>()
   */
   builder.RegisterAssemblyTypes(AppDomain.CurrentDomain.GetAssemblies()).AsClosedTypesOf(typeof(IInstructionHandler<,>));
   builder.Register<InstructionDispatcher>(c =>
   {
       var context = c.Resolve<IComponentContext>();
       return new InstructionDispatcher(type => context.Resolve(type));
   
   }).As<IInstructionDispatcher>().InstancePerLifetimeScope();

Microsoft.Extensions.DependencyInjection example:

   /*
       * Add handlers manually or get a package like Scrutor to scan assemblies
   */
   .Services.AddTransient<IInstructionHandler<AddCustomerCommand, None>, AddCustomerCommandHandler>()
           .AddTransient<IInstructionHandler<GetCustomerQuery, CustomerData>, GetCustomerQueryHandler>()
           .AddSingleton<IInstructionDispatcher>(provider => new InstructionDispatcher(type => provider.GetRequiredService(type)))

Send your instructions via the InstructionDispatcher to be processed by the handlers

   private readonly IInstructionDispatcher _instructionDispatcher;//set by constructor injection

   var newCustomer = new CustomerData(Guid.NewGuid(), "John Doe");//Data recieved from some client

   _ = await __instructionDispatcher.SendInstruction(new AddCustomerCommand(newCustomer));//just dispatch the command (instruction) for handling

   var customerData = await __instructionDispatcher.SendInstruction(new GetCustomerQuery(newCustomer.CustomerID));


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

    • No dependencies.
  • 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
1.0.0 182 4/8/2025