Operations
Structr.Operations - is simple yet fully functional in-process messaging library, that allows you to easy implement CQS/CQRS.
Installation
Operations package is available on NuGet.
Setup
Basic setup is pretty simple and requires only adding registration of IOperationExecutor
service to your DI-container:
AddOperations()
extension method performs registration of executor service and operation handlers implementing IOperationHandler
or inherited from OperationHandler
and AsyncOperationHandler
classes.
assembliesToScan
params Assembly[]
List of assemblies to search operation handlers.
configureOptions
Action<OperationServiceOptions>
Options to be used by operations handling service.
Additionally configure IOperationExecutor
service by specifying it's type and lifetime used OperationServiceOptions
.
ExecutorServiceType
Type
Changes standard implementation of IOperationExecutor
to specified one, default is typeof(OperationExecutor)
.
ExecutorServiceLifetime
ServiceLifetime
Specifies the lifetime of an IOperationExecutor
service, default is Transient
.
Usage
The basic usage is:
The last step is to inject IOperationExecutor
service and use it:
That's it! Simple use is really simple but there are more cool stuff which can be done with Structr.Operations
such as: operations filtering and handlers decoration.
Operations
Structr.Operation uses term "operation" and corresponding interface IOperation
to describe an object containing data needed to handle operation.
Depending on if your operation is intended to return some result or not, it should implement one of the following interfaces: IOperation<out TResult>
or IOperation
. For example processing of such operation (query) should return int
value:
And no value for such operation (command):
Operation handlers
Operations performing happens in OperationHandlers - separate classes that are inherited from one of the following base classes:
AsyncOperationHandler<TOperation, TResult>
- async and value-returning handler;AsyncOperationHandler<TOperation>
- async and nothing-returning handler;OperationHandler<TOperation, TResult>
- synchronic and value-returning handler;OperationHandler<TOperation>
- synchronic and nothing-returning handler;
The example for simple command and it's handler is:
Thou you could implement IOperationHandler
interface directly but it will create more handwork for you when writing additional return VoidResult.Value
statement to imitate no returning result in corresponding situations. So better to inherit one of four classes described earlier.
Last updated