Data MongoDB
Overview
VersaTul.Data.MongoDB provides repository-oriented access to MongoDB collections, along with configuration, mapping, connection override, and entity abstractions.
It is designed for projects that want MongoDB access wrapped in reusable repositories instead of scattering driver code throughout the application.
When To Use This Package
Use this package when you want to:
Build MongoDB repositories around strongly typed entities.
Define collection mapping and serialization behavior in one place.
Use predicate-based filtering while keeping repository code reusable.
Override connection targets dynamically for the same repository shape.
Keep MongoDB configuration concerns separated from business logic.
Installation
Install the package with the .NET CLI:
dotnet add package VersaTul.Data.MongoDB
Or with the Package Manager Console:
PM> NuGet\Install-Package VersaTul.Data.MongoDB -Version latest
Core Types And Concepts
IDataConfiguration<TKey>andDataConfiguration<TKey>MongoDB configuration types that expose connection name, timeout values, SSL settings, working database name, and collection lookup helpers.
IEntity<TKey>andEntityEntity abstractions for MongoDB document models.
IRepository<TEntity, TKey>andBaseRepository<...>Repository contracts and base implementations for CRUD, query, count, delete, update, and async operations.
IEntityMap<TEntity>andBaseMap<TEntity>Mapping abstractions for collection registration and BSON class-map configuration.
WherePredicate<TEntity>Predicate wrapper for repository filtering.
CollectionNameAttribute for explicitly controlling collection naming.
Key Capabilities
Repositories expose synchronous and asynchronous CRUD operations.
Repositories are queryable and can be composed with LINQ.
Collection name resolution supports attributes and base-entity conventions.
Configuration can create typed collections from connection strings or
MongoUrlinstances.Connection overrides let one repository shape target a different configured database.
Basic Repository Example
using VersaTul.Data.MongoDB;
using VersaTul.Data.MongoDB.Contracts;
public class Car : Entity
{
public string Make { get; set; }
public string Model { get; set; }
public int Year { get; set; }
}
public class CarMap : BaseMap<Car>
{
public CarMap() : base("cars")
{
}
}
public interface ICarRepository : IRepository<Car>
{
}
public class CarRepository : BaseRepository<Car, IEntityMap<Car>>, ICarRepository
{
public CarRepository(IDataConfiguration<string> configuration, IEntityMap<Car> entityMap)
: base(configuration, entityMap)
{
}
}
Filtering Example
var cars = carRepository.Find(
new WherePredicate<Car>(model => model.Make.Contains(searchTerm) || model.Model.Contains(searchTerm)));
Connection Override Notes
The repository base supports connection override patterns, which are useful when the same repository shape needs to target another configured database.
This is powerful, but it also means lifetime management matters. If you register repositories as singletons and change the active connection, that change can persist longer than you intended.
Notes
DataConfiguration<TKey>centralizes collection naming, timeouts, and connection-string access.BaseMap<TEntity>is the right place for BSON class-map customization and nested serializer setup.WherePredicate<TEntity>keeps filtering expressions explicit and reusable.