Skip to content

Commit

Permalink
manually merged Pull Request bilal-fazlani#141
Browse files Browse the repository at this point in the history
  • Loading branch information
asulwer committed Jul 24, 2018
1 parent 30868af commit 624ac83
Show file tree
Hide file tree
Showing 5 changed files with 250 additions and 79 deletions.
103 changes: 84 additions & 19 deletions TracerEnabledDbContext.Core.Identity/TrackerIdentityContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,57 @@ public class TrackerIdentityContext<TUser, TRole, TKey, TUserClaim, TUserRole, T
private Func<string> _usernameFactory;
private string _defaultUsername;
private Action<dynamic> _metadataConfiguration;
private bool _trackingEnabled = true;
private bool _additionTrackingEnabled = true;
private bool _modificationTrackingEnabled = true;
private bool _deletionTrackingEnabled = true;

public bool TrackingEnabled
{
get
{
return GlobalTrackingConfig.Enabled && _trackingEnabled;
return GlobalTrackingConfig.Enabled && (_additionTrackingEnabled || _modificationTrackingEnabled || _deletionTrackingEnabled);
}
set
{
_trackingEnabled = value;
AdditionTrackingEnabled = value;
ModificationTrackingEnabled = value;
DeletionTrackingEnabled = value;
}
}
public bool AdditionTrackingEnabled
{
get
{
return GlobalTrackingConfig.AdditionsEnabled && _additionTrackingEnabled;
}
set
{
_additionTrackingEnabled = value;
}
}
public bool ModificationTrackingEnabled
{
get
{
return GlobalTrackingConfig.ModificationsEnabled && _modificationTrackingEnabled;
}
set
{
_modificationTrackingEnabled = value;
}
}
public bool DeletionTrackingEnabled
{
get
{
return GlobalTrackingConfig.DeletionsEnabled && _deletionTrackingEnabled;
}
set
{
_deletionTrackingEnabled = value;
}
}

public virtual DbSet<AuditLog> AuditLogs { get; set; }
public virtual DbSet<AuditLogDetail> AuditLogDetails { get; set; }

Expand Down Expand Up @@ -181,17 +219,30 @@ public virtual int SaveChanges(object userName)
dynamic metadata = new ExpandoObject();
_metadataConfiguration?.Invoke(metadata);

_coreTracker.AuditChanges(userName, metadata);
if(ModificationTrackingEnabled)
_coreTracker.AuditModifications(userName, metadata);
if (DeletionTrackingEnabled)
_coreTracker.AuditDeletions(userName, metadata);

IEnumerable<EntityEntry> addedEntries = _coreTracker.GetAdditions();
// Call the original SaveChanges(), which will save both the changes made and the audit records...Note that added entry auditing is still remaining.
int result = base.SaveChanges();
//By now., we have got the primary keys of added entries of added entiries because of the call to savechanges.
int result;
if (AdditionTrackingEnabled)
{
IEnumerable<EntityEntry> addedEntries = _coreTracker.GetAdditions();
// Call the original SaveChanges(), which will save both the changes made and the audit records...Note that added entry auditing is still remaining.
result = base.SaveChanges();
//By now., we have got the primary keys of added entries of added entiries because of the call to savechanges.

_coreTracker.AuditAdditions(userName, addedEntries, metadata);
_coreTracker.AuditAdditions(userName, addedEntries, metadata);

//save changes to audit of added entries
base.SaveChanges();
//save changes to audit of added entries
base.SaveChanges();
}
else
{
//save changes
result = base.SaveChanges();
}

return result;
}
/// <summary>
Expand Down Expand Up @@ -233,18 +284,32 @@ public virtual async Task<int> SaveChangesAsync(object userName, CancellationTok
dynamic metadata = new ExpandoObject();
_metadataConfiguration?.Invoke(metadata);

_coreTracker.AuditChanges(userName, metadata);
if (ModificationTrackingEnabled)
_coreTracker.AuditModifications(userName, metadata);

IEnumerable<EntityEntry> addedEntries = _coreTracker.GetAdditions();
if (DeletionTrackingEnabled)
_coreTracker.AuditDeletions(userName, metadata);

// Call the original SaveChanges(), which will save both the changes made and the audit records...Note that added entry auditing is still remaining.
int result = await base.SaveChangesAsync(cancellationToken);

//By now., we have got the primary keys of added entries of added entiries because of the call to savechanges.
_coreTracker.AuditAdditions(userName, addedEntries, metadata);
int result;
if (AdditionTrackingEnabled)
{
IEnumerable<EntityEntry> addedEntries = _coreTracker.GetAdditions();

// Call the original SaveChanges(), which will save both the changes made and the audit records...Note that added entry auditing is still remaining.
result = await base.SaveChangesAsync(cancellationToken);

//save changes to audit of added entries
await base.SaveChangesAsync(cancellationToken);
//By now., we have got the primary keys of added entries of added entiries because of the call to savechanges.
_coreTracker.AuditAdditions(userName, addedEntries, metadata);

//save changes to audit of added entries
await base.SaveChangesAsync(cancellationToken);
}
else
{
//save changes
result = await base.SaveChangesAsync(cancellationToken);
}

return result;
}
Expand Down
100 changes: 81 additions & 19 deletions TrackerEnabledDbContext.Core/TrackerContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,57 @@ public class TrackerContext : DbContext, ITrackerContext
private Func<string> _usernameFactory;
private string _defaultUsername;
private Action<dynamic> _metadataConfiguration;
private bool _trackingEnabled = true;
private bool _additionTrackingEnabled = true;
private bool _modificationTrackingEnabled = true;
private bool _deletionTrackingEnabled = true;

