Razory.MongoDB 2.1.1

The owner has unlisted this package. This could mean that the package is deprecated, has security vulnerabilities or shouldn't be used anymore.
dotnet add package Razory.MongoDB --version 2.1.1
                    
NuGet\Install-Package Razory.MongoDB -Version 2.1.1
                    
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="Razory.MongoDB" Version="2.1.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Razory.MongoDB" Version="2.1.1" />
                    
Directory.Packages.props
<PackageReference Include="Razory.MongoDB" />
                    
Project file
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 Razory.MongoDB --version 2.1.1
                    
#r "nuget: Razory.MongoDB, 2.1.1"
                    
#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 Razory.MongoDB@2.1.1
                    
#: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=Razory.MongoDB&version=2.1.1
                    
Install as a Cake Addin
#tool nuget:?package=Razory.MongoDB&version=2.1.1
                    
Install as a Cake Tool

Razory.MongoDB

Build & Test NuGet License: MIT

MongoDB provider for Razory. Implements IRepository and IQueryObject<T> using the official MongoDB .NET Driver.

Installation

dotnet add package Razory.MongoDB

Setup

1. Implement IMongoEntityConfiguration<T> for each entity

public class ProductConfiguration : IMongoEntityConfiguration<Product>
{
    public string Name => "products"; // collection name

    public void ConfigureCollection(IMongoCollection<Product> collection)
    {
        // optional: create indexes, TTLs, etc.
        collection.Indexes.CreateOne(
            new CreateIndexModel<Product>(
                Builders<Product>.IndexKeys.Ascending(p => p.Category)));
    }
}

2. Implement IDatabaseConfiguration

public class MyMongoDatabaseConfiguration : IDatabaseConfiguration
{
    private readonly List<IEntityConfiguration> configurations =
    [
        new ProductConfiguration(),
        new OrderConfiguration(),
    ];

    public string ConnectionString => "mongodb://localhost:27017";

    public string GetDatabaseName<T>() where T : EntityBase => "my_database";

    public IEntityConfiguration<T> GetEntityConfiguration<T>() where T : EntityBase =>
        configurations.OfType<IEntityConfiguration<T>>().Single();

    public IEnumerable<IEntityConfiguration> GetAllEntityConfigurations() => configurations;
}

3. Register

builder.Services.ConfigureRazoryMongoDB(new MyMongoDatabaseConfiguration());

This registers:

  • IRepositoryMongoRepository
  • IQueryObject<T>MongoQueryObject<T>
  • All internal commands (ICreateDocumentCommand, IUpdateDocumentCommand, etc.)
  • ICollectionResolver, IDatabaseResolver

Usage

Inject IRepository for CRUD and IQueryObject<T> for queries:

public class ProductService(IRepository repository, IQueryObject<Product> query)
{
    public Task<Product> GetByIdAsync(Guid id) =>
        repository.GetAsync<Product>(id);

    public Task InsertAsync(Product product) =>
        repository.InsertAsync(product);

    public Task UpsertAsync(Product product) =>
        repository.UpsertAsync(p => p.Id == product.Id, product);

    public Task<IEnumerable<Product>> GetActiveByCategoryAsync(string category) =>
        query
            .FilterBy<ProductFilter>(f => f.ActiveOnly().WithCategory(category))
            .SortAscending(p => p.Name)
            .Query()
            .GetAllAsync();

    public Task<IEnumerable<Product>> GetPageAsync(int page, int size) =>
        query
            .FilterBy(p => p.IsActive)
            .Query()
            .Skip((page - 1) * size)
            .Limit(size)
            .GetAllAsync();
}

MongoDB-specific result features

Query() returns IMongoQueryObjectResult<T> and Aggregate() returns IMongoQueryObjectAggregateResult<T>. These extend the base interfaces with MongoDB-native operations:

Native projection via IMongoQueryObjectProjectionDefinition<TSource, TDestination>

public class ProductSummaryProjection : IMongoQueryObjectProjectionDefinition<Product, ProductSummary>
{
    public ProjectionDefinition<Product, ProductSummary> Definition(
        ProjectionDefinitionBuilder<Product> builder) =>
        builder.Expression(p => new ProductSummary
        {
            Id = p.Id,
            Name = p.Name,
        });
}

var result = (IMongoQueryObjectResult<Product>)query.Query();
var summaries = await result.Project(new ProductSummaryProjection()).GetAllAsync();

Aggregate Unwind

var result = (IMongoQueryObjectAggregateResult<Order>)query.Aggregate();
var items = await result.Unwind<OrderItem>("Items").GetAllAsync();

Repository

github.com/johnyricalde/Razory

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  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.

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