Skip to content

Commit

Permalink
Remove enforced use of `MongoDB.Driver.Core.Extensions.DiagnosticSour…
Browse files Browse the repository at this point in the history
…ces` in favour of using `OnConfiguring(...)` (#302)
  • Loading branch information
mburumaxwell authored Aug 13, 2024
1 parent 130932c commit ea467a1
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting;
using MongoDB.Driver;
using MongoDB.Driver.Core.Extensions.DiagnosticSources;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using Tingle.Extensions.MongoDB;
Expand Down Expand Up @@ -33,29 +32,27 @@ public static IServiceCollection AddMongoDatabaseSetup<TContext>(this IServiceCo
this IServiceCollection services,
IConfiguration configuration,
Action<MongoDbContextOptionsBuilder>? optionsAction = null,
InstrumentationOptions? instrumentationOptions = null,
ServiceLifetime contextLifetime = ServiceLifetime.Singleton,
ServiceLifetime optionsLifetime = ServiceLifetime.Singleton)
where TContext : MongoDbContext
{
var connectionString = configuration.GetConnectionString("Mongo");
return connectionString is not null
? AddMongoDbContext<TContext>(services, connectionString: connectionString, optionsAction, instrumentationOptions, contextLifetime, optionsLifetime)
? AddMongoDbContext<TContext>(services, connectionString, optionsAction, contextLifetime, optionsLifetime)
: AddMongoDbContext<TContext>(services, optionsAction, contextLifetime, optionsLifetime);
}

public static IServiceCollection AddMongoDbContext<[DynamicallyAccessedMembers(MongoDbContext.DynamicallyAccessedMemberTypes)] TContext>(
this IServiceCollection services,
string connectionString,
Action<MongoDbContextOptionsBuilder>? optionsAction = null,
InstrumentationOptions? instrumentationOptions = null,
ServiceLifetime contextLifetime = ServiceLifetime.Singleton,
ServiceLifetime optionsLifetime = ServiceLifetime.Singleton)
where TContext : MongoDbContext
{
return AddMongoDbContext<TContext>(services, options =>
{
options.UseMongoConnectionString(connectionString, instrumentationOptions);
options.UseMongoConnectionString(connectionString);
optionsAction?.Invoke(options);
}, contextLifetime, optionsLifetime);
}
Expand Down
28 changes: 7 additions & 21 deletions src/Tingle.Extensions.MongoDB/MongoDbContextOptions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using MongoDB.Driver;
using MongoDB.Driver.Core.Extensions.DiagnosticSources;
using System.Diagnostics.CodeAnalysis;

namespace Microsoft.Extensions.DependencyInjection;
Expand Down Expand Up @@ -121,9 +120,8 @@ public virtual MongoDbContextOptionsBuilder UseApplicationServiceProvider(IServi
/// Sets the <see cref="MongoUrl"/> to use when configuring the context.
/// </summary>
/// <param name="url">The <see cref="MongoUrl"/> to be used</param>
/// <param name="instrumentationOptions">The options to use for instrumentation.</param>
/// <returns></returns>
public virtual MongoDbContextOptionsBuilder UseMongoUrl(MongoUrl url, InstrumentationOptions? instrumentationOptions = null)
public virtual MongoDbContextOptionsBuilder UseMongoUrl(MongoUrl url)
{
ArgumentNullException.ThrowIfNull(url);
if (string.IsNullOrWhiteSpace(url.DatabaseName))
Expand All @@ -133,15 +131,6 @@ public virtual MongoDbContextOptionsBuilder UseMongoUrl(MongoUrl url, Instrument

options.AddMetadata(url);

// add the default event subscriber
ConfigureMongoClientSettings(settings =>
{
settings.ClusterConfigurator = builder =>
{
builder.Subscribe(new DiagnosticsActivityEventSubscriber(instrumentationOptions ?? new() { CaptureCommandText = true }));
};
});

return this;
}

Expand All @@ -154,12 +143,11 @@ public virtual MongoDbContextOptionsBuilder UseMongoUrl(MongoUrl url, Instrument
/// <c>mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]</c>
/// e.g. <c>mongodb://localhost:27017/myDatabase</c>
/// </param>
/// <param name="instrumentationOptions">The options to use for instrumentation.</param>
/// <returns></returns>
public virtual MongoDbContextOptionsBuilder UseMongoConnectionString(string connectionString, InstrumentationOptions? instrumentationOptions = null)
public virtual MongoDbContextOptionsBuilder UseMongoConnectionString(string connectionString)
{
ArgumentNullException.ThrowIfNull(connectionString);
return UseMongoUrl(new MongoUrl(connectionString), instrumentationOptions);
return UseMongoUrl(new MongoUrl(connectionString));
}

/// <summary>
Expand Down Expand Up @@ -228,10 +216,9 @@ public MongoDbContextOptionsBuilder() : this(new MongoDbContextOptions<TContext>
/// Sets the <see cref="MongoUrl"/> to use when configuring the context.
/// </summary>
/// <param name="url">The <see cref="MongoUrl"/> to be used</param>
/// <param name="instrumentationOptions">The options to use for instrumentation.</param>
/// <returns></returns>
public new virtual MongoDbContextOptionsBuilder<TContext> UseMongoUrl(MongoUrl url, InstrumentationOptions? instrumentationOptions = null)
=> (MongoDbContextOptionsBuilder<TContext>)base.UseMongoUrl(url, instrumentationOptions);
public new virtual MongoDbContextOptionsBuilder<TContext> UseMongoUrl(MongoUrl url)
=> (MongoDbContextOptionsBuilder<TContext>)base.UseMongoUrl(url);

/// <summary>
/// Sets the <see cref="MongoUrl"/> to use when configuring the context by
Expand All @@ -242,10 +229,9 @@ public MongoDbContextOptionsBuilder() : this(new MongoDbContextOptions<TContext>
/// <c>mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]</c>
/// e.g. <c>mongodb://localhost:27017/myDatabase</c>
/// </param>
/// <param name="instrumentationOptions">The options to use for instrumentation.</param>
/// <returns></returns>
public new virtual MongoDbContextOptionsBuilder<TContext> UseMongoConnectionString(string connectionString, InstrumentationOptions? instrumentationOptions = null)
=> (MongoDbContextOptionsBuilder<TContext>)base.UseMongoConnectionString(connectionString, instrumentationOptions);
public new virtual MongoDbContextOptionsBuilder<TContext> UseMongoConnectionString(string connectionString)
=> (MongoDbContextOptionsBuilder<TContext>)base.UseMongoConnectionString(connectionString);

/// <summary>
/// Further configure the existing instance of <see cref="MongoClientSettings"/>.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks" Version="8.0.7" />
<PackageReference Include="MongoDB.Driver" Version="2.27.0" />
<PackageReference Include="MongoDB.Driver.Core.Extensions.DiagnosticSources" Version="1.4.0" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit ea467a1

Please sign in to comment.