jbogard / MediatR

Simple, unambitious mediator implementation in .NET
Apache License 2.0
10.84k stars 1.15k forks source link

Implement IRequest, how to enter Behavior #1018

Closed snowchenlei closed 3 months ago

snowchenlei commented 3 months ago

Implement IRequest, how to enter Behavior

services.AddTransient(typeof(IPipelineBehavior<,>), typeof(ValidationBehavior<,>));

public record CreateRequest(string Name) : IRequest { }

public class CreateRequestHandler : IRequestHandler<CreateRequest> {}

The above code does not execute ValidationBehavior.Handle

snowchenlei commented 3 months ago

I know.The problem lies in the definition of ValidationBehavior.The problem is with the generic constraints of the ValidationBehavior parameter. A common definition should be as follows:

public class ValidationBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse> where TRequest : notnull