Skip to content

Commit

Permalink
Adjust IServiceCollection extensions to return the collection inste…
Browse files Browse the repository at this point in the history
…ad of `void`

# Motivations
Returning the `IServiceCollection` allows for clean chaining of `Add..` calls, like so

```
services
    .AddDiscordHost()
    .AddCommandService()
    .AddInteractionService()

```

# Modifications
- Return the `IServiceCollection` instead of `void`
- Add some commas to places in the comments, for grammar-sake
  • Loading branch information
Twinki14 committed Jul 1, 2024
1 parent 42607f1 commit c515633
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions Discord.Addons.Hosting/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,38 +34,42 @@ public static class ServiceCollectionExtensions
/// Adds and optionally configures a <see cref="DiscordShardedClient"/> along with the required services.
/// </summary>
/// <remarks>
/// A <see cref="IServiceProvider"/> is supplied so you can pull data from additional services if required.
/// A <see cref="IServiceProvider"/> is supplied, so you can pull data from additional services if required.
/// </remarks>
/// <param name="collection">The service collection to configure.</param>
/// <param name="config">The delegate for the <see cref="DiscordHostConfiguration" /> that will be used to configure the host.</param>
/// <exception cref="InvalidOperationException">Thrown if client is already added to the service collection</exception>
public static void AddDiscordShardedHost(this IServiceCollection collection, Action<DiscordHostConfiguration, IServiceProvider> config)
public static IServiceCollection AddDiscordShardedHost(this IServiceCollection collection, Action<DiscordHostConfiguration, IServiceProvider> config)
{
collection.AddDiscordHostInternal<DiscordShardedClient>(config);

if (collection.Any(x => x.ServiceType.BaseType == typeof(BaseSocketClient)))
throw new InvalidOperationException("Cannot add more than one Discord Client to host");

collection.AddSingleton<DiscordShardedClient, InjectableDiscordShardedClient>();

return collection;
}

/// <summary>
/// Adds and optionally configures a <see cref="DiscordSocketClient"/> along with the required services.
/// </summary>
/// <remarks>
/// A <see cref="IServiceProvider"/> is supplied so you can pull data from additional services if required.
/// A <see cref="IServiceProvider"/> is supplied, so you can pull data from additional services if required.
/// </remarks>
/// <param name="builder">The host builder to configure.</param>
/// <param name="config">The delegate for the <see cref="DiscordHostConfiguration" /> that will be used to configure the host.</param>
/// <exception cref="InvalidOperationException">Thrown if client is already added to the service collection</exception>
public static void AddDiscordHost(this IServiceCollection builder, Action<DiscordHostConfiguration, IServiceProvider> config)
public static IServiceCollection AddDiscordHost(this IServiceCollection builder, Action<DiscordHostConfiguration, IServiceProvider> config)
{
builder.AddDiscordHostInternal<DiscordSocketClient>(config);

if (builder.Any(x => x.ServiceType.BaseType == typeof(BaseSocketClient)))
throw new InvalidOperationException("Cannot add more than one Discord Client to host");

builder.AddSingleton<DiscordSocketClient, InjectableDiscordSocketClient>();

return builder;
}

private static void AddDiscordHostInternal<T>(this IServiceCollection collection, Action<DiscordHostConfiguration, IServiceProvider> config) where T: BaseSocketClient
Expand Down Expand Up @@ -96,19 +100,19 @@ static bool ValidateToken(string token)
/// </summary>
/// <param name="collection">The service collection to configure.</param>
/// <exception cref="InvalidOperationException">Thrown if <see cref="CommandService"/> is already added to the collection</exception>
public static void AddCommandService(this IServiceCollection collection) => collection.AddCommandService((context, config) => { });
public static IServiceCollection AddCommandService(this IServiceCollection collection) => collection.AddCommandService((context, config) => { });

/// <summary>
/// Adds a <see cref="CommandService"/> instance to the host for use with a Discord.NET client. />
/// </summary>
/// <remarks>
/// A <see cref="IServiceProvider"/> is supplied so you can pull data from additional services if required.
/// A <see cref="IServiceProvider"/> is supplied, so you can pull data from additional services if required.
/// </remarks>
/// <param name="collection">The service collection to configure.</param>
/// <param name="config">The delegate for configuring the <see cref="CommandServiceConfig" /> that will be used to initialise the service.</param>
/// <exception cref="ArgumentNullException">Thrown if config is null</exception>
/// <exception cref="InvalidOperationException">Thrown if <see cref="CommandService"/> is already added to the collection</exception>
public static void AddCommandService(this IServiceCollection collection, Action<CommandServiceConfig, IServiceProvider> config)
public static IServiceCollection AddCommandService(this IServiceCollection collection, Action<CommandServiceConfig, IServiceProvider> config)
{
ArgumentNullException.ThrowIfNull(config);

Expand All @@ -119,27 +123,29 @@ public static void AddCommandService(this IServiceCollection collection, Action<

collection.AddSingleton<CommandService, InjectableCommandService>();
collection.AddHostedService<CommandServiceRegistrationHost>();

return collection;
}

/// <summary>
/// Adds a <see cref="InteractionService"/> instance to the host for use with a Discord.NET client. />
/// </summary>
/// <param name="collection">The service collection to configure.</param>
/// <exception cref="InvalidOperationException">Thrown if <see cref="InteractionService"/> is already added to the collection</exception>
public static void AddInteractionService(this IServiceCollection collection) => collection.AddInteractionService((_, _) => { });
public static IServiceCollection AddInteractionService(this IServiceCollection collection) => collection.AddInteractionService((_, _) => { });


/// <summary>
/// Adds a <see cref="InteractionService"/> instance to the host for use with a Discord.NET client. />
/// </summary>
/// <remarks>
/// A <see cref="IServiceProvider"/> is supplied so you can pull data from additional services if required.
/// A <see cref="IServiceProvider"/> is supplied, so you can pull data from additional services if required.
/// </remarks>
/// <param name="collection">The service collection to configure.</param>
/// <param name="config">The delegate for configuring the <see cref="InteractionServiceConfig" /> that will be used to initialise the service.</param>
/// <exception cref="ArgumentNullException">Thrown if config is null</exception>
/// <exception cref="InvalidOperationException">Thrown if <see cref="InteractionService"/> is already added to the collection</exception>
public static void AddInteractionService(this IServiceCollection collection, Action<InteractionServiceConfig, IServiceProvider> config)
public static IServiceCollection AddInteractionService(this IServiceCollection collection, Action<InteractionServiceConfig, IServiceProvider> config)
{
ArgumentNullException.ThrowIfNull(config);

Expand All @@ -150,6 +156,7 @@ public static void AddInteractionService(this IServiceCollection collection, Act

collection.AddSingleton<InteractionService, InjectableInteractionService>();
collection.AddHostedService<InteractionServiceRegistrationHost>();


return collection;
}
}
}

0 comments on commit c515633

Please sign in to comment.