< Summary - erichiller/mkmrk.Channels coverage

Information
Class: Microsoft.Extensions.DependencyInjection.BroadcastChannelServiceCollectionExtensions<TData, TResponse>
Assembly: mkmrk.Channels
File(s): /home/runner/work/mkmrk.Channels/mkmrk.Channels/src/mkmrk.Channels/Extensions/ServiceCollectionExtensions.cs
Tag: 161_8859726157
Line coverage
86%
Covered lines: 38
Uncovered lines: 6
Coverable lines: 44
Total lines: 106
Line coverage: 86.3%
Branch coverage
87%
Covered branches: 7
Total branches: 8
Branch coverage: 87.5%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
AddBroadcastChannel<TData, TResponse>(...)100%6100%
AddBroadcastChannels(...)100%1100%
AddBroadcastChannelsAsChannel(...)100%10%

File(s)

/home/runner/work/mkmrk.Channels/mkmrk.Channels/src/mkmrk.Channels/Extensions/ServiceCollectionExtensions.cs

#LineLine coverage
 1using System.Threading.Channels;
 2
 3using Microsoft.Extensions.DependencyInjection.Extensions;
 4
 5using mkmrk.Channels;
 6
 7namespace Microsoft.Extensions.DependencyInjection;
 8
 9/// <summary>
 10/// Extensions for <see cref="IServiceCollection"/> to add the required services for <see cref="BroadcastChannel{TData,T
 11/// </summary>
 12public static class BroadcastChannelServiceCollectionExtensions {
 13    /// <summary>
 14    /// Add the required services for <see cref="BroadcastChannel{TData,TResponse}"/>.
 15    /// </summary>
 16    /// <remarks>
 17    /// <list type="deflist">
 18    /// <item><term><see cref="BroadcastChannel{TData, TResponse}"/></term><description>will be added as a Singleton</de
 19    /// <item><term><see cref="BroadcastChannelWriter{TData, TResponse}"/></term><description>will be added as a Singlet
 20    /// <item><term><see cref="BroadcastChannelReader{TData, TResponse}"/></term><description>will be added as Transient
 21    /// </list>
 22    /// A single writer should be used throughout the life of an application. When readers are disposed of, the object w
 23    /// There are some cases where a Singleton reader might be desirable, such as for long lived <c>IHostedService</c>,
 24    /// but this will have to be added manually using the full <see cref="BroadcastChannel{TData, TResponse}"/> form rat
 25    /// </remarks>
 226    public static IServiceCollection AddBroadcastChannel<TData, TResponse>( this IServiceCollection services ) where TRe
 227        services.TryAddSingleton<IBroadcastChannel<TData, TResponse>, BroadcastChannel<TData, TResponse>>();
 228        services.TryAddSingleton<IBroadcastChannelWriter<TData, TResponse>, BroadcastChannelWriter<TData, TResponse>>();
 229        services.TryAddTransient<IBroadcastChannelReader<TData, TResponse>, BroadcastChannelReader<TData, TResponse>>();
 30        /*
 31         * AddSingleton instead of TryAddSingleton here because there could be more than one IBroadcastChannelWriter<TDa
 32         * so keep them all and let the user determine which they want.
 33         */
 234        services.AddSingleton<IBroadcastChannel<TData>>( sp => {
 235            IBroadcastChannel<TData> broadcastChannel = sp.GetRequiredService<IBroadcastChannel<TData, TResponse>>() as 
 236                                                        ?? ThrowHelper.ThrowInvalidCastException<IBroadcastChannel<TData
 237            return broadcastChannel;
 238        } );
 239        services.AddSingleton<IBroadcastChannelWriter<TData>>( sp => {
 240            IBroadcastChannelWriter<TData, TResponse> broadcastChannelWriter = sp.GetRequiredService<IBroadcastChannelWr
 241            return broadcastChannelWriter;
 242        } );
 243        services.AddTransient<IBroadcastChannelReader<TData>>( sp => {
 244            IBroadcastChannelReader<TData, TResponse> broadcastChannelReader = sp.GetRequiredService<IBroadcastChannelWr
 245            return broadcastChannelReader;
 246        } );
 47
 48        // reader sources
 249        services.TryAddTransient<BroadcastChannelReaderSource<TData, TResponse>>(); // concrete so that the implicit con
 250        services.TryAddTransient<IBroadcastChannelReaderSource<TData, TResponse>, BroadcastChannelReaderSource<TData, TR
 251        services.TryAddTransient<IBroadcastChannelReaderSource<TData>, BroadcastChannelReaderSource<TData, TResponse>>()
 252        services.TryAddTransient<IBroadcastChannelAddReaderProvider<TData>, BroadcastChannelReaderSource<TData, TRespons
 53
 54
 255        return services;
 256    }
 57
 58    /// <summary>
 59    /// Add the required services for <b><i>any</i></b> requested BroadcastChannel resource.
 60    /// </summary>
 61    /// <remarks>
 62    /// It is important to note that requesting <c>BroadcastChannel&lt;TData&gt;</c> will
 63    /// not result in the same instance as requesting <c>BroadcastChannel&lt;TData,IBroadcastChannelResponse&gt;</c>.
 64    /// <br/>
 65    /// For Example:
 66    /// <code>
 67    /// var writerResponseTypeSpecified = _host.Services.GetRequiredService&lt;BroadcastChannelReader&lt;ChannelMessageS
 68    /// var readerNoResponseTypeSpecified = _host.Services.GetRequiredService&lt;BroadcastChannelReader&lt;ChannelMessag
 69    /// Console.WriteLine(writerResponseTypeSpecified.ReaderCount); // 0
 70    /// var writerNoResponseTypeSpecified = _host.Services.GetRequiredService&lt;BroadcastChannelReader&lt;ChannelMessag
 71    /// Console.WriteLine(writerNoResponseTypeSpecified.ReaderCount); // 1
 72    /// </code>
 73    /// </remarks>
 74    /// <inheritdoc cref="AddBroadcastChannel{TData,TResponse}" path="/remarks" />
 275    public static IServiceCollection AddBroadcastChannels( this IServiceCollection services ) {
 76        // Data and response generic type parameters
 277        services.TryAddSingleton( typeof(IBroadcastChannel<>), typeof(BroadcastChannel<>) );
 278        services.TryAddSingleton( typeof(IBroadcastChannelWriter<>), typeof(BroadcastChannelWriter<>) );
 279        services.TryAddTransient( typeof(IBroadcastChannelReader<>), typeof(BroadcastChannelReader<>) );
 280        services.TryAddTransient( typeof(IBroadcastChannelReaderSource<>), typeof(BroadcastChannelReaderSource<>) );
 281        services.TryAddTransient( typeof(IBroadcastChannelAddReaderProvider<>), typeof(BroadcastChannelReaderSource<>) )
 82
 83
 84        // ChannelMux
 285        services.TryAddTransient( typeof(ChannelMux<,>) );
 286        services.TryAddTransient( typeof(ChannelMux<,,>) );
 287        services.TryAddTransient( typeof(ChannelMux<,,,>) );
 288        services.TryAddTransient( typeof(ChannelMux<,,,,>) );
 289        services.TryAddTransient( typeof(ChannelMux<,,,,,>) );
 290        services.TryAddTransient( typeof(ChannelMux<,,,,,,>) );
 291        services.TryAddTransient( typeof(ChannelMux<,,,,,,,>) );
 292        return services;
 293    }
 94
 95    /// <summary>
 96    /// Add the required services for <b><i>any</i></b> requested <see cref="BroadcastChannel{TData,TResponse}"/>
 97    /// replacing any requests for <see cref="Channel{T}"/>, <see cref="ChannelWriter{T}"/>, and <see cref="ChannelReade
 98    /// </summary>
 99    /// <inheritdoc cref="AddBroadcastChannel{TData,TResponse}" path="/remarks" />
 0100    public static IServiceCollection AddBroadcastChannelsAsChannel( this IServiceCollection services ) {
 0101        services.TryAddSingleton( typeof(Channel<>), typeof(BroadcastChannel<>) );
 0102        services.TryAddSingleton( typeof(ChannelWriter<>), typeof(BroadcastChannelWriter<>) );
 0103        services.TryAddTransient( typeof(ChannelReader<>), typeof(BroadcastChannelReader<>) );
 0104        return services;
 0105    }
 106}