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.
IServiceCollection
Registering with Immediate.Handlers supports Microsoft.Extensions.DependencyInjection.Abstractions
directly. To register handlers with DI, simply add the following to your Program.cs
:
In your Program.cs
, add a call to services.AddXxxHandlers()
, where Xxx
is the shortened form of the project name.
- For a project named
Web
, it will beservices.AddWebHandlers()
- For a project named
Application.Web
, it will beservices.AddApplicationWebHandlers()
This registers all handlers in the assembly marked with [Handler]
, along with their IHandler<TRequest, TResponse>
interface.