Merge remote-tracking branch 'origin/patch/3.2.x'
This commit is contained in:
commit
ee07b83e60
|
|
@ -60,9 +60,4 @@
|
|||
<ItemGroup>
|
||||
<PackageReference Include="System.Text.Json" VersionOverride="$(SystemTextJsonVersion)" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="App_Data\" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
|
|
|||
|
|
@ -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<MongoDbStore<TDocument>>()
|
||||
.AddScoped<TStore>()
|
||||
;
|
||||
.AddScoped<TStore>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -35,8 +35,13 @@ public abstract class PersistenceFeatureBase : FeatureBase
|
|||
/// <typeparam name="TDocument">The document type of the collection.</typeparam>
|
||||
protected void AddCollection<TDocument>(string collectionName) where TDocument : class
|
||||
{
|
||||
Services.AddScoped(
|
||||
sp => sp.GetRequiredService<IMongoDatabase>()
|
||||
.GetCollection<TDocument>(collectionName));
|
||||
Services.AddScoped(sp =>
|
||||
{
|
||||
var collectionNamingStrategy = sp.GetRequiredService<ICollectionNamingStrategy>();
|
||||
var formattedCollectionName = collectionNamingStrategy.GetCollectionName(collectionName);
|
||||
|
||||
return sp.GetRequiredService<IMongoDatabase>()
|
||||
.GetCollection<TDocument>(formattedCollectionName);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
namespace Elsa.MongoDb.Contracts;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a naming strategy to use when creating the name of a MongoDB collection.
|
||||
/// </summary>
|
||||
public interface ICollectionNamingStrategy
|
||||
{
|
||||
/// <summary>
|
||||
/// Returns a collection name from the specified base collection name.
|
||||
/// </summary>
|
||||
string GetCollectionName(string collectionName);
|
||||
}
|
||||
|
|
@ -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
|
|||
/// </summary>
|
||||
public Action<MongoDbOptions> Options { get; set; } = _ => { };
|
||||
|
||||
/// <summary>
|
||||
/// A delegate that creates an instance of an implementation of <see cref="ICollectionNamingStrategy"/>.
|
||||
/// </summary>
|
||||
public Func<IServiceProvider, ICollectionNamingStrategy> CollectionNamingStrategy { get; set; } = sp => sp.GetRequiredService<DefaultNamingStrategy>();
|
||||
|
||||
/// <inheritdoc />
|
||||
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<DefaultNamingStrategy>();
|
||||
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<IOptions<MongoDbOptions>>().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<IMongoClient>();
|
||||
return client.GetDatabase(mongoUrl.DatabaseName);
|
||||
}
|
||||
|
||||
private static string GetApplicationName(MongoClientSettings settings) =>
|
||||
|
|
|
|||
|
|
@ -0,0 +1,17 @@
|
|||
using Elsa.MongoDb.Contracts;
|
||||
|
||||
namespace Elsa.MongoDb.NamingStrategies;
|
||||
|
||||
/// <summary>
|
||||
/// Returns the same collection name, without modifying it.
|
||||
/// </summary>
|
||||
public class DefaultNamingStrategy : ICollectionNamingStrategy
|
||||
{
|
||||
/// <summary>
|
||||
/// Returns the same collection name, without modifying it.
|
||||
/// </summary>
|
||||
public string GetCollectionName(string collectionName)
|
||||
{
|
||||
return collectionName;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue