From bb23e6193e95e8e410621eb2a7b64027f28b1752 Mon Sep 17 00:00:00 2001 From: Denis Nutiu Date: Sun, 30 May 2021 21:16:01 +0300 Subject: [PATCH] Inject MongoClient as a dependency in FeedbackReceiverService. --- .../Services/TestFeedbackReceiverService.cs | 6 ++++++ Retroactiune.WebAPI/Services/FeedbackReceiverService.cs | 6 +----- Retroactiune.WebAPI/Startup.cs | 7 +++++++ Retroactiune.WebAPI/TestingStartup.cs | 8 +++++++- 4 files changed, 21 insertions(+), 6 deletions(-) create mode 100644 Retroactiune.UnitTests/Retroactiune.WebAPI/Services/TestFeedbackReceiverService.cs diff --git a/Retroactiune.UnitTests/Retroactiune.WebAPI/Services/TestFeedbackReceiverService.cs b/Retroactiune.UnitTests/Retroactiune.WebAPI/Services/TestFeedbackReceiverService.cs new file mode 100644 index 0000000..8cc8643 --- /dev/null +++ b/Retroactiune.UnitTests/Retroactiune.WebAPI/Services/TestFeedbackReceiverService.cs @@ -0,0 +1,6 @@ +namespace Retroactiune.Tests.Retroactiune.WebAPI.Services +{ + public class TestFeedbackReceiverService + { + } +} \ No newline at end of file diff --git a/Retroactiune.WebAPI/Services/FeedbackReceiverService.cs b/Retroactiune.WebAPI/Services/FeedbackReceiverService.cs index e88a329..28e9b1e 100644 --- a/Retroactiune.WebAPI/Services/FeedbackReceiverService.cs +++ b/Retroactiune.WebAPI/Services/FeedbackReceiverService.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.Threading.Tasks; -using Microsoft.Extensions.Logging; using MongoDB.Driver; using Retroactiune.Models; using Retroactiune.Settings; @@ -15,14 +14,11 @@ namespace Retroactiune.Services public class FeedbackReceiverService : IFeedbackReceiverService { private readonly IMongoCollection _collection; - private readonly ILogger _logger; - public FeedbackReceiverService(IMongoDbSettings settings, ILogger logger) + public FeedbackReceiverService(IMongoClient client, IMongoDbSettings settings) { - var client = new MongoClient(settings.ConnectionString); var database = client.GetDatabase(settings.DatabaseName); _collection = database.GetCollection(settings.FeedbackReceiverCollectionName); - _logger = logger; } public async Task CreateManyAsync(IEnumerable items) diff --git a/Retroactiune.WebAPI/Startup.cs b/Retroactiune.WebAPI/Startup.cs index 9a0e57d..20e394e 100644 --- a/Retroactiune.WebAPI/Startup.cs +++ b/Retroactiune.WebAPI/Startup.cs @@ -6,6 +6,7 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; +using MongoDB.Driver; using Retroactiune.Services; using Retroactiune.Settings; @@ -28,11 +29,17 @@ namespace Retroactiune services.AddSingleton(sp => sp.GetRequiredService>().Value); + // AutoMapper services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies()); // Services services.AddSingleton(); + services.AddSingleton(i => + { + var settings = i.GetService>(); + return new MongoClient(settings.Value.ConnectionString); + }); // WebAPI services.AddControllers(); diff --git a/Retroactiune.WebAPI/TestingStartup.cs b/Retroactiune.WebAPI/TestingStartup.cs index 0484d34..2e25b91 100644 --- a/Retroactiune.WebAPI/TestingStartup.cs +++ b/Retroactiune.WebAPI/TestingStartup.cs @@ -3,6 +3,7 @@ using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; +using MongoDB.Driver; using Retroactiune.Services; using Retroactiune.Settings; @@ -33,7 +34,12 @@ namespace Retroactiune // Services services.AddSingleton(); - + services.AddSingleton(i => + { + var settings = i.GetService>(); + return new MongoClient(settings.Value.ConnectionString); + }); + // WebAPI services.AddControllers(); }