Adds Autofac extensions for MongoDb connector [delivers #153372694, #161731818]

This commit is contained in:
Tim Hess 2018-12-05 15:36:48 -06:00
Родитель 4758b71871
Коммит 6523910a31
6 изменённых файлов: 160 добавлений и 26 удалений

Просмотреть файл

@ -0,0 +1,58 @@
// Copyright 2017 the original author or authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using Autofac;
using Autofac.Builder;
using Microsoft.Extensions.Configuration;
using Steeltoe.CloudFoundry.Connector;
using Steeltoe.CloudFoundry.Connector.MongoDb;
using Steeltoe.CloudFoundry.Connector.Services;
using System;
namespace Steeltoe.CloudFoundry.ConnectorAutofac
{
public static class MongoDbContainerBuilderExtensions
{
/// <summary>
/// Adds MongoDb classes (MongoClient, IMongoClient and MongoUrl) to your Autofac Container
/// </summary>
/// <param name="container">Your Autofac Container Builder</param>
/// <param name="config">Application configuration</param>
/// <param name="serviceName">Cloud Foundry service name binding</param>
/// <returns>the RegistrationBuilder for (optional) additional configuration</returns>
public static IRegistrationBuilder<object, SimpleActivatorData, SingleRegistrationStyle> RegisterMongoDbConnection(this ContainerBuilder container, IConfiguration config, string serviceName = null)
{
if (container == null)
{
throw new ArgumentNullException(nameof(container));
}
if (config == null)
{
throw new ArgumentNullException(nameof(config));
}
MongoDbServiceInfo info = serviceName == null
? config.GetSingletonServiceInfo<MongoDbServiceInfo>()
: config.GetRequiredServiceInfo<MongoDbServiceInfo>(serviceName);
var mongoOptions = new MongoDbConnectorOptions(config);
var clientFactory = new MongoDbConnectorFactory(info, mongoOptions, MongoDbTypeLocator.MongoClient);
var urlFactory = new MongoDbConnectorFactory(info, mongoOptions, MongoDbTypeLocator.MongoUrl);
container.Register(c => urlFactory.Create(null)).As(MongoDbTypeLocator.MongoUrl);
return container.Register(c => clientFactory.Create(null)).As(MongoDbTypeLocator.IMongoClient, MongoDbTypeLocator.MongoClient);
}
}
}

Просмотреть файл

@ -67,6 +67,27 @@ namespace Steeltoe.CloudFoundry.Connector
return null;
}
/// <summary>
/// Search a list of assemblies for the first matching type
/// </summary>
/// <param name="assemblyNames">List of assembly names to search</param>
/// <param name="typeNames">List of suitable types</param>
/// <param name="typeName">To use in exception</param>
/// <param name="assemblyShortDescription">Describe what might be missing</param>
/// <returns>An appropriate type</returns>
/// <remarks>Great for finding an implementation type that could have one or more names in one or more assemblies</remarks>
/// <exception cref="ConnectorException">When type isn't found</exception>
public static Type FindTypeOrThrow(string[] assemblyNames, string[] typeNames, string typeName, string assemblyShortDescription)
{
var type = FindType(assemblyNames, typeNames);
if (type == null)
{
throw new ConnectorException($"Unable to find {typeName}, are you missing {assemblyShortDescription}?");
}
return type;
}
/// <summary>
/// Find a type from within an assembly
/// </summary>

Просмотреть файл

