* Added Result class to communicate with router from handler

* Removed "ExecuteOnlyFirstFoundHanlder" in sake of testing new Result pattern based routing system
* Removed obsolete option property "DescendDescriptorIndex"
* Changed router logic
* Changed handlers pool logic
This commit is contained in:
2025-08-02 02:32:38 +04:00
parent b8e4398b50
commit 16d11990ec
26 changed files with 347 additions and 115 deletions
@@ -51,10 +51,10 @@ namespace Telegrator.Handlers.Building
/// <param name="container">The handler container (unused).</param>
/// <param name="cancellation">The cancellation token (unused).</param>
/// <returns>A completed task.</returns>
protected override Task ExecuteInternal(IHandlerContainer container, CancellationToken cancellation)
protected override Task<Result> ExecuteInternal(IHandlerContainer container, CancellationToken cancellation)
{
ResetEvent.Set();
return Task.CompletedTask;
return Task.FromResult(Result.Ok());
}
/// <summary>
@@ -31,7 +31,7 @@ namespace Telegrator.Handlers.Building
/// <param name="container">The handler container with execution context.</param>
/// <param name="cancellation">The cancellation token.</param>
/// <returns>A task representing the asynchronous execution.</returns>
public override Task Execute(IAbstractHandlerContainer<TUpdate> container, CancellationToken cancellation)
public override Task<Result> Execute(IAbstractHandlerContainer<TUpdate> container, CancellationToken cancellation)
=> HandlerAction.Invoke(container, cancellation);
}
}
@@ -11,7 +11,7 @@ namespace Telegrator.Handlers.Building
/// <param name="container">The handler container with execution context.</param>
/// <param name="cancellation">The cancellation token.</param>
/// <returns>A task representing the asynchronous execution.</returns>
public delegate Task AbstractHandlerAction<TUpdate>(IAbstractHandlerContainer<TUpdate> container, CancellationToken cancellation) where TUpdate : class;
public delegate Task<Result> AbstractHandlerAction<TUpdate>(IAbstractHandlerContainer<TUpdate> container, CancellationToken cancellation) where TUpdate : class;
/// <summary>
/// Builder class for creating regular handlers that can process updates.
@@ -74,10 +74,10 @@ namespace Telegrator.Handlers.Components
/// <param name="container">The handler container.</param>
/// <param name="cancellationToken">Cancellation token.</param>
/// <returns>A task representing the asynchronous operation.</returns>
protected override sealed async Task ExecuteInternal(IHandlerContainer container, CancellationToken cancellationToken)
protected override sealed async Task<Result> ExecuteInternal(IHandlerContainer container, CancellationToken cancellationToken)
{
Container = (IAbstractHandlerContainer<TUpdate>)container;
await Execute(Container, cancellationToken);
return await Execute(Container, cancellationToken);
}
/// <summary>
@@ -86,6 +86,6 @@ namespace Telegrator.Handlers.Components
/// <param name="container">The handler container.</param>
/// <param name="cancellation">Cancellation token.</param>
/// <returns>A task representing the asynchronous operation.</returns>
public abstract Task Execute(IAbstractHandlerContainer<TUpdate> container, CancellationToken cancellation);
public abstract Task<Result> Execute(IAbstractHandlerContainer<TUpdate> container, CancellationToken cancellation);
}
}
@@ -118,13 +118,13 @@ namespace Telegrator.Handlers.Components
/// <param name="container">The handler container.</param>
/// <param name="cancellation">The cancellation token.</param>
/// <exception cref="Exception">Thrown when no branch method is set.</exception>
public override async Task Execute(IAbstractHandlerContainer<TUpdate> container, CancellationToken cancellation)
public override async Task<Result> Execute(IAbstractHandlerContainer<TUpdate> container, CancellationToken cancellation)
{
if (branchMethodInfo is null)
throw new Exception();
Cancellation = cancellation;
await BranchExecuteWrapper(container, branchMethodInfo);
return await BranchExecuteWrapper(container, branchMethodInfo);
}
/// <summary>
@@ -132,21 +132,20 @@ namespace Telegrator.Handlers.Components
/// </summary>
/// <param name="container">The handler container.</param>
/// <param name="methodInfo">The method to execute.</param>
protected virtual async Task BranchExecuteWrapper(IAbstractHandlerContainer<TUpdate> container, MethodInfo methodInfo)
protected virtual async Task<Result> BranchExecuteWrapper(IAbstractHandlerContainer<TUpdate> container, MethodInfo methodInfo)
{
if (methodInfo.ReturnType == typeof(void))
{
methodInfo.Invoke(this, []);
return;
return Result.Ok();
}
else
{
object branchReturn = methodInfo.Invoke(this, []);
if (branchReturn == null)
return;
if (branchReturn is Task branchTask)
await branchTask;
if (branchReturn is not Task<Result> branchTask)
throw new InvalidOperationException();
return await branchTask;
}
}
@@ -24,10 +24,16 @@ namespace Telegrator.Handlers.Components
/// <param name="container">The <see cref="IHandlerContainer"/> for the update.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
public async Task Execute(IHandlerContainer container, CancellationToken cancellationToken = default)
public async Task<Result> Execute(IHandlerContainer container, CancellationToken cancellationToken = default)
{
await ExecuteInternal(container, cancellationToken);
LifetimeToken.LifetimeEnded();
try
{
return await ExecuteInternal(container, cancellationToken);
}
finally
{
LifetimeToken.LifetimeEnded();
}
}
/// <summary>
@@ -36,6 +42,6 @@ namespace Telegrator.Handlers.Components
/// <param name="container">The <see cref="IHandlerContainer"/> for the update.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
protected abstract Task ExecuteInternal(IHandlerContainer container, CancellationToken cancellationToken);
protected abstract Task<Result> ExecuteInternal(IHandlerContainer container, CancellationToken cancellationToken);
}
}
+30
View File
@@ -0,0 +1,30 @@
namespace Telegrator.Handlers
{
public sealed class Result
{
public bool Positive { get; }
public bool RouteNext { get; }
public Type? NextType { get; }
internal Result(bool positive, bool routeNext, Type? nextType)
{
Positive = positive;
RouteNext = routeNext;
NextType = nextType;
}
public static Result Ok()
=> new Result(true, false, null);
public static Result Fault()
=> new Result(false, false, null);
public static Result Next()
=> new Result(true, true, null);
public static Result Next<T>()
=> new Result(true, true, typeof(T));
}
}