STX.EFxceptions.Interfaces
0.1.2
dotnet add package STX.EFxceptions.Interfaces --version 0.1.2
NuGet\Install-Package STX.EFxceptions.Interfaces -Version 0.1.2
<PackageReference Include="STX.EFxceptions.Interfaces" Version="0.1.2" />
paket add STX.EFxceptions.Interfaces --version 0.1.2
#r "nuget: STX.EFxceptions.Interfaces, 0.1.2"
// Install STX.EFxceptions.Interfaces as a Cake Addin #addin nuget:?package=STX.EFxceptions.Interfaces&version=0.1.2 // Install STX.EFxceptions.Interfaces as a Cake Tool #tool nuget:?package=STX.EFxceptions.Interfaces&version=0.1.2
<p align="center"> <img width="25%" height="25%" src="https://github.com/The-Standard-Organization/STX.EFxceptions.Core/blob/main/Resources/EFxceptions.png?raw=true"> </p>
STX.EFxceptions
We have designed and developed this library as a abstract base wrapper around the existing EntityFramework DbContext implementation to provide the following values:
<ol> <li>Meaningful exceptions for error codes.</li> <li>Simplified integrations</li> <li>Test-friendly implementation.</li> </ol>
<br>
This solution consists of the following core projects:
<ul> <li><strong>STX.EFxceptions.Interfaces</strong>: A Standardized .NET library that provides interfaces for the core components to capture exceptions thrown by EntityFramework and converts them into meaningful exceptions.</li> <li><strong>STX.EFxceptions.Core</strong>: A Standardized .NET library that provides an abstract DBContext to capture exceptions thrown by EntityFramework and converts them into meaningful exceptions.</li> <li><strong>STX.EFxceptions.Identity.Core</strong>: A Standardized .NET library that provides an abstract DBContext that implements AspNetCore.Identity to captures exceptions thrown by EntityFramework and converts them into meaningful exceptions.</li> </ul>
STX.EFxceptions.Interfaces
This library provides the interfaces which are used by the core components to get error codes and provide meaningful exceptions.
STX.EFxceptions.Core
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
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
Product | Versions 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. |
-
net8.0
- Microsoft.EntityFrameworkCore (>= 8.0.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
This beta release is a rewrite of the original EFxceptions library.