* Added protection from NotImplementedException to HandlerBuilderBase
* Moved AddHandler<T> and AddHandler(Type) methods from IHandlersCollection to extension methods * Added public constructor to IHost types form extensibility * Code cleanup
This commit is contained in:
@@ -55,10 +55,15 @@ namespace Telegrator.Hosting.Web
|
||||
IFeatureCollection IApplicationBuilder.ServerFeatures => ((IApplicationBuilder)_innerApp).ServerFeatures;
|
||||
IDictionary<string, object?> IApplicationBuilder.Properties => ((IApplicationBuilder)_innerApp).Properties;
|
||||
|
||||
internal TelegramBotWebHost(WebApplicationBuilder webApplicationBuilder, HostHandlersCollection handlers)
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="WebApplicationBuilder"/> class.
|
||||
/// </summary>
|
||||
/// <param name="webApplicationBuilder">The proxied instance of host builder.</param>
|
||||
/// <param name="handlers"></param>
|
||||
public TelegramBotWebHost(WebApplicationBuilder webApplicationBuilder, IHandlersCollection handlers)
|
||||
{
|
||||
// Registering this host in services for easy access
|
||||
RegisterHostServices(webApplicationBuilder, handlers);
|
||||
RegisterHostServices(webApplicationBuilder.Services, handlers);
|
||||
|
||||
// Building proxy application
|
||||
_innerApp = webApplicationBuilder.Build();
|
||||
@@ -172,7 +177,7 @@ namespace Telegrator.Hosting.Web
|
||||
_disposed = true;
|
||||
}
|
||||
|
||||
private void LogHandlers(HostHandlersCollection handlers)
|
||||
private void LogHandlers(IHandlersCollection handlers)
|
||||
{
|
||||
StringBuilder logBuilder = new StringBuilder("Registered handlers : ");
|
||||
if (!handlers.Keys.Any())
|
||||
@@ -194,15 +199,15 @@ namespace Telegrator.Hosting.Web
|
||||
Logger.LogInformation(logBuilder.ToString());
|
||||
}
|
||||
|
||||
private void RegisterHostServices(WebApplicationBuilder hostApplicationBuilder, HostHandlersCollection handlers)
|
||||
private void RegisterHostServices(IServiceCollection service, IHandlersCollection handlers)
|
||||
{
|
||||
//hostApplicationBuilder.Services.RemoveAll<IHost>();
|
||||
//hostApplicationBuilder.Services.AddSingleton<IHost>(this);
|
||||
//service.RemoveAll<IHost>();
|
||||
//service.AddSingleton<IHost>(this);
|
||||
|
||||
hostApplicationBuilder.Services.AddSingleton<ITelegramBotHost>(this);
|
||||
hostApplicationBuilder.Services.AddSingleton<ITelegramBotWebHost>(this);
|
||||
hostApplicationBuilder.Services.AddSingleton<ITelegratorBot>(this);
|
||||
hostApplicationBuilder.Services.AddSingleton<IHandlersCollection>(handlers);
|
||||
service.AddSingleton<ITelegramBotHost>(this);
|
||||
service.AddSingleton<ITelegramBotWebHost>(this);
|
||||
service.AddSingleton<ITelegratorBot>(this);
|
||||
service.AddSingleton<IHandlersCollection>(handlers);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,10 +5,10 @@ using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Telegram.Bot;
|
||||
using Telegram.Bot.Polling;
|
||||
using Telegrator.Hosting.Components;
|
||||
using Telegrator.Hosting.Configuration;
|
||||
using Telegrator.Hosting.Providers;
|
||||
using Telegrator.Hosting.Providers.Components;
|
||||
using Telegrator.MadiatorCore;
|
||||
|
||||
#pragma warning disable IDE0001
|
||||
@@ -21,7 +21,7 @@ namespace Telegrator.Hosting.Web
|
||||
{
|
||||
private readonly WebApplicationBuilder _innerBuilder;
|
||||
private readonly TelegramBotWebOptions _settings;
|
||||
private readonly HostHandlersCollection _handlers;
|
||||
private readonly IHandlersCollection _handlers;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public IHandlersCollection Handlers => _handlers;
|
||||
@@ -38,28 +38,49 @@ namespace Telegrator.Hosting.Web
|
||||
/// <inheritdoc/>
|
||||
public IHostEnvironment Environment => _innerBuilder.Environment;
|
||||
|
||||
internal TelegramBotWebHostBuilder(WebApplicationBuilder webApplicationBuilder, TelegramBotWebOptions settings)
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="TelegramBotWebHostBuilder"/> class.
|
||||
/// </summary>
|
||||
/// <param name="webApplicationBuilder"></param>
|
||||
/// <param name="settings"></param>
|
||||
public TelegramBotWebHostBuilder(WebApplicationBuilder webApplicationBuilder, TelegramBotWebOptions settings)
|
||||
{
|
||||
_innerBuilder = webApplicationBuilder;
|
||||
_innerBuilder = webApplicationBuilder ?? throw new ArgumentNullException(nameof(webApplicationBuilder));
|
||||
_settings = settings ?? throw new ArgumentNullException(nameof(settings));
|
||||
_handlers = new HostHandlersCollection(Services, _settings);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="TelegramBotWebHostBuilder"/> class.
|
||||
/// </summary>
|
||||
/// <param name="webApplicationBuilder"></param>
|
||||
/// <param name="handlers"></param>
|
||||
/// <param name="settings"></param>
|
||||
public TelegramBotWebHostBuilder(WebApplicationBuilder webApplicationBuilder, TelegramBotWebOptions settings, IHandlersCollection handlers)
|
||||
{
|
||||
_innerBuilder = webApplicationBuilder ?? throw new ArgumentNullException(nameof(webApplicationBuilder));
|
||||
_settings = settings ?? throw new ArgumentNullException(nameof(settings));
|
||||
_handlers = handlers ?? throw new ArgumentNullException(nameof(settings));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Builds the host.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public TelegramBotWebHost Build()
|
||||
{
|
||||
foreach (PreBuildingRoutine preBuildRoutine in _handlers.PreBuilderRoutines)
|
||||
if (_handlers is IHostHandlersCollection hostHandlers)
|
||||
{
|
||||
try
|
||||
foreach (PreBuildingRoutine preBuildRoutine in hostHandlers.PreBuilderRoutines)
|
||||
{
|
||||
preBuildRoutine.Invoke(this);
|
||||
}
|
||||
catch (NotImplementedException)
|
||||
{
|
||||
_ = 0xBAD + 0xC0DE;
|
||||
try
|
||||
{
|
||||
preBuildRoutine.Invoke(this);
|
||||
}
|
||||
catch (NotImplementedException)
|
||||
{
|
||||
_ = 0xBAD + 0xC0DE;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
using Telegrator.MadiatorCore;
|
||||
|
||||
namespace Telegrator.Hosting.Providers.Components
|
||||
{
|
||||
/// <summary>
|
||||
/// Collection class for managing handler descriptors organized by update type for host apps.
|
||||
/// Provides functionality for collecting, adding, scanning, and organizing handlers.
|
||||
/// </summary>
|
||||
public interface IHostHandlersCollection : IHandlersCollection
|
||||
{
|
||||
/// <summary>
|
||||
/// List of tasks that should be completed right before building the bot
|
||||
/// </summary>
|
||||
public List<PreBuildingRoutine> PreBuilderRoutines { get; }
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
using System.Reflection;
|
||||
using Telegrator.Configuration;
|
||||
using Telegrator.Hosting.Components;
|
||||
using Telegrator.Hosting.Providers.Components;
|
||||
using Telegrator.MadiatorCore;
|
||||
using Telegrator.MadiatorCore.Descriptors;
|
||||
using Telegrator.Providers;
|
||||
@@ -15,7 +16,7 @@ namespace Telegrator.Hosting.Providers
|
||||
public delegate void PreBuildingRoutine(ITelegramBotHostBuilder builder);
|
||||
|
||||
/// <inheritdoc/>
|
||||
public class HostHandlersCollection(IServiceCollection hostServiceColletion, ITelegratorOptions options) : HandlersCollection(options)
|
||||
public class HostHandlersCollection(IServiceCollection hostServiceColletion, ITelegratorOptions options) : HandlersCollection(options), IHostHandlersCollection
|
||||
{
|
||||
private readonly IServiceCollection Services = hostServiceColletion;
|
||||
|
||||
@@ -25,20 +26,14 @@ namespace Telegrator.Hosting.Providers
|
||||
/// <summary>
|
||||
/// List of tasks that should be completed right before building the bot
|
||||
/// </summary>
|
||||
public readonly List<PreBuildingRoutine> PreBuilderRoutines = [];
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override IHandlersCollection AddHandler(Type handlerType)
|
||||
{
|
||||
if (handlerType.IsPreBuildingRoutine(out MethodInfo? routineMethod))
|
||||
PreBuilderRoutines.Add(routineMethod.CreateDelegate<PreBuildingRoutine>(null));
|
||||
|
||||
return base.AddHandler(handlerType);
|
||||
}
|
||||
public List<PreBuildingRoutine> PreBuilderRoutines { get; } = [];
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override IHandlersCollection AddDescriptor(HandlerDescriptor descriptor)
|
||||
{
|
||||
if (descriptor.HandlerType.IsPreBuildingRoutine(out MethodInfo? routineMethod))
|
||||
PreBuilderRoutines.Add(routineMethod.CreateDelegate<PreBuildingRoutine>(null));
|
||||
|
||||
switch (descriptor.Type)
|
||||
{
|
||||
case DescriptorType.General:
|
||||
|
||||
@@ -5,9 +5,6 @@ using System.Text;
|
||||
using Telegram.Bot.Types.Enums;
|
||||
using Telegrator.Configuration;
|
||||
using Telegrator.Hosting.Components;
|
||||
using Telegrator.Hosting.Logging;
|
||||
using Telegrator.Logging;
|
||||
using Telegrator.Hosting.Providers;
|
||||
using Telegrator.MadiatorCore;
|
||||
using Telegrator.MadiatorCore.Descriptors;
|
||||
|
||||
@@ -19,13 +16,14 @@ namespace Telegrator.Hosting
|
||||
public class TelegramBotHost : ITelegramBotHost
|
||||
{
|
||||
private readonly IHost _innerHost;
|
||||
private readonly IServiceProvider _serviceProvider;
|
||||
private readonly IUpdateRouter _updateRouter;
|
||||
private readonly ILogger<TelegramBotHost> _logger;
|
||||
|
||||
private bool _disposed;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public IServiceProvider Services => _innerHost.Services;
|
||||
public IServiceProvider Services => _serviceProvider;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public IUpdateRouter UpdateRouter => _updateRouter;
|
||||
@@ -38,15 +36,16 @@ namespace Telegrator.Hosting
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="TelegramBotHost"/> class.
|
||||
/// </summary>
|
||||
/// <param name="hostApplicationBuilder">The service provider.</param>
|
||||
/// <param name="hostApplicationBuilder">The proxied instance of host builder.</param>
|
||||
/// <param name="handlers"></param>
|
||||
internal TelegramBotHost(HostApplicationBuilder hostApplicationBuilder, HostHandlersCollection handlers)
|
||||
public TelegramBotHost(HostApplicationBuilder hostApplicationBuilder, IHandlersCollection handlers)
|
||||
{
|
||||
// Registering this host in services for easy access
|
||||
RegisterHostServices(hostApplicationBuilder, handlers);
|
||||
RegisterHostServices(hostApplicationBuilder.Services, handlers);
|
||||
|
||||
// Building proxy hoster
|
||||
_innerHost = hostApplicationBuilder.Build();
|
||||
_serviceProvider = _innerHost.Services;
|
||||
|
||||
// Initializing bot info, as it requires to make a request via tg bot
|
||||
Services.GetRequiredService<ITelegramBotInfo>();
|
||||
@@ -131,7 +130,7 @@ namespace Telegrator.Hosting
|
||||
_disposed = true;
|
||||
}
|
||||
|
||||
private void LogHandlers(HostHandlersCollection handlers)
|
||||
private void LogHandlers(IHandlersCollection handlers)
|
||||
{
|
||||
StringBuilder logBuilder = new StringBuilder("Registered handlers : ");
|
||||
if (!handlers.Keys.Any())
|
||||
@@ -153,14 +152,14 @@ namespace Telegrator.Hosting
|
||||
Logger.LogInformation(logBuilder.ToString());
|
||||
}
|
||||
|
||||
private void RegisterHostServices(HostApplicationBuilder hostApplicationBuilder, HostHandlersCollection handlers)
|
||||
private void RegisterHostServices(IServiceCollection services, IHandlersCollection handlers)
|
||||
{
|
||||
//hostApplicationBuilder.Services.RemoveAll<IHost>();
|
||||
//hostApplicationBuilder.Services.AddSingleton<IHost>(this);
|
||||
//services.RemoveAll<IHost>();
|
||||
//services.AddSingleton<IHost>(this);
|
||||
|
||||
hostApplicationBuilder.Services.AddSingleton<ITelegramBotHost>(this);
|
||||
hostApplicationBuilder.Services.AddSingleton<ITelegratorBot>(this);
|
||||
hostApplicationBuilder.Services.AddSingleton<IHandlersCollection>(handlers);
|
||||
services.AddSingleton<ITelegramBotHost>(this);
|
||||
services.AddSingleton<ITelegratorBot>(this);
|
||||
services.AddSingleton<IHandlersCollection>(handlers);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ using Telegrator.Hosting;
|
||||
using Telegrator.Hosting.Components;
|
||||
using Telegrator.Hosting.Configuration;
|
||||
using Telegrator.Hosting.Providers;
|
||||
using Telegrator.Hosting.Providers.Components;
|
||||
using Telegrator.MadiatorCore;
|
||||
|
||||
#pragma warning disable IDE0001
|
||||
@@ -21,7 +22,7 @@ namespace Telegrator.Hosting
|
||||
{
|
||||
private readonly HostApplicationBuilder _innerBuilder;
|
||||
private readonly TelegramBotHostBuilderSettings _settings;
|
||||
private readonly HostHandlersCollection _handlers;
|
||||
private readonly IHandlersCollection _handlers;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public IHandlersCollection Handlers => _handlers;
|
||||
@@ -43,30 +44,48 @@ namespace Telegrator.Hosting
|
||||
/// </summary>
|
||||
/// <param name="hostApplicationBuilder"></param>
|
||||
/// <param name="settings"></param>
|
||||
internal TelegramBotHostBuilder(HostApplicationBuilder hostApplicationBuilder, TelegramBotHostBuilderSettings? settings = null)
|
||||
public TelegramBotHostBuilder(HostApplicationBuilder hostApplicationBuilder, TelegramBotHostBuilderSettings? settings = null)
|
||||
{
|
||||
_innerBuilder = hostApplicationBuilder;
|
||||
_innerBuilder = hostApplicationBuilder ?? throw new ArgumentNullException(nameof(hostApplicationBuilder));
|
||||
_settings = settings ?? new TelegramBotHostBuilderSettings();
|
||||
_handlers = new HostHandlersCollection(Services, _settings);
|
||||
|
||||
_innerBuilder.Logging.ClearProviders();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="TelegramBotHostBuilder"/> class.
|
||||
/// </summary>
|
||||
/// <param name="hostApplicationBuilder"></param>
|
||||
/// <param name="handlers"></param>
|
||||
/// <param name="settings"></param>
|
||||
public TelegramBotHostBuilder(HostApplicationBuilder hostApplicationBuilder, IHandlersCollection handlers, TelegramBotHostBuilderSettings? settings = null)
|
||||
{
|
||||
_innerBuilder = hostApplicationBuilder ?? throw new ArgumentNullException(nameof(hostApplicationBuilder));
|
||||
_settings = settings ?? new TelegramBotHostBuilderSettings();
|
||||
_handlers = handlers ?? throw new ArgumentNullException(nameof(handlers));
|
||||
|
||||
_innerBuilder.Logging.ClearProviders();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Builds the host.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public TelegramBotHost Build()
|
||||
{
|
||||
foreach (PreBuildingRoutine preBuildRoutine in _handlers.PreBuilderRoutines)
|
||||
if (_handlers is IHostHandlersCollection hostHandlers)
|
||||
{
|
||||
try
|
||||
foreach (PreBuildingRoutine preBuildRoutine in hostHandlers.PreBuilderRoutines)
|
||||
{
|
||||
preBuildRoutine.Invoke(this);
|
||||
}
|
||||
catch (NotImplementedException)
|
||||
{
|
||||
_ = 0xBAD + 0xC0DE;
|
||||
try
|
||||
{
|
||||
preBuildRoutine.Invoke(this);
|
||||
}
|
||||
catch (NotImplementedException)
|
||||
{
|
||||
_ = 0xBAD + 0xC0DE;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using Telegram.Bot;
|
||||
using System.ComponentModel;
|
||||
using Telegram.Bot;
|
||||
using Telegram.Bot.Polling;
|
||||
using Telegram.Bot.Types;
|
||||
using Telegram.Bot.Types.Enums;
|
||||
@@ -42,28 +43,49 @@ namespace Telegrator.Handlers.Components
|
||||
// Executing pre processor
|
||||
if (aspects != null)
|
||||
{
|
||||
Result? preResult = await aspects
|
||||
.ExecutePre(this, container, cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
try
|
||||
{
|
||||
Result? preResult = await aspects
|
||||
.ExecutePre(this, container, cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
if (!preResult.Positive)
|
||||
return preResult;
|
||||
if (!preResult.Positive)
|
||||
return preResult;
|
||||
}
|
||||
catch (NotImplementedException)
|
||||
{
|
||||
_ = 0xBAD + 0xC0DE;
|
||||
}
|
||||
}
|
||||
|
||||
// Executing handler
|
||||
Result execResult = await ExecuteInternal(container, cancellationToken).ConfigureAwait(false);
|
||||
if (!execResult.Positive)
|
||||
return execResult;
|
||||
|
||||
// Executing post processor
|
||||
if (aspects != null)
|
||||
try
|
||||
{
|
||||
Result postResult = await aspects
|
||||
.ExecutePost(this, container, cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
// Executing handler
|
||||
Result execResult = await ExecuteInternal(container, cancellationToken).ConfigureAwait(false);
|
||||
if (!execResult.Positive)
|
||||
return execResult;
|
||||
}
|
||||
catch (NotImplementedException)
|
||||
{
|
||||
_ = 0xBAD + 0xC0DE;
|
||||
}
|
||||
|
||||
if (!postResult.Positive)
|
||||
return postResult;
|
||||
try
|
||||
{
|
||||
// Executing post processor
|
||||
if (aspects != null)
|
||||
{
|
||||
Result postResult = await aspects
|
||||
.ExecutePost(this, container, cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
if (!postResult.Positive)
|
||||
return postResult;
|
||||
}
|
||||
}
|
||||
catch (NotImplementedException)
|
||||
{
|
||||
_ = 0xBAD + 0xC0DE;
|
||||
}
|
||||
|
||||
// Success
|
||||
@@ -77,9 +99,16 @@ namespace Telegrator.Handlers.Components
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
await described.UpdateRouter
|
||||
.HandleErrorAsync(described.Client, exception, HandleErrorSource.HandleUpdateError, cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
try
|
||||
{
|
||||
await described.UpdateRouter
|
||||
.HandleErrorAsync(described.Client, exception, HandleErrorSource.HandleUpdateError, cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
catch (NotImplementedException)
|
||||
{
|
||||
_ = 0xBAD + 0xC0DE;
|
||||
}
|
||||
|
||||
return Result.Fault();
|
||||
}
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
using Telegram.Bot.Types.Enums;
|
||||
using Telegrator.Handlers.Components;
|
||||
using Telegrator.MadiatorCore.Descriptors;
|
||||
|
||||
namespace Telegrator.MadiatorCore
|
||||
@@ -32,14 +31,6 @@ namespace Telegrator.MadiatorCore
|
||||
/// <returns>The handler descriptor list for the given update type.</returns>
|
||||
public HandlerDescriptorList this[UpdateType updateType] { get; }
|
||||
|
||||
/*
|
||||
/// <summary>
|
||||
/// Collects all handlers domain-wide and returns a new <see cref="IHandlersCollection"/>.
|
||||
/// </summary>
|
||||
/// <returns>A new <see cref="IHandlersCollection"/> with all handlers collected.</returns>
|
||||
public IHandlersCollection CollectHandlersDomainWide();
|
||||
*/
|
||||
|
||||
/// <summary>
|
||||
/// Adds a <see cref="HandlerDescriptor"/> to the collection and returns the updated collection.
|
||||
/// </summary>
|
||||
@@ -47,21 +38,6 @@ namespace Telegrator.MadiatorCore
|
||||
/// <returns>The updated <see cref="IHandlersCollection"/>.</returns>
|
||||
public IHandlersCollection AddDescriptor(HandlerDescriptor descriptor);
|
||||
|
||||
/// <summary>
|
||||
/// Adds a handler of the specified type to the collection and returns the updated collection.
|
||||
/// </summary>
|
||||
/// <typeparam name="THandler">The type of handler to add, must inherit from <see cref="UpdateHandlerBase"/>.</typeparam>
|
||||
/// <returns>The updated <see cref="IHandlersCollection"/>.</returns>
|
||||
public IHandlersCollection AddHandler<THandler>() where THandler : UpdateHandlerBase;
|
||||
|
||||
/// <summary>
|
||||
/// Adds a handler of the specified type to the collection and returns the updated collection.
|
||||
/// </summary>
|
||||
/// <param name="handlerType">The type of handler to add.</param>
|
||||
/// <returns>The updated <see cref="IHandlersCollection"/>.</returns>
|
||||
/// <exception cref="Exception">Thrown if the handler type is invalid.</exception>
|
||||
public IHandlersCollection AddHandler(Type handlerType);
|
||||
|
||||
/// <summary>
|
||||
/// Gets the <see cref="HandlerDescriptorList"/> for the specified <see cref="HandlerDescriptor"/>.
|
||||
/// </summary>
|
||||
|
||||
@@ -88,42 +88,6 @@ namespace Telegrator.Providers
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a handler type to the collection.
|
||||
/// </summary>
|
||||
/// <typeparam name="THandler">The type of handler to add.</typeparam>
|
||||
/// <returns>This collection instance for method chaining.</returns>
|
||||
public virtual IHandlersCollection AddHandler<THandler>() where THandler : UpdateHandlerBase
|
||||
{
|
||||
AddHandler(typeof(THandler));
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a handler type to the collection.
|
||||
/// </summary>
|
||||
/// <param name="handlerType">The type of handler to add.</param>
|
||||
/// <returns>This collection instance for method chaining.</returns>
|
||||
/// <exception cref="Exception">Thrown when the type is not a valid handler implementation.</exception>
|
||||
public virtual IHandlersCollection AddHandler(Type handlerType)
|
||||
{
|
||||
if (!handlerType.IsHandlerRealization())
|
||||
throw new Exception();
|
||||
|
||||
if (handlerType.IsCustomDescriptorsProvider())
|
||||
{
|
||||
foreach (HandlerDescriptor handlerDescriptor in InvokeCustomDescriptorsProvider(handlerType))
|
||||
AddDescriptor(handlerDescriptor);
|
||||
}
|
||||
else
|
||||
{
|
||||
HandlerDescriptor descriptor = new HandlerDescriptor(DescriptorType.General, handlerType);
|
||||
AddDescriptor(descriptor);
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or creates a descriptor list for the specified update type.
|
||||
/// </summary>
|
||||
@@ -163,20 +127,5 @@ namespace Telegrator.Providers
|
||||
|
||||
CommandAliasses.AddRange(alliasAttribute.Alliases);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Invokes a custom descriptors provider to get handler descriptors.
|
||||
/// </summary>
|
||||
/// <param name="handlerType">The handler type that implements ICustomDescriptorsProvider.</param>
|
||||
/// <returns>A collection of handler descriptors from the custom provider.</returns>
|
||||
/// <exception cref="Exception">Thrown when the handler type doesn't have a parameterless constructor or cannot be instantiated.</exception>
|
||||
protected virtual IEnumerable<HandlerDescriptor> InvokeCustomDescriptorsProvider(Type handlerType)
|
||||
{
|
||||
if (!handlerType.HasParameterlessCtor())
|
||||
throw new Exception();
|
||||
|
||||
ICustomDescriptorsProvider? provider = (ICustomDescriptorsProvider?)Activator.CreateInstance(handlerType);
|
||||
return provider == null ? throw new Exception() : provider.DescribeHandlers();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -86,7 +86,7 @@ namespace Telegrator
|
||||
throw new InvalidDataException("Message does not contain a command");
|
||||
|
||||
if (message is not { Text.Length: > 0 })
|
||||
throw new ArgumentNullException("Command text cannot be null or empty");
|
||||
throw new ArgumentNullException(nameof(message), "Command text cannot be null or empty");
|
||||
|
||||
if (!message.Text.Contains(' '))
|
||||
throw new MissingMemberException("Command dont contains arguments");
|
||||
@@ -620,6 +620,45 @@ namespace Telegrator
|
||||
public static HandlerBuilder<CallbackQuery> CreateCallbackQuery(this IHandlersCollection handlers)
|
||||
=> handlers.CreateHandler<CallbackQuery>(UpdateType.CallbackQuery);
|
||||
|
||||
/// <summary>
|
||||
/// Adds a handler type to the collection.
|
||||
/// </summary>
|
||||
/// <param name="handlers">The handlers collection.</param>
|
||||
/// <typeparam name="THandler">The type of handler to add.</typeparam>
|
||||
/// <returns>This collection instance for method chaining.</returns>
|
||||
public static IHandlersCollection AddHandler<THandler>(this IHandlersCollection handlers) where THandler : UpdateHandlerBase
|
||||
=> handlers.AddHandler(typeof(THandler));
|
||||
|
||||
/// <summary>
|
||||
/// Adds a handler type to the collection.
|
||||
/// </summary>
|
||||
/// <param name="handlers">The handlers collection.</param>
|
||||
/// <param name="handlerType">The type of handler to add.</param>
|
||||
/// <returns>This collection instance for method chaining.</returns>
|
||||
/// <exception cref="Exception">Thrown when the type is not a valid handler implementation.</exception>
|
||||
public static IHandlersCollection AddHandler(this IHandlersCollection handlers, Type handlerType)
|
||||
{
|
||||
if (!handlerType.IsHandlerRealization())
|
||||
throw new Exception();
|
||||
|
||||
if (handlerType.IsCustomDescriptorsProvider())
|
||||
{
|
||||
if (!handlerType.HasParameterlessCtor())
|
||||
throw new Exception();
|
||||
|
||||
ICustomDescriptorsProvider provider = (ICustomDescriptorsProvider)Activator.CreateInstance(handlerType);
|
||||
foreach (HandlerDescriptor handlerDescriptor in provider.DescribeHandlers())
|
||||
handlers.AddDescriptor(handlerDescriptor);
|
||||
}
|
||||
else
|
||||
{
|
||||
HandlerDescriptor descriptor = new HandlerDescriptor(DescriptorType.General, handlerType);
|
||||
handlers.AddDescriptor(descriptor);
|
||||
}
|
||||
|
||||
return handlers;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates implicit handler from method
|
||||
/// </summary>
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
#if DEBUG
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
||||
using System.Collections.Immutable;
|
||||
using System.Text;
|
||||
using System.Xml.Linq;
|
||||
using Telegrator.RoslynExtensions;
|
||||
using Telegrator.RoslynGenerators.RoslynExtensions;
|
||||
|
||||
#if DEBUG
|
||||
namespace Telegrator.RoslynGenerators
|
||||
{
|
||||
/// <summary>
|
||||
|
||||
@@ -3,7 +3,6 @@ using Microsoft.CodeAnalysis.CSharp;
|
||||
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
||||
using System.Collections.Immutable;
|
||||
using System.Text;
|
||||
using Telegrator.RoslynExtensions;
|
||||
using Telegrator.RoslynGenerators.RoslynExtensions;
|
||||
|
||||
#if DEBUG
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
namespace Telegrator.RoslynExtensions
|
||||
namespace Telegrator.RoslynGenerators.RoslynExtensions
|
||||
{
|
||||
public static class CollectionsExtensions
|
||||
{
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
using Microsoft.CodeAnalysis;
|
||||
|
||||
namespace Telegrator.RoslynExtensions
|
||||
namespace Telegrator.RoslynGenerators.RoslynExtensions
|
||||
{
|
||||
public static class DiagnosticsHelper
|
||||
{
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
namespace Telegrator.RoslynExtensions;
|
||||
namespace Telegrator.RoslynGenerators.RoslynExtensions;
|
||||
|
||||
public class TargteterNotFoundException() : Exception() { }
|
||||
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.CSharp;
|
||||
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
||||
using Telegrator.RoslynExtensions;
|
||||
|
||||
namespace Telegrator.RoslynGenerators.RoslynExtensions
|
||||
{
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
using System.Text;
|
||||
|
||||
namespace Telegrator.RoslynExtensions
|
||||
namespace Telegrator.RoslynGenerators.RoslynExtensions
|
||||
{
|
||||
public static class StringBuilderExtensions
|
||||
{
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
namespace Telegrator.RoslynExtensions
|
||||
namespace Telegrator.RoslynGenerators.RoslynExtensions
|
||||
{
|
||||
public static class StringExtensions
|
||||
{
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
using Microsoft.CodeAnalysis;
|
||||
|
||||
namespace Telegrator.RoslynExtensions;
|
||||
namespace Telegrator.RoslynGenerators.RoslynExtensions;
|
||||
|
||||
public static class SymbolsExtensions
|
||||
{
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
using Microsoft.CodeAnalysis.CSharp;
|
||||
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
||||
|
||||
namespace Telegrator.RoslynExtensions
|
||||
namespace Telegrator.RoslynGenerators.RoslynExtensions
|
||||
{
|
||||
public static class SyntaxNodesExtensions
|
||||
{
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
using Microsoft.CodeAnalysis;
|
||||
|
||||
namespace Telegrator.RoslynExtensions
|
||||
namespace Telegrator.RoslynGenerators.RoslynExtensions
|
||||
{
|
||||
public static class SyntaxTokenExtensions
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user