Mongo.GenericClient 8.1.6

There is a newer version of this package available.
See the version list below for details.
dotnet add package Mongo.GenericClient --version 8.1.6
                    
NuGet\Install-Package Mongo.GenericClient -Version 8.1.6
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Mongo.GenericClient" Version="8.1.6" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Mongo.GenericClient" Version="8.1.6" />
                    
Directory.Packages.props
<PackageReference Include="Mongo.GenericClient" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Mongo.GenericClient --version 8.1.6
                    
#r "nuget: Mongo.GenericClient, 8.1.6"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Mongo.GenericClient@8.1.6
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Mongo.GenericClient&version=8.1.6
                    
Install as a Cake Addin
#tool nuget:?package=Mongo.GenericClient&version=8.1.6
                    
Install as a Cake Tool

Mongo.GenericClient

Generic library to manage a MongoDB database.

Required environment variables

  • "MONGODB_CONNECTION_STRING" (e.g., "mongodb://root:root@localhost:27017")

  • "MONGODB_DATABASE_NAME" (e.g., "my_mongodb")

Interfaces

Reposirory

namespace Mongo.GenericClient.Core.Repositories
{
    using Mongo.GenericClient.Core.Entities;
    using MongoDB.Driver;

    public interface IGenericRepository<TEntity>
        where TEntity : IEntity
    {
        IMongoCollection<TEntity> Collection { get; }
    }
}

Services

namespace Mongo.GenericClient.Core.Services
{
    using System.Collections.Generic;
    using System.Threading.Tasks;
    using Mongo.GenericClient.Core.Entities;
    using Mongo.GenericClient.Models;
    using MongoDB.Bson;

    public interface IReadService<TEntity>
        where TEntity : IEntity
    {
        IEnumerable<TEntity> GetAll(Expression<Func<TEntity, bool>>? filter = null);

        IMongoQueryable<TEntity> AsQueryable();

        Task<PaginationResult<TEntity>> GetPaginatedAsync(
            int pageNum,
            int pageSize);
        
        Task<PaginationResult<TEntity>> GetPaginatedAsync(
            Expression<Func<TEntity, bool>> filter,
            int pageNum,
            int pageSize);

        Task<TEntity> GetByIdAsync(ObjectId id);

        Task<TEntity> GetByIdAsync(string id);
    }
}
namespace Mongo.GenericClient.Core.Services
{
    using System.Threading.Tasks;
    using Mongo.GenericClient.Core.Entities;
    using MongoDB.Bson;

    public interface IWriteService<TEntity>
        where TEntity : IEntity
    {
        Task<TEntity> CreateAsync(TEntity entity);

        Task<bool> UpdateAsync(TEntity entity);

        Task DeleteAsync(ObjectId id);
        
        Task DeleteAsync(string id);
    }
}

IEntity

namespace Mongo.GenericClient.Core.Entities
{
    using MongoDB.Bson;

    public interface IEntity
    {
        ObjectId Id { get; set; }
    }
}

MongoHelper

namespace Mongo.GenericClient
{
    using MongoDB.Driver;

    public static class MongoHelper
    {
        private static readonly MongoClient client = new MongoClient(AppConfig.ConnectionString);
        private static readonly IMongoDatabase database = client.GetDatabase(AppConfig.DatabaseName);

        public static MongoClient Client => client;
        
        public static IMongoDatabase Database => database;
    }
}

Usage

Defining an entity and its collection name

The class must implement IEntity and have the attribute CollectionName

namespace Mongo.GenericClient.Tests.SetUp
{
    using Mongo.GenericClient.Core.Attributes;
    using Mongo.GenericClient.Core.Entities;
    using MongoDB.Bson;

    [CollectionName("persons")]
    public class PersonEntity : IEntity
    {
        public ObjectId Id { get ; set;  }

        public string Name { get; set; } = string.Empty;

        public int Age { get; set; }
    }
}

Creating a collection (or just inserting data)

Using the repository

await this.repository.Collection.InsertOneAsync(entity);

Using the service

await this.writeService.CreateAsync(entity);

Getting a list of documents as entities

var allEntities = this.readService.GetAll()
var filteredEntities = this.readService.GetAll(x => x.Age == 30)

Namespace Mongo.GenericClient.DependencyInjection

Some extension methods have been added to facilitate the registration of repositories and services.

Services and Repository separately:

services.RegisterGenericRepository<PersonEntity>(RegisterMode.Transient);
services.RegisterGenericServices<PersonEntity>(RegisterMode.Transient);

Services and repository at the same time:

services.RegisterGenericRepositoryAndServices<PersonEntity>(RegisterMode.Transient);

Note

  • In order to run the tests, you must first run docker compose up -d.

  • You can then manage the database at http://localhost:8081, with:

    • User: user
    • Password: pwd
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
10.1.5 82 9/4/2026
10.1.4 105 7/31/2026
10.1.3 103 5/17/2026
10.1.2 122 4/6/2026
10.1.1 115 2/28/2026
10.1.0 122 1/11/2026
10.0.1 120 1/3/2026
10.0.0 147 12/6/2025
9.0.4 198 9/20/2025
9.0.3 134 5/10/2025
9.0.2 167 3/28/2025
9.0.1 155 2/23/2025
9.0.0 163 12/8/2024
8.3.0 154 11/22/2024
8.2.0 154 11/1/2024
8.1.8 145 10/12/2024
8.1.7 158 10/9/2024
8.1.6 163 9/29/2024
8.1.5 159 9/29/2024
8.1.4 157 9/28/2024
Loading failed