I inherited a C# ASP.Net Core (now .Net 5.0) project with a bunch of entities models like this:
public class Foo
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid ID { get; set; }
[Column(TypeName = "varchar(36)")]
public string CreatedBy { get; set; }
public DateTime CreatedAt { get; set; }
[Column(TypeName = "varchar(36)")]
public string RDoxFindingsID { get; set; }
[Column(TypeName = "varchar(36)")]
public string LastUpdatedBy { get; set; }
public DateTime LastUpdatedAt { get; set; }
public string F001 { get; set; }
...
public class Bar
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid ID { get; set; }
[Column(TypeName = "varchar(36)")]
public string CreatedBy { get; set; }
public DateTime CreatedAt { get; set; }
[Column(TypeName = "varchar(36)")]
public string RDoxFindingsID { get; set; }
[Column(TypeName = "varchar(36)")]
public string LastUpdatedBy { get; set; }
public DateTime LastUpdatedAt { get; set; }
public string Q100 { get; set; }
... <= The same "header" fields (CreatedBy, CreatedAt, etc. etc) are copied/pasted at the top of each different model
So my Create and Edit razor pages have a lot of duplicate code like this:
public class CreateModel : PageModel
{
...
public async Task<IActionResult> OnPostAsync()
{
if (!ModelState.IsValid)
{
return Page();
}
Foo.CreatedAt = DateTime.Now;
Foo.CreatedBy = Findings.CreatedBy;
Foo.LastUpdatedAt = DateTime.Now;
Foo.LastUpdatedBy = Findings.CreatedBy;
_context.Foo.Add(Foo);
await _context.SaveChangesAsync();
Bar.CreatedAt = DateTime.Now;
Bar.CreatedBy = Findings.CreatedBy;
Bar.LastUpdatedAt = DateTime.Now;
Bar.LastUpdatedBy = Findings.CreatedBy;
_context.Bar.Add(Bar);
await _context.SaveChangesAsync();
...
Q: Is there any clever way I can refactor my C# code such that I can write ONE method I can call for ANY of these models? Without copying/pasting each of these 4-6 lines over and over for each individual model?
NOTE: I CANNOT change the model. But I can do whatever I want with the "Pages" code.