AmelioMongoRepository 1.0.0
dotnet add package AmelioMongoRepository --version 1.0.0
NuGet\Install-Package AmelioMongoRepository -Version 1.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="AmelioMongoRepository" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="AmelioMongoRepository" Version="1.0.0" />
<PackageReference Include="AmelioMongoRepository" />
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 AmelioMongoRepository --version 1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: AmelioMongoRepository, 1.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 AmelioMongoRepository@1.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=AmelioMongoRepository&version=1.0.0
#tool nuget:?package=AmelioMongoRepository&version=1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
AmelioMongoRepository
A lightweight and extensible MongoDB repository pattern for .NET applications. Supports full CRUD operations, custom queries, and aggregate joins with simple integration.
๐ฆ Installation
Add the NuGet package:
dotnet add package MongoRepositoryPackage
โ๏ธ Configuration
In your appsettings.json, add your MongoDB settings:
{
"MongoDbSettings": {
"ConnectionString": "mongodb://localhost:27017",
"DatabaseName": "YourDatabaseName"
}
}
๐ Getting Started
1. Setup your Models
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
using MongoRepositoryPackage.Attributes;
public class Category : IEntity
{
[BsonId(IdGenerator = typeof(UniqueIdGenerator))]
public string Id { get; set; }
public string Title { get; set; }
}
public class Product : IEntity
{
[BsonId(IdGenerator = typeof(UniqueIdGenerator))]
public string Id { get; set; }
public string Name { get; set; }
public double Price { get; set; }
public string CategoryId { get; set; }
}
2. Register Repositories
var builder = Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((context, config) =>
{
config.AddJsonFile("appsettings.json", optional: false);
})
.ConfigureServices((context, services) =>
{
services.AddMongoRepository<Product>(context.Configuration);
services.AddMongoRepository<Category>(context.Configuration);
});
var host = builder.Build();
3. Use the Repositories
var productRepo = host.Services.GetRequiredService<IMongoRepository<Product>>();
var categoryRepo = host.Services.GetRequiredService<IMongoRepository<Category>>();
// Insert category
var newCategory = new Category { Title = "Electronics" };
await categoryRepo.InsertOneAsync(newCategory);
// Insert product linked to category
var newProduct = new Product { Name = "Laptop", Price = 1500.00, CategoryId = newCategory.Id };
await productRepo.InsertOneAsync(newProduct);
// Fetch all
var products = await productRepo.GetAllAsync();
Console.WriteLine($"Found {products.Count} products.");
4. Aggregate Lookup Example
Join Product with Category (one-to-one relationship):
public class ProductWithCategory
{
public string Id { get; set; }
public string Name { get; set; }
public double Price { get; set; }
public string CategoryId { get; set; }
public Category Category { get; set; }
}
public class ProductAggregateService
{
private readonly IMongoCollection<Product> _products;
private readonly IMongoCollection<Category> _categories;
public ProductAggregateService(IMongoRepository<Product> productRepo, IMongoRepository<Category> categoryRepo)
{
_products = productRepo.AsCollection();
_categories = categoryRepo.AsCollection();
}
public async Task<List<ProductWithCategory>> GetProductsWithCategoryAsync()
{
var unwindOptions = new AggregateUnwindOptions<ProductWithCategory> { PreserveNullAndEmptyArrays = true };
return await _products.Aggregate()
.Lookup<Product, Category, ProductWithCategory>(
_categories,
p => p.CategoryId,
c => c.Id,
result => result.Category)
.Unwind(result => result.Category, unwindOptions)
.ToListAsync();
}
}
๐งช Output
Found 2 product(s).
Found 2 product(s) with category.
Test Product 1 -> Category 1
Test Product 2 -> Category 2
โ Features
- Generic repository pattern with DI support
- Attribute-based ID generation (
[BsonId(IdGenerator = typeof(UniqueIdGenerator))]) - Aggregation with
$lookupsupport - Clean MongoDB collection naming
- Easily extendable
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net9.0 is compatible. 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.
-
net9.0
- Microsoft.Extensions.Configuration (>= 10.0.0-preview.6.25358.103)
- Microsoft.Extensions.Configuration.Binder (>= 10.0.0-preview.6.25358.103)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.0-preview.6.25358.103)
- MongoDB.Driver (>= 3.4.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 |
|---|---|---|
| 1.0.0 | 87 | 8/1/2025 |