Cosmos.EventSourcing.Linq.Extensions 0.0.5

dotnet add package Cosmos.EventSourcing.Linq.Extensions --version 0.0.5
                    
NuGet\Install-Package Cosmos.EventSourcing.Linq.Extensions -Version 0.0.5
                    
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="Cosmos.EventSourcing.Linq.Extensions" Version="0.0.5" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Cosmos.EventSourcing.Linq.Extensions" Version="0.0.5" />
                    
Directory.Packages.props
<PackageReference Include="Cosmos.EventSourcing.Linq.Extensions" />
                    
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 Cosmos.EventSourcing.Linq.Extensions --version 0.0.5
                    
#r "nuget: Cosmos.EventSourcing.Linq.Extensions, 0.0.5"
                    
#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 Cosmos.EventSourcing.Linq.Extensions@0.0.5
                    
#: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=Cosmos.EventSourcing.Linq.Extensions&version=0.0.5
                    
Install as a Cake Addin
#tool nuget:?package=Cosmos.EventSourcing.Linq.Extensions&version=0.0.5
                    
Install as a Cake Tool

Cosmos.EventSourcing.Linq.Extensions

NuGet

Extensiones de LINQ para trabajar con IQueryable de forma asíncrona, optimizadas para Marten.

Descripción

Este paquete proporciona métodos de extensión que permiten realizar operaciones LINQ de manera asíncrona sobre colecciones IQueryable. Es especialmente útil cuando trabajas con proyecciones y consultas en aplicaciones con Event Sourcing usando Marten.

Características

  • ToListAsync: Convierte un IQueryable<T> a una lista de forma asíncrona
  • AnyAsync: Verifica la existencia de elementos de forma asíncrona
  • AnyAsync con predicado: Verifica la existencia de elementos que cumplan una condición
  • Compatible con Marten IQueryable
  • Soporte para CancellationToken

Instalación

dotnet add package Cosmos.EventSourcing.Linq.Extensions

Uso

ToListAsync

Convierte una consulta en una lista de forma asíncrona:

using Cosmos.EventSourcing.Linq.Extensions;
using Marten;

public class ProductQueryService
{
    private readonly IDocumentSession _session;

    public ProductQueryService(IDocumentSession session)
    {
        _session = session;
    }

    public async Task<IReadOnlyList<Product>> GetActiveProductsAsync(CancellationToken ct)
    {
        var products = await _session
            .Query<Product>()
            .Where(p => p.IsActive)
            .OrderBy(p => p.Name)
            .ToListAsync(ct);

        return products;
    }
}

AnyAsync

Verifica si existen elementos en la consulta:

public async Task<bool> HasActiveProductsAsync(CancellationToken ct)
{
    var hasProducts = await _session
        .Query<Product>()
        .Where(p => p.IsActive)
        .AnyAsync(ct);

    return hasProducts;
}

AnyAsync con Predicado

Verifica si existen elementos que cumplan una condición:

public async Task<bool> HasProductsInCategoryAsync(string categoryId, CancellationToken ct)
{
    var hasProducts = await _session
        .Query<Product>()
        .AnyAsync(p => p.CategoryId == categoryId && p.IsActive, ct);

    return hasProducts;
}

Ejemplo Completo

using Cosmos.EventSourcing.Linq.Extensions;
using Marten;

public class OrderQueryService
{
    private readonly IDocumentSession _session;

    public OrderQueryService(IDocumentSession session)
    {
        _session = session;
    }

    public async Task<IReadOnlyList<Order>> GetPendingOrdersAsync(CancellationToken ct)
    {
        // Verificar si hay órdenes pendientes
        var hasPendingOrders = await _session
            .Query<Order>()
            .AnyAsync(o => o.Status == OrderStatus.Pending, ct);

        if (!hasPendingOrders)
        {
            return Array.Empty<Order>();
        }

        // Obtener todas las órdenes pendientes
        var orders = await _session
            .Query<Order>()
            .Where(o => o.Status == OrderStatus.Pending)
            .OrderBy(o => o.CreatedAt)
            .ToListAsync(ct);

        return orders;
    }

    public async Task<IReadOnlyList<Order>> GetRecentOrdersByCustomerAsync(
        string customerId,
        CancellationToken ct)
    {
        var recentOrders = await _session
            .Query<Order>()
            .Where(o => o.CustomerId == customerId)
            .OrderByDescending(o => o.CreatedAt)
            .Take(10)
            .ToListAsync(ct);

        return recentOrders;
    }
}

Ventajas

  • Asincronía: Evita bloquear el thread mientras se ejecutan consultas a la base de datos
  • Cancelación: Soporte completo para CancellationToken
  • Compatibilidad: Funciona perfectamente con Marten y sus IQueryable
  • Simplicidad: API familiar para desarrolladores que conocen LINQ

Compatibilidad con Marten

Este paquete está diseñado para trabajar con Marten, una librería de Document Database y Event Store para PostgreSQL. Las extensiones delegan internamente a las implementaciones de Marten, proporcionando una API más simple y consistente.

Requisitos

  • .NET 10.0 o superior

Dependencias

  • Marten (v8.16.1)

Licencia

Copyright © Cosmos. Todos los derechos reservados.

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.
  • net10.0

    • No dependencies.

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.5 197 12/3/2025
0.0.4 199 11/25/2025
0.0.1 252 9/19/2025