vendredi 19 février 2016

Updating common fields in models

I have some problems with duplicated code. I have multiple models that have same properties like this:

public DateTimeOffset CreatedDate { get; set; }
public DateTimeOffset ModifiedDate { get; set; }
public virtual AppUser CreatedBy { get; set; }
public virtual AppUser ModifiedBy { get; set; }

Now I am writing identical code to update this fields in multiple actions of controllers, like this:

model.CreatedBy = user;
model.CreatedDate = now;
model.ModifiedBy = user;
model.ModifiedDate = now; 

I am wondering is there any method or pattern to avoid this duplicate codes? Is any best practice in this situation? I am using EntityFramework and ASP.net Core Web API. (if that make sense).

Aucun commentaire:

Enregistrer un commentaire