DescriptorAspectsSet optimizations

This commit is contained in:
2025-08-19 06:38:56 +04:00
parent 2d97fe5339
commit 6905045cde
5 changed files with 11 additions and 48 deletions
@@ -15,7 +15,7 @@
<EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<PackageReadmeFile>README.md</PackageReadmeFile>
<Version>1.15.3</Version>
<Version>1.15.4</Version>
</PropertyGroup>
<ItemGroup>
+1 -1
View File
@@ -16,7 +16,7 @@
<EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<PackageReadmeFile>README.md</PackageReadmeFile>
<Version>1.15.3</Version>
<Version>1.15.4</Version>
</PropertyGroup>
<ItemGroup>
@@ -10,16 +10,6 @@ namespace Telegrator.MadiatorCore.Descriptors
/// </summary>
public sealed class DescriptorAspectsSet
{
/// <summary>
/// Gets a value indicating whether the handler implements <see cref="IPreProcessor"/>.
/// </summary>
public bool SelfPre { get; private set; }
/// <summary>
/// Gets a value indicating whether the handler implements <see cref="IPostProcessor"/>.
/// </summary>
public bool SelfPost { get; private set; }
/// <summary>
/// Gets the type of the external pre-processor, if specified via <see cref="BeforeExecutionAttribute{T}"/>.
/// </summary>
@@ -33,14 +23,10 @@ namespace Telegrator.MadiatorCore.Descriptors
/// <summary>
/// Initializes a new instance of the <see cref="DescriptorAspectsSet"/> class.
/// </summary>
/// <param name="selfPre">Whether the handler implements <see cref="IPreProcessor"/>.</param>
/// <param name="typedPre">The type of external pre-processor, if any.</param>
/// <param name="selfPost">Whether the handler implements <see cref="IPostProcessor"/>.</param>
/// <param name="typedPost">The type of external post-processor, if any.</param>
public DescriptorAspectsSet(bool selfPre, Type? typedPre, bool selfPost, Type? typedPost)
public DescriptorAspectsSet(Type? typedPre, Type? typedPost)
{
SelfPre = selfPre;
SelfPost = selfPost;
TypedPre = typedPre;
TypedPost = typedPost;
}
@@ -55,16 +41,13 @@ namespace Telegrator.MadiatorCore.Descriptors
/// <exception cref="InvalidOperationException">Thrown when handler claims to implement <see cref="IPreProcessor"/> but doesn't.</exception>
public async Task<Result> ExecutePre(UpdateHandlerBase handler, IHandlerContainer container, CancellationToken cancellationToken)
{
if (SelfPre)
if (handler is IPreProcessor preProcessor)
{
if (handler is not IPreProcessor preProcessor)
throw new InvalidOperationException();
return await preProcessor.BeforeExecution(container, cancellationToken).ConfigureAwait(false);
}
else if (TypedPre != null)
{
IPreProcessor preProcessor = (IPreProcessor)Activator.CreateInstance(TypedPre);
preProcessor = (IPreProcessor)Activator.CreateInstance(TypedPre);
return await preProcessor.BeforeExecution(container, cancellationToken).ConfigureAwait(false);
}
@@ -81,16 +64,13 @@ namespace Telegrator.MadiatorCore.Descriptors
/// <exception cref="InvalidOperationException">Thrown when handler claims to implement <see cref="IPostProcessor"/> but doesn't.</exception>
public async Task<Result> ExecutePost(UpdateHandlerBase handler, IHandlerContainer container, CancellationToken cancellationToken)
{
if (SelfPost)
if (handler is IPostProcessor postProcessor)
{
if (handler is not IPostProcessor postProcessor)
throw new InvalidOperationException();
return await postProcessor.AfterExecution(container, cancellationToken).ConfigureAwait(false);
}
else if (TypedPost != null)
{
IPostProcessor postProcessor = (IPostProcessor)Activator.CreateInstance(TypedPost);
postProcessor = (IPostProcessor)Activator.CreateInstance(TypedPost);
return await postProcessor.AfterExecution(container, cancellationToken).ConfigureAwait(false);
}
@@ -90,26 +90,9 @@ namespace Telegrator.MadiatorCore.Descriptors
/// <returns>A <see cref="DescriptorAspectsSet"/> containing the aspects configuration.</returns>
public static DescriptorAspectsSet GetAspects(Type handlerType)
{
bool selfPre = handlerType.GetInterface(nameof(IPreProcessor)) != null;
bool selfPost = handlerType.GetInterface(nameof(IPostProcessor)) != null;
Type? typedPre = null;
Type? typedPost = null;
if (!selfPre)
{
Attribute? preAttr = handlerType.GetCustomAttribute(typeof(BeforeExecutionAttribute<>));
if (preAttr != null)
typedPre = preAttr.GetType().GetGenericArguments()[0];
}
if (!selfPost)
{
Attribute? postAttr = handlerType.GetCustomAttribute(typeof(AfterExecutionAttribute<>));
if (postAttr != null)
typedPost = postAttr.GetType().GetGenericArguments()[0];
}
return new DescriptorAspectsSet(selfPre, typedPre, selfPost, typedPost);
Type? typedPre = handlerType.GetCustomAttribute(typeof(BeforeExecutionAttribute<>))?.GetType().GetGenericArguments()[0];
Type? typedPost = handlerType.GetCustomAttribute(typeof(AfterExecutionAttribute<>)).GetType().GetGenericArguments()[0];
return new DescriptorAspectsSet(typedPre, typedPost);
}
}
}
+1 -1
View File
@@ -17,7 +17,7 @@
<PackageTags>telegram;bot;mediator;attributes;aspect;hosting;host;framework;easy;simple;handlers</PackageTags>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<Version>1.15.3</Version>
<Version>1.15.4</Version>
</PropertyGroup>
<ItemGroup>