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
<PackageReference Include="Mongo.Common.Repo" Version="2.0.0" />
<PackageVersion Include="Mongo.Common.Repo" Version="2.0.0" />
<PackageReference Include="Mongo.Common.Repo" />
paket add Mongo.Common.Repo --version 2.0.0
#r "nuget: Mongo.Common.Repo, 2.0.0"
#:package Mongo.Common.Repo@2.0.0
#addin nuget:?package=Mongo.Common.Repo&version=2.0.0
#tool nuget:?package=Mongo.Common.Repo&version=2.0.0
MongoDB Common Repo Package
Table of Contents
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 theId: Guid
,IsDeprecated: bool
,UpdatedOn: DateTime
andCreatedOn: DateTime
properties. - The name of the model class would be the collection name.
- In the
Program.cs
class, register theConfigureMongoSettings()
to the service pipeline. In addition to theIServiceCollections
, the extension method also accepts theconnectionString
anddatabaseName
interface. - Create a repository class that implements the
Repository<TCollection>
class. TheTCollection
should be a model class that implements theIBaseEntity
. The base class constructor accepts an instance ofMongoDbSettings
.
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));
Links
To view the source code or get in touch:
Product | Versions 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. |
-
net8.0
- Microsoft.Extensions.Configuration.Abstractions (>= 8.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.0)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 8.0.0)
- MongoDB.Driver (>= 2.23.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
- Update README.md