Mongo.Common.Repo 2.0.0

dotnet add package Mongo.Common.Repo --version 2.0.0
                    
NuGet\Install-Package Mongo.Common.Repo -Version 2.0.0
                    
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.Common.Repo" Version="2.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Mongo.Common.Repo" Version="2.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Mongo.Common.Repo" />
                    
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.Common.Repo --version 2.0.0
                    
#r "nuget: Mongo.Common.Repo, 2.0.0"
                    
#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.Common.Repo@2.0.0
                    
#: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.Common.Repo&version=2.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Mongo.Common.Repo&version=2.0.0
                    
Install as a Cake Tool

MongoDB Common Repo Package

Table of Contents

  1. Introduction
  2. Getting Started
  3. Usage Guide
  4. Links

Introduction


This package is aimed at simplifying connection to, and performing CRUD operation on MongoDB.

Getting Started


To use this package, it is required to have MongoDbSettings section in the appsettings.json with DatabaseName and ConnectionString properties.

  • All the database models must implement the IBaseEntity interface with include the Id: Guid, IsDeprecated: bool, UpdatedOn: DateTime and CreatedOn: DateTime properties.
  • The name of the model class would be the collection name.
  • In the Program.cs class, register the ConfigureMongoSettings() to the service pipeline. In addition to the IServiceCollections, the extension method also accepts the connectionString and databaseName interface.
  • Create a repository class that implements the Repository<TCollection> class. The TCollection should be a model class that implements the IBaseEntity. The base class constructor accepts an instance of MongoDbSettings.

Usage Guide


Say you want to have a collection named Person on MongoDB, create a class with that name Person, and implement the IBaseEntity interface. This class will have the Name property in addition to properties implemented from the interface:

{
    public Guid Id { get; set; }
    public DateTime CreatedOn { get; set; }
    public DateTime UpdatedOn { get; set; }
    public bool IsDeprecated { get; set; }

    public string Name { get; set; }
}

Add this line in the Program.cs

builder.Services.ConfigureMongoSettings("localhost:27017", "PersonDb");

Add a repository class and an interface to implement. This repository class will inherit from the Repository class which has implementations of methods to perform operations with Mongo DB. Let's call this class and interface, PersonRepository and IPersonRepository respectively. We are only going to show the content for the PersonRepository.cs

public class PersonRepository : Repository<Person>, IPersonRepository
{
    public PersonRepository(MongoDbSettings settings) : base(settings) {}

    public async Task AddAsync(Person person) =>
        await CreateAsync(person);

    public async Task AddRangeAsync(ICollection<Person> persons) =>
        await CreateManyAsync(persons);

    public async Task EditAsync(Expression<Func<Person, bool>> expression, Person person) =>
        await UpdateAsync(expression, person);

    public async Task DeleteAsync(Expression<Func<Person, bool>> expression) =>
        await RemoveAsync(expression);

    public async Task<Person?> FindAsync(Expression<Func<Person, bool>> expression) =>
        await GetAsync(expression);

    public async Task<ICollection<Person>> FindManyAsync(Expression<Func<Person, bool>> expression) =>
        await GetManyAsync(expression);

    public IQueryable<Person> FindAsQueryable() =>
        GetAsQueryable();

    public async Task<long> Count(Expression<Func<Person, bool>> expression) =>
        await CountAsync(expression);

    public async Task DeleteMany(Expression<Func<Person, bool>> expression, CancellationToken cancellationToken) =>
        await _collection.RemoveManyAsync(expression, cancellationToken);

    public async Task<bool> Exists(Expression<Func<Person, bool>> expression) =>
        await ExistsAsync(expression);
}

You can then, through the IPersonRepository, call this method. Say you injected this interface into a class with the name repository, you can call the method that gets a Person by id as such: var person = await repository.FindAsync(p => p.Id.Equals(id));


To view the source code or get in touch:

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
2.0.0 190 1/16/2025
2.0.0-rc1 115 1/7/2025
1.1.0 118 2/11/2025
1.0.0 320 1/5/2024

- Update README.md