Closed pillepalle1 closed 1 year ago
I suppose a more generalised version of this question is: Is it possible to swap out the return object in an implementation of IPipelineBehaviour
? Since the handler is registered as an open generic IPipelineBehaviour<,>
, there seems to not really a way to make assertion about TResult
other in the where clause in the class declaration, which cannot be used, because then we don't have the inner type, ie.
public class MyBehaviour<TRequest,TResult> : IPipelineBehaviour<TRequest,TResult>
where TRequest : IRequest<TResult>
where TResult : OneOf<???,Problem>
{
// Implementation goes here
}
The ??? denotes information that is missing. Is there a mechanism implemented to make this kind of assertion to the implementation?
If registered with concrete types, it works, but that is then pointless as there would be one handler registered for each Query
or Command
services.AddTransient(typeof(IPipelineBehaviour<DevCmd,Oneof<Object,ProblemDetails>>), typeof(ValidationBehaviour<OneOf<Object,ProblemDetails>>);
My project uses https://github.com/mcintyre321/OneOf (similar to
Result
objects) to avoid throwing exceptions. It works well withIRequest<OneOf<T, ProblemDetails>>
andIRequestHandler<TRequest,OneOf<T,ProblemDetails>>
, but now I am implementing validation viaIPipelineBehaviour<TRequest, TResult>
and want to returnProblemDetails
if validation fails andnext()
if the validation succeeds. The problem here is that, even thoughTResult
is guaranteed to beOneOf<T,ProblemDetails>
for someT
, the methodHandle(...)
has no way of knowing that and hence does not let me return objects the way I want.I tried declaring it as
public class ValidationBehaviour<TRequest,TResult> : IPipelineBehaviour<TRequest,OneOf<TResult,ProblemDetails>>
but in that case the handler is not invoked, even though registered asservices.AddTransient(typeof(IPipelineBehavior<,>), typeof(ValidationBehaviour<,>));
The same would go with some
Result<T>
. Is there a workaround for this issue or is there no way around throwingException
s ?