📄 IrbyImaging/Repository/IAuditRepository.cs Documentation
📚 Table of Contents
- Overview & Purpose
- Key Functional Areas
- Dependencies & Technologies
- Data Models
- Interface Definition
- Sample Implementation
- Usage Examples
- UML Class Diagram
- Error Handling & Return Types
- Integration & Registration
- Conclusion
🔍 1. Overview & Purpose
IAuditRepository is part of the IrbyImaging.UI.Repository namespace and defines a contract for persisting audit logs within the IrbyImaging application.
By abstracting the logging mechanism behind an interface, the system gains:
- Loose coupling between business logic and data storage
- Testability via mocking/stubbing in unit tests
- Swappable implementations (e.g., SQL, NoSQL, flat files)
Primary Responsibility
Provide a method to insert audit entries (user actions, system events, etc.) into a storage medium.
🎯 2. Key Functional Areas
| Feature | Description |
|---|---|
| Audit Insertion | InsertAuditLogs(AuditModel objauditmodel) |
| Abstraction | Defines an interface contract for any audit provider. |
| Extensibility | Supports multiple concrete implementations. |
⚙️ 3. Dependencies & Technologies
- Language: C#
- Platform: .NET (Core / Framework)
- Project Layer: UI → Repository
- Namespace:
IrbyImaging.UI.Repository - Model Dependency:
IrbyImaging.UI.Models.AuditModel
No third-party NuGet packages are required by the interface itself.
🗃️ 4. Data Models
AuditModel (in IrbyImaging.UI.Models)
This model carries the details of an audit event. A typical AuditModel might include:
| Property | Type | Description |
|---|---|---|
UserId |
string |
Identifier of the acting user |
Action |
string |
Description of the performed action |
Timestamp |
DateTime |
Date and time when the action occurred |
Details |
string |
Additional context or payload |
namespace IrbyImaging.UI.Models {
public class AuditModel {
public string UserId { get; set; }
public string Action { get; set; }
public DateTime Timestamp { get; set; }
public string Details { get; set; }
}
}
🏷️ 5. Interface Definition
using IrbyImaging.UI.Models;
namespace IrbyImaging.UI.Repository
{
/// <summary>
/// Defines repository operations for audit logging.
/// </summary>
public interface IAuditRepository
{
/// <summary>
/// Inserts a new audit log entry into the audit store.
/// </summary>
/// <param name="objAuditModel">
/// The <see cref="AuditModel"/> containing audit details.
/// </param>
void InsertAuditLogs(AuditModel objAuditModel);
}
}
- Interface Name:
IAuditRepository - Method:
InsertAuditLogs(AuditModel objAuditModel)– Persists one audit record.
🛠️ 6. Sample Implementation
Here's an example concrete class that writes audit logs to a relational database using Entity Framework Core:
using IrbyImaging.UI.Models;
using Microsoft.EntityFrameworkCore;
namespace IrbyImaging.UI.Repository
{
public class AuditRepository : IAuditRepository
{
private readonly AuditDbContext _context;
public AuditRepository(AuditDbContext context)
{
_context = context;
}
public void InsertAuditLogs(AuditModel objAuditModel)
{
// Map AuditModel to EF entity, e.g., AuditEntity
var entity = new AuditEntity
{
UserId = objAuditModel.UserId,
Action = objAuditModel.Action,
Timestamp = objAuditModel.Timestamp,
Details = objAuditModel.Details
};
_context.AuditLogs.Add(entity);
_context.SaveChanges();
}
}
}
📋 7. Usage Examples
Dependency Injection Setup
// In Startup.cs or DI configuration
services.AddScoped<IAuditRepository, AuditRepository>();
Writing an Audit Log
public class ImageService
{
private readonly IAuditRepository _auditRepo;
public ImageService(IAuditRepository auditRepo)
{
_auditRepo = auditRepo;
}
public void DeleteImage(int imageId, string currentUser)
{
// Perform deletion logic...
// Log the deletion
_auditRepo.InsertAuditLogs(new AuditModel {
UserId = currentUser,
Action = $"DeleteImage:{imageId}",
Timestamp = DateTime.UtcNow,
Details = $"User {currentUser} deleted image #{imageId}"
});
}
}
🧩 8. UML Class Diagram
classDiagram
direction LR
class IAuditRepository {
<<interface>>
+ void InsertAuditLogs(AuditModel obj)
}
class AuditModel {
+ string UserId
+ string Action
+ DateTime Timestamp
+ string Details
}
class AuditRepository {
- AuditDbContext _context
+ void InsertAuditLogs(AuditModel obj)
}
IAuditRepository <|.. AuditRepository
AuditRepository --> AuditDbContext
AuditRepository ..> AuditModel
⚠️ 9. Error Handling & Return Types
- Return Type:
void - Exception Strategy:
- Let exceptions bubble up (e.g.,
DbUpdateException) - Optionally, you may wrap calls in try/catch and log errors separately.
- Let exceptions bubble up (e.g.,
Example with explicit error handling:
public void InsertAuditLogs(AuditModel obj)
{
try
{
// ...
_context.SaveChanges();
}
catch (Exception ex)
{
// Consider logging to a fallback store or alerting system
throw new AuditLogException("Failed to persist audit log.", ex);
}
}
🔗 10. Integration & Registration
- ASP.NET Core DI:
services.AddScoped<IAuditRepository, AuditRepository>(); - Manual Composition (non-DI scenarios):
var context = new AuditDbContext(...); IAuditRepository auditRepo = new AuditRepository(context);
No external web APIs or endpoints are exposed by the interface itself.
✔️ 11. Conclusion
IAuditRepository plays a vital role in ensuring that all significant user and system events are tracked consistently and reliably. By programming against this interface, IrbyImaging achieves:
- 🔄 Consistency in audit log operations across modules
- ⚙️ Flexibility to swap out storage mechanisms
- ✅ Testability for robust unit testing
Implement your preferred persistence strategy behind this contract to keep your auditing concerns cleanly separated from business logic.
Happy auditing! 🎉