DK89.DomainBase
1.0.2
dotnet add package DK89.DomainBase --version 1.0.2
NuGet\Install-Package DK89.DomainBase -Version 1.0.2
<PackageReference Include="DK89.DomainBase" Version="1.0.2" />
paket add DK89.DomainBase --version 1.0.2
#r "nuget: DK89.DomainBase, 1.0.2"
// Install DK89.DomainBase as a Cake Addin #addin nuget:?package=DK89.DomainBase&version=1.0.2 // Install DK89.DomainBase as a Cake Tool #tool nuget:?package=DK89.DomainBase&version=1.0.2
Example of Usage for AuditableBase
and AuditableBase<TIdType>
The following examples demonstrate how to use the AuditableBase
and AuditableBase<TIdType>
classes in your project.
AuditableBase
Example
using DK89.DomainBase;
using System;
namespace MyProject.Domain
{
/// <summary>
/// Example entity inheriting from AuditableBase with a default GUID as ID.
/// </summary>
public class Product : AuditableBase
{
public string Name { get; set; } = default!;
public decimal Price { get; set; }
}
}
// Usage
var product = new Product
{
Id = Guid.NewGuid(),
Name = "Example Product",
Price = 19.99M,
CreatedBy = "Admin",
CreatedDate = DateTime.UtcNow
};
Console.WriteLine($"Product: {product.Name}, Created By: {product.CreatedBy}, Created At: {product.CreatedDate}");
AuditableBase<TIdType>
Example
using DK89.DomainBase;
using System;
namespace MyProject.Domain
{
/// <summary>
/// Example entity inheriting from AuditableBase with a custom ID type.
/// </summary>
public class Order : AuditableBase<int>
{
public DateTime OrderDate { get; set; }
public string CustomerName { get; set; } = default!;
}
}
// Usage
var order = new Order
{
Id = 1,
OrderDate = DateTime.UtcNow,
CustomerName = "John Doe",
CreatedBy = "System",
CreatedDate = DateTime.UtcNow
};
Console.WriteLine($"Order ID: {order.Id}, Customer: {order.CustomerName}, Order Date: {order.OrderDate}");
Key Features of AuditableBase
and AuditableBase<TIdType>
Auditable Fields:
CreatedBy
: Indicates the user who created the entity.CreatedDate
: Timestamp of when the entity was created.EditedBy
: Indicates the user who last edited the entity.EditedDate
: Timestamp of the last edit.IsDeleted
: Boolean flag to indicate soft deletion.
Customizable ID Type:
- The default
AuditableBase
class uses a GUID for theId
. - Use
AuditableBase<TIdType>
to define an ID with a custom type, such asint
,string
, or another type.
- The default
When to Use These Classes
- Entities in a Domain Model: Use these classes as base types for entities that require auditing fields and flexible ID management.
- Soft Deletions: Handle soft deletions gracefully using the
IsDeleted
property.
Additional Notes
- The properties in the base classes can be overridden in derived classes as needed.
- Ensure you initialize the
Id
property in your derived classes, particularly when using custom ID types.
======================================================================
AuditHistory Class
Overview
The AuditHistory
class is a sealed class that inherits from AuditableBase
. It provides a detailed representation of changes made to entities within an application. This design ensures that audit entries include not only the change-specific details but also any common audit fields provided by the AuditableBase
class.
Features
Properties from AuditHistory
- EntityName: The type of entity being modified (e.g., "Product", "Order").
- EntityId: A unique identifier for the specific entity instance.
- PropertyName: The name of the property that was changed.
- OldValue: The property's value before the change.
- NewValue: The property's value after the change.
Inherited from AuditableBase
Ensure that your AuditableBase
class contains fields such as:
- CreatedAt: The timestamp when the audit record was created.
- CreatedBy: The user or system that created the record.
- ModifiedAt: The timestamp of the most recent modification (if applicable).
- ModifiedBy: The user or system that last modified the record.
Benefits
- Extensibility: By inheriting from
AuditableBase
, common audit fields can be reused across other classes. - Strong Type Safety: Sealed class ensures that
AuditHistory
is not further extended, preserving its integrity. - Simplified Initialization: Default values (
default!
) ensure that properties are properly initialized, reducing runtime errors.
Example Usage
Initialization Example
var auditEntry = new AuditHistory
{
EntityName = "Product",
EntityId = Guid.Parse("123e4567-e89b-12d3-a456-426614174000"),
PropertyName = "Price",
OldValue = "100",
NewValue = "120",
CreatedAt = DateTime.UtcNow,
CreatedBy = "AdminUser"
};
// Example: Save `auditEntry` to a database or log for tracking
Console.WriteLine($"Audit Entry for {auditEntry.EntityName} ({auditEntry.EntityId})");
Console.WriteLine($"{auditEntry.PropertyName} changed from {auditEntry.OldValue} to {auditEntry.NewValue}");
Notes
- Ensure that your
AuditableBase
implementation aligns with the audit requirements of your project. - Extend logging mechanisms or database mappings as needed for seamless integration.
Future Enhancements
- Improved Property Tracking: Include metadata for composite properties or collection changes.
- Integration with ORM: Support for automatic tracking with Entity Framework or similar tools.
- Custom Serialization: Add custom JSON converters for auditing frameworks.
Contribution
Contributions to improve this class and its documentation are welcome! Feel free to submit a pull request.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. 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 was computed. 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. |
.NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.1 is compatible. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.1
- System.ComponentModel.Annotations (>= 5.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.