Tolitech.Infrastructure.Persistence.ConstraintValidator 1.0.0-preview.5

This is a prerelease version of Tolitech.Infrastructure.Persistence.ConstraintValidator.
dotnet add package Tolitech.Infrastructure.Persistence.ConstraintValidator --version 1.0.0-preview.5
                    
NuGet\Install-Package Tolitech.Infrastructure.Persistence.ConstraintValidator -Version 1.0.0-preview.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="Tolitech.Infrastructure.Persistence.ConstraintValidator" Version="1.0.0-preview.5" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Tolitech.Infrastructure.Persistence.ConstraintValidator" Version="1.0.0-preview.5" />
                    
Directory.Packages.props
<PackageReference Include="Tolitech.Infrastructure.Persistence.ConstraintValidator" />
                    
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 Tolitech.Infrastructure.Persistence.ConstraintValidator --version 1.0.0-preview.5
                    
#r "nuget: Tolitech.Infrastructure.Persistence.ConstraintValidator, 1.0.0-preview.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 Tolitech.Infrastructure.Persistence.ConstraintValidator@1.0.0-preview.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=Tolitech.Infrastructure.Persistence.ConstraintValidator&version=1.0.0-preview.5&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=Tolitech.Infrastructure.Persistence.ConstraintValidator&version=1.0.0-preview.5&prerelease
                    
Install as a Cake Tool

Tolitech.Infrastructure.Persistence.ConstraintValidator

A static library for managing and validating database constraint exceptions in a database-agnostic way, with specific implementations for PostgreSQL and SQL Server.

Overview

This library helps you identify and handle integrity constraint exceptions (primary key, foreign key, not null, check) thrown by databases, converting them into custom exceptions for easier handling in your application.

  • Tolitech.Infrastructure.Persistence.ConstraintValidator: Core and interface for validators.
  • Tolitech.Infrastructure.Persistence.ConstraintValidator.PostgreSql: PostgreSQL-specific validator.
  • Tolitech.Infrastructure.Persistence.ConstraintValidator.SqlServer: SQL Server-specific validator.

Installation

Add the desired package to your project:

dotnet add package Tolitech.Infrastructure.Persistence.ConstraintValidator

# For PostgreSQL:
dotnet add package Tolitech.Infrastructure.Persistence.ConstraintValidator.PostgreSql

# For SQL Server:
dotnet add package Tolitech.Infrastructure.Persistence.ConstraintValidator.SqlServer

Basic Usage

1. Register the validator

At application startup (e.g., Startup):

using Tolitech.Infrastructure.Persistence.ConstraintValidator;
using Tolitech.Infrastructure.Persistence.ConstraintValidator.PostgreSql;
using Tolitech.Infrastructure.Persistence.ConstraintValidator.SqlServer;

// For PostgreSQL
ConstraintValidatorManager.AddValidator(new PostgreSqlConstraintValidator());

// For SQL Server
ConstraintValidatorManager.AddValidator(new SqlServerConstraintValidator());

2. Handle database exceptions

When catching exceptions from database operations:

try
{
    // Database operation
}
catch (Exception ex)
{
    Exception handled = ConstraintValidatorManager.Handle(ex);
    if (handled is DatabaseConstraintViolationException)
    {
        // Custom logic for constraint violations
    }
    else
    {
        // Other handling
    }
}

Examples of Handled Exceptions

PostgreSQL

  • Primary key violation: SqlState = "23505"
  • Foreign key violation: SqlState = "23503"
  • Check constraint violation: SqlState = "23514"
  • Not null violation: SqlState = "23502"

SQL Server

  • Primary key violation: Number = 2627
  • Foreign key violation: Number = 547
  • Not null violation: Number = 515

Advanced Integration

Remove or clear validators

ConstraintValidatorManager.RemoveValidator(yourValidator);
ConstraintValidatorManager.ClearValidators();

Custom implementation

Implement IConstraintValidator to create validators for other databases:

public class MyDbConstraintValidator : IConstraintValidator
{
    public Exception HandleConstraintViolation(Exception exception)
    {
        // Your logic
    }
}

Benefits

  • Centralized constraint exception handling.
  • Easy for internationalization and logging.
  • Extensible for multiple databases.

Modern Examples

Usage with Entity Framework Core

try
{
    await dbContext.SaveChangesAsync();
}
catch (Exception ex)
{
    var handled = ConstraintValidatorManager.Handle(ex);
    if (handled is PrimaryKeyViolationException)
        // Notify user about duplicate
}

Usage in APIs

[HttpPost]
public IActionResult Create([FromBody] MyObject obj)
{
    try
    {
        // ...
    }
    catch (Exception ex)
    {
        var handled = ConstraintValidatorManager.Handle(ex);
        if (handled is DatabaseConstraintViolationException)
            return BadRequest(handled.Message);
        throw;
    }
}

Tolitech.Infrastructure.Persistence.ConstraintValidator makes constraint exception handling simple, clean, and ready for multiple databases.

Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (3)

Showing the top 3 NuGet packages that depend on Tolitech.Infrastructure.Persistence.ConstraintValidator:

Package Downloads
Tolitech.Infrastructure.Persistence.EntityFrameworkCore

The Tolitech.Infrastructure.Persistence.EntityFrameworkCore repository provides a foundational implementation for the Repository pattern, Unit of Work, and Specification Query Builder using Entity Framework Core. Simplify database interaction, promote code organization, and maintenance using these widely recognized patterns.

Tolitech.Infrastructure.Persistence.ConstraintValidator.SqlServer

A static library for managing and validating database constraint exceptions specifically for SQL Server.

Tolitech.Infrastructure.Persistence.ConstraintValidator.PostgreSql

A static library for managing and validating database constraint exceptions specifically for PostgreSQL.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0-preview.5 32 a day ago
1.0.0-preview.4 108 5 days ago
1.0.0-preview.3 110 16 days ago
1.0.0-preview.2 116 16 days ago
1.0.0-preview.1 103 7 months ago