Skip to content

[Bug]: Missing Closed Generic Query Behavior Registration #221

@eneshoxha

Description

@eneshoxha

Title: Add AddQueryPipelineBehavior<T>() for closed generic query behaviors

Labels: bug, mediator

Body:

Problem

MediatorOptions provides closed-generic registration for commands and notifications:

  • AddCommandPipelineBehavior<TBehavior>() -- closed generic
  • AddNotificationPipelineBehavior<TBehavior>() -- closed generic

But queries only have:

  • AddOpenQueryPipelineBehavior(Type) -- open generic only

There is no AddQueryPipelineBehavior<TBehavior>() for registering a closed generic query behavior (e.g., a behavior that only applies to GetUserQuery).

Proposed Solution

Add the missing method to MediatorOptions:

public MediatorOptions AddQueryPipelineBehavior<TBehavior>()
    where TBehavior : class
{
    var behaviorType = typeof(TBehavior);

    if (behaviorType.IsGenericTypeDefinition)
        throw new ArgumentException("Open generic types must be registered using AddOpenQueryPipelineBehavior");

    var implementsQueryBehavior =
        behaviorType.GetInterfaces().Any(i => i.IsGenericType &&
                                              i.GetGenericTypeDefinition() == typeof(IQueryPipelineBehavior<,>));

    if (!implementsQueryBehavior)
        throw new ArgumentException("Type must implement IQueryPipelineBehavior<,>");

    QueryBehaviors.Add(behaviorType);
    return this;
}

Also update RegisterPipelineBehaviors in ServiceCollectionExtensions.cs to handle closed query behaviors the same way it handles closed notification behaviors (checking IsGenericTypeDefinition and registering against specific interfaces).

Metadata

Metadata

Assignees

Labels

enhancementNew feature or requestfeatureThis label is in use for minor version increments

Type

No fields configured for Bug.

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions