Rename IMongoDbSettings to IDatabaseSettings and move related files from Settings to Database directory.

This commit is contained in:
Denis-Cosmin Nutiu 2021-06-11 23:17:03 +03:00
parent 1e2b598f7c
commit c2a4c0e147
12 changed files with 39 additions and 35 deletions

View file

@ -14,10 +14,10 @@ using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using MongoDB.Bson;
using MongoDB.Driver;
using Retroactiune.Database;
using Retroactiune.DataTransferObjects;
using Retroactiune.IntegrationTests.Retroactiune.WebAPI.Fixtures;
using Retroactiune.Models;
using Retroactiune.Settings;
using Xunit;
namespace Retroactiune.IntegrationTests.Retroactiune.WebAPI.Controllers
@ -30,7 +30,7 @@ namespace Retroactiune.IntegrationTests.Retroactiune.WebAPI.Controllers
public TestFeedbackReceiver(WebApiTestingFactory factory)
{
_client = factory.CreateClient();
var dbSettings = factory.Services.GetService<IOptions<RetroactiuneDbSettings>>();
var dbSettings = factory.Services.GetService<IOptions<DatabaseSettings>>();
_mongoDb = new MongoDbFixture(dbSettings);
}

View file

@ -3,20 +3,20 @@ using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.Options;
using MongoDB.Driver;
using Retroactiune.Database;
using Retroactiune.Models;
using Retroactiune.Settings;
namespace Retroactiune.IntegrationTests.Retroactiune.WebAPI.Fixtures
{
public class MongoDbFixture : IAsyncDisposable
{
private readonly IMongoDbSettings _settings;
private readonly IDatabaseSettings _settings;
public IMongoDatabase Database { get; }
public IMongoCollection<FeedbackReceiver> FeedbackReceiverCollection =>
Database.GetCollection<FeedbackReceiver>(_settings.FeedbackReceiverCollectionName);
public MongoDbFixture(IOptions<RetroactiuneDbSettings> options)
public MongoDbFixture(IOptions<DatabaseSettings> options)
{
_settings = options.Value;
var client = new MongoClient(_settings.ConnectionString);

View file

@ -5,9 +5,9 @@ using System.Threading.Tasks;
using AutoFixture.Xunit2;
using MongoDB.Driver;
using Moq;
using Retroactiune.Database;
using Retroactiune.Models;
using Retroactiune.Services;
using Retroactiune.Settings;
using Xunit;
namespace Retroactiune.Tests.Retroactiune.WebAPI.Services
@ -20,7 +20,7 @@ namespace Retroactiune.Tests.Retroactiune.WebAPI.Services
// Arrange
var mongoDatabaseMock = new Mock<IMongoDatabase>();
var mongoClientMock = new Mock<IMongoClient>();
var mongoSettingsMock = new Mock<IMongoDbSettings>();
var mongoSettingsMock = new Mock<IDatabaseSettings>();
var mongoCollectionMock = new Mock<IMongoCollection<FeedbackReceiver>>();
mongoSettingsMock.SetupGet(i => i.DatabaseName).Returns("MyDB");
@ -55,7 +55,7 @@ namespace Retroactiune.Tests.Retroactiune.WebAPI.Services
// Arrange
var mongoDatabaseMock = new Mock<IMongoDatabase>();
var mongoClientMock = new Mock<IMongoClient>();
var mongoSettingsMock = new Mock<IMongoDbSettings>();
var mongoSettingsMock = new Mock<IDatabaseSettings>();
var mongoCollectionMock = new Mock<IMongoCollection<FeedbackReceiver>>();
mongoSettingsMock.SetupGet(i => i.DatabaseName).Returns("MyDB");
@ -96,7 +96,7 @@ namespace Retroactiune.Tests.Retroactiune.WebAPI.Services
// Arrange
var mongoDatabaseMock = new Mock<IMongoDatabase>();
var mongoClientMock = new Mock<IMongoClient>();
var mongoSettingsMock = new Mock<IMongoDbSettings>();
var mongoSettingsMock = new Mock<IDatabaseSettings>();
var mongoCollectionMock = new Mock<IMongoCollection<FeedbackReceiver>>();
mongoSettingsMock.SetupGet(i => i.DatabaseName).Returns("MyDB");
@ -137,7 +137,7 @@ namespace Retroactiune.Tests.Retroactiune.WebAPI.Services
// Arrange
var mongoDatabaseMock = new Mock<IMongoDatabase>();
var mongoClientMock = new Mock<IMongoClient>();
var mongoSettingsMock = new Mock<IMongoDbSettings>();
var mongoSettingsMock = new Mock<IDatabaseSettings>();
var mongoCollectionMock = new Mock<IMongoCollection<FeedbackReceiver>>();
mongoSettingsMock.SetupGet(i => i.DatabaseName).Returns("MyDB");
@ -172,7 +172,7 @@ namespace Retroactiune.Tests.Retroactiune.WebAPI.Services
// Arrange
var mongoDatabaseMock = new Mock<IMongoDatabase>();
var mongoClientMock = new Mock<IMongoClient>();
var mongoSettingsMock = new Mock<IMongoDbSettings>();
var mongoSettingsMock = new Mock<IDatabaseSettings>();
var mongoCollectionMock = new Mock<IMongoCollection<FeedbackReceiver>>();
var mongoCursorMock = new Mock<IAsyncCursor<FeedbackReceiver>>();

View file

@ -3,9 +3,9 @@ using System.Threading;
using System.Threading.Tasks;
using MongoDB.Driver;
using Moq;
using Retroactiune.Database;
using Retroactiune.Models;
using Retroactiune.Services;
using Retroactiune.Settings;
using Xunit;
namespace Retroactiune.Tests.Retroactiune.WebAPI.Services
@ -18,7 +18,7 @@ namespace Retroactiune.Tests.Retroactiune.WebAPI.Services
// Setup
var mongoDatabaseMock = new Mock<IMongoDatabase>();
var mongoClientMock = new Mock<IMongoClient>();
var mongoSettingsMock = new Mock<IMongoDbSettings>();
var mongoSettingsMock = new Mock<IDatabaseSettings>();
var mongoCollectionMock = new Mock<IMongoCollection<Token>>();
mongoSettingsMock.SetupGet(i => i.DatabaseName).Returns("MyDB");
@ -45,7 +45,7 @@ namespace Retroactiune.Tests.Retroactiune.WebAPI.Services
// Setup
var mongoDatabaseMock = new Mock<IMongoDatabase>();
var mongoClientMock = new Mock<IMongoClient>();
var mongoSettingsMock = new Mock<IMongoDbSettings>();
var mongoSettingsMock = new Mock<IDatabaseSettings>();
var mongoCollectionMock = new Mock<IMongoCollection<Token>>();
mongoSettingsMock.SetupGet(i => i.DatabaseName).Returns("MyDB");

View file

@ -1,6 +1,10 @@
namespace Retroactiune.Settings
namespace Retroactiune.Database
{
public class RetroactiuneDbSettings : IMongoDbSettings
/// <summary>
/// DatabaseSettingsOptions acts as a model for the database settings, it is used in conjunction with the built in
/// ASP .NET Core configuration options.
/// </summary>
public class DatabaseSettings : IDatabaseSettings
{
public string FeedbackCollectionName { get; set; }
public string TokensCollectionName { get; set; }

View file

@ -1,9 +1,9 @@
namespace Retroactiune.Settings
namespace Retroactiune.Database
{
/// <summary>
/// Interface for repressing the application's MongoDb settings Options.
/// Interface for repressing the application's database settings.
/// </summary>
public interface IMongoDbSettings
public interface IDatabaseSettings
{
public string FeedbackCollectionName { get; set; }
public string TokensCollectionName { get; set; }

View file

@ -3,8 +3,8 @@ using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using MongoDB.Driver;
using Retroactiune.Database;
using Retroactiune.Models;
using Retroactiune.Settings;
namespace Retroactiune.Services
{
@ -16,7 +16,7 @@ namespace Retroactiune.Services
{
private readonly IMongoCollection<FeedbackReceiver> _collection;
public FeedbackReceiverService(IMongoClient client, IMongoDbSettings settings)
public FeedbackReceiverService(IMongoClient client, IDatabaseSettings settings)
{
var database = client.GetDatabase(settings.DatabaseName);
_collection = database.GetCollection<FeedbackReceiver>(settings.FeedbackReceiverCollectionName);

View file

@ -2,8 +2,8 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using MongoDB.Driver;
using Retroactiune.Database;
using Retroactiune.Models;
using Retroactiune.Settings;
namespace Retroactiune.Services
{
@ -11,7 +11,7 @@ namespace Retroactiune.Services
{
private readonly IMongoCollection<Token> _collection;
public TokensService(IMongoClient client, IMongoDbSettings settings)
public TokensService(IMongoClient client, IDatabaseSettings settings)
{
var database = client.GetDatabase(settings.DatabaseName);
_collection = database.GetCollection<Token>(settings.TokensCollectionName);

View file

@ -9,8 +9,8 @@ using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using MongoDB.Driver;
using Retroactiune.Database;
using Retroactiune.Services;
using Retroactiune.Settings;
namespace Retroactiune
{
@ -28,9 +28,9 @@ namespace Retroactiune
public void ConfigureServices(IServiceCollection services)
{
// Database Configuration
services.Configure<RetroactiuneDbSettings>(Configuration.GetSection(nameof(RetroactiuneDbSettings)));
services.AddSingleton<IMongoDbSettings>(sp =>
sp.GetRequiredService<IOptions<RetroactiuneDbSettings>>().Value);
services.Configure<DatabaseSettings>(Configuration.GetSection(nameof(DatabaseSettings)));
services.AddSingleton<IDatabaseSettings>(sp =>
sp.GetRequiredService<IOptions<DatabaseSettings>>().Value);
// AutoMapper
services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
@ -41,7 +41,7 @@ namespace Retroactiune
services.AddSingleton<ITokensService, TokensService>();
services.AddSingleton<IMongoClient, MongoClient>(i =>
{
var settings = i.GetService<IOptions<RetroactiuneDbSettings>>();
var settings = i.GetService<IOptions<DatabaseSettings>>();
return new MongoClient(settings.Value.ConnectionString);
});

View file

@ -4,8 +4,8 @@ using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using MongoDB.Driver;
using Retroactiune.Database;
using Retroactiune.Services;
using Retroactiune.Settings;
namespace Retroactiune
{
@ -26,9 +26,9 @@ namespace Retroactiune
public void ConfigureServices(IServiceCollection services)
{
// Database Configuration
services.Configure<RetroactiuneDbSettings>(Configuration.GetSection(nameof(RetroactiuneDbSettings)));
services.AddSingleton<IMongoDbSettings>(sp =>
sp.GetRequiredService<IOptions<RetroactiuneDbSettings>>().Value);
services.Configure<DatabaseSettings>(Configuration.GetSection(nameof(DatabaseSettings)));
services.AddSingleton<IDatabaseSettings>(sp =>
sp.GetRequiredService<IOptions<DatabaseSettings>>().Value);
services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
@ -36,7 +36,7 @@ namespace Retroactiune
services.AddSingleton<IFeedbackReceiverService, FeedbackReceiverService>();
services.AddSingleton<IMongoClient, MongoClient>(i =>
{
var settings = i.GetService<IOptions<RetroactiuneDbSettings>>();
var settings = i.GetService<IOptions<DatabaseSettings>>();
return new MongoClient(settings.Value.ConnectionString);
});

View file

@ -1,5 +1,5 @@
{
"RetroactiuneDbSettings": {
"DatabaseSettings": {
"FeedbackCollectionName": "feedback",
"TokensCollectionName": "tokens",
"FeedbackReceiverCollectionName": "feedback_receiver",

View file

@ -1,5 +1,5 @@
{
"RetroactiuneDbSettings": {
"DatabaseSettings": {
"FeedbackCollectionName": "feedback",
"TokensCollectionName": "tokens",
"FeedbackReceiverCollectionName": "feedback_receiver",