Support for EF6 + Autofac [delivers #155996598]

This commit is contained in:
Tim Hess 2018-06-15 23:24:22 -05:00
Родитель 6d76fb9a1f
Коммит 43593cabb2
10 изменённых файлов: 400 добавлений и 0 удалений

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

@ -42,6 +42,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Steeltoe.CloudFoundry.Conne
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Steeltoe.CloudFoundry.ConnectorAutofac.Test", "test\Steeltoe.CloudFoundry.ConnectorAutofac.Test\Steeltoe.CloudFoundry.ConnectorAutofac.Test.csproj", "{59F8BEF1-D25D-4B18-A060-97E4C5B37600}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Steeltoe.CloudFoundry.Connector.EF6Autofac", "src\Steeltoe.CloudFoundry.Connector.EF6Autofac\Steeltoe.CloudFoundry.Connector.EF6Autofac.csproj", "{58BA19D1-5AA9-4AEC-8D5C-08834A9622A7}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Steeltoe.CloudFoundry.Connector.EF6Autofac.Test", "test\Steeltoe.CloudFoundry.Connector.EF6Autofac.Test\Steeltoe.CloudFoundry.Connector.EF6Autofac.Test.csproj", "{B1CCD2A8-856E-4582-9261-47F537A81B44}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -172,6 +176,30 @@ Global
{59F8BEF1-D25D-4B18-A060-97E4C5B37600}.Release|x64.Build.0 = Release|Any CPU
{59F8BEF1-D25D-4B18-A060-97E4C5B37600}.Release|x86.ActiveCfg = Release|Any CPU
{59F8BEF1-D25D-4B18-A060-97E4C5B37600}.Release|x86.Build.0 = Release|Any CPU
{58BA19D1-5AA9-4AEC-8D5C-08834A9622A7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{58BA19D1-5AA9-4AEC-8D5C-08834A9622A7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{58BA19D1-5AA9-4AEC-8D5C-08834A9622A7}.Debug|x64.ActiveCfg = Debug|Any CPU
{58BA19D1-5AA9-4AEC-8D5C-08834A9622A7}.Debug|x64.Build.0 = Debug|Any CPU
{58BA19D1-5AA9-4AEC-8D5C-08834A9622A7}.Debug|x86.ActiveCfg = Debug|Any CPU
{58BA19D1-5AA9-4AEC-8D5C-08834A9622A7}.Debug|x86.Build.0 = Debug|Any CPU
{58BA19D1-5AA9-4AEC-8D5C-08834A9622A7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{58BA19D1-5AA9-4AEC-8D5C-08834A9622A7}.Release|Any CPU.Build.0 = Release|Any CPU
{58BA19D1-5AA9-4AEC-8D5C-08834A9622A7}.Release|x64.ActiveCfg = Release|Any CPU
{58BA19D1-5AA9-4AEC-8D5C-08834A9622A7}.Release|x64.Build.0 = Release|Any CPU
{58BA19D1-5AA9-4AEC-8D5C-08834A9622A7}.Release|x86.ActiveCfg = Release|Any CPU
{58BA19D1-5AA9-4AEC-8D5C-08834A9622A7}.Release|x86.Build.0 = Release|Any CPU
{B1CCD2A8-856E-4582-9261-47F537A81B44}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B1CCD2A8-856E-4582-9261-47F537A81B44}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B1CCD2A8-856E-4582-9261-47F537A81B44}.Debug|x64.ActiveCfg = Debug|Any CPU
{B1CCD2A8-856E-4582-9261-47F537A81B44}.Debug|x64.Build.0 = Debug|Any CPU
{B1CCD2A8-856E-4582-9261-47F537A81B44}.Debug|x86.ActiveCfg = Debug|Any CPU
{B1CCD2A8-856E-4582-9261-47F537A81B44}.Debug|x86.Build.0 = Debug|Any CPU
{B1CCD2A8-856E-4582-9261-47F537A81B44}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B1CCD2A8-856E-4582-9261-47F537A81B44}.Release|Any CPU.Build.0 = Release|Any CPU
{B1CCD2A8-856E-4582-9261-47F537A81B44}.Release|x64.ActiveCfg = Release|Any CPU
{B1CCD2A8-856E-4582-9261-47F537A81B44}.Release|x64.Build.0 = Release|Any CPU
{B1CCD2A8-856E-4582-9261-47F537A81B44}.Release|x86.ActiveCfg = Release|Any CPU
{B1CCD2A8-856E-4582-9261-47F537A81B44}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@ -187,6 +215,8 @@ Global
{B40DE919-FBC0-4CC6-B36F-98364FCA4203} = {1CEE3AF7-DC06-4BBE-B28D-62FB1BD4A7A5}
{73EEC113-97AC-4F74-8099-2C0EDEAA9596} = {9BB0395E-B595-4B95-89ED-F55AF12B4EE7}
{59F8BEF1-D25D-4B18-A060-97E4C5B37600} = {1CEE3AF7-DC06-4BBE-B28D-62FB1BD4A7A5}
{58BA19D1-5AA9-4AEC-8D5C-08834A9622A7} = {9BB0395E-B595-4B95-89ED-F55AF12B4EE7}
{B1CCD2A8-856E-4582-9261-47F537A81B44} = {1CEE3AF7-DC06-4BBE-B28D-62FB1BD4A7A5}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {112EBBFA-82B9-483E-B258-8611B70B3A3A}

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

@ -0,0 +1,55 @@
// 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.MySql;
using Steeltoe.CloudFoundry.Connector.Services;
using System;
namespace Steeltoe.CloudFoundry.Connector.EF6Autofac
{
public static class MySqlDbContextContainerBuilderExtensions
{
/// <summary>
/// Add your MySql-based DbContext to the ContainerBuilder
/// </summary>
/// <typeparam name="TContext">Your DbContext</typeparam>
/// <param name="container">Autofac <see cref="ContainerBuilder" /></param>
/// <param name="config">Your app config</param>
/// <param name="serviceName">Name of service instance</param>
/// <returns><see cref="IRegistrationBuilder{TLimit, TActivatorData, TRegistrationStyle}"/></returns>
public static IRegistrationBuilder<object, SimpleActivatorData, SingleRegistrationStyle> RegisterDbContext<TContext>(this ContainerBuilder container, IConfiguration config, string serviceName = null)
{
if (container == null)
{
throw new ArgumentNullException(nameof(container));
}
if (config == null)
{
throw new ArgumentNullException(nameof(config));
}
MySqlServiceInfo info = serviceName == null
? config.GetSingletonServiceInfo<MySqlServiceInfo>()
: config.GetRequiredServiceInfo<MySqlServiceInfo>(serviceName);
var mySqlConfig = new MySqlProviderConnectorOptions(config);
var factory = new MySqlProviderConnectorFactory(info, mySqlConfig, typeof(TContext));
return container.Register(c => factory.Create(null)).As<TContext>();
}
}
}

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

@ -0,0 +1,55 @@
// 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.Services;
using Steeltoe.CloudFoundry.Connector.SqlServer;
using System;
namespace Steeltoe.CloudFoundry.Connector.EF6Autofac
{
public static class SqlServerDbContextContainerBuilderExtensions
{
/// <summary>
/// Add your SqlServer-based DbContext to the ContainerBuilder
/// </summary>
/// <typeparam name="TContext">Your DbContext</typeparam>
/// <param name="container">Autofac <see cref="ContainerBuilder" /></param>
/// <param name="config">Your app config</param>
/// <param name="serviceName">Name of service instance</param>
/// <returns><see cref="IRegistrationBuilder{TLimit, TActivatorData, TRegistrationStyle}"/></returns>
public static IRegistrationBuilder<object, SimpleActivatorData, SingleRegistrationStyle> RegisterDbContext<TContext>(this ContainerBuilder container, IConfiguration config, string serviceName = null)
{
if (container == null)
{
throw new ArgumentNullException(nameof(container));
}
if (config == null)
{
throw new ArgumentNullException(nameof(config));
}
SqlServerServiceInfo info = serviceName == null
? config.GetSingletonServiceInfo<SqlServerServiceInfo>()
: config.GetRequiredServiceInfo<SqlServerServiceInfo>(serviceName);
var sqlServerConfig = new SqlServerProviderConnectorOptions(config);
var factory = new SqlServerProviderConnectorFactory(info, sqlServerConfig, typeof(TContext));
return container.Register(c => factory.Create(null)).As<TContext>();
}
}
}

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

@ -0,0 +1,40 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\..\versions.props" />
<PropertyGroup>
<Description>CloudFoundry Connector Extensions for Entity Framework 6 with Autofac</Description>
<VersionPrefix>$(SteeltoeVersion)</VersionPrefix>
<VersionSuffix>$(VersionSuffix)</VersionSuffix>
<Authors>Pivotal;dtillman</Authors>
<TargetFramework>netstandard2.0</TargetFramework>
<AssemblyName>Steeltoe.CloudFoundry.Connector.EF6Autofac</AssemblyName>
<PackageId>Steeltoe.CloudFoundry.Connector.EF6Autofac</PackageId>
<PackageTags>CloudFoundry;ASPNET;Autofac;EntityFramework</PackageTags>
<PackageIconUrl>https://steeltoe.io/images/transparent.png</PackageIconUrl>
<PackageProjectUrl>https://steeltoe.io</PackageProjectUrl>
<PackageLicenseUrl>https://www.apache.org/licenses/LICENSE-2.0</PackageLicenseUrl>
</PropertyGroup>
<PropertyGroup>
<DocumentationFile>bin\$(Configuration)\$(TargetFramework)\Steeltoe.CloudFoundry.Connector.EF6Autofac.xml</DocumentationFile>
</PropertyGroup>
<ItemGroup Condition="'$(CI_BUILD)' == ''">
<ProjectReference Include="..\Steeltoe.CloudFoundry.ConnectorBase\Steeltoe.CloudFoundry.ConnectorBase.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Autofac" Version="$(AutofacVersion)" />
<PackageReference Include="StyleCop.Analyzers" Version="$(StyleCopVersion)">
<PrivateAssets>All</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup Condition="'$(CI_BUILD)' == 'True'">
<PackageReference Include="Steeltoe.CloudFoundry.ConnectorBase" Version="$(SteeltoeVersion)$(SteeltoeVersionSuffix)" />
</ItemGroup>
<PropertyGroup>
<NoWarn>SA1101;SA1124;SA1201;SA1309;SA1310;SA1401;SA1600;SA1652;1591</NoWarn>
</PropertyGroup>
<ItemGroup>
<AdditionalFiles Include="..\..\stylecop.json">
<Link>stylecop.json</Link>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</AdditionalFiles>
</ItemGroup>
</Project>

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

@ -0,0 +1,27 @@
// 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 MySql.Data.Entity;
using System.Data.Entity;
namespace Steeltoe.CloudFoundry.Connector.EF6Autofac.Test
{
[DbConfigurationType(typeof(MySqlEFConfiguration))]
public class GoodMySqlDbContext : DbContext
{
public GoodMySqlDbContext(string str)
{
}
}
}

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

@ -0,0 +1,25 @@
// 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 System.Data.Entity;
namespace Steeltoe.CloudFoundry.Connector.EF6Autofac.Test
{
public class GoodSqlServerDbContext : DbContext
{
public GoodSqlServerDbContext(string str)
{
}
}
}

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

@ -0,0 +1,61 @@
// 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 System;
using Xunit;
namespace Steeltoe.CloudFoundry.Connector.EF6Autofac.Test
{
public class MySqlDbContextContainerBuilderExtensionsTest
{
[Fact]
public void RegisterMySqlDbContext_Requires_Builder()
{
// arrange
IConfiguration config = new ConfigurationBuilder().Build();
// act & assert
Assert.Throws<ArgumentNullException>(() => MySqlDbContextContainerBuilderExtensions.RegisterDbContext<GoodMySqlDbContext>(null, config));
}
[Fact]
public void RegisterMySqlDbContext_Requires_Config()
{
// arrange
var cb = new ContainerBuilder();
// act & assert
Assert.Throws<ArgumentNullException>(() => MySqlDbContextContainerBuilderExtensions.RegisterDbContext<GoodMySqlDbContext>(cb, null));
}
[Fact]
public void RegisterMySqlDbContext_AddsToContainer()
{
// arrange
var container = new ContainerBuilder();
IConfiguration config = new ConfigurationBuilder().Build();
// act
var regBuilder = MySqlDbContextContainerBuilderExtensions.RegisterDbContext<GoodMySqlDbContext>(container, config);
var services = container.Build();
var dbConn = services.Resolve<GoodMySqlDbContext>();
// assert
Assert.NotNull(dbConn);
Assert.IsType<GoodMySqlDbContext>(dbConn);
}
}
}

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

@ -0,0 +1,61 @@
// 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 System;
using Xunit;
namespace Steeltoe.CloudFoundry.Connector.EF6Autofac.Test
{
public class SqlServerDbContextContainerBuilderExtensionsTest
{
[Fact]
public void RegisterSqlServerDbContext_Requires_Builder()
{
// arrange
IConfiguration config = new ConfigurationBuilder().Build();
// act & assert
Assert.Throws<ArgumentNullException>(() => SqlServerDbContextContainerBuilderExtensions.RegisterDbContext<GoodSqlServerDbContext>(null, config));
}
[Fact]
public void RegisterMySqlDbContext_Requires_Config()
{
// arrange
var cb = new ContainerBuilder();
// act & assert
Assert.Throws<ArgumentNullException>(() => SqlServerDbContextContainerBuilderExtensions.RegisterDbContext<GoodSqlServerDbContext>(cb, null));
}
[Fact]
public void RegisterMySqlDbContext_AddsToContainer()
{
// arrange
var container = new ContainerBuilder();
IConfiguration config = new ConfigurationBuilder().Build();
// act
var regBuilder = SqlServerDbContextContainerBuilderExtensions.RegisterDbContext<GoodSqlServerDbContext>(container, config);
var services = container.Build();
var dbConn = services.Resolve<GoodSqlServerDbContext>();
// assert
Assert.NotNull(dbConn);
Assert.IsType<GoodSqlServerDbContext>(dbConn);
}
}
}

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

@ -0,0 +1,42 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\..\versions.props" />
<PropertyGroup>
<TargetFrameworks>net461</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<None Update="xunit.runner.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<PropertyGroup>
<NoWarn>SA1101;SA1124;SA1201;SA1309;SA1310;SA1401;SA1600;SA1652;1591</NoWarn>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\Steeltoe.CloudFoundry.Connector.EF6Autofac\Steeltoe.CloudFoundry.Connector.EF6Autofac.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="EntityFramework" Version="$(EF6Version)" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(TestSdkVersion)" />
<PackageReference Include="MySql.Data.Entity" Version="$(MySqlVersion)" />
<PackageReference Include="xunit" Version="$(XunitVersion)" />
<PackageReference Include="xunit.runner.visualstudio" Version="$(XunitStudioVersion)" />
<DotNetCliToolReference Include="dotnet-xunit" Version="$(XunitVersion)" />
<PackageReference Include="StyleCop.Analyzers" Version="$(StyleCopVersion)">
<PrivateAssets>All</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<AdditionalFiles Include="..\..\stylecop.json">
<Link>stylecop.json</Link>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</AdditionalFiles>
</ItemGroup>
</Project>

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

@ -0,0 +1,4 @@
{
"maxParallelThreads": 1,
"parallelizeTestCollections": false
}