Tolitech.Application.Exceptions 1.0.0-preview.2

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

Tolitech.Application.Exceptions

Overview

Tolitech.Application.Exceptions is a modern .NET library designed to provide a robust set of custom exception types for handling database constraint violations and other application-specific errors. By leveraging clear, purpose-driven exception classes, this library enables developers to write more maintainable, expressive, and reliable error handling code in .NET applications.

Features

  • Specialized Exception Types: Includes custom exceptions for primary key, foreign key, unique index, and check constraint violations, as well as a base exception for general database constraint errors.
  • Consistent Error Handling: Promotes best practices by encouraging the use of specific exception types, making error handling code more readable and maintainable.
  • Extensible Design: Easily extend the library with your own custom exceptions to suit your application's needs.
  • Unit-Tested: Comprehensive unit tests ensure reliability and correctness of exception behavior.

Exception Types

  • DatabaseConstraintViolationException: Base class for all database constraint violation exceptions.
  • PrimaryKeyViolationException: Thrown when a primary key constraint is violated.
  • ForeignKeyViolationException: Thrown when a foreign key constraint is violated.
  • UniqueIndexViolationException: Thrown when a unique index constraint is violated.
  • CheckConstraintViolationException: Thrown when a check constraint is violated.

Usage Example

try
{
    // Database operation that may violate constraints
}
catch (PrimaryKeyViolationException ex)
{
    // Handle primary key violation
    Console.WriteLine($"Primary key error: {ex.Message}");
}
catch (DatabaseConstraintViolationException ex)
{
    // Handle other database constraint violations
    Console.WriteLine($"Database constraint error: {ex.Message}");
}

Extending the Library

You can create your own custom exceptions by inheriting from DatabaseConstraintViolationException or any other provided base class. This allows you to tailor error handling to your application's domain.

public class CustomBusinessRuleException : DatabaseConstraintViolationException
{
    public CustomBusinessRuleException(string message) : base(message) { }
}

Testing

Unit tests are provided to verify the correct behavior of all exception types, including constructor logic and property assignments. This ensures that your error handling code remains robust as your application evolves.

Summary

Tolitech.Application.Exceptions empowers .NET developers to implement clear, maintainable, and reliable error handling strategies for database operations and beyond. Its extensible and well-tested design makes it a valuable addition to any modern .NET application.

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

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Tolitech.Application.Exceptions:

Package Downloads
Tolitech.Infrastructure.Persistence.ConstraintValidator

A static library providing tools for managing and validating database constraint exceptions. It allows for the registration of custom constraint validators and handles exceptions efficiently.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0-preview.2 60 7/18/2025
1.0.0-preview.1 114 7/14/2025