Skip to content

Commit

Permalink
MongoDb Provider - Switch to LINQ2 (#4001)
Browse files Browse the repository at this point in the history
Added an option to allow the user to opt-in for LINQ3 provider
that actually causes regressions. See #4000
  • Loading branch information
alkampfergit committed May 11, 2023
1 parent 047c51d commit 6112389
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 15 deletions.
@@ -1,5 +1,6 @@
using System;
using Elsa.Persistence.MongoDb.Options;
using Elsa.Persistence.MongoDb.Services;
using Elsa.Webhooks.Models;
using Elsa.Webhooks.Persistence.MongoDb.Data;
using Microsoft.Extensions.Options;
Expand All @@ -11,9 +12,8 @@ public class ElsaMongoDbContext
{
public ElsaMongoDbContext(IOptions<ElsaMongoDbOptions> options)
{
var connectionString = options.Value.ConnectionString;
var mongoClient = new MongoClient(connectionString);
var databaseName = options.Value.DatabaseName is not null and not "" ? options.Value.DatabaseName : MongoUrl.Create(connectionString).DatabaseName;
var mongoClient = ElsaMongoDbDriverHelpers.CreateClient(options.Value);
var databaseName = options.Value.DatabaseName is not null and not "" ? options.Value.DatabaseName : MongoUrl.Create(options.Value.ConnectionString).DatabaseName;

if (databaseName == null)
throw new Exception("Please specify a database name, either via the connection string or via the DatabaseName setting.");
Expand Down
Expand Up @@ -4,7 +4,7 @@
<Import Project="..\..\..\..\configureawait.props"/>

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFrameworks>netcoreapp3.1;net5.0;net6.0;net7.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Description>
Expand Down
@@ -1,4 +1,5 @@
using Elsa.Persistence.MongoDb.Options;
using Elsa.Persistence.MongoDb.Services;
using Elsa.Secrets.Models;
using Elsa.Secrets.Persistence.MongoDb.Data;
using Microsoft.Extensions.Options;
Expand All @@ -10,9 +11,8 @@ public class ElsaMongoDbContext
{
public ElsaMongoDbContext(IOptions<ElsaMongoDbOptions> options)
{
var connectionString = options.Value.ConnectionString;
var mongoClient = new MongoClient(connectionString);
var databaseName = options.Value.DatabaseName is not null and not "" ? options.Value.DatabaseName : MongoUrl.Create(connectionString).DatabaseName;
var mongoClient = ElsaMongoDbDriverHelpers.CreateClient(options.Value);
var databaseName = options.Value.DatabaseName is not null and not "" ? options.Value.DatabaseName : MongoUrl.Create(options.Value.ConnectionString).DatabaseName;

if (databaseName == null)
throw new Exception("Please specify a database name, either via the connection string or via the DatabaseName setting.");
Expand Down
Expand Up @@ -4,7 +4,7 @@
<Import Project="..\..\..\..\configureawait.props" />

<PropertyGroup>
<TargetFrameworks>netcoreapp3.1;net5.0;net6.0;net7.0</TargetFrameworks>
<TargetFrameworks>netstandard2.1;net5.0;net6.0;net7.0</TargetFrameworks>
<Description>
Elsa Workflow Settings is an optional part of Elsa Workflows.
This package provides a MongoDb persistence provider.
Expand Down
@@ -1,5 +1,6 @@
using System;
using Elsa.Persistence.MongoDb.Options;
using Elsa.Persistence.MongoDb.Services;
using Elsa.WorkflowSettings.Models;
using Elsa.WorkflowSettings.Persistence.MongoDb.Data;
using Microsoft.Extensions.Options;
Expand All @@ -11,9 +12,8 @@ public class ElsaMongoDbContext
{
public ElsaMongoDbContext(IOptions<ElsaMongoDbOptions> options)
{
var connectionString = options.Value.ConnectionString;
var mongoClient = new MongoClient(connectionString);
var databaseName = options.Value.DatabaseName is not null and not "" ? options.Value.DatabaseName : MongoUrl.Create(connectionString).DatabaseName;
var mongoClient = ElsaMongoDbDriverHelpers.CreateClient(options.Value);
var databaseName = options.Value.DatabaseName is not null and not "" ? options.Value.DatabaseName : MongoUrl.Create(options.Value.ConnectionString).DatabaseName;

if (databaseName == null)
throw new Exception("Please specify a database name, either via the connection string or via the DatabaseName setting.");
Expand Down
Expand Up @@ -4,7 +4,7 @@
<Import Project="..\..\..\configureawait.props"/>

<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<TargetFrameworks>netstandard2.1;net5.0;net6.0;net7.0</TargetFrameworks>
<Description>
Elsa is a set of workflow libraries and tools that enable super-fast workflowing capabilities in any .NET Core application.
This package provides a MongoDb persistence provider.
Expand Down
Expand Up @@ -10,5 +10,12 @@ public class ElsaMongoDbOptions
/// <see cref="Elsa.Persistence.MongoDb.Serializers.VariablesSerializer"/>
/// </summary>
public bool DoNotRegisterVariablesSerializer { get; set; }

/// <summary>
/// If true it will use the new LINQ3 provider introduced in MongoDB.Driver 2.19.0 but
/// it has some breaking changes, so it is preferibly to enable only if you are really sure
/// that it works without any problem in your project.
/// </summary>
public bool UseNewLinq3Provider { get; set; }
}
}
Expand Up @@ -10,9 +10,8 @@ public class ElsaMongoDbContext : IElsaMongoDbContext
{
public ElsaMongoDbContext(IOptions<ElsaMongoDbOptions> options)
{
var connectionString = options.Value.ConnectionString;
var mongoClient = new MongoClient(connectionString);
var databaseName = options.Value.DatabaseName is not null and not "" ? options.Value.DatabaseName : MongoUrl.Create(connectionString).DatabaseName;
var mongoClient = ElsaMongoDbDriverHelpers.CreateClient(options.Value);
var databaseName = options.Value.DatabaseName is not null and not "" ? options.Value.DatabaseName : MongoUrl.Create(options.Value.ConnectionString).DatabaseName;

if (databaseName == null)
throw new Exception("Please specify a database name, either via the connection string or via the DatabaseName setting.");
Expand Down
@@ -0,0 +1,33 @@
using Elsa.Persistence.MongoDb.Options;
using MongoDB.Driver;
using System;
using System.Collections.Concurrent;

namespace Elsa.Persistence.MongoDb.Services
{
public static class ElsaMongoDbDriverHelpers
{
/// <summary>
/// Avoid creating too much instances for MongoClient.
/// </summary>
private static readonly ConcurrentDictionary<string, MongoClient> _clients = new();

public static MongoClient CreateClient(ElsaMongoDbOptions options)
{
if (string.IsNullOrEmpty(options.ConnectionString)) throw new ArgumentException("Connection string is required.", nameof(options.ConnectionString));
if (!_clients.TryGetValue(options.ConnectionString, out MongoClient client))
{
var connectionString = options.ConnectionString;
var clientSettings = MongoClientSettings.FromConnectionString(connectionString);
if (options.UseNewLinq3Provider == false)
{
clientSettings.LinqProvider = MongoDB.Driver.Linq.LinqProvider.V2;
}
client = new MongoClient(clientSettings);
_clients[options.ConnectionString] = client;
}

return client;
}
}
}

0 comments on commit 6112389

Please sign in to comment.