Skip to content

Commit

Permalink
Added client and database to public api of the MongDB context (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
mgernand authored Dec 28, 2022
1 parent 1e9692d commit e4abf5e
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
using System;
using JetBrains.Annotations;

/// <inheritdoc />
[UsedImplicitly]
internal sealed class EntityFrameworkCoreContextProvider : ContextProviderBase<EntityFrameworkCoreContext>
public sealed class EntityFrameworkCoreContextProvider : ContextProviderBase<EntityFrameworkCoreContext>
{
private readonly IServiceProvider serviceProvider;

Expand Down
3 changes: 2 additions & 1 deletion src/Fluxera.Repository.InMemory/InMemoryContextProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
using System;
using JetBrains.Annotations;

/// <inheritdoc />
[UsedImplicitly]
internal sealed class InMemoryContextProvider : ContextProviderBase<InMemoryContext>
public sealed class InMemoryContextProvider : ContextProviderBase<InMemoryContext>
{
/// <inheritdoc />
public InMemoryContextProvider(
Expand Down
3 changes: 2 additions & 1 deletion src/Fluxera.Repository.LiteDB/LiteContextProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
using System;
using JetBrains.Annotations;

/// <inheritdoc />
[UsedImplicitly]
internal sealed class LiteContextProvider : ContextProviderBase<LiteContext>
public sealed class LiteContextProvider : ContextProviderBase<LiteContext>
{
/// <inheritdoc />
public LiteContextProvider(
Expand Down
22 changes: 15 additions & 7 deletions src/Fluxera.Repository.MongoDB/MongoContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ public abstract class MongoContext : Disposable
{
private static readonly ConcurrentDictionary<string, IMongoClient> Clients = new ConcurrentDictionary<string, IMongoClient>();

private IMongoClient client;
private ConcurrentQueue<Func<Task>> commands;
private IMongoDatabase database;

private bool isConfigured;

Expand All @@ -52,6 +50,16 @@ protected MongoContext()
/// </summary>
public IClientSessionHandle Session { get; private set; }

/// <summary>
/// Gets the client for this context.
/// </summary>
public IMongoClient Client { get; private set; }

/// <summary>
/// Gets the database for this context.
/// </summary>
public IMongoDatabase Database { get; private set; }

/// <summary>
/// Adds a command for execution.
/// </summary>
Expand Down Expand Up @@ -176,18 +184,18 @@ internal void Configure(RepositoryName repositoryName, IServiceProvider serviceP
}
}

this.client = Clients[connectionString];
this.database = this.client.GetDatabase(databaseName);
this.Client = Clients[connectionString];
this.Database = this.Client.GetDatabase(databaseName);

// Start a transaction, if UoW is configured and the cluster is a replica set..
IRepositoryRegistry repositoryRegistry = serviceProvider.GetRequiredService<IRepositoryRegistry>();
RepositoryOptions repositoryOptions = repositoryRegistry.GetRepositoryOptionsFor(repositoryName);

if(repositoryOptions.IsUnitOfWorkEnabled)
{
if(this.client.Cluster.Description.Type == ClusterType.ReplicaSet)
if(this.Client.Cluster.Description.Type == ClusterType.ReplicaSet)
{
this.Session = this.client.StartSession();
this.Session = this.Client.StartSession();
this.Session.StartTransaction();
}
else
Expand All @@ -213,7 +221,7 @@ internal void Configure(RepositoryName repositoryName, IServiceProvider serviceP
internal IMongoCollection<T> GetCollection<T>()
{
string collectionName = typeof(T).Name.Pluralize();
return this.database.GetCollection<T>(collectionName);
return this.Database.GetCollection<T>(collectionName);
}

private void ClearCommands()
Expand Down
3 changes: 2 additions & 1 deletion src/Fluxera.Repository.MongoDB/MongoContextProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
using System;
using JetBrains.Annotations;

/// <inheritdoc />
[UsedImplicitly]
internal sealed class MongoContextProvider : ContextProviderBase<MongoContext>
public sealed class MongoContextProvider : ContextProviderBase<MongoContext>
{
/// <inheritdoc />
public MongoContextProvider(
Expand Down

0 comments on commit e4abf5e

Please sign in to comment.