Zeltik.Mongo.PersonalExtensions
0.0.0.1-Alpha
dotnet add package Zeltik.Mongo.PersonalExtensions --version 0.0.0.1-Alpha
NuGet\Install-Package Zeltik.Mongo.PersonalExtensions -Version 0.0.0.1-Alpha
<PackageReference Include="Zeltik.Mongo.PersonalExtensions" Version="0.0.0.1-Alpha" />
<PackageVersion Include="Zeltik.Mongo.PersonalExtensions" Version="0.0.0.1-Alpha" />
<PackageReference Include="Zeltik.Mongo.PersonalExtensions" />
paket add Zeltik.Mongo.PersonalExtensions --version 0.0.0.1-Alpha
#r "nuget: Zeltik.Mongo.PersonalExtensions, 0.0.0.1-Alpha"
#:package Zeltik.Mongo.PersonalExtensions@0.0.0.1-Alpha
#addin nuget:?package=Zeltik.Mongo.PersonalExtensions&version=0.0.0.1-Alpha&prerelease
#tool nuget:?package=Zeltik.Mongo.PersonalExtensions&version=0.0.0.1-Alpha&prerelease
Zeltik.Mongo "Extension"
This extension is packaged for my own personal use. It is not recommended for general use unless you want to have a headache and lost some time.
Description
This extension provides additional functionality for interacting with MongoDB. It includes some utilities that I find useful for me in my own projects. However, please note that this extension may not have undergone extensive testing and may not be suitable for production environments.
Features
- Extension method for
IServiceCollectionto simplify MongoDB integration - Integration of MongoDB context and options using
MongoDbContext(specifically, inheriting it) - Simplified usage of
MongoDB.Driverlibrary (I guess)
Installation
You can install the Zeltik.Mongo Extension as a NuGet package. Follow the steps below:
- Open your project in Visual Studio.
- Right-click on your project in the Solution Explorer and select "Manage NuGet Packages".
- In the NuGet Package Manager, search for "Zeltik.Mongo" and select the package in the search results.
- Click on the "Install" button to install the package into your project.
After installing the NuGet package, the necessary dependencies and files will be added to your project.
Usage
After installing the Zeltik.Mongo Extension, you can start using its utilities in your code. Here's an example of how to integrate MongoDB using the provided code:
//Import necessary namespaces
using Microsoft.Extensions.Options;
using Example.Models;
using MongoDB.Bson;
using MongoDB.Bson.Serialization;
using MongoDB.Bson.Serialization.IdGenerators;
using MongoDB.Bson.Serialization.Serializers;
using MongoDB.Driver;
using Zeltik.Mongo;
namespace Example.Data
{
//Inherit from MongoDbContext
public class AppDbContext : MongoDbContext
{
//This constructor is for dependency injection for one MongoOptions
public AppDbContext(IOptions<MongoOptions> options) : base(options) { }
//If you want to have multiple contexts and options, you'll need to use the next constructor but before, delete the first constructor
public AppDbContext(IOptionsMonitor<MongoOptions> manager) : base(manager.Get("instanceName")) { }
//Use GetCollection as a get accessor for your Context properties
public IMongoCollection<Product> Products => GetCollection<Product>("Products");
//Use this method for Configure your entities
protected override void OnConfiguring()
{
ConfigureProductMap();
}
private static void ConfigureProductMap()
{
BsonClassMap.RegisterClassMap<Product>(cm =>
{
cm.AutoMap();
cm.SetIgnoreExtraElements(false);
cm.MapIdMember(x => x.Id).SetIdGenerator(StringObjectIdGenerator.Instance)
.SetSerializer(new StringSerializer(BsonType.ObjectId)).SetIgnoreIfDefault(true);
cm.SetIdMember(cm.GetMemberMap(x => x.Id));
});
}
}
}
//Then, add your context to your IServiceCollection
builder.Services.AddMongoContext<AppDbContext>(options => options.SetDatabaseName("your_databaseName").SetConnectionString("your_connection"));
//And finally (I guess), inject it to your service or repository (basically, wherever you want)
public class ProductRepository : IProductRepository
{
private readonly AppDbContext _context;
public ProductRepository(AppDbContext context)
{
_context = context;
}
public async Task<Product> GetAsync(string id)
{
return await (await _context.Products.FindAsync(new BsonDocument("_id", new ObjectId(id)))).FirstOrDefaultAsync();
}
public async Task<IEnumerable<Product>> GetAsync()
{
return await (await _context.Products.FindAsync(new BsonDocument())).ToListAsync();
}
}
Please note that the above example is just a basic usage demonstration. Refer to the documentation or the source code for more advanced usage scenarios (if casually there are).
Disclaimer
Warning: This Zeltik.Mongo "Extension" is provided "as is" and is intended for personal use only (or at least it's recommended just for that). It may contain bugs, limitations, or other issues that could cause headaches or unexpected behavior. Use it at your own risk.
Contributing
Contributions to this Zeltik.Mongo "Extension" are not currently accepted, as it is developed and maintained solely for personal use. However, if you find any bugs or have suggestions, feel free to open an issue in the repository.
Licensing
This extension (Zeltik.Mongo) is released under the MIT License
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net7.0 is compatible. 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. 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. |
-
net7.0
- Microsoft.Extensions.Options (>= 7.0.1)
- MongoDB.Driver (>= 2.19.2)
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 |
|---|---|---|
| 0.0.0.1-Alpha | 177 | 6/20/2023 |