Operations
Last updated
Last updated
Structr.Operations - is simple yet fully functional in-process messaging library, that allows you to easy implement CQS/CQRS.
Operations package is available on NuGet.
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.
Parameter name | type | Description |
---|---|---|
Additionally configure IOperationExecutor
service by specifying it's type and lifetime used OperationServiceOptions
.
Property name | Property type | Description |
---|---|---|
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.
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):
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.
assembliesToScan
params Assembly[]
List of assemblies to search operation handlers.
configureOptions
Action<OperationServiceOptions>
Options to be used by operations handling service.
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
.