From e035b3f986df1fbd38bad7d7e570d5421eccf66e Mon Sep 17 00:00:00 2001 From: Emopusta Date: Wed, 2 Oct 2024 00:40:16 +0300 Subject: [PATCH] Add open behavior type must implement IPipelineBehavior interface rule and some refactor with summaries. --- src/MediatR/Entities/OpenBehavior.cs | 41 +++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/src/MediatR/Entities/OpenBehavior.cs b/src/MediatR/Entities/OpenBehavior.cs index e7201da3..71088415 100644 --- a/src/MediatR/Entities/OpenBehavior.cs +++ b/src/MediatR/Entities/OpenBehavior.cs @@ -1,20 +1,53 @@ -using System; using Microsoft.Extensions.DependencyInjection; +using System; +using System.Linq; namespace MediatR.Entities; /// -/// Creates open behavior entity. +/// Represents a registration entity for pipeline behaviors with a specified service lifetime. /// public class OpenBehavior { + /// + /// Initializes a new instance of the class. + /// + /// The type of the pipeline behavior to register. + /// The lifetime of the registered service. Defaults to Transient. + /// Thrown if the specified type does not implement IPipelineBehavior. + /// Thrown if is null. public OpenBehavior(Type openBehaviorType, ServiceLifetime serviceLifetime = ServiceLifetime.Transient) { + ValidatePipelineBehaviorType(openBehaviorType); OpenBehaviorType = openBehaviorType; ServiceLifetime = serviceLifetime; } - public Type? OpenBehaviorType { get; } + /// + /// The type of the open behavior. + /// + public Type OpenBehaviorType { get; } + + /// + /// The service lifetime of the open behavior. + /// public ServiceLifetime ServiceLifetime { get; } - + /// + /// Validates whether the specified type implements the interface. + /// + /// The type to validate. + /// Thrown if the type does not implement . + /// Thrown if is null. + private static void ValidatePipelineBehaviorType(Type openBehaviorType) + { + if (openBehaviorType == null) throw new ArgumentNullException(nameof(openBehaviorType)); + + bool isPipelineBehavior = openBehaviorType.GetInterfaces() + .Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IPipelineBehavior<,>)); + + if (!isPipelineBehavior) + { + throw new InvalidOperationException($"The type \"{openBehaviorType.Name}\" must implement IPipelineBehavior<,> interface."); + } + } } \ No newline at end of file