Remove "extends IHostedService" from classes where it's not required (#236)

Originally, these classes were services because I thought that all
DI-resolvable classes need to be services. However, this is not true, so
we can make these classes (notably Utility and GuildDataService) not
extend anything. `UtilityService` was renamed to `Utility` for
simplicity

---------

Signed-off-by: Octol1ttle <l1ttleofficial@outlook.com>
This commit is contained in:
Octol1ttle 2023-12-20 21:33:52 +05:00 committed by GitHub
parent 6688481093
commit bd4c5b26da
Signed by: GitHub
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 20 additions and 41 deletions

View file

@ -11,7 +11,7 @@ namespace Octobot.Services;
/// <summary>
/// Handles saving, loading, initializing and providing <see cref="GuildData" />.
/// </summary>
public sealed class GuildDataService : IHostedService
public sealed class GuildDataService
{
private readonly ConcurrentDictionary<Snowflake, GuildData> _datas = new();
private readonly ILogger<GuildDataService> _logger;
@ -24,16 +24,6 @@ public sealed class GuildDataService : IHostedService
lifetime.ApplicationStopping.Register(ApplicationStopping);
}
public Task StartAsync(CancellationToken ct)
{
return Task.CompletedTask;
}
public Task StopAsync(CancellationToken ct)
{
return Task.CompletedTask;
}
private void ApplicationStopping()
{
SaveAsync(CancellationToken.None).GetAwaiter().GetResult();

View file

@ -30,10 +30,10 @@ public sealed partial class MemberUpdateService : BackgroundService
private readonly IDiscordRestGuildAPI _guildApi;
private readonly GuildDataService _guildData;
private readonly ILogger<MemberUpdateService> _logger;
private readonly UtilityService _utility;
private readonly Utility _utility;
public MemberUpdateService(IDiscordRestChannelAPI channelApi, IDiscordRestGuildAPI guildApi,
GuildDataService guildData, ILogger<MemberUpdateService> logger, UtilityService utility)
GuildDataService guildData, ILogger<MemberUpdateService> logger, Utility utility)
{
_channelApi = channelApi;
_guildApi = guildApi;

View file

@ -19,10 +19,10 @@ public sealed class ScheduledEventUpdateService : BackgroundService
private readonly IDiscordRestGuildScheduledEventAPI _eventApi;
private readonly GuildDataService _guildData;
private readonly ILogger<ScheduledEventUpdateService> _logger;
private readonly UtilityService _utility;
private readonly Utility _utility;
public ScheduledEventUpdateService(IDiscordRestChannelAPI channelApi, IDiscordRestGuildScheduledEventAPI eventApi,
GuildDataService guildData, ILogger<ScheduledEventUpdateService> logger, UtilityService utility)
GuildDataService guildData, ILogger<ScheduledEventUpdateService> logger, Utility utility)
{
_channelApi = channelApi;
_eventApi = eventApi;

View file

@ -1,7 +1,6 @@
using System.Drawing;
using System.Text;
using System.Text.Json.Nodes;
using Microsoft.Extensions.Hosting;
using Octobot.Data;
using Octobot.Extensions;
using Remora.Discord.API.Abstractions.Objects;
@ -17,14 +16,14 @@ namespace Octobot.Services;
/// Provides utility methods that cannot be transformed to extension methods because they require usage
/// of some Discord APIs.
/// </summary>
public sealed class UtilityService : IHostedService
public sealed class Utility
{
private readonly IDiscordRestChannelAPI _channelApi;
private readonly IDiscordRestGuildScheduledEventAPI _eventApi;
private readonly IDiscordRestGuildAPI _guildApi;
private readonly IDiscordRestUserAPI _userApi;
public UtilityService(
public Utility(
IDiscordRestChannelAPI channelApi, IDiscordRestGuildScheduledEventAPI eventApi, IDiscordRestGuildAPI guildApi,
IDiscordRestUserAPI userApi)
{
@ -34,16 +33,6 @@ public sealed class UtilityService : IHostedService
_userApi = userApi;
}
public Task StartAsync(CancellationToken ct)
{
return Task.CompletedTask;
}
public Task StopAsync(CancellationToken ct)
{
return Task.CompletedTask;
}
/// <summary>
/// Checks whether or not a member can interact with another member
/// </summary>