From 1b4ce452eed73b84e25175b6d218bbb08594688c Mon Sep 17 00:00:00 2001 From: rosca-sabina <43028000+rosca-sabina@users.noreply.github.com> Date: Sat, 12 Oct 2024 13:16:09 +0300 Subject: [PATCH 1/3] Add MongoDB collection naming strategy (#6005) --- .../Common/PersistenceFeatureBase.cs | 15 ++++++++++----- .../Contracts/ICollectionNamingStrategy.cs | 12 ++++++++++++ .../Elsa.MongoDb/Features/MongoDbFeature.cs | 11 +++++++++++ .../NamingStrategies/DefaultNamingStrategy.cs | 17 +++++++++++++++++ 4 files changed, 50 insertions(+), 5 deletions(-) create mode 100644 src/modules/Elsa.MongoDb/Contracts/ICollectionNamingStrategy.cs create mode 100644 src/modules/Elsa.MongoDb/NamingStrategies/DefaultNamingStrategy.cs diff --git a/src/modules/Elsa.MongoDb/Common/PersistenceFeatureBase.cs b/src/modules/Elsa.MongoDb/Common/PersistenceFeatureBase.cs index e08af18c4..3cad80172 100644 --- a/src/modules/Elsa.MongoDb/Common/PersistenceFeatureBase.cs +++ b/src/modules/Elsa.MongoDb/Common/PersistenceFeatureBase.cs @@ -1,5 +1,6 @@ using Elsa.Features.Abstractions; using Elsa.Features.Services; +using Elsa.MongoDb.Contracts; using Microsoft.Extensions.DependencyInjection; using MongoDB.Driver; @@ -24,8 +25,7 @@ public abstract class PersistenceFeatureBase : FeatureBase { Services .AddScoped>() - .AddScoped() - ; + .AddScoped(); } /// @@ -35,8 +35,13 @@ public abstract class PersistenceFeatureBase : FeatureBase /// The document type of the collection. protected void AddCollection(string collectionName) where TDocument : class { - Services.AddScoped( - sp => sp.GetRequiredService() - .GetCollection(collectionName)); + Services.AddScoped(sp => + { + var collectionNamingStrategy = sp.GetRequiredService(); + var formattedCollectionName = collectionNamingStrategy.GetCollectionName(collectionName); + + return sp.GetRequiredService() + .GetCollection(formattedCollectionName); + }); } } \ No newline at end of file diff --git a/src/modules/Elsa.MongoDb/Contracts/ICollectionNamingStrategy.cs b/src/modules/Elsa.MongoDb/Contracts/ICollectionNamingStrategy.cs new file mode 100644 index 000000000..ae25b868d --- /dev/null +++ b/src/modules/Elsa.MongoDb/Contracts/ICollectionNamingStrategy.cs @@ -0,0 +1,12 @@ +namespace Elsa.MongoDb.Contracts; + +/// +/// Represents a naming strategy to use when creating the name of a MongoDB collection. +/// +public interface ICollectionNamingStrategy +{ + /// + /// Returns a collection name from the specified base collection name. + /// + string GetCollectionName(string collectionName); +} diff --git a/src/modules/Elsa.MongoDb/Features/MongoDbFeature.cs b/src/modules/Elsa.MongoDb/Features/MongoDbFeature.cs index 2b8014974..a0addcb26 100644 --- a/src/modules/Elsa.MongoDb/Features/MongoDbFeature.cs +++ b/src/modules/Elsa.MongoDb/Features/MongoDbFeature.cs @@ -2,11 +2,14 @@ using System.Text.Json; using Elsa.Features.Abstractions; using Elsa.Features.Services; using Elsa.KeyValues.Entities; +using Elsa.MongoDb.Contracts; +using Elsa.MongoDb.NamingStrategies; using Elsa.MongoDb.Options; using Elsa.MongoDb.Serializers; using Elsa.Workflows.Memory; using Elsa.Workflows.Runtime.Entities; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.Options; using MongoDB.Bson; using MongoDB.Bson.Serialization; @@ -36,12 +39,20 @@ public class MongoDbFeature : FeatureBase /// public Action Options { get; set; } = _ => { }; + /// + /// A delegate that creates an instance of an implementation of . + /// + public Func CollectionNamingStrategy { get; set; } = sp => sp.GetRequiredService(); + /// public override void Apply() { Services.Configure(Options); Services.AddScoped(sp => CreateDatabase(sp, ConnectionString)); + + Services.TryAddScoped(); + Services.AddScoped(CollectionNamingStrategy); RegisterSerializers(); RegisterClassMaps(); diff --git a/src/modules/Elsa.MongoDb/NamingStrategies/DefaultNamingStrategy.cs b/src/modules/Elsa.MongoDb/NamingStrategies/DefaultNamingStrategy.cs new file mode 100644 index 000000000..e266d4cbf --- /dev/null +++ b/src/modules/Elsa.MongoDb/NamingStrategies/DefaultNamingStrategy.cs @@ -0,0 +1,17 @@ +using Elsa.MongoDb.Contracts; + +namespace Elsa.MongoDb.NamingStrategies; + +/// +/// Returns the same collection name, without modifying it. +/// +public class DefaultNamingStrategy : ICollectionNamingStrategy +{ + /// + /// Returns the same collection name, without modifying it. + /// + public string GetCollectionName(string collectionName) + { + return collectionName; + } +} From 090a3fd1b0701b210268ec1784bbbcb47be4fb89 Mon Sep 17 00:00:00 2001 From: rosca-sabina <43028000+rosca-sabina@users.noreply.github.com> Date: Sat, 12 Oct 2024 13:21:39 +0300 Subject: [PATCH 2/3] Register MongoDb client as singleton (#6013) Co-authored-by: Sipke Schoorstra --- .../Elsa.MongoDb/Features/MongoDbFeature.cs | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/modules/Elsa.MongoDb/Features/MongoDbFeature.cs b/src/modules/Elsa.MongoDb/Features/MongoDbFeature.cs index a0addcb26..b0aabc67d 100644 --- a/src/modules/Elsa.MongoDb/Features/MongoDbFeature.cs +++ b/src/modules/Elsa.MongoDb/Features/MongoDbFeature.cs @@ -49,7 +49,9 @@ public class MongoDbFeature : FeatureBase { Services.Configure(Options); - Services.AddScoped(sp => CreateDatabase(sp, ConnectionString)); + var mongoUrl = new MongoUrl(ConnectionString); + Services.AddSingleton(sp => CreateMongoClient(sp, mongoUrl)); + Services.AddScoped(sp => CreateDatabase(sp, mongoUrl)); Services.TryAddScoped(); Services.AddScoped(CollectionNamingStrategy); @@ -95,11 +97,10 @@ public class MongoDbFeature : FeatureBase } } - private static IMongoDatabase CreateDatabase(IServiceProvider sp, string connectionString) + private static IMongoClient CreateMongoClient(IServiceProvider sp, MongoUrl mongoUrl) { var options = sp.GetRequiredService>().Value; - var mongoUrl = new MongoUrl(connectionString); var settings = MongoClientSettings.FromUrl(mongoUrl); settings.ClusterConfigurator = cb => cb.Subscribe(new DiagnosticsActivityEventSubscriber()); @@ -111,8 +112,13 @@ public class MongoDbFeature : FeatureBase settings.RetryWrites = options.RetryWrites; settings.SslSettings = options.SslSettings; - var mongoClient = new MongoClient(settings); - return mongoClient.GetDatabase(mongoUrl.DatabaseName); + return new MongoClient(settings); + } + + private static IMongoDatabase CreateDatabase(IServiceProvider sp, MongoUrl mongoUrl) + { + var client = sp.GetRequiredService(); + return client.GetDatabase(mongoUrl.DatabaseName); } private static string GetApplicationName(MongoClientSettings settings) => From 190591824725feb81142f7a7990439f0312c32ca Mon Sep 17 00:00:00 2001 From: Sipke Schoorstra Date: Sat, 12 Oct 2024 12:27:54 +0200 Subject: [PATCH 3/3] Update `System.Text.Json` package reference to use property variable Switched from a hardcoded version to a property variable `$(SystemTextJsonVersion)` for the `System.Text.Json` package reference in multiple project files. This change centralizes version management and ensures consistency across all projects. --- Directory.Build.props | 3 ++- src/bundles/Elsa.Server.Web/Elsa.Server.Web.csproj | 2 +- .../Elsa.ServerAndStudio.Web/Elsa.ServerAndStudio.Web.csproj | 2 +- src/bundles/Elsa.Studio.Web/Elsa.Studio.Web.csproj | 2 +- src/clients/Elsa.Api.Client/Elsa.Api.Client.csproj | 2 +- .../Elsa.EntityFrameworkCore.Common.csproj | 2 +- .../Elsa.EntityFrameworkCore.MySql.csproj | 2 +- .../Elsa.EntityFrameworkCore.PostgreSql.csproj | 2 +- .../Elsa.EntityFrameworkCore.SqlServer.csproj | 2 +- .../Elsa.EntityFrameworkCore.Sqlite.csproj | 2 +- .../Elsa.EntityFrameworkCore/Elsa.EntityFrameworkCore.csproj | 2 +- src/modules/Elsa.FileStorage/Elsa.FileStorage.csproj | 2 +- src/modules/Elsa.Http/Elsa.Http.csproj | 2 +- src/modules/Elsa.MongoDb/Elsa.MongoDb.csproj | 2 +- .../Elsa.Quartz.EntityFrameworkCore.MySql.csproj | 2 +- .../Elsa.Quartz.EntityFrameworkCore.PostgreSql.csproj | 2 +- .../Elsa.Quartz.EntityFrameworkCore.SqlServer.csproj | 2 +- .../Elsa.Quartz.EntityFrameworkCore.Sqlite.csproj | 2 +- src/modules/Elsa.Telnyx/Elsa.Telnyx.csproj | 2 +- .../Elsa.WorkflowProviders.BlobStorage.csproj | 2 +- src/modules/Elsa.Workflows.Api/Elsa.Workflows.Api.csproj | 2 +- 21 files changed, 22 insertions(+), 21 deletions(-) diff --git a/Directory.Build.props b/Directory.Build.props index c24271ac0..645da893e 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -27,6 +27,7 @@ true - 3.2.0-rc4.473 + 3.2.1-preview.558 + 8.0.5 \ No newline at end of file diff --git a/src/bundles/Elsa.Server.Web/Elsa.Server.Web.csproj b/src/bundles/Elsa.Server.Web/Elsa.Server.Web.csproj index 71cb87c96..ba521fe46 100644 --- a/src/bundles/Elsa.Server.Web/Elsa.Server.Web.csproj +++ b/src/bundles/Elsa.Server.Web/Elsa.Server.Web.csproj @@ -56,7 +56,7 @@ - + diff --git a/src/bundles/Elsa.ServerAndStudio.Web/Elsa.ServerAndStudio.Web.csproj b/src/bundles/Elsa.ServerAndStudio.Web/Elsa.ServerAndStudio.Web.csproj index 23d188639..e2d1c97f8 100644 --- a/src/bundles/Elsa.ServerAndStudio.Web/Elsa.ServerAndStudio.Web.csproj +++ b/src/bundles/Elsa.ServerAndStudio.Web/Elsa.ServerAndStudio.Web.csproj @@ -42,7 +42,7 @@ - + \ No newline at end of file diff --git a/src/bundles/Elsa.Studio.Web/Elsa.Studio.Web.csproj b/src/bundles/Elsa.Studio.Web/Elsa.Studio.Web.csproj index 432ff7c6c..2cb27ad13 100644 --- a/src/bundles/Elsa.Studio.Web/Elsa.Studio.Web.csproj +++ b/src/bundles/Elsa.Studio.Web/Elsa.Studio.Web.csproj @@ -18,7 +18,7 @@ - + diff --git a/src/clients/Elsa.Api.Client/Elsa.Api.Client.csproj b/src/clients/Elsa.Api.Client/Elsa.Api.Client.csproj index 7ce828980..5358e76f3 100644 --- a/src/clients/Elsa.Api.Client/Elsa.Api.Client.csproj +++ b/src/clients/Elsa.Api.Client/Elsa.Api.Client.csproj @@ -19,7 +19,7 @@ - + diff --git a/src/modules/Elsa.EntityFrameworkCore.Common/Elsa.EntityFrameworkCore.Common.csproj b/src/modules/Elsa.EntityFrameworkCore.Common/Elsa.EntityFrameworkCore.Common.csproj index eab7ce6b2..26a175db6 100644 --- a/src/modules/Elsa.EntityFrameworkCore.Common/Elsa.EntityFrameworkCore.Common.csproj +++ b/src/modules/Elsa.EntityFrameworkCore.Common/Elsa.EntityFrameworkCore.Common.csproj @@ -19,7 +19,7 @@ - + diff --git a/src/modules/Elsa.EntityFrameworkCore.MySql/Elsa.EntityFrameworkCore.MySql.csproj b/src/modules/Elsa.EntityFrameworkCore.MySql/Elsa.EntityFrameworkCore.MySql.csproj index 5df8320c1..6ab40ff8f 100644 --- a/src/modules/Elsa.EntityFrameworkCore.MySql/Elsa.EntityFrameworkCore.MySql.csproj +++ b/src/modules/Elsa.EntityFrameworkCore.MySql/Elsa.EntityFrameworkCore.MySql.csproj @@ -14,7 +14,7 @@ - + diff --git a/src/modules/Elsa.EntityFrameworkCore.PostgreSql/Elsa.EntityFrameworkCore.PostgreSql.csproj b/src/modules/Elsa.EntityFrameworkCore.PostgreSql/Elsa.EntityFrameworkCore.PostgreSql.csproj index ce9f15c01..078b0ff95 100644 --- a/src/modules/Elsa.EntityFrameworkCore.PostgreSql/Elsa.EntityFrameworkCore.PostgreSql.csproj +++ b/src/modules/Elsa.EntityFrameworkCore.PostgreSql/Elsa.EntityFrameworkCore.PostgreSql.csproj @@ -14,7 +14,7 @@ - + diff --git a/src/modules/Elsa.EntityFrameworkCore.SqlServer/Elsa.EntityFrameworkCore.SqlServer.csproj b/src/modules/Elsa.EntityFrameworkCore.SqlServer/Elsa.EntityFrameworkCore.SqlServer.csproj index 2a69f65ae..26f28d9a2 100644 --- a/src/modules/Elsa.EntityFrameworkCore.SqlServer/Elsa.EntityFrameworkCore.SqlServer.csproj +++ b/src/modules/Elsa.EntityFrameworkCore.SqlServer/Elsa.EntityFrameworkCore.SqlServer.csproj @@ -21,6 +21,6 @@ - + \ No newline at end of file diff --git a/src/modules/Elsa.EntityFrameworkCore.Sqlite/Elsa.EntityFrameworkCore.Sqlite.csproj b/src/modules/Elsa.EntityFrameworkCore.Sqlite/Elsa.EntityFrameworkCore.Sqlite.csproj index 6580755a3..10feafd59 100644 --- a/src/modules/Elsa.EntityFrameworkCore.Sqlite/Elsa.EntityFrameworkCore.Sqlite.csproj +++ b/src/modules/Elsa.EntityFrameworkCore.Sqlite/Elsa.EntityFrameworkCore.Sqlite.csproj @@ -15,7 +15,7 @@ - + diff --git a/src/modules/Elsa.EntityFrameworkCore/Elsa.EntityFrameworkCore.csproj b/src/modules/Elsa.EntityFrameworkCore/Elsa.EntityFrameworkCore.csproj index 3dd2e4a6a..1a05f4320 100644 --- a/src/modules/Elsa.EntityFrameworkCore/Elsa.EntityFrameworkCore.csproj +++ b/src/modules/Elsa.EntityFrameworkCore/Elsa.EntityFrameworkCore.csproj @@ -13,7 +13,7 @@ - + diff --git a/src/modules/Elsa.FileStorage/Elsa.FileStorage.csproj b/src/modules/Elsa.FileStorage/Elsa.FileStorage.csproj index 0393bc645..648f20efc 100644 --- a/src/modules/Elsa.FileStorage/Elsa.FileStorage.csproj +++ b/src/modules/Elsa.FileStorage/Elsa.FileStorage.csproj @@ -17,7 +17,7 @@ - + diff --git a/src/modules/Elsa.Http/Elsa.Http.csproj b/src/modules/Elsa.Http/Elsa.Http.csproj index 1024756e2..26f35e9b5 100644 --- a/src/modules/Elsa.Http/Elsa.Http.csproj +++ b/src/modules/Elsa.Http/Elsa.Http.csproj @@ -13,7 +13,7 @@ - + diff --git a/src/modules/Elsa.MongoDb/Elsa.MongoDb.csproj b/src/modules/Elsa.MongoDb/Elsa.MongoDb.csproj index 39d38e218..2a84690bf 100644 --- a/src/modules/Elsa.MongoDb/Elsa.MongoDb.csproj +++ b/src/modules/Elsa.MongoDb/Elsa.MongoDb.csproj @@ -16,7 +16,7 @@ - + diff --git a/src/modules/Elsa.Quartz.EntityFrameworkCore.MySql/Elsa.Quartz.EntityFrameworkCore.MySql.csproj b/src/modules/Elsa.Quartz.EntityFrameworkCore.MySql/Elsa.Quartz.EntityFrameworkCore.MySql.csproj index a2e95de32..677393259 100644 --- a/src/modules/Elsa.Quartz.EntityFrameworkCore.MySql/Elsa.Quartz.EntityFrameworkCore.MySql.csproj +++ b/src/modules/Elsa.Quartz.EntityFrameworkCore.MySql/Elsa.Quartz.EntityFrameworkCore.MySql.csproj @@ -16,7 +16,7 @@ - + diff --git a/src/modules/Elsa.Quartz.EntityFrameworkCore.PostgreSql/Elsa.Quartz.EntityFrameworkCore.PostgreSql.csproj b/src/modules/Elsa.Quartz.EntityFrameworkCore.PostgreSql/Elsa.Quartz.EntityFrameworkCore.PostgreSql.csproj index 8cca95901..9b887cfcd 100644 --- a/src/modules/Elsa.Quartz.EntityFrameworkCore.PostgreSql/Elsa.Quartz.EntityFrameworkCore.PostgreSql.csproj +++ b/src/modules/Elsa.Quartz.EntityFrameworkCore.PostgreSql/Elsa.Quartz.EntityFrameworkCore.PostgreSql.csproj @@ -22,7 +22,7 @@ - + diff --git a/src/modules/Elsa.Quartz.EntityFrameworkCore.SqlServer/Elsa.Quartz.EntityFrameworkCore.SqlServer.csproj b/src/modules/Elsa.Quartz.EntityFrameworkCore.SqlServer/Elsa.Quartz.EntityFrameworkCore.SqlServer.csproj index f33bc9af8..6a8f00321 100644 --- a/src/modules/Elsa.Quartz.EntityFrameworkCore.SqlServer/Elsa.Quartz.EntityFrameworkCore.SqlServer.csproj +++ b/src/modules/Elsa.Quartz.EntityFrameworkCore.SqlServer/Elsa.Quartz.EntityFrameworkCore.SqlServer.csproj @@ -24,6 +24,6 @@ - + \ No newline at end of file diff --git a/src/modules/Elsa.Quartz.EntityFrameworkCore.Sqlite/Elsa.Quartz.EntityFrameworkCore.Sqlite.csproj b/src/modules/Elsa.Quartz.EntityFrameworkCore.Sqlite/Elsa.Quartz.EntityFrameworkCore.Sqlite.csproj index 283ff1ffb..1a5ad7e07 100644 --- a/src/modules/Elsa.Quartz.EntityFrameworkCore.Sqlite/Elsa.Quartz.EntityFrameworkCore.Sqlite.csproj +++ b/src/modules/Elsa.Quartz.EntityFrameworkCore.Sqlite/Elsa.Quartz.EntityFrameworkCore.Sqlite.csproj @@ -16,7 +16,7 @@ - + diff --git a/src/modules/Elsa.Telnyx/Elsa.Telnyx.csproj b/src/modules/Elsa.Telnyx/Elsa.Telnyx.csproj index ae23a5915..79b745a66 100644 --- a/src/modules/Elsa.Telnyx/Elsa.Telnyx.csproj +++ b/src/modules/Elsa.Telnyx/Elsa.Telnyx.csproj @@ -24,7 +24,7 @@ - + diff --git a/src/modules/Elsa.WorkflowProviders.BlobStorage/Elsa.WorkflowProviders.BlobStorage.csproj b/src/modules/Elsa.WorkflowProviders.BlobStorage/Elsa.WorkflowProviders.BlobStorage.csproj index c6c83beb6..779e52c49 100644 --- a/src/modules/Elsa.WorkflowProviders.BlobStorage/Elsa.WorkflowProviders.BlobStorage.csproj +++ b/src/modules/Elsa.WorkflowProviders.BlobStorage/Elsa.WorkflowProviders.BlobStorage.csproj @@ -17,6 +17,6 @@ - + diff --git a/src/modules/Elsa.Workflows.Api/Elsa.Workflows.Api.csproj b/src/modules/Elsa.Workflows.Api/Elsa.Workflows.Api.csproj index 95079450f..c41a9e2d3 100644 --- a/src/modules/Elsa.Workflows.Api/Elsa.Workflows.Api.csproj +++ b/src/modules/Elsa.Workflows.Api/Elsa.Workflows.Api.csproj @@ -17,6 +17,6 @@ - +