STX.EFxceptions.Identity.Core 0.1.7

dotnet add package STX.EFxceptions.Identity.Core --version 0.1.7                
NuGet\Install-Package STX.EFxceptions.Identity.Core -Version 0.1.7                
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="STX.EFxceptions.Identity.Core" Version="0.1.7" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add STX.EFxceptions.Identity.Core --version 0.1.7                
#r "nuget: STX.EFxceptions.Identity.Core, 0.1.7"                
#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.
// Install STX.EFxceptions.Identity.Core as a Cake Addin
#addin nuget:?package=STX.EFxceptions.Identity.Core&version=0.1.7

// Install STX.EFxceptions.Identity.Core as a Cake Tool
#tool nuget:?package=STX.EFxceptions.Identity.Core&version=0.1.7                

STX.EFxceptions.Core

BuildThe Standard The Standard - COMPLIANT The Standard Community

STX.EFxceptions.Core

Introduction

We have designed and developed this library as an abstract base wrapper around the existing EntityFramework DbContext implementation to provide the following values:

  1. Meaningful exceptions for error codes
  2. Simplified integrations
  3. Test-friendly implementation

This solution consists of the following core projects:

  • A STX.EFxceptions.Abstractions : Standardized .NET library that provides interfaces for the core components to capture exceptions thrown by EntityFramework and converts them into meaningful exceptions.
  • A STX.EFxceptions.Core : Standardized .NET library that provides an abstract DBContext to capture exceptions thrown by EntityFramework and converts them into meaningful exceptions.
  • A STX.EFxceptions.Identity.Core : Standardized .NET library that provides an abstract DBContext that implements AspNetCore.Identity to captures exceptions thrown by EntityFramework and converts them into meaningful exceptions.

Standard-Compliance

This library was built according to The Standard. The library follows engineering principles, patterns and tooling as recommended by The Standard.

This library is also a community effort which involved many nights of pair-programming, test-driven development and in-depth exploration research and design discussions.

Standard-Promise

The most important fulfillment aspect in a Standard complaint system is aimed towards contributing to people, its evolution, and principles. An organization that systematically honors an environment of learning, training, and sharing knowledge is an organization that learns from the past, makes calculated risks for the future, and brings everyone within it up to speed on the current state of things as honestly, rapidly, and efficiently as possible.

We believe that everyone has the right to privacy, and will never do anything that could violate that right. We are committed to writing ethical and responsible software, and will always strive to use our skills, coding, and systems for the good. We believe that these beliefs will help to ensure that our software(s) are safe and secure and that it will never be used to harm or collect personal data for malicious purposes.

The Standard Community as a promise to you is in upholding these values.

Get the Packages

STX.EFxceptions.Abstractions

preview version

This library provides the interfaces which are used by the core components to get error codes and provide meaningful exceptions.

STX.EFxceptions.Core

preview version

This library provides an abstract DBContext which is used to capture exceptions thrown by EntityFramework and converts them into meaningful exceptions. Custom implementations for the various Entity Framework database providers should inherit from this abstract class.

STX.EFxceptions.Identity.Core

preview version

This library provides an abstract DBContext that inherits from Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityDbContext which is used to capture exceptions thrown by EntityFramework and converts them into meaningful exceptions. Custom implementations for the various Entity Framework database providers that make use of Microsoft.AspNetCore.Identity should inherit from this abstract class.

How to use

These core libraries are designed to be used as a base class for your DbContext implementation. If you would like to create your own custom DbContext implementation, you can inherit from the abstract base class provided by this library.

Example

Lets use SQL Server as an example. If you would like to create a custom implementation that uses SQL Server, you can inherit from the abstract base class provided by this library.

    public class EFxceptionsContext : DbContextBase<SqlException>
    {
        protected override IDbErrorBroker<SqlException> CreateErrorBroker()
        {
            return new SqlErrorBroker();
        }

        protected override IDbErrorBroker<SqlException> CreateErrorBroker() =>
            new SqlServerErrorBroker();

        protected override IEFxceptionService CreateEFxceptionService(IDbErrorBroker<SqlException> errorBroker)
        {
            return new SqlServerEFxceptionService<SqlException>(errorBroker);
        }
    }

