Audit fields in WebAPI

  • Thread starter Thread starter Context Menu Solution
  • Start date Start date
C

Context Menu Solution

Guest
Hi,

I have a slightly tricky situation at hand and need some help here. I am developing a WebAPI application in C# using Domain Driven Design architecture and using Code First using Entity Framework. Its almost complete but I want some refactoring.

So Its WebAPI calls into a Application Layer which gets Repository objects using constructor Injection in Application and connects to the database using EF. A seperate Domain object has all the entities. Application layer gets the domain entities, converts them into ViewModels(DTOs) and sends to API.

While things are working, some of my entities require Audit Info i.e. CreatedOn, ModifiedOn, CreatedByUserId, ModifiedByUserId.

So currently, I am manually adding these values in Application layer to populate DTO, then populate domain/entity object using automapper, and makes a call to repository to Insert or Update.

I want to refactor this code, as the number of entities are increasing, this code is being repeated e.g

SomeDTO.CreatedOn = DateTime.Now(),
SomeDTO.CreatedBy = userId etc...

My current entity design is something like this:

public class SomeEntity : Entity
and Entity is defined as Public class Entity{public Guid Id;}

Now, I am thinking along the lines to have AuditInfo in this parent Entity

So change it to be:
public class SomeEntity : AuditEntity
and Entity is defined as Public class AuditEntity{public Guid Id; public DateTime CreatedOn; public Guid CreatedByUserId etc.. }

But I am just not able to understand how and when to populate this AuditEntity and how to attach it to SomeEntity.

I hope I have explained my challenge well. Thanks alot for any hints.

Thanks








vishal

Continue reading...
 
Back
Top