0

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.

What I would like to do is have this Audit entity populate once on each call, and be somehow added to entity, perhaps using automapper transformation, if possible. any hits will be highly appreciated.

Thanks

  • You're not the first one facing this problem. Please look at other (Stack Overflow) posts on this point, for example, https://stackoverflow.com/q/52020107/861716. Apart from that, this question is too broad/opinion-based. There are many ways to implement this somehow. – Gert Arnold Mar 20 '20 at 12:50
  • This example is quite close but its still unclear as to how to send userId to the EF repository. I dont want to add additional parameter so which means, I will have to add this to entity values while sending to repository hence that still require some uniform way. – user3836777 Mar 20 '20 at 14:08

0 Answers0