Mongo.Common.Repo
1.0.0
dotnet add package Mongo.Common.Repo --version 1.0.0
NuGet\Install-Package Mongo.Common.Repo -Version 1.0.0
<PackageReference Include="Mongo.Common.Repo" Version="1.0.0" />
paket add Mongo.Common.Repo --version 1.0.0
#r "nuget: Mongo.Common.Repo, 1.0.0"
// Install Mongo.Common.Repo as a Cake Addin #addin nuget:?package=Mongo.Common.Repo&version=1.0.0 // Install Mongo.Common.Repo as a Cake Tool #tool nuget:?package=Mongo.Common.Repo&version=1.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
,UpdatedAt: DateTime
andCreatedAt: DateTime
properties. - The name of the model class would be the collection name.
- In the
Program.cs
class, register theConfigureMongoConnection()
to the service pipeline. In addition to theIServiceCollections
, the extension method also accepts theIConfiguration
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 ofIOptions<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 CreatedAt { get; set; }
public DateTime UpdatedAt { get; set; }
public bool IsDeprecated { get; set; }
public string Name { get; set; }
}
Add this section to the appsettings.json
"MongoDbSettings": {
"ConnectionString": "https://localhost:27017",
"DatabaseName": "DatabaseName"
}
Add this to the container in the Program.cs
builder.Services.ConfigureMongoConnection(builder.Configuration);
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(IOptions<MongoDbSettings> options) : base(options) {}
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<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 | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. 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. |
-
net6.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.
Version | Downloads | Last updated |
---|---|---|
1.0.0 | 169 | 1/5/2024 |