nightowl
nightowl

Reputation: 339

Audit in Data Layer instead of single Layer Application

This is my first kind of task and i am using this tutorial. Link

The differences are that this tutorial is made in a single layer so it is easy accessing the Identity properties.

My project has

  1. Data Layer, where DB context is located (and all models dbsets)
  2. Entities(Models) that has reference to Data, Service and Presentation Layers
  3. Services that have reference to presentation layer
  4. Presentation Layer

The problem is that i put Audit model in Entities Layer, but in the tutorial it has foreign key to AspNetUsers table and code-first migration did not pass.

he ForeignKeyAttribute on property 'AuditUserId' on type 'Namespace.Entities.AuditActions.Audit' is not valid. The navigation property 'AuditUser' was not found on the dependent type 'Namespace.Entities.AuditActions.Audit'. The Name value should be a valid navigation property name

Second problem is that i cant access the User info from Data Layer where i have put the save overrides.

var currentUser = OwinContextHelper.CurrentApplicationUser;

I cant Access the OwinHelpper Class which is placed in presentation layer from the Data Layer where the overrides are placed.

How do i implement this tutorial in my application?

Any idea will be welcomed.

Be easy on me am a junior dev :)

Upvotes: 1

Views: 155

Answers (1)

thepirat000
thepirat000

Reputation: 13114

Trying to help with your second problem:

I would recommend the Audit.NET/Audit.EF libraries for this kind of use case (actually I guess you are already using it).

You can avoid the information passing from presentation layer to the data layer with the use of a Custom Action. The library gives you the possibility to hook into the audit creation, and you can put that code on the presentation layer, for example:

class Presentation
{
    void StartUp()
    {
        // Use SqlServerDataProvider to store the audit events on SQL
        Audit.Core.Configuration.Setup()
            .UseSqlServer(_ => _
                .ConnectionString("...")
                .TableName("Event")
                .IdColumnName("EventId")
                .JsonColumnName("Data"));

        // Add a custom action to have a custom field 
        Audit.Core.Configuration.AddOnCreatedAction(scope =>
        {
            scope.SetCustomField("OwinUser", OwinContextHelper.CurrentApplicationUser);
        });
    }
}

Upvotes: 1

Related Questions