Pandatech.EFCore.AuditBase 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Pandatech.EFCore.AuditBase --version 1.0.0                
NuGet\Install-Package Pandatech.EFCore.AuditBase -Version 1.0.0                
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="Pandatech.EFCore.AuditBase" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Pandatech.EFCore.AuditBase --version 1.0.0                
#r "nuget: Pandatech.EFCore.AuditBase, 1.0.0"                
#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 Pandatech.EFCore.AuditBase as a Cake Addin
#addin nuget:?package=Pandatech.EFCore.AuditBase&version=1.0.0

// Install Pandatech.EFCore.AuditBase as a Cake Tool
#tool nuget:?package=Pandatech.EFCore.AuditBase&version=1.0.0                

1. Pandatech.EFCore.AuditBase

Pandatech.EFCore.AuditBase is a comprehensive auditing library for Entity Framework Core, designed to make entity tracking, deletion management, and concurrency control straightforward and efficient.

1.1. Features

  • Automatic Auditing: Automatically tracks creation and update times, along with the corresponding user IDs.
  • Soft Delete Support: Implements soft deletion logic, allowing entities to be marked as deleted without physical removal from the database.
  • Concurrency Control: Integrates versioning to handle concurrency, minimizing data conflicts.
  • Enforced Best Practices: Enforces the use of provided methods for updates and deletions, ensuring consistent audit trails.
  • Query Filter Management: Allows for the exclusion of soft-deleted entities from queries, with an option to include them when necessary.
  • Seamless Integration: Designed to integrate smoothly with EF Core projects, enhancing data integrity and compliance.

1.2. Getting Started

To integrate Pandatech.EFCore.AuditBase into your project, install the NuGet package:

Install-Package Pandatech.EFCore.AuditBase

1.3. Usage

  1. Inherit from AuditEntityBase in your entity classes to enable auditing.
  2. Use MarkAsUpdated(userId) and MarkAsDeleted(userId) methods to handle entity updates and deletions.
  3. Apply DbContextExtensions.UseAuditPropertyValidation in your DbContext to enforce audit method usage.
  4. Leverage ModelBuilderExtensions.FilterOutDeletedMarkedObjects to automatically exclude soft-deleted entities from EF Core queries.

1.3.1. Entity Inheritance Example:

Assuming you have a Product entity in your application, you would inherit from AuditEntityBase to include auditing properties:

using Pandatech.VerticalSlices.Domain.Shared;

public class Product : AuditEntityBase
{
    public int Id { get; set; }
    public string Name { get; set; }
    public decimal Price { get; set; }
}

By inheriting from AuditEntityBase, Product automatically gains auditing properties like CreatedAt, CreatedByUserId, UpdatedAt, UpdatedByUserId, Deleted and Version.

1.3.2. Using MarkAsUpdated and MarkAsDeleted:

When updating or deleting an entity, use the provided methods to ensure the audit properties are correctly updated:

public void UpdateProduct(Product product, long updatingUserId)
{
    // Perform your update logic...
    
    product.MarkAsUpdated(updatingUserId);
    _dbContext.SaveChanges();
}

public void DeleteProduct(Product product, long deletingUserId)
{
    product.MarkAsDeleted(deletingUserId);
    _dbContext.SaveChanges();
}

1.3.3. DbContext Configuration:

In your DbContext, ensure you call UseAuditPropertyValidation to enforce the usage of audit methods and FilterOutDeletedMarkedObjects to apply the global filter for soft-deleted entities:

using Microsoft.EntityFrameworkCore;
using Pandatech.VerticalSlices.Infrastructure.Context;

public class MyDbContext : DbContext
{
    public DbSet<Product> Products { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.FilterOutDeletedMarkedObjects(); // Apply global query filter for soft deletes
    }

    public MyDbContext(DbContextOptions<MyDbContext> options) : base(options)
    {
        this.UseAuditPropertyValidation(); // Enforce audit method usage
    }
}

1.3.4. Ignoring Soft Delete Filter:

If you need to include soft-deleted entities in a specific query, use IgnoreQueryFilters():

var allProductsIncludingDeleted = _dbContext.Products.IgnoreQueryFilters().ToList();

1.4. Handling Concurrency

The AuditEntityBase includes a versioning mechanism to manage concurrent updates. In the event of a conflict, a concurrency exception will be thrown. This can be gracefully handled using try-catch blocks or integrated with Pandatech.ResponseCrafter for automated response management. This concurrency control is enabled by default and is not locking the row as it uses optimistic lock.

1.5. Contributing

Contributions are welcome! Please submit a pull request or open an issue to propose changes or report bugs.

1.6. License

Pandatech.EFCore.AuditBase is licensed under the MIT License.

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 (1)

Showing the top 1 NuGet packages that depend on Pandatech.EFCore.AuditBase:

Package Downloads
Pandatech.SharedKernel.Postgres

Pandatech.SharedKernel.Postgres simplifies PostgreSQL integration in ASP.NET Core applications by providing utilities for Entity Framework Core setup, health checks, and other enhancements.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.0.0 34 11/21/2024
1.2.1 95 11/11/2024
1.2.0 94 10/17/2024
1.1.0 119 7/12/2024
1.0.3 135 6/7/2024
1.0.2 107 5/31/2024
1.0.1 149 4/22/2024
1.0.0 119 4/3/2024

InitialCommit