Skip to content

Commit c515633

Browse files
committed
Adjust IServiceCollection extensions to return the collection instead 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
1 parent 42607f1 commit c515633

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

Discord.Addons.Hosting/ServiceCollectionExtensions.cs

+19-12
Original file line numberDiff line numberDiff line change
@@ -34,38 +34,42 @@ public static class ServiceCollectionExtensions
3434
/// Adds and optionally configures a <see cref="DiscordShardedClient"/> along with the required services.
3535
/// </summary>
3636
/// <remarks>
37-
/// A <see cref="IServiceProvider"/> is supplied so you can pull data from additional services if required.
37+
/// A <see cref="IServiceProvider"/> is supplied, so you can pull data from additional services if required.
3838
/// </remarks>
3939
/// <param name="collection">The service collection to configure.</param>
4040
/// <param name="config">The delegate for the <see cref="DiscordHostConfiguration" /> that will be used to configure the host.</param>
4141
/// <exception cref="InvalidOperationException">Thrown if client is already added to the service collection</exception>
42-
public static void AddDiscordShardedHost(this IServiceCollection collection, Action<DiscordHostConfiguration, IServiceProvider> config)
42+
public static IServiceCollection AddDiscordShardedHost(this IServiceCollection collection, Action<DiscordHostConfiguration, IServiceProvider> config)
4343
{
4444
collection.AddDiscordHostInternal<DiscordShardedClient>(config);
4545

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

4949
collection.AddSingleton<DiscordShardedClient, InjectableDiscordShardedClient>();
50+
51+
return collection;
5052
}
5153

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

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

6870
builder.AddSingleton<DiscordSocketClient, InjectableDiscordSocketClient>();
71+
72+
return builder;
6973
}
7074

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

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

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

120124
collection.AddSingleton<CommandService, InjectableCommandService>();
121125
collection.AddHostedService<CommandServiceRegistrationHost>();
126+
127+
return collection;
122128
}
123129

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

131137

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

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

151157
collection.AddSingleton<InteractionService, InjectableInteractionService>();
152158
collection.AddHostedService<InteractionServiceRegistrationHost>();
153-
159+
160+
return collection;
154161
}
155-
}
162+
}

0 commit comments

Comments
 (0)