Skip to content

Commit

Permalink
Also allow DefaultHandler(false) to skip a handler when multiple hand…
Browse files Browse the repository at this point in the history
…lers for a type is found
  • Loading branch information
volkanceylan committed Jan 3, 2025
1 parent e250695 commit bf3ea8c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Serenity.Services;
namespace Serenity.Services;

/// <summary>
/// Default implementation for the <see cref="IDefaultHandlerFactory"/>
Expand Down Expand Up @@ -36,6 +36,13 @@ private Type GetHandlerType((Type rowType, Type handlerInterface) args)
if (defaults.Count() == 1)
return defaults.First();

if (!defaults.Any())
{
defaults = handlers.Where(x => x.GetAttribute<DefaultHandlerAttribute>()?.Value != false);
if (defaults.Count() == 1)
return defaults.First();
}

throw new InvalidProgramException($"There are multiple {args.handlerInterface.FullName} types " +
$"for row type {args.rowType.FullName}. Please add [DefaultHandler] to one of them.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,17 @@ public static IServiceCollection AddCustomRequestHandlers(this IServiceCollectio
if (defaults.Length != 1)
{
if (defaults.Length == 0)
throw new InvalidProgramException($"There are multiple {pair.Key} request handler types ({string.Join(", ", pair.Value)}). " +
"Please add [DefaultHandler] to one of them!");
defaults = pair.Value.Where(x => x.GetAttribute<DefaultHandlerAttribute>()?.Value != false).ToArray();

throw new InvalidProgramException($"There are multiple {pair.Key} request handler types with [DefaultHandler] attribute ({string.Join(", ", (IEnumerable<Type>)defaults)}). " +
"Please use [DefaultHandler] on only one of them!");
if (defaults.Length != 1)
{
if (defaults.Length == 0)
throw new InvalidProgramException($"There are multiple {pair.Key} request handler types ({string.Join(", ", pair.Value)}). " +
"Please add [DefaultHandler] to one of them!");

throw new InvalidProgramException($"There are multiple {pair.Key} request handler types with [DefaultHandler] attribute ({string.Join(", ", (IEnumerable<Type>)defaults)}). " +
"Please use [DefaultHandler] on only one of them!");
}
}

collection.AddTransient(pair.Key, defaults[0]);
Expand Down

0 comments on commit bf3ea8c

Please sign in to comment.