MongoDbService 3.1.0

dotnet add package MongoDbService --version 3.1.0
NuGet\Install-Package MongoDbService -Version 3.1.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="MongoDbService" Version="3.1.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add MongoDbService --version 3.1.0
#r "nuget: MongoDbService, 3.1.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.
// Install MongoDbService as a Cake Addin
#addin nuget:?package=MongoDbService&version=3.1.0

// Install MongoDbService as a Cake Tool
#tool nuget:?package=MongoDbService&version=3.1.0

MongoDbService

MongoDbService is an open-source C# class library that provides a wrapper around the official MongoDB.Driver

Features

  • Creates a collection called ConnectionRecord that keeps track of the number of compute instances that established a connection to your MongoDB instance.
  • Ensures that all your projects have a uniform way of reading your MongoDB configurations
  • Abstracts the code that is responsible for creating the connection to your MongoDB instance, so you can focus on your code having only the Business logic.

Contributing

We welcome contributions! If you find a bug, have an idea for improvement, please submit an issue or a pull request on GitHub.

Getting Started

NuGet Package

To include MongoDbService in your project, install the NuGet package:

dotnet add package MongoDbService

Then in your appsettings.json add the following sample configuration and change the values to mtch the details of your MongoDB instance.

"MongoDbSettings": {
  "MongoDatabaseName": "YourDatabaseName",
  "ConnectionString": "mongodb+srv://.........@gpcluster.0bulb.mongodb.net/myDatabase?retryWrites=true&w=majority"
}

After the above is done, you can just Dependency inject the MongoService in your C# class.

For example:

Lets say your had a DTO to represent a record of an IMongoCollection called Vehicle

ο»Ώusing MongoDB.Bson.Serialization.Attributes;

namespace YourNameSpace
{
    public sealed class Vehicle
    {
        [BsonId]
        public required string Id { get; init; }
        public required string  Name { get; set; }
    }
}

Then you could have another class called VehicleHandler to add and remove Vehicle instances as follows:

using MongoDB.Driver;
using MongoDbService;

namespace YourNameSpace
{
	public sealed class VehicleHandler
	{
		private IMongoCollection<Vehicle> _vehicleCollection;

		public VehicleHandler(
			MongoService mongoService
		)
		{
        _vehicleCollection = mongoService.Database.GetCollection<Vehicle>(nameof(Vehicle), new MongoCollectionSettings() { ReadConcern = ReadConcern.Majority, WriteConcern = WriteConcern.WMajority });
		}

		public async Task AddVehicle(string vehicleName)
		{
			await _vehicleCollection.InsertOneAsync(new Vehicle() { Id = Guid.NewGuid().ToString(), Name = vehicleName });
		}

		public async Task<DeleteResult> RemoveVehicle(string vehicleId)
		{
			return await _vehicleCollection.DeleteOneAsync(Builders<Vehicle>.Filter.Eq(v => v.Id, vehicleId));
		}
	}
}

GitHub Repository

Visit our GitHub repository for the latest updates, documentation, and community contributions. https://github.com/prmeyn/MongoDbService

License

This project is licensed under the GNU GENERAL PUBLIC LICENSE.

Happy coding! πŸš€πŸŒπŸ“š

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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on MongoDbService:

Package Downloads
MongoDbTokenManager

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
3.1.0 0 7/7/2024
3.0.0 5 7/6/2024
2.0.0 35 7/5/2024
1.0.0 73 6/23/2024