Skip to content

teqbench/teqbench.system.data.nosql.repository

Repository files navigation

System Data NoSql Repository

Build Status Badge Build Number Badge Coverage

Overview

Base implementation for a NoSql document repository. Override the appropriate methods to create DB if does not exist, create collection if does not exist (might not need to implement as in case of MongoDB...collection will be created upon first reference if does not exist), as well as methods to dispose of managed/unmanaged objects.

Contents

Developer Environment Setup

General

.NET

Usage

Add NuGet Package To Project

dotnet add package TeqBench.System.Data.NoSql.Repository

Update Source Code

/// <summary>
/// MongoDB respository operations interface.
/// </summary>
public interface IRepository<TDocument> : IRepository where TDocument : IDocument
{
}

/// <summary>
/// Base implementation for a repository. Override the appropriate methods to create DB if does not exist,
/// create collection if does not exist (might not need to implement as in case of MongoDB...collection will be created upon
/// first reference if does not exist), as well as methods to dispose of managed/unmanaged objects.
/// </summary>
public abstract class BaseRepository : IRepository
{
    /// <summary>
    /// Initializes this instance.
    /// </summary>
    public virtual async void InitializeAsync()
    {
        // Cannot have async operations in contructor, so create an async init routine to ecapsulate 
        // async operations to be performed right have instance is created. 
        await this.CreateDatabaseIfDoesNotExistAsync();
        await this.CreateCollectionIfDoesNotExistAsync();
    }

    /// <summary>
    /// Creates the database if does not exist asynchronously.
    /// </summary>
    protected virtual async Task CreateDatabaseIfDoesNotExistAsync()
    {
        await Task.CompletedTask;
    }

    /// <summary>
    /// Creates the document collection if does not exist asynchronously.
    /// </summary>
    protected virtual async Task CreateCollectionIfDoesNotExistAsync()
    {
        await Task.CompletedTask;
    }
}

Licensing

License