diff --git a/src/apps/Elsa.Server.Web/Elsa.Server.Web.csproj b/src/apps/Elsa.Server.Web/Elsa.Server.Web.csproj index 0b1709cd1..42c329c61 100644 --- a/src/apps/Elsa.Server.Web/Elsa.Server.Web.csproj +++ b/src/apps/Elsa.Server.Web/Elsa.Server.Web.csproj @@ -60,9 +60,4 @@ - - - - - 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 c32d42335..e5d716fa0 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,22 @@ 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)); + var mongoUrl = new MongoUrl(ConnectionString); + Services.AddSingleton(sp => CreateMongoClient(sp, mongoUrl)); + Services.AddScoped(sp => CreateDatabase(sp, mongoUrl)); + + Services.TryAddScoped(); + Services.AddScoped(CollectionNamingStrategy); RegisterSerializers(); RegisterClassMaps(); @@ -81,11 +94,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()); @@ -97,8 +109,13 @@ public class MongoDbFeature : FeatureBase settings.RetryWrites = options.RetryWrites; settings.SslSettings = options.SslSettings; - var mongoClient = new MongoClient(settings); - return mongoClient.GetDatabase(options.DatabaseName ?? 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) => 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; + } +}