πŸ“„ CommonEntityProps Documentation


Filename: IrbyImaging.DomainClasses/CommonEntityProps.cs
Assembly: IrbyImaging.DomainClasses
Author: kvamshi
Created: 2022-01-11
Last Modified: 2022-01-11


πŸš€ Table of Contents

  1. Overview & Purpose
  2. Dependencies & Technologies
  3. Class Diagram (UML)
  4. Key Functional Areas
  5. API & Properties
  6. Helper Methods / Utilities
  7. Usage Examples
  8. Conclusion

1. Overview & Purpose

The CommonEntityProps class is a ComplexType helper for Entity Framework Core, designed to be inherited or composed by your domain entities. It automatically manages:

  • βœ”οΈ Audit fields: InsertedDate, UpdatedDate, InsertedBy, UpdatedBy
  • βœ”οΈ Concurrency control: Timestamp (row version)

Why Use CommonEntityProps?

  • Consistency: Ensures all entities share the same audit properties.
  • Automation: Auto-populates insertion/update dates and usernames.
  • Concurrency Safety: Timestamp prevents lost updates in concurrent scenarios.

2. Dependencies & Technologies

Technology / Library Purpose
Microsoft.EntityFrameworkCore ORM & Complex Type support
Microsoft.AspNetCore.Http Access HTTP context for current user identity
System.ComponentModel.DataAnnotations Data annotations for validation attributes
System.ComponentModel.DataAnnotations.Schema Schema annotations (e.g., [ComplexType])
.NET Standard / .NET Core Base platform

3. Class Diagram (UML)

classDiagram
    direction LR
    class CommonEntityProps {
        <<ComplexType>>
        - DateTime _insertedDate
        - DateTime _updatedDate
        - string  _insertedBy
        - string  _updatedBy
        - Byte[]  Timestamp
        - IHttpContextAccessor _httpContextAccessor
        + CommonEntityProps()
        + DateTime InsertedDate { get; set; }
        + DateTime UpdatedDate { get; set; }
        + string InsertedBy { get; set; }
        + string UpdatedBy { get; set; }
        + Byte[] Timestamp { get; set; }
        # string GetIdentity()
    }

4. Key Functional Areas

  1. Constructor Initialization

    • Sets both InsertedDate & UpdatedDate to DateTime.UtcNow.
    • Retrieves current user identity and sets InsertedBy & UpdatedBy.
  2. Audit Properties

    • InsertedDate / UpdatedDate: Timestamps for create/update events.
    • InsertedBy / UpdatedBy: Username of actor performing the action.
  3. Concurrency Control

    • [Timestamp] attribute on Timestamp property.
    • EF Core uses this rowversion to detect conflicting updates.
  4. Identity Resolution

    • GetIdentity() attempts to fetch the current user name from IHttpContextAccessor.

5. API & Properties

Property Name Type Annotations Description
InsertedDate DateTime β€” UTC timestamp when entity was created.
UpdatedDate DateTime β€” UTC timestamp when entity was last modified.
InsertedBy string [MaxLength(64)] Username of creator.
UpdatedBy string [MaxLength(64)] Username of last modifier.
Timestamp Byte[] [Timestamp] Row-version for concurrency checks.
  • Getter logic returns the private backing field.
  • Setter logic for InsertedBy/UpdatedBy ensures non-empty values, fallback to GetIdentity().

6. Helper Methods / Utilities

GetIdentity()

private string GetIdentity()
{
    string identityName = string.Empty;
    // Fallback to IHttpContextAccessor if System.Web is not available
    if (string.IsNullOrEmpty(identityName))
    {
        identityName = _httpContextAccessor.HttpContext.User.Identity.Name;
    }
    return identityName;
}
  • πŸ“Œ Purpose: Retrieves the current user’s identity from the HTTP context.
  • πŸ”„ Fallback: Could be extended for Windows Identity or other authentication schemes.

7. Usage Examples

7.1. Inherit in an Entity

public class Patient : CommonEntityProps
{
    public int Id { get; set; }
    public string Name { get; set; }
    // other patient-specific props...
}

7.2. Register IHttpContextAccessor

In Startup.cs or equivalent:

public void ConfigureServices(IServiceCollection services)
{
    services.AddHttpContextAccessor();
    services.AddDbContext<AppDbContext>(options => 
        options.UseSqlServer(Configuration.GetConnectionString("Default")));
}

7.3. Save Changes & Audit

EF Core will automatically populate audit fields:

using(var context = new AppDbContext())
{
    var patient = new Patient { Name = "John Doe" };
    context.Patients.Add(patient);
    context.SaveChanges();
    // patient.InsertedDate and patient.InsertedBy are set
}

8. Conclusion

CommonEntityProps is your go‑to solution for:

  • πŸ” Securing your data with row‑version concurrency.
  • πŸ“‹ Tracking who created/modified each record and when.
  • πŸ“ˆ Standardizing audit logic across all EF Core entities.

By integrating this helper into your domain models, you ensure robust, consistent, and maintainable data auditing with minimal boilerplate. πŸš€


Need more help? Feel free to explore the source, extend the GetIdentity() logic, or reach out to the IrbyImaging team!