2025-07-24 23:19:59 +04:00
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using Telegrator.Configuration;
|
|
|
|
|
using Telegrator.Hosting.Components;
|
|
|
|
|
using Telegrator.MadiatorCore;
|
|
|
|
|
using Telegrator.MadiatorCore.Descriptors;
|
|
|
|
|
using Telegrator.Providers;
|
|
|
|
|
|
|
|
|
|
namespace Telegrator.Hosting.Providers
|
|
|
|
|
{
|
2025-07-26 00:01:46 +04:00
|
|
|
/// <summary>
|
|
|
|
|
/// Pre host building task
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="builder"></param>
|
2025-07-28 20:35:48 +04:00
|
|
|
public delegate void PreBuildingRoutine(ITelegramBotHostBuilder builder);
|
2025-07-26 00:01:46 +04:00
|
|
|
|
|
|
|
|
/// <inheritdoc/>
|
2025-07-28 20:35:48 +04:00
|
|
|
public class HostHandlersCollection(IServiceCollection hostServiceColletion, ITelegratorOptions options) : HandlersCollection(options)
|
2025-07-24 23:19:59 +04:00
|
|
|
{
|
|
|
|
|
private readonly IServiceCollection Services = hostServiceColletion;
|
2025-07-26 00:01:46 +04:00
|
|
|
|
|
|
|
|
/// <inheritdoc/>
|
2025-07-24 23:19:59 +04:00
|
|
|
protected override bool MustHaveParameterlessCtor => false;
|
|
|
|
|
|
2025-07-26 00:01:46 +04:00
|
|
|
/// <summary>
|
|
|
|
|
/// List of tasks that should be completed right before building the bot
|
|
|
|
|
/// </summary>
|
|
|
|
|
public readonly List<PreBuildingRoutine> PreBuilderRoutines = [];
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc/>
|
2025-07-24 23:19:59 +04:00
|
|
|
public override IHandlersCollection AddHandler(Type handlerType)
|
|
|
|
|
{
|
2025-07-26 00:01:46 +04:00
|
|
|
if (handlerType.IsPreBuildingRoutine(out MethodInfo? routineMethod))
|
|
|
|
|
PreBuilderRoutines.Add(routineMethod.CreateDelegate<PreBuildingRoutine>(null));
|
2025-07-24 23:19:59 +04:00
|
|
|
|
|
|
|
|
return base.AddHandler(handlerType);
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-26 00:01:46 +04:00
|
|
|
/// <inheritdoc/>
|
2025-07-24 23:19:59 +04:00
|
|
|
public override IHandlersCollection AddDescriptor(HandlerDescriptor descriptor)
|
|
|
|
|
{
|
|
|
|
|
switch (descriptor.Type)
|
|
|
|
|
{
|
|
|
|
|
case DescriptorType.General:
|
|
|
|
|
{
|
|
|
|
|
if (descriptor.InstanceFactory != null)
|
|
|
|
|
Services.AddScoped(descriptor.HandlerType, _ => descriptor.InstanceFactory.Invoke());
|
|
|
|
|
else
|
|
|
|
|
Services.AddScoped(descriptor.HandlerType);
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case DescriptorType.Keyed:
|
|
|
|
|
{
|
|
|
|
|
if (descriptor.InstanceFactory != null)
|
|
|
|
|
Services.AddKeyedScoped(descriptor.HandlerType, descriptor.ServiceKey, (_, _) => descriptor.InstanceFactory.Invoke());
|
|
|
|
|
else
|
|
|
|
|
Services.AddKeyedScoped(descriptor.HandlerType, descriptor.ServiceKey);
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case DescriptorType.Singleton:
|
|
|
|
|
{
|
|
|
|
|
Services.AddSingleton(descriptor.HandlerType, descriptor.SingletonInstance ?? (descriptor.InstanceFactory != null
|
|
|
|
|
? descriptor.InstanceFactory.Invoke()
|
|
|
|
|
: throw new Exception()));
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case DescriptorType.Implicit:
|
|
|
|
|
{
|
|
|
|
|
Services.AddKeyedSingleton(descriptor.HandlerType, descriptor.ServiceKey, descriptor.SingletonInstance ?? (descriptor.InstanceFactory != null
|
|
|
|
|
? descriptor.InstanceFactory.Invoke()
|
|
|
|
|
: throw new Exception()));
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return base.AddDescriptor(descriptor);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|