Table of Contents

Class DefaultMongoClientFactory<TContext>

Namespace
Arc4u.MongoDB
Assembly
Arc4u.MongoDB.dll

Default IMongoClientFactory<TContext>: creates the client lazily, once, from the named MongoDB.Driver.MongoClientSettings of the database.

public class DefaultMongoClientFactory<TContext> : IMongoClientFactory<TContext> where TContext : DbContext

Type Parameters

TContext

The type of the database context.

Inheritance
DefaultMongoClientFactory<TContext>
Implements
Inherited Members

Constructors

DefaultMongoClientFactory(IOptionsMonitor<MongoClientSettings>, IServiceProvider)

Creates the factory.

public DefaultMongoClientFactory(IOptionsMonitor<MongoClientSettings> clientSettings, IServiceProvider serviceProvider)

Parameters

clientSettings IOptionsMonitor<MongoClientSettings>

The named client settings; the name is the database name in lower case.

serviceProvider IServiceProvider

Used to resolve the registered TContext.

Exceptions

InvalidOperationException

TContext is not registered.

Methods

CreateClient()

Gets the MongoDB client of the context.

public IMongoClient CreateClient()

Returns

IMongoClient

The client.

Remarks

The client is created once from the named MongoDB.Driver.MongoClientSettings of the database.

Exceptions

MongoClientException

No MongoDB.Driver.MongoClientSettings are registered under the database name in lower case.

GetCollection<TEntity>()

Gets the collection the entity type is mapped to in the DbContext.

public IMongoCollection<TEntity> GetCollection<TEntity>()

Returns

IMongoCollection<TEntity>

The collection.

Type Parameters

TEntity

The document type of the collection.

Exceptions

TypeNotMappedToCollectionException

TEntity is not mapped to a collection.

TypeMappedToMoreThanOneCollectionException<TEntity>

TEntity is mapped to several collections.

GetCollection<TEntity>(string)

Gets a collection of the database by name.

public IMongoCollection<TEntity> GetCollection<TEntity>(string collectionName)

Parameters

collectionName string

The name of the collection.

Returns

IMongoCollection<TEntity>

The collection.

Type Parameters

TEntity

The document type of the collection.