From 66a39bc4e2bbd97f3f5ee9082072e5ebf64bb5f6 Mon Sep 17 00:00:00 2001 From: Juliette Ruchel Date: Wed, 27 Jan 2021 12:19:55 -0300 Subject: [PATCH 1/5] Created extension --- .../Extensions/ServiceCollectionExtension.cs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 Analytics/Extensions/ServiceCollectionExtension.cs diff --git a/Analytics/Extensions/ServiceCollectionExtension.cs b/Analytics/Extensions/ServiceCollectionExtension.cs new file mode 100644 index 00000000..e19fc0ff --- /dev/null +++ b/Analytics/Extensions/ServiceCollectionExtension.cs @@ -0,0 +1,16 @@ +using Microsoft.Extensions.DependencyInjection; +using System; +using System.Collections.Generic; +using System.Text; + +namespace Segment.Extensions +{ + public static class ServiceCollectionExtension + { + public static void AddAnalytics(this IServiceCollection services, string writeKey, Config config = null) + { + var client = new Client(writeKey, config); + services.AddSingleton(client); + } + } +} From 7d9a99f0307f9bd2fb53ad1092592dd7fd847880 Mon Sep 17 00:00:00 2001 From: Juliette Ruchel Date: Wed, 27 Jan 2021 12:41:29 -0300 Subject: [PATCH 2/5] Fixed extension --- Analytics/Analytics.csproj | 14 +++++ .../Extensions/ServiceCollectionExtension.cs | 16 ++++-- .../ServiceCollectionExtensionTests.cs | 51 +++++++++++++++++++ Test/Test.csproj | 1 + 4 files changed, 78 insertions(+), 4 deletions(-) create mode 100644 Test/Extensions/ServiceCollectionExtensionTests.cs diff --git a/Analytics/Analytics.csproj b/Analytics/Analytics.csproj index 28231b72..4660af64 100644 --- a/Analytics/Analytics.csproj +++ b/Analytics/Analytics.csproj @@ -82,4 +82,18 @@ + + + + 2.0.0 + + + + + + + 2.0.0 + + + diff --git a/Analytics/Extensions/ServiceCollectionExtension.cs b/Analytics/Extensions/ServiceCollectionExtension.cs index e19fc0ff..ecd1f139 100644 --- a/Analytics/Extensions/ServiceCollectionExtension.cs +++ b/Analytics/Extensions/ServiceCollectionExtension.cs @@ -1,16 +1,24 @@ +#if NETSTANDARD2_0 using Microsoft.Extensions.DependencyInjection; -using System; -using System.Collections.Generic; -using System.Text; +#endif namespace Segment.Extensions { public static class ServiceCollectionExtension { +#if NETSTANDARD2_0 public static void AddAnalytics(this IServiceCollection services, string writeKey, Config config = null) { - var client = new Client(writeKey, config); + Config configuration; + + if(config == null) + configuration = new Config(); + else + configuration = config; + + var client = new Client(writeKey, configuration); services.AddSingleton(client); } +#endif } } diff --git a/Test/Extensions/ServiceCollectionExtensionTests.cs b/Test/Extensions/ServiceCollectionExtensionTests.cs new file mode 100644 index 00000000..37dd6bf6 --- /dev/null +++ b/Test/Extensions/ServiceCollectionExtensionTests.cs @@ -0,0 +1,51 @@ +using Microsoft.Extensions.DependencyInjection; +using NUnit.Framework; +using System; +using System.Collections.Generic; +using System.Text; +using Segment.Extensions; + + +namespace Segment.Test.Extensions +{ + [TestFixture] + public class ServiceCollectionExtensionTests + { + + [Test] + public void TestInicializationOfClientWithServicesCollection() + { + var writeKey = "writeKey"; + var services = new ServiceCollection(); + services.AddAnalytics(writeKey); + + var provider = services.BuildServiceProvider(); + var analyticsInstance = provider.GetRequiredService(typeof(IAnalyticsClient)); + + Assert.IsNotNull(analyticsInstance); + + var client = analyticsInstance as Client; + Assert.AreEqual("writeKey", client.WriteKey); + } + + [Test] + public void TestInicializationOfClientWithServicesCollectionAndConfig() + { + var writeKey = "writeKey"; + var threadCount = 10; + var config = new Config { Threads = threadCount }; + + var services = new ServiceCollection(); + services.AddAnalytics(writeKey, config); + + var provider = services.BuildServiceProvider(); + var analyticsInstance = provider.GetRequiredService(typeof(IAnalyticsClient)); + + Assert.IsNotNull(analyticsInstance); + + var client = analyticsInstance as Client; + Assert.AreEqual("writeKey", client.WriteKey); + Assert.AreEqual(threadCount, client.Config.Threads); + } + } +} diff --git a/Test/Test.csproj b/Test/Test.csproj index cb4bf904..57d48387 100644 --- a/Test/Test.csproj +++ b/Test/Test.csproj @@ -9,6 +9,7 @@ + From 4fe928daebed3717f30e64e0a99c6c155c8eeaab Mon Sep 17 00:00:00 2001 From: Juliette Ruchel Date: Wed, 27 Jan 2021 12:46:23 -0300 Subject: [PATCH 3/5] Added tests --- .../Extensions/ServiceCollectionExtension.cs | 4 +- .../ServiceCollectionExtensionTests.cs | 51 +++++++++++++++++++ Test.NetStandard20/Test.NetStandard20.csproj | 1 + 3 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 Test.NetStandard20/Extensions/ServiceCollectionExtensionTests.cs diff --git a/Analytics/Extensions/ServiceCollectionExtension.cs b/Analytics/Extensions/ServiceCollectionExtension.cs index ecd1f139..c9e87078 100644 --- a/Analytics/Extensions/ServiceCollectionExtension.cs +++ b/Analytics/Extensions/ServiceCollectionExtension.cs @@ -1,4 +1,4 @@ -#if NETSTANDARD2_0 +#if NETSTANDARD2_0 || NET461 using Microsoft.Extensions.DependencyInjection; #endif @@ -6,7 +6,7 @@ namespace Segment.Extensions { public static class ServiceCollectionExtension { -#if NETSTANDARD2_0 +#if NETSTANDARD2_0 || NET461 public static void AddAnalytics(this IServiceCollection services, string writeKey, Config config = null) { Config configuration; diff --git a/Test.NetStandard20/Extensions/ServiceCollectionExtensionTests.cs b/Test.NetStandard20/Extensions/ServiceCollectionExtensionTests.cs new file mode 100644 index 00000000..37dd6bf6 --- /dev/null +++ b/Test.NetStandard20/Extensions/ServiceCollectionExtensionTests.cs @@ -0,0 +1,51 @@ +using Microsoft.Extensions.DependencyInjection; +using NUnit.Framework; +using System; +using System.Collections.Generic; +using System.Text; +using Segment.Extensions; + + +namespace Segment.Test.Extensions +{ + [TestFixture] + public class ServiceCollectionExtensionTests + { + + [Test] + public void TestInicializationOfClientWithServicesCollection() + { + var writeKey = "writeKey"; + var services = new ServiceCollection(); + services.AddAnalytics(writeKey); + + var provider = services.BuildServiceProvider(); + var analyticsInstance = provider.GetRequiredService(typeof(IAnalyticsClient)); + + Assert.IsNotNull(analyticsInstance); + + var client = analyticsInstance as Client; + Assert.AreEqual("writeKey", client.WriteKey); + } + + [Test] + public void TestInicializationOfClientWithServicesCollectionAndConfig() + { + var writeKey = "writeKey"; + var threadCount = 10; + var config = new Config { Threads = threadCount }; + + var services = new ServiceCollection(); + services.AddAnalytics(writeKey, config); + + var provider = services.BuildServiceProvider(); + var analyticsInstance = provider.GetRequiredService(typeof(IAnalyticsClient)); + + Assert.IsNotNull(analyticsInstance); + + var client = analyticsInstance as Client; + Assert.AreEqual("writeKey", client.WriteKey); + Assert.AreEqual(threadCount, client.Config.Threads); + } + } +} diff --git a/Test.NetStandard20/Test.NetStandard20.csproj b/Test.NetStandard20/Test.NetStandard20.csproj index aa332bfb..8fa73a7f 100644 --- a/Test.NetStandard20/Test.NetStandard20.csproj +++ b/Test.NetStandard20/Test.NetStandard20.csproj @@ -9,6 +9,7 @@ + From 4fbf71f6ea549ed8ce26e4bfbe79f14d80afab28 Mon Sep 17 00:00:00 2001 From: Juliette Ruchel Date: Thu, 4 Feb 2021 08:46:39 -0300 Subject: [PATCH 4/5] added metadata info --- Analytics/Analytics.csproj | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Analytics/Analytics.csproj b/Analytics/Analytics.csproj index 28231b72..50ff02e5 100644 --- a/Analytics/Analytics.csproj +++ b/Analytics/Analytics.csproj @@ -15,6 +15,9 @@ analytics, Segment packageIcon.png README.md + MIT + https://github.com/segmentio/Analytics.NET.git + git From facc64c75d7d66d4d057d7e6a925b9c222355445 Mon Sep 17 00:00:00 2001 From: Juliette Ruchel Date: Mon, 8 Feb 2021 10:38:11 -0300 Subject: [PATCH 5/5] removed packageLicenseFile --- Analytics/Analytics.csproj | 1 - 1 file changed, 1 deletion(-) diff --git a/Analytics/Analytics.csproj b/Analytics/Analytics.csproj index 50ff02e5..e394d50b 100644 --- a/Analytics/Analytics.csproj +++ b/Analytics/Analytics.csproj @@ -14,7 +14,6 @@ Check out the changelog at: https://raw.githubusercontent.com/segmentio/Analytics.NET/master/CHANGELOG.md analytics, Segment packageIcon.png - README.md MIT https://github.com/segmentio/Analytics.NET.git git