IntraDotNet.Domain.Core 1.0.0

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

IntraDotNet.Domain.Core

The IntraDotNet.Domain.Core library provides a set of interfaces that help implement common auditing and versioning patterns in your domain entities. These interfaces are designed to work seamlessly with Entity Framework Core and other ORM frameworks to automatically track entity lifecycle events.

Features

  • Audit Tracking: Automatically track creation, update, and soft deletion events
  • Concurrency Control: Built-in row versioning support for optimistic concurrency
  • Flexible Implementation: Modular interfaces that can be implemented individually or combined
  • .NET 9.0 Support: Built for the latest .NET framework with nullable reference types enabled

Interfaces

Core Auditing Interfaces

ICreateAuditable

Tracks entity creation information:

public interface ICreateAuditable
{
    public DateTimeOffset? CreatedOn { get; set; }
    public string? CreatedBy { get; set; }
}
IUpdateAuditable

Tracks entity update information:

public interface IUpdateAuditable
{
    public DateTimeOffset? LastUpdateOn { get; set; }
    public string? LastUpdateBy { get; set; }
}
ISoftDeleteAuditable

Tracks soft deletion without physically removing entities:

public interface ISoftDeleteAuditable
{
    public DateTimeOffset? DeletedOn { get; set; }
    public string? DeletedBy { get; set; }
}
IAuditable

Combines all auditing interfaces for comprehensive tracking:

public interface IAuditable : ICreateAuditable, IUpdateAuditable, ISoftDeleteAuditable
{
}

Concurrency Control

IRowVersion

Provides optimistic concurrency control:

public interface IRowVersion
{
    [Timestamp]
    byte[]? RowVersion { get; set; }
}

Usage Examples

Basic Entity with Full Auditing

using IntraDotNet.Domain.Core;

public class Product : IAuditable, IRowVersion
{
    public int Id { get; set; }
    public string Name { get; set; } = string.Empty;
    public decimal Price { get; set; }
    
    // IAuditable properties
    public DateTimeOffset? CreatedOn { get; set; }
    public string? CreatedBy { get; set; }
    public DateTimeOffset? LastUpdateOn { get; set; }
    public string? LastUpdateBy { get; set; }
    public DateTimeOffset? DeletedOn { get; set; }
    public string? DeletedBy { get; set; }
    
    // IRowVersion property
    public byte[]? RowVersion { get; set; }
}

Entity with Selective Auditing

using IntraDotNet.Domain.Core;

public class Category : ICreateAuditable, IUpdateAuditable
{
    public int Id { get; set; }
    public string Name { get; set; } = string.Empty;
    
    // Only track creation and updates, not deletions
    public DateTimeOffset? CreatedOn { get; set; }
    public string? CreatedBy { get; set; }
    public DateTimeOffset? LastUpdateOn { get; set; }
    public string? LastUpdateBy { get; set; }
}

Entity Framework Core Integration

public class ApplicationDbContext : DbContext
{
    public DbSet<Product> Products { get; set; }
    public DbSet<Category> Categories { get; set; }
    
    public override int SaveChanges()
    {
        UpdateAuditableEntities();
        return base.SaveChanges();
    }
    
    public override async Task<int> SaveChangesAsync(CancellationToken cancellationToken = default)
    {
        UpdateAuditableEntities();
        return await base.SaveChangesAsync(cancellationToken);
    }
    
    private void UpdateAuditableEntities()
    {
        var entries = ChangeTracker.Entries()
            .Where(e => e.State == EntityState.Added || e.State == EntityState.Modified);
            
        var currentUser = GetCurrentUser(); // Implement your user resolution logic
        var timestamp = DateTimeOffset.UtcNow;
        
        foreach (var entry in entries)
        {
            if (entry.Entity is ICreateAuditable createAuditable && entry.State == EntityState.Added)
            {
                createAuditable.CreatedOn = timestamp;
                createAuditable.CreatedBy = currentUser;
            }
            
            if (entry.Entity is IUpdateAuditable updateAuditable && entry.State == EntityState.Modified)
            {
                updateAuditable.LastUpdateOn = timestamp;
                updateAuditable.LastUpdateBy = currentUser;
            }
        }
    }
    
    private string GetCurrentUser()
    {
        // Implement your logic to get the current user
        return "system"; // placeholder
    }
}

Soft Delete Implementation

public static class SoftDeleteExtensions
{
    public static void SoftDelete<T>(this T entity, string deletedBy) 
        where T : ISoftDeleteAuditable
    {
        entity.DeletedOn = DateTimeOffset.UtcNow;
        entity.DeletedBy = deletedBy;
    }
    
    public static IQueryable<T> WhereNotDeleted<T>(this IQueryable<T> query) 
        where T : ISoftDeleteAuditable
    {
        return query.Where(e => e.DeletedOn == null);
    }
}

// Usage
var product = await context.Products.FindAsync(id);
if (product != null)
{
    product.SoftDelete("current-user");
    await context.SaveChangesAsync();
}

// Query non-deleted entities
var activeProducts = context.Products.WhereNotDeleted().ToList();

Installation

Install the package via NuGet:

dotnet add package IntraDotNet.Domain.Core

Or via Package Manager Console:

Install-Package IntraDotNet.Domain.Core

Requirements

  • .NET 9.0 or later
  • Compatible with Entity Framework Core and other ORM frameworks

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Repository

https://github.com/MegaByteMark/intradotnet-domain-core

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

Showing the top 2 NuGet packages that depend on IntraDotNet.Domain.Core:

Package Downloads
IntraDotNet.EFCore.Infrastructure

Optimisation classes to remove boilerplate when generating entityframework dbcontexts

IntraDotNet.EFCore.Infrastructure.Repositories

A convenience library to remove repository pattern boilerplate.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0 486 7/23/2025