Pixiray.AI.Data.Mongodb
0.0.1
dotnet add package Pixiray.AI.Data.Mongodb --version 0.0.1
NuGet\Install-Package Pixiray.AI.Data.Mongodb -Version 0.0.1
<PackageReference Include="Pixiray.AI.Data.Mongodb" Version="0.0.1" />
<PackageVersion Include="Pixiray.AI.Data.Mongodb" Version="0.0.1" />
<PackageReference Include="Pixiray.AI.Data.Mongodb" />
paket add Pixiray.AI.Data.Mongodb --version 0.0.1
#r "nuget: Pixiray.AI.Data.Mongodb, 0.0.1"
#:package Pixiray.AI.Data.Mongodb@0.0.1
#addin nuget:?package=Pixiray.AI.Data.Mongodb&version=0.0.1
#tool nuget:?package=Pixiray.AI.Data.Mongodb&version=0.0.1
Pixiray.AI.Data.Mongodb
A high-performance .NET MongoDB repository library with flexible entity support, automatic caching, bulk operations, and comprehensive querying capabilities. Build modern applications without being forced to inherit from base entities.
โจ Features
- ๐ฅ Flexible Entity System - Works with POCOs, BsonDocument, and ExpandoObject
- โก High Performance - Async-first with parallel operations and buffer optimization
- ๐ง Smart Caching - Automatic timestamp-based cache invalidation
- ๐ฆ Bulk Operations - Efficient bulk insert, update, and delete operations
- ๐ Advanced Querying - LINQ expressions with pattern matching support
- ๐๏ธ Dependency Injection - First-class DI support with decorator pattern
- ๐ Observability - Built-in logging, metrics, and transaction auditing
- ๐ Unit of Work - Transaction support with automatic rollback
- ๐ฏ Zero Inheritance - No need to inherit from base entity classes
๐ Quick Start
Installation
dotnet add package Pixiray.AI.Data.Mongodb
Basic Setup
using Pixiray.AI.Data.Mongodb.Extensions;
// Configure services
services.AddMongoDbRepositories("mongodb://localhost:27017", "MyDatabase", options =>
{
options.EnableAutoTimestamps = true;
options.HandleBsonDocuments = true;
options.HandleExpandoObjects = true;
});
// Add caching
services.AddRepositoryCaching(TimeSpan.FromMinutes(10));
Simple Entity Example
// No inheritance required!
public class User
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string Id { get; set; } = ObjectId.GenerateNewId().ToString();
public string Name { get; set; } = string.Empty;
public string Email { get; set; } = string.Empty;
public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
}
// Use in your service
public class UserService
{
private readonly IRepository<User> _userRepository;
public UserService(IRepository<User> userRepository)
{
_userRepository = userRepository;
}
public async Task<User> CreateUserAsync(string name, string email)
{
var user = new User { Name = name, Email = email };
await _userRepository.InsertAsync(user);
return user;
}
public async Task<User?> GetUserAsync(string id)
{
return await _userRepository.GetByIdAsync(id); // Cached automatically!
}
public async Task<IEnumerable<User>> SearchUsersAsync(string searchTerm)
{
return await _userRepository.ListAsync(u => u.Name.Contains(searchTerm));
}
}
๐ฏ Entity Flexibility
Work with any entity type:
1. Simple POCOs
public class Product
{
public string Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
2. BsonDocument (Dynamic)
var document = new BsonDocument
{
["_id"] = ObjectId.GenerateNewId(),
["name"] = "Dynamic Product",
["properties"] = new BsonDocument
{
["color"] = "blue",
["size"] = "large"
}
};
await _bsonRepository.InsertAsync(document);
3. ExpandoObject (Dynamic .NET)
dynamic entity = new ExpandoObject();
entity._id = ObjectId.GenerateNewId().ToString();
entity.name = "Dynamic Entity";
entity.isActive = true;
await _expandoRepository.InsertAsync(entity);
๐ง Smart Caching
The library automatically manages cache timestamps without requiring inheritance:
// First call - fetches from database and caches
var user = await userRepository.GetByIdAsync("123");
// Second call - returns from cache (much faster!)
var cachedUser = await userRepository.GetByIdAsync("123");
// Update invalidates cache automatically
user.Name = "Updated Name";
await userRepository.UpdateAsync(user);
// Next call fetches fresh data from database
var freshUser = await userRepository.GetByIdAsync("123");
๐ฆ Bulk Operations
Efficient bulk operations for high-throughput scenarios:
// Bulk insert
var users = new List<User> { /* ... */ };
await bulkRepository.BulkInsertAsync(users);
// Bulk update
var updates = users.Select(u => new { Filter = u.Id, Update = u });
await bulkRepository.BulkUpdateAsync(updates);
// Bulk delete
var idsToDelete = new[] { "id1", "id2", "id3" };
await bulkRepository.BulkDeleteAsync(idsToDelete);
๐ Unit of Work & Transactions
public class OrderService
{
private readonly IUnitOfWork _unitOfWork;
public async Task ProcessOrderAsync(Order order)
{
await _unitOfWork.BeginTransactionAsync();
try
{
var userRepo = _unitOfWork.GetRepository<User>();
var productRepo = _unitOfWork.GetRepository<Product>();
var orderRepo = _unitOfWork.GetRepository<Order>();
// All operations within the same transaction
await userRepo.UpdateAsync(order.User);
await productRepo.UpdateAsync(order.Product);
await orderRepo.InsertAsync(order);
await _unitOfWork.CommitAsync();
}
catch
{
await _unitOfWork.RollbackAsync();
throw;
}
}
}
๐ Advanced Querying
// Complex LINQ queries
var activeUsers = await repository.ListAsync(u =>
u.IsActive &&
u.CreatedAt > DateTime.UtcNow.AddDays(-30) &&
u.Roles.Contains("Admin"));
// Pattern matching
var users = await repository.ListByPatternAsync("Name", "*john*", PatternType.Wildcard);
// Pagination
var (users, totalCount) = await repository.GetPagedAsync(
filter: u => u.IsActive,
pageNumber: 1,
pageSize: 20,
orderBy: u => u.CreatedAt);
๐ Configuration Options
services.AddMongoDbRepositories(connectionString, databaseName, options =>
{
// Automatic timestamp injection
options.EnableAutoTimestamps = true;
options.TimestampFieldName = "__cacheTimestamp";
// Entity type support
options.HandleBsonDocuments = true;
options.HandleExpandoObjects = true;
// Backward compatibility
options.UseTimestampedEntityFallback = true;
options.PreserveUserTimestamps = true;
// Exclude specific types from automatic timestamping
options.ExcludedTypes.Add(typeof(AuditLog));
});
๐๏ธ Architecture
The library follows clean architecture principles with:
- Repository Pattern - Simple, testable data access
- Decorator Pattern - Composable features (caching, retry, logging)
- Dependency Injection - Framework-agnostic IoC support
- Async/Await - Non-blocking operations throughout
- Interface Segregation - Focused, single-responsibility interfaces
๐ Documentation
๐งช Testing
# Run tests
dotnet test
# Run with coverage
dotnet test --collect:"XPlat Code Coverage"
๐ค Contributing
We welcome contributions! Please see our Contributing Guide for details.
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests
- Submit a pull request
๐ License
This project is licensed under the MIT License - see the LICENSE file for details.
๐โโ๏ธ Support
- ๐ Documentation
- ๐ Issue Tracker
- ๐ฌ Discussions
๐ฏ Roadmap
- Redis caching support
- GraphQL integration
- More database providers
- Performance optimizations
- Additional bulk operations
Built with โค๏ธ by Pixiray AI
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. |
-
net9.0
- Microsoft.Extensions.Caching.Memory (>= 9.0.7)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.7)
- Microsoft.Extensions.Hosting.Abstractions (>= 9.0.7)
- Microsoft.Extensions.Logging.Abstractions (>= 9.0.7)
- Microsoft.Extensions.Options (>= 9.0.7)
- MongoDB.Driver (>= 3.4.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 |
---|---|---|
0.0.1 | 436 | 7/24/2025 |
See https://github.com/pixiray/Data.MongoDB/releases for release notes.