DescriptorAspectsSet optimizations
This commit is contained in:
@@ -15,7 +15,7 @@
|
|||||||
<EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild>
|
<EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild>
|
||||||
<PackageLicenseFile>LICENSE</PackageLicenseFile>
|
<PackageLicenseFile>LICENSE</PackageLicenseFile>
|
||||||
<PackageReadmeFile>README.md</PackageReadmeFile>
|
<PackageReadmeFile>README.md</PackageReadmeFile>
|
||||||
<Version>1.15.3</Version>
|
<Version>1.15.4</Version>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
@@ -16,7 +16,7 @@
|
|||||||
<EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild>
|
<EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild>
|
||||||
<PackageLicenseFile>LICENSE</PackageLicenseFile>
|
<PackageLicenseFile>LICENSE</PackageLicenseFile>
|
||||||
<PackageReadmeFile>README.md</PackageReadmeFile>
|
<PackageReadmeFile>README.md</PackageReadmeFile>
|
||||||
<Version>1.15.3</Version>
|
<Version>1.15.4</Version>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
@@ -10,16 +10,6 @@ namespace Telegrator.MadiatorCore.Descriptors
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public sealed class DescriptorAspectsSet
|
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>
|
/// <summary>
|
||||||
/// Gets the type of the external pre-processor, if specified via <see cref="BeforeExecutionAttribute{T}"/>.
|
/// Gets the type of the external pre-processor, if specified via <see cref="BeforeExecutionAttribute{T}"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -33,14 +23,10 @@ namespace Telegrator.MadiatorCore.Descriptors
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes a new instance of the <see cref="DescriptorAspectsSet"/> class.
|
/// Initializes a new instance of the <see cref="DescriptorAspectsSet"/> class.
|
||||||
/// </summary>
|
/// </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="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>
|
/// <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;
|
TypedPre = typedPre;
|
||||||
TypedPost = typedPost;
|
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>
|
/// <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)
|
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);
|
return await preProcessor.BeforeExecution(container, cancellationToken).ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
else if (TypedPre != null)
|
else if (TypedPre != null)
|
||||||
{
|
{
|
||||||
IPreProcessor preProcessor = (IPreProcessor)Activator.CreateInstance(TypedPre);
|
preProcessor = (IPreProcessor)Activator.CreateInstance(TypedPre);
|
||||||
return await preProcessor.BeforeExecution(container, cancellationToken).ConfigureAwait(false);
|
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>
|
/// <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)
|
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);
|
return await postProcessor.AfterExecution(container, cancellationToken).ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
else if (TypedPost != null)
|
else if (TypedPost != null)
|
||||||
{
|
{
|
||||||
IPostProcessor postProcessor = (IPostProcessor)Activator.CreateInstance(TypedPost);
|
postProcessor = (IPostProcessor)Activator.CreateInstance(TypedPost);
|
||||||
return await postProcessor.AfterExecution(container, cancellationToken).ConfigureAwait(false);
|
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>
|
/// <returns>A <see cref="DescriptorAspectsSet"/> containing the aspects configuration.</returns>
|
||||||
public static DescriptorAspectsSet GetAspects(Type handlerType)
|
public static DescriptorAspectsSet GetAspects(Type handlerType)
|
||||||
{
|
{
|
||||||
bool selfPre = handlerType.GetInterface(nameof(IPreProcessor)) != null;
|
Type? typedPre = handlerType.GetCustomAttribute(typeof(BeforeExecutionAttribute<>))?.GetType().GetGenericArguments()[0];
|
||||||
bool selfPost = handlerType.GetInterface(nameof(IPostProcessor)) != null;
|
Type? typedPost = handlerType.GetCustomAttribute(typeof(AfterExecutionAttribute<>)).GetType().GetGenericArguments()[0];
|
||||||
Type? typedPre = null;
|
return new DescriptorAspectsSet(typedPre, typedPost);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,7 +17,7 @@
|
|||||||
<PackageTags>telegram;bot;mediator;attributes;aspect;hosting;host;framework;easy;simple;handlers</PackageTags>
|
<PackageTags>telegram;bot;mediator;attributes;aspect;hosting;host;framework;easy;simple;handlers</PackageTags>
|
||||||
<PackageReadmeFile>README.md</PackageReadmeFile>
|
<PackageReadmeFile>README.md</PackageReadmeFile>
|
||||||
<PackageLicenseFile>LICENSE</PackageLicenseFile>
|
<PackageLicenseFile>LICENSE</PackageLicenseFile>
|
||||||
<Version>1.15.3</Version>
|
<Version>1.15.4</Version>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
Reference in New Issue
Block a user