@ -45,36 +45,18 @@ namespace Steeltoe.CloudFoundry.Connector.MongoDb
/// Gets IMongoClient from MongoDB Library
/// </summary>
/// <exception cref="ConnectorException">When type is not found</exception>
public static Type IMongoClient
{
get
{
var type = ConnectorHelpers.FindType(Assemblies, ConnectionInterfaceTypeNames);
if (type == null)
{
throw new ConnectorException("Unable to find IMongoClient, are you missing a MongoDB driver?");
}
return type;
}
}
public static Type IMongoClient { get { return ConnectorHelpers.FindTypeOrThrow(Assemblies, ConnectionInterfaceTypeNames, "IMongoClient", "a MongoDB driver"); } }
/// <summary>
/// Gets MongoClient from MongoDB Library
/// </summary>
/// <exception cref="ConnectorException">When type is not found</exception>
public static Type MongoClient
{
get
{
var type = ConnectorHelpers.FindType(Assemblies, ConnectionTypeNames);
if (type == null)
{
throw new ConnectorException("Unable to find MongoClient, are you missing a MongoDB driver?");
}
public static Type MongoClient { get { return ConnectorHelpers.FindTypeOrThrow(Assemblies, ConnectionTypeNames, "MongoClient", "a MongoDB driver"); } }
return type;
}
}
/// <summary>
/// Gets MongoUrl from MongoDB Library
/// </summary>
/// <exception cref="ConnectorException">When type is not found</exception>
public static Type MongoUrl { get { return ConnectorHelpers.FindTypeOrThrow(Assemblies, MongoConnectionInfo, "MongoUrl", "a MongoDB driver"); } }
}
}

Просмотреть файл

@ -24,7 +24,11 @@ namespace Steeltoe.CloudFoundry.Connector.Services
}
public SqlServerServiceInfo(string id, string url, string username, string password)
: base(id, url.Replace("jdbc:", string.Empty).Replace(';', '/'), username, password)
: base(
id,
url != null ? url.Replace("jdbc:", string.Empty).Replace(';', '/') : string.Empty,
username,
password)
{
}
}

Просмотреть файл

@ -0,0 +1,68 @@
// Copyright 2017 the original author or authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using Autofac;
using Microsoft.Extensions.Configuration;
using MongoDB.Driver;
using System;
using Xunit;
namespace Steeltoe.CloudFoundry.ConnectorAutofac.Test
{
public class MongoDbContainerBuilderExtensionsTest
{
[Fact]
public void RegisterMongoDb_Requires_Builder()
{
// arrange
IConfiguration config = new ConfigurationBuilder().Build();
// act & assert
Assert.Throws<ArgumentNullException>(() => MongoDbContainerBuilderExtensions.RegisterMongoDbConnection(null, config));
}
[Fact]
public void RegisterMongoDb_Requires_Config()
{
// arrange
ContainerBuilder cb = new ContainerBuilder();
// act & assert
Assert.Throws<ArgumentNullException>(() => MongoDbContainerBuilderExtensions.RegisterMongoDbConnection(cb, null));
}
[Fact]
public void RegisterMongoDb_AddsTypesToContainer()
{
// arrange
ContainerBuilder container = new ContainerBuilder();
IConfiguration config = new ConfigurationBuilder().Build();
// act
var regBuilder = container.RegisterMongoDbConnection(config);
var services = container.Build();
var mongoClient = services.Resolve<MongoClient>();
var iMongoClient = services.Resolve<IMongoClient>();
var mongoUrl = services.Resolve<MongoUrl>();
// assert
Assert.NotNull(mongoClient);
Assert.NotNull(iMongoClient);
Assert.NotNull(mongoUrl);
Assert.Equal(typeof(MongoClient).FullName, mongoClient.GetType().FullName);
Assert.Equal(typeof(MongoClient).FullName, iMongoClient.GetType().FullName);
Assert.Equal(typeof(MongoUrl).FullName, mongoUrl.GetType().FullName);
}
}
}

Просмотреть файл

@ -6,6 +6,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Caching.Redis" Version="$(AspNetCoreTestVersion)" />
<PackageReference Include="MongoDB.Driver" Version="2.5.0" />
<PackageReference Include="Npgsql" Version="$(NpgsqlVersion)" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="$(PomeloEFCoreVersion)" />
<PackageReference Include="RabbitMQ.Client" Version="$(RabbitClientVersion)" />