views:

194

answers:

1

I'm using Dynamic Data and LINQ to SQL for some admin pages on a .NET 3.5 web app. All my admin tables have a CreatedBy, CreatedDate, UpdatedBy, and UpdatedDate.

I'm looking for a way to inject the setting of these properties before the objects are inserted and updated.

I've seen an object_inserting hook if you have a linq to sql datasource in the web form, but I'm using dynamic data...is there an easy way to generically set that? And I've also looked at modifying each of the partial classes for my admin objects, but the closest hook I see is to implement the OnValidate method with the Insert action. Any suggestions? TIA.

A: 

David Turvey has published a great example of adding in an OnSaving and OnSaved methods for your entities, check here: Adding OnSaving an OnSaved Events to LINQ to SQL Entities

By implementing the above on your entities, you can extend them with a partial class, e.g.

partial class MyAdminEntity : EntityBase
{
  internal override OnSaving(ChangeAction changeAction)
  {
    if (changeAction == ChangeAction.Insert)
    {
      CreatedBy = "<username>";
      CreatedDate = DateTime.Now;
    }
    else if (changeAction == ChangeAction.Update)
    {
      CreatedBy = "<username>";
      CreatedDate = DateTime.Now;
    }
  }
}
Matthew Abbott