Class MongoRepository<T>
- Namespace
- adas_core.Infrastructure.Repositories
- Assembly
- adas-core.Infrastructure.dll
Abstract base repository providing common MongoDB persistence operations for a given document type T.
Concrete repositories must implement GetCollectionName() to identify the target collection,
and may override CreateIndexes(), InsertInitialLoad(), and InsertOneAsync(T)
to customize schema setup, seeding, and insertion behavior.
public abstract class MongoRepository<T> : IMongoRepository<T>
Type Parameters
TThe domain model type persisted in the MongoDB collection.
- Inheritance
-
MongoRepository<T>
- Implements
- Derived
- Inherited Members
- Extension Methods
Constructors
MongoRepository(IMongoDatabase)
Initializes a new instance of the MongoRepository<T> class using the provided database.
protected MongoRepository(IMongoDatabase database)
Parameters
databaseIMongoDatabaseThe MongoDB database instance used to access collections. Must not be null.
Fields
Db
The MongoDB database instance used by the repository.
protected readonly IMongoDatabase Db
Field Value
- IMongoDatabase
Properties
Collection
Gets the underlying MongoDB.Driver.IMongoCollection<TDocument> for the repository. On first access, ensures the collection exists (creating it if necessary), then triggers asynchronous index creation and initial data loading via CreateIndexes() and InsertInitialLoad().
public IMongoCollection<T> Collection { get; set; }
Property Value
- IMongoCollection<T>
The MongoDB collection of
Tdocuments.
Methods
CollectionExists(string)
Determines whether a collection with the specified name exists in the current database. Errors are logged and the method returns false in that case.
protected bool CollectionExists(string collectionName)
Parameters
collectionNamestringThe name of the collection to check.
Returns
- bool
true if a collection with the given name exists; otherwise, false. Returns false when an exception is thrown while querying the database.
CreateIndexes()
Creates the indexes required for the collection. The base implementation is a no-op; derived classes should override this method to define their own indexes.
public virtual Task CreateIndexes()
Returns
DeleteAsync(ObjectId)
Asynchronously finds and deletes a single document identified by its _id.
Returns the deleted document, or the default value of T if not found or on error.
public Task<T?> DeleteAsync(ObjectId id)
Parameters
idObjectIdThe MongoDB.Bson.ObjectId value of the document's
_idfield.
Returns
- Task<T>
A Task<TResult> representing the asynchronous operation. The task result contains the deleted document, or null / default if no document matched or an error occurred.
DeleteAsync(string)
Asynchronously finds and deletes a single document identified by a string _id.
Returns the deleted document, or the default value of T if not found or on error.
protected Task<T?> DeleteAsync(string id)
Parameters
idstringThe string value of the document's
_idfield.
Returns
- Task<T>
A Task<TResult> representing the asynchronous operation. The task result contains the deleted document, or null / default if no document matched or an error occurred.
GetCollectionName()
When implemented in a derived class, returns the name of the MongoDB collection used to store documents of type T.
public abstract string GetCollectionName()
Returns
- string
The MongoDB collection name as a string.
InsertInitialLoad()
Performs an initial data load (seeding) for the collection. The base implementation is a no-op; derived classes should override this method to provide custom seeding logic.
public virtual Task InsertInitialLoad()
Returns
InsertManyAsync(List<T>)
Asynchronously inserts multiple documents into the collection using unordered semantics (a single failed insert does not abort the batch). Errors are logged and swallowed.
public virtual Task InsertManyAsync(List<T> obj)
Parameters
objList<T>The list of documents of type
Tto insert.
Returns
InsertOneAsync(T)
Asynchronously inserts a single document into the collection. Errors are logged and swallowed.
public virtual Task InsertOneAsync(T obj)
Parameters
objTThe document of type
Tto insert.
Returns
UpdateManyObjectIdAsync(string, ObjectId, ObjectId)
Asynchronously updates all documents in the collection where the specified field equals oldId,
setting that field to the new id. Errors are logged and swallowed.
protected Task UpdateManyObjectIdAsync(string nameId, ObjectId id, ObjectId oldId)
Parameters
nameIdstringThe name of the field to match and update.
idObjectIdThe new MongoDB.Bson.ObjectId value to assign.
oldIdObjectIdThe existing MongoDB.Bson.ObjectId value to replace.
Returns
UpdateOneAsync(ObjectId, T)
Asynchronously replaces (or upserts) a single document identified by its _id.
Uses MongoDB.Driver.ReplaceOptions with MongoDB.Driver.ReplaceOptions.IsUpsert set to true
so that the document is created if it does not exist. Errors are logged and swallowed.
public Task UpdateOneAsync(ObjectId id, T obj)
Parameters
idObjectIdThe MongoDB.Bson.ObjectId value of the document's
_idfield.objTThe replacement document of type
T.