diff --git a/Connectors.sln b/Connectors.sln index 1ca4df6..ba78bf2 100644 --- a/Connectors.sln +++ b/Connectors.sln @@ -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} diff --git a/src/Steeltoe.CloudFoundry.Connector.EF6Autofac/MySqlDbContextContainerBuilderExtensions.cs b/src/Steeltoe.CloudFoundry.Connector.EF6Autofac/MySqlDbContextContainerBuilderExtensions.cs new file mode 100644 index 0000000..195f3c7 --- /dev/null +++ b/src/Steeltoe.CloudFoundry.Connector.EF6Autofac/MySqlDbContextContainerBuilderExtensions.cs @@ -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 + { + /// + /// Add your MySql-based DbContext to the ContainerBuilder + /// + /// Your DbContext + /// Autofac + /// Your app config + /// Name of service instance + /// + public static IRegistrationBuilder RegisterDbContext(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() + : config.GetRequiredServiceInfo(serviceName); + + var mySqlConfig = new MySqlProviderConnectorOptions(config); + var factory = new MySqlProviderConnectorFactory(info, mySqlConfig, typeof(TContext)); + return container.Register(c => factory.Create(null)).As(); + } + } +} \ No newline at end of file diff --git a/src/Steeltoe.CloudFoundry.Connector.EF6Autofac/SqlServerDbContextContainerBuilderExtensions.cs b/src/Steeltoe.CloudFoundry.Connector.EF6Autofac/SqlServerDbContextContainerBuilderExtensions.cs new file mode 100644 index 0000000..7cd31d6 --- /dev/null +++ b/src/Steeltoe.CloudFoundry.Connector.EF6Autofac/SqlServerDbContextContainerBuilderExtensions.cs @@ -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 + { + /// + /// Add your SqlServer-based DbContext to the ContainerBuilder + /// + /// Your DbContext + /// Autofac + /// Your app config + /// Name of service instance + /// + public static IRegistrationBuilder RegisterDbContext(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() + : config.GetRequiredServiceInfo(serviceName); + + var sqlServerConfig = new SqlServerProviderConnectorOptions(config); + var factory = new SqlServerProviderConnectorFactory(info, sqlServerConfig, typeof(TContext)); + return container.Register(c => factory.Create(null)).As(); + } + } +} diff --git a/src/Steeltoe.CloudFoundry.Connector.EF6Autofac/Steeltoe.CloudFoundry.Connector.EF6Autofac.csproj b/src/Steeltoe.CloudFoundry.Connector.EF6Autofac/Steeltoe.CloudFoundry.Connector.EF6Autofac.csproj new file mode 100644 index 0000000..57a7359 --- /dev/null +++ b/src/Steeltoe.CloudFoundry.Connector.EF6Autofac/Steeltoe.CloudFoundry.Connector.EF6Autofac.csproj @@ -0,0 +1,40 @@ + + + + CloudFoundry Connector Extensions for Entity Framework 6 with Autofac + $(SteeltoeVersion) + $(VersionSuffix) + Pivotal;dtillman + netstandard2.0 + Steeltoe.CloudFoundry.Connector.EF6Autofac + Steeltoe.CloudFoundry.Connector.EF6Autofac + CloudFoundry;ASPNET;Autofac;EntityFramework + https://steeltoe.io/images/transparent.png + https://steeltoe.io + https://www.apache.org/licenses/LICENSE-2.0 + + + bin\$(Configuration)\$(TargetFramework)\Steeltoe.CloudFoundry.Connector.EF6Autofac.xml + + + + + + + + All + + + + + + + SA1101;SA1124;SA1201;SA1309;SA1310;SA1401;SA1600;SA1652;1591 + + + + stylecop.json + Always + + + \ No newline at end of file diff --git a/test/Steeltoe.CloudFoundry.Connector.EF6Autofac.Test/GoodMySqlDbContext.cs b/test/Steeltoe.CloudFoundry.Connector.EF6Autofac.Test/GoodMySqlDbContext.cs new file mode 100644 index 0000000..1070519 --- /dev/null +++ b/test/Steeltoe.CloudFoundry.Connector.EF6Autofac.Test/GoodMySqlDbContext.cs @@ -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) + { + } + } +} diff --git a/test/Steeltoe.CloudFoundry.Connector.EF6Autofac.Test/GoodSqlServerDbContext.cs b/test/Steeltoe.CloudFoundry.Connector.EF6Autofac.Test/GoodSqlServerDbContext.cs new file mode 100644 index 0000000..7f82d4a --- /dev/null +++ b/test/Steeltoe.CloudFoundry.Connector.EF6Autofac.Test/GoodSqlServerDbContext.cs @@ -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) + { + } + } +} diff --git a/test/Steeltoe.CloudFoundry.Connector.EF6Autofac.Test/MySqlDbContextContainerBuilderExtensionsTest.cs b/test/Steeltoe.CloudFoundry.Connector.EF6Autofac.Test/MySqlDbContextContainerBuilderExtensionsTest.cs new file mode 100644 index 0000000..1abbb32 --- /dev/null +++ b/test/Steeltoe.CloudFoundry.Connector.EF6Autofac.Test/MySqlDbContextContainerBuilderExtensionsTest.cs @@ -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(() => MySqlDbContextContainerBuilderExtensions.RegisterDbContext(null, config)); + } + + [Fact] + public void RegisterMySqlDbContext_Requires_Config() + { + // arrange + var cb = new ContainerBuilder(); + + // act & assert + Assert.Throws(() => MySqlDbContextContainerBuilderExtensions.RegisterDbContext(cb, null)); + } + + [Fact] + public void RegisterMySqlDbContext_AddsToContainer() + { + // arrange + var container = new ContainerBuilder(); + IConfiguration config = new ConfigurationBuilder().Build(); + + // act + var regBuilder = MySqlDbContextContainerBuilderExtensions.RegisterDbContext(container, config); + var services = container.Build(); + var dbConn = services.Resolve(); + + // assert + Assert.NotNull(dbConn); + Assert.IsType(dbConn); + } + } +} diff --git a/test/Steeltoe.CloudFoundry.Connector.EF6Autofac.Test/SqlServerDbContextContainerBuilderExtensionsTest.cs b/test/Steeltoe.CloudFoundry.Connector.EF6Autofac.Test/SqlServerDbContextContainerBuilderExtensionsTest.cs new file mode 100644 index 0000000..1051f32 --- /dev/null +++ b/test/Steeltoe.CloudFoundry.Connector.EF6Autofac.Test/SqlServerDbContextContainerBuilderExtensionsTest.cs @@ -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(() => SqlServerDbContextContainerBuilderExtensions.RegisterDbContext(null, config)); + } + + [Fact] + public void RegisterMySqlDbContext_Requires_Config() + { + // arrange + var cb = new ContainerBuilder(); + + // act & assert + Assert.Throws(() => SqlServerDbContextContainerBuilderExtensions.RegisterDbContext(cb, null)); + } + + [Fact] + public void RegisterMySqlDbContext_AddsToContainer() + { + // arrange + var container = new ContainerBuilder(); + IConfiguration config = new ConfigurationBuilder().Build(); + + // act + var regBuilder = SqlServerDbContextContainerBuilderExtensions.RegisterDbContext(container, config); + var services = container.Build(); + var dbConn = services.Resolve(); + + // assert + Assert.NotNull(dbConn); + Assert.IsType(dbConn); + } + } +} diff --git a/test/Steeltoe.CloudFoundry.Connector.EF6Autofac.Test/Steeltoe.CloudFoundry.Connector.EF6Autofac.Test.csproj b/test/Steeltoe.CloudFoundry.Connector.EF6Autofac.Test/Steeltoe.CloudFoundry.Connector.EF6Autofac.Test.csproj new file mode 100644 index 0000000..27cdf39 --- /dev/null +++ b/test/Steeltoe.CloudFoundry.Connector.EF6Autofac.Test/Steeltoe.CloudFoundry.Connector.EF6Autofac.Test.csproj @@ -0,0 +1,42 @@ + + + + net461 + + + + + PreserveNewest + + + + + SA1101;SA1124;SA1201;SA1309;SA1310;SA1401;SA1600;SA1652;1591 + + + + + + + + + + + + + + + + All + + + + + + stylecop.json + Always + + + + + \ No newline at end of file diff --git a/test/Steeltoe.CloudFoundry.Connector.EF6Autofac.Test/xunit.runner.json b/test/Steeltoe.CloudFoundry.Connector.EF6Autofac.Test/xunit.runner.json new file mode 100644 index 0000000..4c62fd7 --- /dev/null +++ b/test/Steeltoe.CloudFoundry.Connector.EF6Autofac.Test/xunit.runner.json @@ -0,0 +1,4 @@ +{ + "maxParallelThreads": 1, + "parallelizeTestCollections": false +} \ No newline at end of file