using MongoDbGenericRepository.DataAccess.Create;
using MongoDbGenericRepository.Models;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace MongoDbGenericRepository
{
///
/// The base Repository, it is meant to be inherited from by your custom custom MongoRepository implementation.
/// Its constructor must be given a connection string and a database name.
///
public abstract partial class BaseMongoRepository : IBaseMongoRepository_Create
{
private readonly object _initLock = new object();
private volatile IMongoDbCreator _mongoDbCreator;
///
/// The MongoDbCreator field.
///
protected virtual IMongoDbCreator MongoDbCreator
{
get
{
if (_mongoDbCreator != null) { return _mongoDbCreator; }
lock (_initLock)
{
if (_mongoDbCreator == null)
{
_mongoDbCreator = new MongoDbCreator(MongoDbContext);
}
}
return _mongoDbCreator;
}
set { _mongoDbCreator = value; }
}
///
/// Asynchronously adds a document to the collection.
/// Populates the Id and AddedAtUtc fields if necessary.
///
/// The type representing a Document.
/// The type of the primary key for a Document.
/// The document you want to add.
/// An optional cancellation Token.
public virtual async Task AddOneAsync(TDocument document, CancellationToken cancellationToken = default)
where TDocument : IDocument
where TKey : IEquatable
{
await MongoDbCreator.AddOneAsync(document, cancellationToken);
}
///
/// Asynchronously adds a document to the collection.
/// Populates the Id and AddedAtUtc fields if necessary.
///
/// The type representing a Document.
/// The document you want to add.
/// An optional cancellation Token.
public virtual async Task AddOneAsync(TDocument document, CancellationToken cancellationToken = default)
where TDocument : IDocument
{
await MongoDbCreator.AddOneAsync(document, cancellationToken);
}
///
/// Adds a document to the collection.
/// Populates the Id and AddedAtUtc fields if necessary.
///
/// The type representing a Document.
/// The type of the primary key for a Document.
/// The document you want to add.
public virtual void AddOne(TDocument document)
where TDocument : IDocument
where TKey : IEquatable
{
MongoDbCreator.AddOne(document);
}
///
/// Adds a document to the collection.
/// Populates the Id and AddedAtUtc fields if necessary.
///
/// The type representing a Document.
/// The document you want to add.
public virtual void AddOne(TDocument document) where TDocument : IDocument
{
MongoDbCreator.AddOne(document);
}
///
/// Asynchronously adds a list of documents to the collection.
/// Populates the Id and AddedAtUtc fields if necessary.
///
/// The type representing a Document.
/// The type of the primary key for a Document.
/// The documents you want to add.
/// An optional cancellation Token.
public virtual async Task AddManyAsync(IEnumerable documents, CancellationToken cancellationToken = default)
where TDocument : IDocument
where TKey : IEquatable
{
await MongoDbCreator.AddManyAsync(documents, cancellationToken);
}
///
/// Asynchronously adds a list of documents to the collection.
/// Populates the Id and AddedAtUtc fields if necessary.
///
/// The type representing a Document.
/// The documents you want to add.
/// An optional cancellation Token.
public virtual async Task AddManyAsync(IEnumerable documents, CancellationToken cancellationToken = default)
where TDocument : IDocument
{
await MongoDbCreator.AddManyAsync(documents, cancellationToken);
}
///
/// Adds a list of documents to the collection.
/// Populates the Id and AddedAtUtc fields if necessary.
///
/// The type representing a Document.
/// The type of the primary key for a Document.
/// The documents you want to add.
public virtual void AddMany(IEnumerable documents)
where TDocument : IDocument
where TKey : IEquatable
{
MongoDbCreator.AddMany(documents);
}
///
/// Adds a list of documents to the collection.
/// Populates the Id and AddedAtUtc fields if necessary.
///
/// The type representing a Document.
/// The documents you want to add.
public virtual void AddMany(IEnumerable documents)
where TDocument : IDocument
{
MongoDbCreator.AddMany(documents);
}
}
}