Skip to content

Commit

Permalink
Merge pull request #1065 from Emopusta/feature/open-behavior-multiple…
Browse files Browse the repository at this point in the history
…-addition-extension

Open behavior multiple registration extensions
  • Loading branch information
jbogard authored Sep 11, 2024
2 parents fb30902 + b106f2d commit 585f0dc
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/MediatR/Entities/OpenBehavior.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using Microsoft.Extensions.DependencyInjection;

namespace MediatR.Entities;
/// <summary>
/// Creates open behavior entity.
/// </summary>
public class OpenBehavior
{
public OpenBehavior(Type openBehaviorType, ServiceLifetime serviceLifetime = ServiceLifetime.Transient)
{
OpenBehaviorType = openBehaviorType;
ServiceLifetime = serviceLifetime;
}

public Type? OpenBehaviorType { get; }
public ServiceLifetime ServiceLifetime { get; }


}
32 changes: 32 additions & 0 deletions src/MediatR/MicrosoftExtensionsDI/MediatrServiceConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Reflection;
using MediatR;
using MediatR.Entities;
using MediatR.NotificationPublishers;
using MediatR.Pipeline;
using MediatR.Registration;
Expand Down Expand Up @@ -222,6 +223,37 @@ public MediatRServiceConfiguration AddOpenBehavior(Type openBehaviorType, Servic
return this;
}

/// <summary>
/// Registers multiple open behavior types against the <see cref="IPipelineBehavior{TRequest,TResponse}"/> open generic interface type
/// </summary>
/// <param name="openBehaviorTypes">An open generic behavior type list includes multiple open generic behavior types.</param>
/// <param name="serviceLifetime">Optional service lifetime, defaults to <see cref="ServiceLifetime.Transient"/>.</param>
/// <returns>This</returns>
public MediatRServiceConfiguration AddOpenBehaviors(IEnumerable<Type> openBehaviorTypes, ServiceLifetime serviceLifetime = ServiceLifetime.Transient)
{
foreach (var openBehaviorType in openBehaviorTypes)
{
AddOpenBehavior(openBehaviorType, serviceLifetime);
}

return this;
}

/// <summary>
/// Registers open behaviors against the <see cref="IPipelineBehavior{TRequest,TResponse}"/> open generic interface type
/// </summary>
/// <param name="openBehaviors">An open generic behavior list includes multiple <see cref="OpenBehavior"/> open generic behaviors.</param>
/// <returns>This</returns>
public MediatRServiceConfiguration AddOpenBehaviors(IEnumerable<OpenBehavior> openBehaviors)
{
foreach (var openBehavior in openBehaviors)
{
AddOpenBehavior(openBehavior.OpenBehaviorType!, openBehavior.ServiceLifetime);
}

return this;
}

/// <summary>
/// Register a closed stream behavior type
/// </summary>
Expand Down

0 comments on commit 585f0dc

Please sign in to comment.