Immediate.Handlers
Creating handlers
Handlers can be created by using the following code:
This will automatically generate a new class, GetUsersQuery.Handler, which encapsulates the following:
- attaching any behaviors which may be relevant for the query
- using a class to receive any DI services, such as
UsersService
Consuming code can now do the following:
For Command handlers, use a ValueTask, and Immediate.Handlers will insert a return type
of ValueTuple to your handler automatically.
CancellationTokens are also optional, and may be omitted if unnecessary.
In case your project layout does not allow direct for references between consumer and handler, the handler will implement the IHandler<TRequest, Response> interface.
Registering with IServiceCollection
In your Program.cs, add a call to services.AddXxxHandlers(), where Xxx is the application identifier.
By default, this is the short form of the assembly name. For example:
- For a project named
Web, it will beservices.AddWebHandlers() - For a project named
Application.Web, it will beservices.AddApplicationWebHandlers()
However, this name can be overridden using [assembly: ImmediateAssemblyIdentifierAttribute("SomeIdentifier")].
Calling this AddXxxHandlers() method will register all classes in the assembly marked with [Handler].