Repository: dotnet/efcore
File: src/EFCore.Cosmos/Storage/Internal/SingletonCosmosClientWrapper.cs (C#)

Code before:
        if (options.MaxTcpConnectionsPerEndpoint != null)
        {
            configuration.MaxTcpConnectionsPerEndpoint = options.MaxTcpConnectionsPerEndpoint.Value;
        }

        if (options.MaxRequestsPerTcpConnection != null)
        {
            configuration.MaxRequestsPerTcpConnection = options.MaxRequestsPerTcpConnection.Value;
        }

        if (options.HttpClientFactory != null)
        {
            configuration.HttpClientFactory = options.HttpClientFactory;
        }

        _options = configuration;
    }

    /// <summary>
    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to
    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in
    ///     any release. You should only use it directly in your code with extreme caution and knowing that
    ///     doing so can result in application failures when updating to a new Entity Framework Core release.
    /// </summary>
    public virtual CosmosClient Client
        => NonCapturingLazyInitializer.EnsureInitialized(ref _client, this, CreateClient);

    private static CosmosClient CreateClient(SingletonCosmosClientWrapper wrapper)
        => string.IsNullOrEmpty(wrapper._connectionString)
            ? wrapper._tokenCredential == null
                ? wrapper._endpoint == null
                    ? throw new InvalidOperationException(CosmosStrings.ConnectionInfoMissing)
                    : new CosmosClient(wrapper._endpoint, wrapper._key, wrapper._options)
                : new CosmosClient(wrapper._endpoint, wrapper._tokenCredential, wrapper._options)
            : new CosmosClient(wrapper._connectionString, wrapper._options);

    /// <summary>
    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to
    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in
    ///     any release. You should only use it directly in your code with extreme caution and knowing that
    ///     doing so can result in application failures when updating to a new Entity Framework Core release.
    /// </summary>
    public virtual void Dispose()
    {
        _client?.Dispose();
        _client = null;
    }
}


Review comment (suggestion):
@AndriySvyryd any reason we don't just instantiate and set the CosmosClient in the constructor? Given that this is a singleton in any case, even in cases where the client isn't used, I'm not sure what we get by initializing lazilly...

Code after:
        if (options.HttpClientFactory != null)
        {
            configuration.HttpClientFactory = options.HttpClientFactory;
        }

        _client = options switch
        {
            { ConnectionString: not null and not "" } => new CosmosClient(options.ConnectionString, configuration),
            { TokenCredential: not null } => new CosmosClient(options.AccountEndpoint, options.TokenCredential, configuration),
            { AccountEndpoint: not null } => new CosmosClient(options.AccountEndpoint, options.AccountKey, configuration),
            _ => throw new InvalidOperationException(CosmosStrings.ConnectionInfoMissing)
        };
    }

    /// <summary>
    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to
    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in
    ///     any release. You should only use it directly in your code with extreme caution and knowing that
    ///     doing so can result in application failures when updating to a new Entity Framework Core release.
    /// </summary>
    public virtual CosmosClient Client => _client;

    /// <summary>
    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to
    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in
    ///     any release. You should only use it directly in your code with extreme caution and knowing that
    ///     doing so can result in application failures when updating to a new Entity Framework Core release.
    /// </summary>
    public virtual void Dispose()
    {
        _client.Dispose();
    }
}