NuGet

eQuantic Core Data Class Library

Data abstractions and patterns for domain-driven design

Version: 4.2.3

Downloads: 97K+

Status: stable

Category: data

Installation

dotnet add package eQuantic.Core.Data

Overview

The eQuantic Core Data Library is a specialized extension of the Core Library, focused on abstractions and patterns for data access and persistence.

This library offers robust implementations for Unit of Work, Repository Pattern, and object-relational mapping, facilitating the development of efficient data layers.

With over 85,000 downloads, it's one of the most popular libraries in the ecosystem, offering support for multiple database providers and persistence strategies.

Quick Start

1. DbContext Configuration

Configure your data context by inheriting from base classes.

public class ApplicationDbContext : EQuanticDbContext
{
    public DbSet<Product> Products { get; set; }
    public DbSet<Order> Orders { get; set; }
    
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {
    }
    
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.ApplyConfigurationsFromAssembly(typeof(ApplicationDbContext).Assembly);
    }
}

2. Implementing Unit of Work

Use the Unit of Work pattern to coordinate data operations.

public class OrderService
{
    private readonly IUnitOfWork _unitOfWork;
    
    public OrderService(IUnitOfWork unitOfWork)
    {
        _unitOfWork = unitOfWork;
    }
    
    public async Task<Order> CreateOrderAsync(CreateOrderRequest request)
    {
        var order = new Order(request.CustomerId, request.Items);
        
        await _unitOfWork.GetRepository<Order>().AddAsync(order);
        await _unitOfWork.GetRepository<Product>().UpdateStockAsync(request.Items);
        
        await _unitOfWork.SaveChangesAsync();
        return order;
    }
}

3. Using Specifications

Create complex queries using the Specification pattern.

public class GetOrdersByCustomerSpec : Specification<Order>
{
    public GetOrdersByCustomerSpec(int customerId, DateTime? fromDate = null)
    {
        Criteria = o => o.CustomerId == customerId;
        
        if (fromDate.HasValue)
            Criteria = Criteria.And(o => o.CreatedAt >= fromDate.Value);
            
        AddInclude(o => o.Items);
        AddInclude("Items.Product");
        ApplyOrderByDescending(o => o.CreatedAt);
    }
}

API Reference

interface

IUnitOfWork

Interface to coordinate transactions and data operations.

Example
Task<int> SaveChangesAsync(CancellationToken cancellationToken = default);
IRepository<T> GetRepository<T>() where T : class;
Task BeginTransactionAsync();
Task CommitTransactionAsync();
Task RollbackTransactionAsync();
class

EQuanticDbContext

Base class for Entity Framework contexts with extended functionality.

Example
public abstract class EQuanticDbContext : DbContext
{
    public override Task<int> SaveChangesAsync(CancellationToken cancellationToken = default);
    protected virtual void ApplyAuditInfo();
    protected virtual void ApplyTenantFilter();
}
class

Specification<T>

Base class to implement complex specifications.

Example
public abstract class Specification<T> : ISpecification<T>
{
    protected void AddInclude(Expression<Func<T, object>> includeExpression);
    protected void AddInclude(string includeString);
    protected void ApplyPaging(int skip, int take);
    protected void ApplyOrderBy(Expression<Func<T, object>> orderByExpression);
}

Key Features

Unit of Work Pattern

Complete implementation of the Unit of Work pattern to manage transactions and ensure data consistency.

Advanced Repository Pattern

Generic repositories with support for specifications, sorting, pagination, and projections.

Automatic Mapping

Mapping system between domain entities and DTOs with flexible configuration.

Automatic Auditing

Automatic tracking of entity creation, modification, and deletion.

Multi-Tenant Support

Data isolation per tenant with automatic filters and flexible configuration.

Package Info

Version
4.2.3
Downloads
97K+
Status
stable
Category
data
eQuantic Core Data Class Library - Documentation | eQuantic