Skip to content

Use Base.Record and IUser for audit tracking and more

Adam O'Neil edited this page May 14, 2019 · 2 revisions

Many applications need a standardized approach to tracking user names and timestamps of created and modified data. In addition, you might need back-end permission checks, or perhaps a unified approach to error logging. The starting point for implementing these kinds of features in your apps is to base your model classes on Postulate.Base.Record. This gives you access to a number of virtual methods for validation, permission checks, and trigger-like behavior in your data layer. Several of the Base.Record virtual methods accept an IUser argument. In your application, you would implement IUser on your user profile class in order to make user info from your application available to your data layer.

I'll walk through a demo project MvcSpace that uses these components. I don't have a useful permissions demo at the moment, but I have some other stuff:

  • The BaseTable class defines conventional audit tracking columns that apply to most tables in the application: DateCreated, CreatedBy, DateModified and ModifiedBy. By inheriting from Postulate.Base.Record, it can override the Validate and BeforeSave methods. In this app, the Validate override requires valid URIs where specified with the ValidUri attribute. More generally important is the BeforeSave override, which is used to capture the user name and local time of someone creating or updating a record.
  • The UserProfile class maps to the AspNetUsers database table. Note that it implements IUser, which gives access to the user's LocalTime and of course the UserName. If I had more permission-related properties, I could leverage them in the BaseTable using Postulate.Base.Record CheckFindPermission, CheckSavePermission, or CheckDeletePermission, for instance.
  • The BaseController provides app-wide conventions, among them TrySave, TryUpdate, and TryDelete, which provide standard CRUD methods for the whole application. (I could've used async versions just as well, but for some reason didn't yet.) Notice how these pass the CurrentUser property as the IUser argument. CurrentUser is set in the controller's Initialize override using SessionTableStorage, which is another discussion.
You can’t perform that action at this time.