< Summary

Class:Microsoft.Extensions.Azure.ClientOptionsFactory`2
Assembly:Microsoft.Extensions.Azure
File(s):C:\Git\azure-sdk-for-net\sdk\core\Microsoft.Extensions.Azure\src\Internal\ClientOptionsFactory.cs
Covered lines:37
Uncovered lines:10
Coverable lines:47
Total lines:129
Line coverage:78.7% (37 of 47)
Covered branches:25
Total branches:32
Branch coverage:78.1% (25 of 32)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
.ctor(...)-100%100%
CreateOptions(...)-81.25%83.33%
IsServiceVersionParameter(...)-100%100%
Create(...)-55.56%62.5%

File(s)

C:\Git\azure-sdk-for-net\sdk\core\Microsoft.Extensions.Azure\src\Internal\ClientOptionsFactory.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4using System;
 5using System.Collections.Generic;
 6using System.Reflection;
 7using Microsoft.Extensions.Options;
 8
 9namespace Microsoft.Extensions.Azure
 10{
 11    // Slightly adjusted copy of https://github.com/aspnet/Extensions/blob/master/src/Options/Options/src/OptionsFactory
 12    internal class ClientOptionsFactory<TClient, TOptions> where TOptions : class
 13    {
 14        private const string ServiceVersionParameterTypeName = "ServiceVersion";
 15
 16        private readonly IEnumerable<IConfigureOptions<TOptions>> _setups;
 17        private readonly IEnumerable<IPostConfigureOptions<TOptions>> _postConfigures;
 18
 19        private readonly IEnumerable<ClientRegistration<TClient, TOptions>> _clientRegistrations;
 20
 4221        public ClientOptionsFactory(IEnumerable<IConfigureOptions<TOptions>> setups, IEnumerable<IPostConfigureOptions<T
 22        {
 4223            _setups = setups;
 4224            _postConfigures = postConfigures;
 4225            _clientRegistrations = clientRegistrations;
 4226        }
 27
 28        private TOptions CreateOptions(string name)
 29        {
 4230            object version = null;
 31
 18032            foreach (var clientRegistration in _clientRegistrations)
 33            {
 4834                if (clientRegistration.Name == name)
 35                {
 4436                    version = clientRegistration.Version;
 37                }
 38            }
 39
 4240            ConstructorInfo parameterlessConstructor = null;
 4241            int versionParameterIndex = 0;
 4242            object[] constructorArguments = null;
 43
 16844            foreach (var constructor in typeof(TOptions).GetConstructors())
 45            {
 4246                var parameters = constructor.GetParameters();
 4247                if (parameters.Length == 0)
 48                {
 049                    parameterlessConstructor = constructor;
 050                    continue;
 51                }
 52
 4253                bool allParametersHaveDefaultValue = true;
 17654                for (int i = 0; i < parameters.Length; i++)
 55                {
 4656                    ParameterInfo parameter = parameters[i];
 4657                    if (parameter.HasDefaultValue)
 58                    {
 4659                        if (IsServiceVersionParameter(parameter))
 60                        {
 4261                            versionParameterIndex = i;
 62                        }
 63                    }
 64                    else
 65                    {
 066                        allParametersHaveDefaultValue = false;
 067                        break;
 68                    }
 69                }
 70
 4271                if (allParametersHaveDefaultValue)
 72                {
 4273                    constructorArguments = new object[parameters.Length];
 74
 17675                    for (int i = 0; i < parameters.Length; i++)
 76                    {
 4677                        constructorArguments[i] = parameters[i].DefaultValue;
 78                    }
 79                }
 80            }
 81
 4282            if (version != null)
 83            {
 484                if (constructorArguments != null)
 85                {
 486                    constructorArguments[versionParameterIndex] = version;
 487                    return (TOptions)Activator.CreateInstance(typeof(TOptions), constructorArguments);
 88                }
 89
 090                throw new InvalidOperationException("Unable to find constructor that takes service version");
 91            }
 92
 3893            if (parameterlessConstructor != null)
 94            {
 095                return Activator.CreateInstance<TOptions>();
 96            }
 97
 3898            return (TOptions)Activator.CreateInstance(typeof(TOptions), constructorArguments);
 99        }
 100
 101        private static bool IsServiceVersionParameter(ParameterInfo parameter) =>
 46102            parameter.ParameterType.Name == ServiceVersionParameterTypeName;
 103
 104        /// <summary>
 105        /// Returns a configured <typeparamref name="TOptions"/> instance with the given <paramref name="name"/>.
 106        /// </summary>
 107        public TOptions Create(string name)
 108        {
 42109            var options = CreateOptions(name);
 204110            foreach (var setup in _setups)
 111            {
 60112                if (setup is IConfigureNamedOptions<TOptions> namedSetup)
 113                {
 60114                    namedSetup.Configure(name, options);
 115                }
 0116                else if (name == Microsoft.Extensions.Options.Options.DefaultName)
 117                {
 0118                    setup.Configure(options);
 119                }
 120            }
 0121            foreach (var post in _postConfigures)
 122            {
 0123                post.PostConfigure(name, options);
 124            }
 125
 42126            return options;
 127        }
 128    }
 129}