BertoSoftware.DapperAuditContext 0.2.0-alpha

This is a prerelease version of BertoSoftware.DapperAuditContext.
There is a newer version of this package available.
See the version list below for details.
dotnet add package BertoSoftware.DapperAuditContext --version 0.2.0-alpha
                    
NuGet\Install-Package BertoSoftware.DapperAuditContext -Version 0.2.0-alpha
                    
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="BertoSoftware.DapperAuditContext" Version="0.2.0-alpha" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="BertoSoftware.DapperAuditContext" Version="0.2.0-alpha" />
                    
Directory.Packages.props
<PackageReference Include="BertoSoftware.DapperAuditContext" />
                    
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 BertoSoftware.DapperAuditContext --version 0.2.0-alpha
                    
#r "nuget: BertoSoftware.DapperAuditContext, 0.2.0-alpha"
                    
#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 BertoSoftware.DapperAuditContext@0.2.0-alpha
                    
#: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=BertoSoftware.DapperAuditContext&version=0.2.0-alpha&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=BertoSoftware.DapperAuditContext&version=0.2.0-alpha&prerelease
                    
Install as a Cake Tool

DapperAuditContext

A simple collections of functions and method for CRUD operation in Dapper for generic item with an integrated audit system.

NOTE

This documentation is under development

Version

2025-05-16

0.2.0 - Alpha version

New feature

Added fluent configuration where you can configure the following options:

  • Storage mode
  • Custom log file name
  • Custom table name
  • Added username field in create script database
    • Warning: Migration script is not yet been implemented, so you have to add manually a new user field into AuditTable
ALTER TABLE dbo.AuditTable ADD Username nvarchar(255) NULL

Example:

VB.NET

Imports System

Module Program
    Sub Main(args As String())

        DapperAuditContext.AuditSettings = AuditConfiguration.CreateNew.StoreMode(AuditStoreMode.File).Build

        'Add new value into Person Table with an automatic audit trail, just use DapperAuditContext instead of DapperContext
        Using ctx As New DapperAuditContext

            Dim person As New Model.Person With {
                .Name = "John",
                .Surname = "Doe"
            }

            ctx.InsertOrUpdate(person)

            'Changes a property after save
            person.Surname = "Black"

            'Saves the changes and adds a record in the control table with the difference between the previous save.
            ctx.InsertOrUpdate(person)

        End Using

    End Sub
End Module

C#

using System;

class Program
{
    static void Main(string[] args)
    {
        DapperAuditContext.AuditSettings = AuditConfiguration.CreateNew.StoreMode(AuditStoreMode.File).Build();

        // Add new value into Person Table with an automatic audit trail, just use DapperAuditContext instead of DapperContext
        using (var ctx = new DapperAuditContext())
        {
            var person = new Model.Person
            {
                Name = "John",
                Surname = "Doe"
            };

            ctx.InsertOrUpdate(person);

            // Changes a property after save
            person.Surname = "Black";

            // Saves the changes and adds a record in the control table with the difference between the previous save.
            ctx.InsertOrUpdate(person);
        }
    }
}

2025-05-13

0.1.0 - Alpha version

How to use

  1. Add a reference to the DapperAuditContext project in your project.
  2. Add a using statement to the DapperAuditContext namespace in your code file.
  3. Create a new instance of the DapperAuditContext class, passing in your database connection string.
  4. Use the methods provided by the DapperAuditContext class to perform CRUD operations on your database.
  5. Dispose of the DapperAuditContext instance when you're done using it.
  6. Optionally, you can use the DapperAuditContext class to perform other database operations, such as executing raw SQL queries or stored procedures.
  7. You can use the Audit system to track changes to your data and generate audit logs.

Create a model

A simple data model

VB.NET

Imports Dapper.Contrib.Extensions

Namespace Model

    <Table("Person")>
    <Audit>
    Public Class Person
        <Key>
        Public Property ID As Integer
        Public Property Name As String
        Public Property Surname As String
        <Audit(False)>
        Public Property Address As String

    End Class
End Namespace

C#

using Dapper.Contrib.Extensions;

namespace Model
{

    [Table("Person")]
    [Audit]
    public partial class Person
    {
        [Key]
        public int ID { get; set; }
        public string Name { get; set; }
        public string Surname { get; set; }
        [Audit(false)]
        public string Address { get; set; }

    }
}

You can use Audit attribute on class or in single property to include or exclude trail, the default value is True, so is not necessary to specify a True value for all properties.

A simple use might look like this:

VB.NET

Imports System

Module Program
    Sub Main(args As String())

        'Add new value into Person Table without audit trail
        Using ctx As New DapperContext

            Dim person As New Model.Person With {
                .Name = "John",
                .Surname = "Doe"
                .Address = "Street 23"
            }

            ctx.InsertOrUpdate(person)
        End Using

        'Add new value into Person Table with an automatic audit trail, just use DapperAuditContext instead of DapperContext
        Using ctx As New DapperAuditContext

            Dim person As New Model.Person With {
                .Name = "John",
                .Surname = "Doe"
                .Address = "Street 23"
            }

            ctx.InsertOrUpdate(person)

            'Changes a property after save
            person.Surname = "Black"

            'Saves the changes and adds a record in the control table with the difference between the previous save.
            ctx.InsertOrUpdate(person)

        End Using

    End Sub
End Module

C#


internal static partial class Program
{
    public static void Main(string[] args)
    {

        // Add new value into Person Table without audit trail
        using (var ctx = new DapperContext())
        {

            var person = new Model.Person()
            {
                Name = "John",
                Surname = "Doe"
                Address = "Street 23"
            };

            ctx.InsertOrUpdate(person);
        }

        // Add new value into Person Table with an automatic audit trail, just use DapperAuditContext instead of DapperContext
        using (var ctx = new DapperAuditContext())
        {

            var person = new Model.Person()
            {
                Name = "John",
                Surname = "Doe"
                Address = "Street 23"
            };

            ctx.InsertOrUpdate(person);

            // Changes a property after save
            person.Surname = "Black";

            // Saves the changes and adds a record in the control table with the difference between the previous save.
            ctx.InsertOrUpdate(person);

        }

    }
}

Dipendencies

  • You have to install Dapper.Contrib and assign to a model the [Key] attribute on ID field

Note

  • The InsertOrUpdate method works only if the ‘Key’ attribute has been set to a field of type integer.
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.  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. 
.NET Framework net48 is compatible.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (5)

Showing the top 5 NuGet packages that depend on BertoSoftware.DapperAuditContext:

Package Downloads
BertoSoftware.DapperAuditContext.MySql

A simple collections of functions and method for CRUD operation in Dapper for generic item with an integrated audit system

BertoSoftware.DapperAuditContext.SqlServer

A simple collections of functions and method for CRUD operation in Dapper for generic item with an integrated audit system

BertoSoftware.DapperAuditContext.SQLite

A simple collections of functions and method for CRUD operation in Dapper for generic item with an integrated audit system

BertoSoftware.DapperAuditContext.PostgreSQL

A simple collections of functions and method for CRUD operation in Dapper for generic item with an integrated audit system

BertoSoftware.DapperAuditContext.Firebird

A simple collections of functions and method for CRUD operation in Dapper for generic item with an integrated audit system

GitHub repositories

This package is not used by any popular GitHub repositories.

v0.2.0.0 apha

Added fluent configuration where you can configure the following options:

- Storage mode
- Custom log file name
- Custom table name