< Summary

Information
Class: Microsoft.Extensions.DependencyInjection.ExtensionMethods
Assembly: SimpleOptions
File(s): /home/runner/work/SimpleOptions/SimpleOptions/src/SimpleOptions/Extensions.cs
Tag: 28_8817130891
Line coverage
100%
Covered lines: 22
Uncovered lines: 0
Coverable lines: 22
Total lines: 44
Line coverage: 100%
Branch coverage
100%
Covered branches: 10
Total branches: 10
Branch coverage: 100%
Method coverage

Method coverage is only available for sponsors.

Upgrade to PRO version

Coverage History

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
AddSimpleOptions(...)100%10100%

File(s)

/home/runner/work/SimpleOptions/SimpleOptions/src/SimpleOptions/Extensions.cs

#LineLine coverage
 1using Microsoft.Extensions.Configuration;
 2using Microsoft.Extensions.DependencyInjection.Extensions;
 3
 4namespace Microsoft.Extensions.DependencyInjection;
 5
 6internal delegate void SimpleOptionValid();
 7public static class ExtensionMethods
 8{
 9    public static IServiceCollection AddSimpleOptions<T>(this IServiceCollection services,
 10        string key,
 11        Func<T, IServiceProvider, T>? post = null,
 12        Action<T, IServiceProvider> validate = null
 13        ) where T : class
 214    {
 415        post ??= (v, _) => v;
 416        validate ??= (_, _) => { };
 17
 218        services.TryAddKeyedSingleton<T>(key, (sp, _) =>
 219        {
 220            var configuration = sp.GetRequiredService<IConfiguration>();
 221
 222            var v = configuration.GetSection(key).Get<T>(option =>
 223            {
 224                option.BindNonPublicProperties = true;
 425            });
 226
 227            return post.Invoke(v, sp);
 428        });
 29
 230        services.AddTransient<SimpleOptionValid>(sp =>
 231        {
 232            var v = sp.GetRequiredKeyedService<T>(key);
 433            return () => validate.Invoke(v, sp);
 434        });
 35
 236        services.AddHostedService<ValidatorHostedService>();
 37
 238        return services;
 239    }
 40
 41
 42}
 43
 44