public bool TrackingEnabled
{
get
{
return GlobalTrackingConfig.Enabled && _trackingEnabled;
return GlobalTrackingConfig.Enabled && (_additionTrackingEnabled || _modificationTrackingEnabled || _deletionTrackingEnabled);
}
set
{
_trackingEnabled = value;
_additionTrackingEnabled = value;
_modificationTrackingEnabled = value;
_deletionTrackingEnabled = value;
}
}
public bool AdditionTrackingEnabled
{
get
{
return GlobalTrackingConfig.AdditionsEnabled && _additionTrackingEnabled;
}
set
{
_additionTrackingEnabled = value;
}
}
public bool ModificationTrackingEnabled
{
get
{
return GlobalTrackingConfig.ModificationsEnabled && _modificationTrackingEnabled;
}
set
{
_modificationTrackingEnabled = value;
}
}
public bool DeletionTrackingEnabled
{
get
{
return GlobalTrackingConfig.DeletionsEnabled && _deletionTrackingEnabled;
}
set
{
_deletionTrackingEnabled = value;
}
}

public virtual DbSet<AuditLog> AuditLogs { get; set; }
public virtual DbSet<AuditLogDetail> AuditLogDetails { get; set; }

Expand Down Expand Up @@ -117,17 +155,29 @@ public virtual int SaveChanges(object userName)
dynamic metaData = new ExpandoObject();
_metadataConfiguration?.Invoke(metaData);

_coreTracker.AuditChanges(userName, metaData);
if (ModificationTrackingEnabled)
_coreTracker.AuditModifications(userName, metaData);
if (DeletionTrackingEnabled)
_coreTracker.AuditDeletions(userName, metaData);

IEnumerable<EntityEntry> addedEntries = _coreTracker.GetAdditions();
// Call the original SaveChanges(), which will save both the changes made and the audit records...Note that added entry auditing is still remaining.
int result = base.SaveChanges();
//By now., we have got the primary keys of added entries of added entiries because of the call to savechanges.
int result;
if (AdditionTrackingEnabled)
{
IEnumerable<EntityEntry> addedEntries = _coreTracker.GetAdditions();
// Call the original SaveChanges(), which will save both the changes made and the audit records...Note that added entry auditing is still remaining.
result = base.SaveChanges();
//By now., we have got the primary keys of added entries of added entiries because of the call to savechanges.

_coreTracker.AuditAdditions(userName, addedEntries, metaData);
_coreTracker.AuditAdditions(userName, addedEntries, metaData);
//save changes to audit of added entries
base.SaveChanges();
}
else
{
//save changes
result = base.SaveChanges();
}

//save changes to audit of added entries
base.SaveChanges();
return result;
}
/// <summary>
Expand Down Expand Up @@ -162,18 +212,30 @@ public virtual async Task<int> SaveChangesAsync(object userName, CancellationTok
dynamic metadata = new ExpandoObject();
_metadataConfiguration?.Invoke(metadata);

_coreTracker.AuditChanges(userName, metadata);
if (ModificationTrackingEnabled)
_coreTracker.AuditModifications(userName, metadata);
if (DeletionTrackingEnabled)
_coreTracker.AuditDeletions(userName, metadata);

IEnumerable<EntityEntry> addedEntries = _coreTracker.GetAdditions();
int result;
if (AdditionTrackingEnabled)
{
IEnumerable<EntityEntry> addedEntries = _coreTracker.GetAdditions();

// Call the original SaveChanges(), which will save both the changes made and the audit records...Note that added entry auditing is still remaining.
int result = await base.SaveChangesAsync(cancellationToken);
// Call the original SaveChanges(), which will save both the changes made and the audit records...Note that added entry auditing is still remaining.
result = await base.SaveChangesAsync(cancellationToken);

//By now., we have got the primary keys of added entries of added entiries because of the call to savechanges.
_coreTracker.AuditAdditions(userName, addedEntries, metadata);
//By now., we have got the primary keys of added entries of added entiries because of the call to savechanges.
_coreTracker.AuditAdditions(userName, addedEntries, metadata);

//save changes to audit of added entries
await base.SaveChangesAsync(cancellationToken);
//save changes to audit of added entries
await base.SaveChangesAsync(cancellationToken);
}
else
{
//save changes
result = await base.SaveChangesAsync(cancellationToken);
}

return result;
}
Expand Down
16 changes: 13 additions & 3 deletions TrackerEnabledDbContext/Configuration/GlobalTrackingConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,20 @@ namespace TrackerEnabledDbContext.Common.Configuration
{
public static class GlobalTrackingConfig
{
public static bool Enabled { get; set; } = true;

public static bool Enabled
{
get { return AdditionsEnabled || ModificationsEnabled || DeletionsEnabled; }
set
{
AdditionsEnabled = value;
ModificationsEnabled = value;
DeletionsEnabled = value;
}
}
public static bool AdditionsEnabled { get; set; } = true;
public static bool ModificationsEnabled { get; set; } = true;
public static bool DeletionsEnabled { get; set; } = true;
public static bool TrackEmptyPropertiesOnAdditionAndDeletion { get; set; } = false;

public static bool DisconnectedContext { get; set; } = false;

/// <summary>
Expand Down
Loading

0 comments on commit 624ac83

Please sign in to comment.