Skip to content

Commit

Permalink
Merge pull request #235 from Avanade/feature/structV8
Browse files Browse the repository at this point in the history
Update Liquid framework for net8, and standardize all cartridge configuration injections to use IOptions.
  • Loading branch information
lucianareginalino authored Jun 17, 2024
2 parents fb0e649 + 00ba329 commit cead005
Show file tree
Hide file tree
Showing 19 changed files with 207 additions and 464 deletions.
42 changes: 0 additions & 42 deletions src/Liquid.Repository.Mongo/Configuration/IMongoEntitySettings.cs

This file was deleted.

This file was deleted.

32 changes: 27 additions & 5 deletions src/Liquid.Repository.Mongo/Configuration/MongoEntitySettings.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,46 @@
using Liquid.Core.Settings;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;

namespace Liquid.Repository.Mongo.Configuration
{
/// <summary>
/// Properties set list of service bus configurations.
/// </summary>
public class MongoDbSettings
{
/// <summary>
/// Properties set list of service bus configurations.
/// </summary>
public List<MongoEntitySettings> Settings { get; set; }
}

/// <summary>
/// MongoDB repository data entity settings.
/// </summary>
[ExcludeFromCodeCoverage]
public class MongoEntitySettings : IMongoEntitySettings
public class MongoEntitySettings
{
///<inheritdoc/>
public string CollectionName { get; set; }

///<inheritdoc/>
public string ShardKey { get; set; }

///<inheritdoc/>
public string DatabaseSettingsSectionName { get; set; }
/// <summary>
/// Gets or sets the database connection string.
/// </summary>
/// <value>
/// The database connection string.
/// </value>
public string ConnectionString { get; set; }

///<inheritdoc/>
public DatabaseSettings DatabaseSettings { get; set; }
/// <summary>
/// Gets or sets the name of the database.
/// </summary>
/// <value>
/// The name of the database.
/// </value>
public string DatabaseName { get; set; }
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,5 @@ public MongoEntitySettingsDoesNotExistException(string entityName)
public MongoEntitySettingsDoesNotExistException(string message, Exception innerException) : base(message, innerException)
{
}

///<inheritdoc/>
protected MongoEntitySettingsDoesNotExistException(SerializationInfo info, StreamingContext context) : base(info, context)
{
}
}
}
4 changes: 0 additions & 4 deletions src/Liquid.Repository.Mongo/Exceptions/MongoException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,5 @@ public MongoException(string message, Exception innerException) : base(message,
{
}

///<inheritdoc/>
protected MongoException(SerializationInfo info, StreamingContext context) : base(info, context)
{
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Liquid.Core.Implementations;
using Liquid.Core.Interfaces;
using Liquid.Repository.Mongo.Configuration;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;

Expand All @@ -21,15 +22,24 @@ public static class IServiceCollectionExtensions
/// <typeparam name="TEntity">Type of entity that the repository should correspond to</typeparam>
/// <typeparam name="TIdentifier">Entity identifier type.</typeparam>
/// <param name="services">Extended ServiceCollection object.</param>
/// <param name="entitiesConfigurationRootSectionName">Name of the configuration section where all entities have their repository settings configured. Default: "Liquid:RepositorySettings:Entities".</param>
/// <param name="sectionName">Name of the configuration section where all entities have their repository settings configured.</param>
/// <param name="collectionName">Name of the collection in the database that the repository should correspond to.</param>
/// <param name="activateTelemetry">Specifies whether the telemetry should be activated or not for this repository. Default: True.</param>
public static IServiceCollection AddLiquidMongoRepository<TEntity, TIdentifier>(this IServiceCollection services, string entitiesConfigurationRootSectionName = "Liquid:RepositorySettings:Entities", bool activateTelemetry = true)
public static IServiceCollection AddLiquidMongoRepository<TEntity, TIdentifier>(this IServiceCollection services, string sectionName, string collectionName, bool activateTelemetry = true)
where TEntity : LiquidEntity<TIdentifier>, new()
{
services.TryAddSingleton<IMongoClientFactory, MongoClientFactory>();
services.TryAddSingleton<IMongoEntitySettingsFactory>(provider => { return ActivatorUtilities.CreateInstance<MongoEntitySettingsFactory>(provider, entitiesConfigurationRootSectionName); });

services.AddScoped<IMongoDataContext<TEntity>, MongoDataContext<TEntity>>();
services.AddOptions<MongoDbSettings>()
.Configure<IConfiguration>((settings, configuration) =>
{
configuration.GetSection(sectionName).Bind(settings);
});

services.AddScoped<IMongoDataContext<TEntity>>((provider) =>
{
return ActivatorUtilities.CreateInstance<MongoDataContext<TEntity>>(provider, collectionName);
});


if (activateTelemetry)
Expand Down
6 changes: 4 additions & 2 deletions src/Liquid.Repository.Mongo/IMongoClientFactory.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Liquid.Core.Settings;
using Liquid.Repository.Mongo.Configuration;
using MongoDB.Driver;

namespace Liquid.Repository.Mongo
Expand All @@ -11,7 +12,8 @@ public interface IMongoClientFactory
/// <summary>
/// Provide a new instance of <see cref="MongoClient"/> with db conection started.
/// </summary>
/// <param name="databaseSettings">Database settings used to create unique clients based on DatabaseSettings hash code.</param>
IMongoClient GetClient(DatabaseSettings databaseSettings);
/// <param name="collectionName"></param>
/// <param name="settings"></param>
IMongoClient GetClient(string collectionName, out MongoEntitySettings settings);
}
}
12 changes: 5 additions & 7 deletions src/Liquid.Repository.Mongo/Liquid.Repository.Mongo.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<PackageId>Liquid.Repository.Mongo</PackageId>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<Authors>Avanade Brazil</Authors>
Expand All @@ -10,7 +10,7 @@
<Copyright>Avanade 2019</Copyright>
<PackageProjectUrl>https://github.com/Avanade/Liquid-Application-Framework</PackageProjectUrl>
<PackageIcon>logo.png</PackageIcon>
<Version>6.0.0</Version>
<Version>8.0.0-alpha-01</Version>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<IsPackable>true</IsPackable>
<DebugType>Full</DebugType>
Expand All @@ -25,7 +25,9 @@


<ItemGroup>
<PackageReference Include="MongoDB.Driver" Version="2.25.0" />
<PackageReference Include="Liquid.Core" Version="8.0.0-alpha-04" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.1" />
<PackageReference Include="MongoDB.Driver" Version="2.26.0" />
</ItemGroup>

<ItemGroup>
Expand All @@ -35,8 +37,4 @@
</None>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Liquid.Core\Liquid.Core.csproj" />
</ItemGroup>

</Project>
25 changes: 17 additions & 8 deletions src/Liquid.Repository.Mongo/MongoClientFactory.cs
Original file line number Diff line number Diff line change
@@ -1,39 +1,48 @@
using Liquid.Core.Settings;
using Liquid.Repository.Mongo.Configuration;
using Liquid.Repository.Mongo.Exceptions;
using Microsoft.Extensions.Options;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Liquid.Repository.Mongo
{
///<inheritdoc/>
public class MongoClientFactory : IMongoClientFactory
{
private readonly IDictionary<int, IMongoClient> _mongoClients;
private readonly IOptions<MongoDbSettings> _settings;
private readonly IDictionary<string, IMongoClient> _mongoClients;

/// <summary>
/// Initializes a new instance of the <see cref="MongoClientFactory" /> class.
/// </summary>
public MongoClientFactory()
public MongoClientFactory(IOptions<MongoDbSettings> settings)
{
_mongoClients = new Dictionary<int, IMongoClient>();
_mongoClients = new Dictionary<string, IMongoClient>();
_settings = settings;
}

///<inheritdoc/>
public IMongoClient GetClient(DatabaseSettings databaseSettings)
public IMongoClient GetClient(string collectionName, out MongoEntitySettings settings)
{
if (databaseSettings is null) throw new ArgumentNullException(nameof(databaseSettings));
if (collectionName is null) throw new ArgumentNullException(nameof(collectionName));
settings = _settings.Value.Settings.FirstOrDefault(x => x.CollectionName == collectionName);

Check warning on line 31 in src/Liquid.Repository.Mongo/MongoClientFactory.cs

View workflow job for this annotation

GitHub Actions / call-reusable-build-workflow / build

"Find" method should be used instead of the "FirstOrDefault" extension method. (https://rules.sonarsource.com/csharp/RSPEC-6602)

if (settings is null) throw new MongoEntitySettingsDoesNotExistException(collectionName);

// Try to get from the created clients collection, otherwise creates a new client
IMongoClient mongoClient = _mongoClients.TryGetValue(databaseSettings.GetHashCode(), out mongoClient) ? mongoClient : CreateClient(databaseSettings);
IMongoClient mongoClient = _mongoClients.TryGetValue(collectionName, out mongoClient) ? mongoClient : CreateClient(settings);

return mongoClient;
}

private IMongoClient CreateClient(DatabaseSettings databaseSettings)
private IMongoClient CreateClient(MongoEntitySettings databaseSettings)
{
var mongoClient = new MongoClient(databaseSettings.ConnectionString);

_mongoClients.Add(databaseSettings.GetHashCode(), mongoClient);
_mongoClients.Add(databaseSettings.CollectionName, mongoClient);

return mongoClient;
}
Expand Down
Loading

0 comments on commit cead005

Please sign in to comment.