π CommonEntityProps Documentation
Filename: IrbyImaging.DomainClasses/CommonEntityProps.cs
Assembly: IrbyImaging.DomainClasses
Author: kvamshi
Created: 2022-01-11
Last Modified: 2022-01-11
π Table of Contents
- Overview & Purpose
- Dependencies & Technologies
- Class Diagram (UML)
- Key Functional Areas
- API & Properties
- Helper Methods / Utilities
- Usage Examples
- 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:
Timestampprevents 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
-
Constructor Initialization
- Sets both
InsertedDate&UpdatedDatetoDateTime.UtcNow. - Retrieves current user identity and sets
InsertedBy&UpdatedBy.
- Sets both
-
Audit Properties
InsertedDate/UpdatedDate: Timestamps for create/update events.InsertedBy/UpdatedBy: Username of actor performing the action.
-
Concurrency Control
[Timestamp]attribute onTimestampproperty.- EF Core uses this rowversion to detect conflicting updates.
-
Identity Resolution
GetIdentity()attempts to fetch the current user name fromIHttpContextAccessor.
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/UpdatedByensures non-empty values, fallback toGetIdentity().
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!