Files
Telegrator/src/Telegrator.Hosting/Providers/HostHandlersCollection.cs
T

62 lines
2.2 KiB
C#
Raw Normal View History

2025-07-24 23:19:59 +04:00
using Microsoft.Extensions.DependencyInjection;
2026-03-06 23:19:24 +04:00
using Telegrator.Core;
using Telegrator.Core.Descriptors;
2025-07-24 23:19:59 +04:00
2026-03-09 13:23:21 +04:00
namespace Telegrator.Providers;
/// <inheritdoc/>
public class HostHandlersCollection(IServiceCollection hostServiceColletion, TelegratorOptions options) : HandlersCollection(options)
2025-07-24 23:19:59 +04:00
{
2026-03-09 13:23:21 +04:00
private readonly IServiceCollection Services = hostServiceColletion;
2026-03-09 13:23:21 +04:00
/// <inheritdoc/>
protected override bool MustHaveParameterlessCtor => false;
2025-07-24 23:19:59 +04:00
2026-03-09 13:23:21 +04:00
/// <inheritdoc/>
public override IHandlersCollection AddDescriptor(HandlerDescriptor descriptor)
{
switch (descriptor.Type)
2025-07-24 23:19:59 +04:00
{
2026-03-09 13:23:21 +04:00
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;
}
2025-07-24 23:19:59 +04:00
}
2026-03-09 13:23:21 +04:00
return base.AddDescriptor(descriptor);
2025-07-24 23:19:59 +04:00
}
}