This commit is contained in:
Đăng Nguyễn
2025-10-13 13:17:32 +07:00
parent 511614df72
commit b2df5b22b7
66 changed files with 1957 additions and 376 deletions

View File

@@ -0,0 +1,39 @@
using RobotApp.Interfaces;
using RobotApp.Services.Robot;
using RobotApp.Services.State;
using System.Diagnostics.CodeAnalysis;
namespace RobotApp.Services;
public static class RobotExtensions
{
public static IServiceCollection AddRobot(this IServiceCollection services)
{
services.AddSingleton<RobotStateMachine>();
services.AddInterfaceServiceSingleton<ILocalization, RobotLocalization>();
return services;
}
public static IServiceCollection AddInterfaceServiceSingleton<TService, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TImplementation>(this IServiceCollection services) where TService : class where TImplementation : class, TService
{
services.AddSingleton<TImplementation>();
services.AddSingleton<TService>(sp => sp.GetRequiredService<TImplementation>());
return services;
}
public static IServiceCollection AddHostedServiceSingleton<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] THostedService>(this IServiceCollection services) where THostedService : class, IHostedService
{
services.AddSingleton<THostedService>();
services.AddHostedService(sp => sp.GetRequiredService<THostedService>());
return services;
}
public static IServiceCollection AddHostedInterfaceServiceSingleton<TService, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] THostedService>(this IServiceCollection services) where TService : class where THostedService : class, IHostedService, TService
{
services.AddSingleton<THostedService>();
services.AddSingleton<TService>(sp => sp.GetRequiredService<THostedService>());
services.AddHostedService(sp => sp.GetRequiredService<THostedService>());
return services;
}
}