Skip to content

Commit

Permalink
add generic RequestHandlerToggleAttribute & ctor overloads
Browse files Browse the repository at this point in the history
  • Loading branch information
JonasSamuelsson committed Nov 16, 2023
1 parent 8024d9b commit 67d02f8
Showing 1 changed file with 37 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Linq;
using System.Threading.Tasks;

namespace Handyman.Mediator.Pipeline.RequestHandlerToggle
Expand All @@ -11,22 +10,37 @@ public class RequestHandlerToggleAttribute : RequestPipelineBuilderAttribute
private readonly Type[] _toggleEnabledHandlerTypes;

public RequestHandlerToggleAttribute(Type toggleEnabledHandlerType)
: this(new[] { toggleEnabledHandlerType ?? throw new ArgumentNullException(nameof(toggleEnabledHandlerType)) })
: this(new[] { toggleEnabledHandlerType })
{
}

public RequestHandlerToggleAttribute(Type[] toggleEnabledHandlerTypes)
public RequestHandlerToggleAttribute(Type toggleEnabledHandlerType, Type toggleDisabledHandlerType)
: this(new[] { toggleEnabledHandlerType }, new[] { toggleDisabledHandlerType })
{
if (toggleEnabledHandlerTypes == null)
throw new ArgumentNullException(nameof(toggleEnabledHandlerTypes));
}

if (!toggleEnabledHandlerTypes.Any())
throw new ArgumentException();
public RequestHandlerToggleAttribute(Type toggleEnabledHandlerType, Type[] toggleDisabledHandlerTypes)
: this(new[] { toggleEnabledHandlerType }, toggleDisabledHandlerTypes)
{
}

public RequestHandlerToggleAttribute(Type[] toggleEnabledHandlerTypes)
{
_metadata = new Lazy<RequestHandlerToggleMetadata>(CreateMetadata);
_toggleEnabledHandlerTypes = toggleEnabledHandlerTypes;
}

public RequestHandlerToggleAttribute(Type[] toggleEnabledHandlerTypes, Type toggleDisabledHandlerType)
: this(toggleEnabledHandlerTypes, new[] { toggleDisabledHandlerType })
{
}

public RequestHandlerToggleAttribute(Type[] toggleEnabledHandlerTypes, Type[] toggleDisabledHandlerTypes)
: this(toggleEnabledHandlerTypes)
{
ToggleDisabledHandlerTypes = toggleDisabledHandlerTypes;
}

public string? Name { get; set; }
public string[]? Tags { get; set; }
public Type[]? ToggleDisabledHandlerTypes { get; set; }
Expand All @@ -53,4 +67,20 @@ private RequestHandlerToggleMetadata CreateMetadata()
};
}
}

[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
public class RequestHandlerToggleAttribute<TToggleEnabledHandler> : RequestHandlerToggleAttribute
{
public RequestHandlerToggleAttribute() : base(typeof(TToggleEnabledHandler))
{
}
}

[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
public class RequestHandlerToggleAttribute<TToggleEnabledHandler, TToggleDisabledHandler> : RequestHandlerToggleAttribute
{
public RequestHandlerToggleAttribute() : base(typeof(TToggleEnabledHandler), typeof(TToggleDisabledHandler))
{
}
}
}

0 comments on commit 67d02f8

Please sign in to comment.