Skip to content

Commit

Permalink
Refined Data.Db code
Browse files Browse the repository at this point in the history
  • Loading branch information
JJBussert committed Feb 16, 2021
1 parent 3c24a0e commit 9ffd518
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 26 deletions.
52 changes: 32 additions & 20 deletions src/E13.Common.Data.Db/BaseDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Runtime.CompilerServices;
using System.Text;
using System.Diagnostics;
using System.Reflection;

namespace E13.Common.Data.Db
{
Expand All @@ -15,39 +16,50 @@ public abstract class BaseDbContext : DbContext
/// <summary>
/// The user name used when the user name is null
/// </summary>
private const string UnknownUser = "*Unknown";
public const string UnknownUser = "*Unknown";

protected ILogger Logger { get;}
protected string User { get; set; }
protected BaseDbContext(ILogger logger, string user)
protected BaseDbContext(DbContextOptions options, ILogger logger)
: base(options)
{
Logger = logger;
User = user;
}

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
base.OnConfiguring(optionsBuilder);
}

public override int SaveChanges()
public int SaveChanges(string user, string source = null)
{
var caller = new StackFrame(1).GetMethod();
TagEntries($"{caller.DeclaringType}.{caller.Name}");
if(source == null)
{
var caller = new StackFrame(1).GetMethod();
source = $"{caller.DeclaringType}.{caller.Name}";
}

TagEntries(source, user);
var result = base.SaveChanges();

// saving after a reload effectively clears the change tracker affecting no records
Reload();
base.SaveChanges();

return result;
}

public override int SaveChanges()
{
var caller = new StackFrame(1).GetMethod();

return SaveChanges(UnknownUser, $"{caller.DeclaringType}.{caller.Name}");
}

public void Reload() => ChangeTracker.Entries()
.Where(e => e.Entity != null).ToList()
.ForEach(e => e.State = EntityState.Detached);

private void TagEntries(string source)
private void TagEntries(string source, string user)
{
var entries = ChangeTracker.Entries().Where(e =>
e.Entity is IEntity &&
Expand All @@ -61,27 +73,27 @@ e.Entity is IEntity &&
Logger.LogDebug($"Entity: {entry.Entity.GetType().Name}, State: {entry.State}");
var utcNow = DateTime.UtcNow;

if (entry.Entity is IEntity)
if (entry.Entity is IEntity entity)
{
if (entry.State == EntityState.Added)
{
((IEntity)entry.Entity).Created = utcNow;
((IEntity)entry.Entity).CreatedBy = User ?? UnknownUser;
((IEntity)entry.Entity).CreatedSource = source;
entity.Created = utcNow;
entity.CreatedBy = user;
entity.CreatedSource = source;
}

((IEntity)entry.Entity).Modified = utcNow;
((IEntity)entry.Entity).ModifiedBy = User ?? UnknownUser;
((IEntity)entry.Entity).ModifiedSource = source;
entity.Modified = utcNow;
entity.ModifiedBy = user;
entity.ModifiedSource = source;

if (entry.State == EntityState.Deleted && entry.Entity is IDeletable)
if (entry.State == EntityState.Deleted && entry.Entity is IDeletable deletable)
{
// Implementing IDeletable implies soft deletes required
entry.State = EntityState.Modified;

((IDeletable)entry.Entity).Deleted = utcNow;
((IDeletable)entry.Entity).DeletedBy = User ?? UnknownUser;
((IDeletable)entry.Entity).DeletedSource = source;
deletable.Deleted = utcNow;
deletable.DeletedBy = user;
deletable.DeletedSource = source;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/E13.Common.Data.Db/Repository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ public virtual int Count(Expression<Func<TEntity, bool>> predicate = null)
/// <param name="entity">The entity to insert.</param>
public virtual void Insert(TEntity entity)
{
var entry = DbSet.Add(entity);
DbSet.Add(entity);
}

/// <summary>
Expand Down
6 changes: 4 additions & 2 deletions src/E13.Common.Domain/IDeletable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

namespace E13.Common.Domain
{
public interface IDeletable : IEntity
public interface IDeletable
{
string DeletedBy { get; set; }
string DeletedSource { get; set; }
DateTime Deleted { get; set; }
DateTime? Deleted { get; set; }
public bool IsDeleted()
=> Deleted == null;
}
}
2 changes: 1 addition & 1 deletion src/E13.Common.Domain/IEffectable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace E13.Common.Domain
{
public interface IEffectable : IEntity
public interface IEffectable
{
string EffectiveBy { get; set; }
string EffectiveSource { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion src/E13.Common.Domain/IExpirable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace E13.Common.Domain
{
public interface IExpirable : IEntity
public interface IExpirable
{
string ExpirationBy { get; set; }
string ExpirationSource { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion src/E13.Common.Domain/IOwnable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace E13.Common.Domain
{
public interface IOwnable : IEntity
public interface IOwnable
{
string OwnedBy { get; set; }
string OwnedSource { get; set; }
Expand Down

0 comments on commit 9ffd518

Please sign in to comment.