Next we will have to do an implementation of the IDbErrorBroker interface. This broker is used to get the error code for the exceptions that can be thrown by EntityFramework.

    public interface ISqlServerErrorBroker : IDbErrorBroker<SqlException>
    { }

    public class SqlServerErrorBroker : ISqlServerErrorBroker
    {
        public int GetErrorCode(SqlException exception) => exception.Number;
    }

Finally, we will have to do an implementation of the IEFxceptionService interface and service. This interface and service is used to provide the meaningful exceptions for the various error codes that can be thrown by EntityFramework.

	public interface ISqlServerEFxceptionService : IEFxceptionService<SqlException>
	{ }

	public class SqlServerEFxceptionService<TException> : ISqlServerEFxceptionService
	{
		public SqlServerEFxceptionService(IDbErrorBroker<TException> errorBroker) : base(errorBroker)
		{ }

        public void ThrowMeaningfulException(SqlException sqlException)
        {
            ValidateInnerException(sqlException);
            SqlException innerException = GetException(sqlException.InnerException);
            int errorCode = this.errorBroker.GetErrorCode(innerException);
            ConvertAndThrowMeaningfulException(sqlErrorCode, dbException.Message);

            throw dbUpdateException;
        }

        private void ValidateInnerException(SqlException sqlException)
        {
            if (SqlException.InnerException == null)
            {
                throw sqlException;
            }
        }

        private void ConvertAndThrowMeaningfulException(int code, string message)
        {
            switch (code)
            {
                case 207:
                    throw new InvalidColumnNameSqlServerException(message);
                case 208:
                    throw new InvalidObjectNameSqlServerException(message);
                case 547:
                    throw new ForeignKeyConstraintConflictSqlServerException(message);
                case 2601:
                    throw new DuplicateKeyWithUniqueIndexSqlServerException(message);
                case 2627:
                    throw new DuplicateKeySqlServerException(message);
            }
        }

        private SqlException GetSqlException(Exception exception) => (SqlException)exception;

Current Implementations

STX.EFxceptions.SqlServer

STX.EFxceptions.SQLite

STX.EFxceptions.MySql

STX.EFxceptions.Cosmos

Contact

If you have any suggestions, comments or questions, please feel free to contact me on:

Twitter

LinkedIn

E-Mail

Important Notice

A special thanks to Mr. Hassan Habib and Mr. Christo du Toit for their continuing dedicated contributions.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (8)

Showing the top 5 NuGet packages that depend on STX.EFxceptions.Identity.Core:

Package Downloads
STX.EFxceptions.Identity.Cosmos

A Standardized .NET library for Cosmos DB that implements AspNetCore.Identity, that captures the exceptions thrown by EntityFramework and converts them into meaningful exceptions...

STX.EFxceptions.SQLite.Base

A Standardized .NET library that provides core functionality that is used by the SQLite implementations of EFxceptions

STX.EFxceptions.Identity.SQLite

A Standardized .NET library for SQLite that implements AspNetCore.Identity, that captures the exceptions thrown by EntityFramework and converts them into meaningful exceptions...

STX.EFxceptions.MySql.Base

A Standardized .NET library that provides core functionality that is used by the MySql implementations of EFxceptions

STX.EFxceptions.MySql

A Standardized .NET library for MySql that captures the exceptions thrown by EntityFramework and converts them into meaningful exceptions...

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.1.7 106 8/7/2024
0.1.6 354 2/24/2024
0.1.5 122 2/20/2024
0.1.4 117 2/20/2024
0.1.2 227 1/30/2024
0.1.1 173 1/28/2024
0.1.0 113 1/26/2024

This beta release is a rewrite of the original EFxceptions library.