Skip to content

Commit

Permalink
fixed SaveChanges code w/ Domain interface enhancements
Browse files Browse the repository at this point in the history
  • Loading branch information
JJBussert committed Feb 17, 2021
1 parent d31c503 commit 26ed17f
Show file tree
Hide file tree
Showing 18 changed files with 281 additions and 177 deletions.
44 changes: 23 additions & 21 deletions src/E13.Common.Data.Db/BaseDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ public void Reload() => ChangeTracker.Entries()

private void TagEntries(string source, string user)
{
var e = ChangeTracker.Entries().ToList();

var entries = ChangeTracker.Entries().Where(e =>
e.Entity is IEntity &&
(e.State == EntityState.Added || e.State == EntityState.Modified || e.State == EntityState.Deleted)
Expand All @@ -73,28 +75,28 @@ e.Entity is IEntity &&
Logger.LogDebug($"Entity: {entry.Entity.GetType().Name}, State: {entry.State}");
var utcNow = DateTime.UtcNow;

if (entry.Entity is IEntity entity)
if (entry.State == EntityState.Added && entry.Entity is ICreatable creatable)
{
creatable.Created = utcNow;
creatable.CreatedBy = user;
creatable.CreatedSource = source;
}

if (entry.Entity is IModifiable modifiable)
{
if (entry.State == EntityState.Added)
{
entity.Created = utcNow;
entity.CreatedBy = user;
entity.CreatedSource = source;
}

entity.Modified = utcNow;
entity.ModifiedBy = user;
entity.ModifiedSource = source;

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

deletable.Deleted = utcNow;
deletable.DeletedBy = user;
deletable.DeletedSource = source;
}
modifiable.Modified = utcNow;
modifiable.ModifiedBy = user;
modifiable.ModifiedSource = source;
}

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

deletable.Deleted = utcNow;
deletable.DeletedBy = user;
deletable.DeletedSource = source;
}
}
}
Expand Down
15 changes: 15 additions & 0 deletions src/E13.Common.Domain/ICreatable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace E13.Common.Domain
{
public interface ICreatable : IEntity
{
string CreatedBy { get; set; }
string CreatedSource { get; set; }
DateTime Created { get; set; }
}
}
4 changes: 2 additions & 2 deletions src/E13.Common.Domain/IDeletable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

namespace E13.Common.Domain
{
public interface IDeletable
public interface IDeletable : IEntity
{
string DeletedBy { get; set; }
string DeletedSource { get; set; }
DateTime? Deleted { get; set; }
public bool IsDeleted()
=> Deleted == null;
{ return 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
public interface IEffectable : IEntity
{
string EffectiveBy { get; set; }
string EffectiveSource { get; set; }
Expand Down
8 changes: 0 additions & 8 deletions src/E13.Common.Domain/IEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,5 @@ namespace E13.Common.Domain
public interface IEntity
{
Guid Id { get; set; }

string CreatedBy { get; set; }
string CreatedSource { get; set; }
DateTime Created { get; set; }

string ModifiedBy { get; set; }
string ModifiedSource { get; set; }
DateTime Modified { get; set; }
}
}
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
public interface IExpirable : IEntity
{
string ExpirationBy { get; set; }
string ExpirationSource { get; set; }
Expand Down
15 changes: 15 additions & 0 deletions src/E13.Common.Domain/IModifiable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace E13.Common.Domain
{
public interface IModifiable : IEntity
{
string ModifiedBy { get; set; }
string ModifiedSource { get; set; }
DateTime? Modified { get; set; }
}
}
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
public interface IOwnable : IEntity
{
string OwnedBy { get; set; }
string OwnedSource { get; set; }
Expand Down
6 changes: 4 additions & 2 deletions test/E13.Common.Data.Db.Tests/BaseDbContextTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ public void Setup()
[Test]
public void InMemory_Baseline_OnePerTable()
{
Context.Entities.Count().Should().Be(1);
Context.Creatables.Count().Should().Be(1);
Context.Modifiables.Count().Should().Be(1);
Context.Deletable.Count().Should().Be(1);
Context.Effectable.Count().Should().Be(1);
Context.Ownable.Count().Should().Be(1);
Expand All @@ -40,7 +41,8 @@ public void InMemory_Baseline_OnePerTable()
[Test]
public void InMemory_Baseline_EmptyGuids()
{
Context.Entities.All(e => e.Id == Guid.Empty).Should().BeFalse();
Context.Creatables.All(e => e.Id == Guid.Empty).Should().BeFalse();
Context.Modifiables.All(e => e.Id == Guid.Empty).Should().BeFalse();
Context.Deletable.All(e => e.Id == Guid.Empty).Should().BeFalse();
Context.Effectable.All(e => e.Id == Guid.Empty).Should().BeFalse();
Context.Ownable.All(e => e.Id == Guid.Empty).Should().BeFalse();
Expand Down
64 changes: 64 additions & 0 deletions test/E13.Common.Data.Db.Tests/BaseDbContext_ICreatableTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using E13.Common.Data.Db.Tests.Sample;
using FluentAssertions;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using NUnit.Framework;
using System;
using System.Linq;

namespace E13.Common.Data.Db.Tests
{
public class BaseDbContext_ICreatableTests
{
private TestDbContext Context;

[SetUp]
public void Setup()
{
var services = new ServiceCollection();
services.AddDbContext<TestDbContext>(o => o.UseInMemoryDatabase($"{Guid.NewGuid()}"));

Context = services.BuildServiceProvider().GetService<TestDbContext>();
Context.AddTestData();
}
[Test]
public void InitialData_CreatedSource_AddTestData()
{
var arranged = Context.Creatables.First();

arranged.CreatedSource.Should().Be("E13.Common.Data.Db.Tests.Sample.TestDbContext.AddTestData");
}

[Test]
public void InitialData_CreatedBy_Unknown()
{
var arranged = Context.Creatables.First();

arranged.CreatedBy.Should().Be(BaseDbContext.UnknownUser);
}

[Test]
public void SaveChanges_UnknownUser_CreatedByUnknown()
{
var id = Guid.NewGuid();
Context.Creatables.Add(new TestCreatable { Id = id });
Context.SaveChanges();

var arranged = Context.Creatables.First(e => e.Id == id);

arranged.CreatedBy.Should().Be(BaseDbContext.UnknownUser);
}

[Test]
public void SaveChanges_NamedUser_CreatedByNamedUser()
{
var id = Guid.NewGuid();
Context.Creatables.Add(new TestCreatable { Id = id });
Context.SaveChanges(nameof(SaveChanges_NamedUser_CreatedByNamedUser));

var arranged = Context.Creatables.First(e => e.Id == id);

arranged.CreatedBy.Should().Be(nameof(SaveChanges_NamedUser_CreatedByNamedUser));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public void SaveChanges_Deleting_SetsDeleted()

Context.Deletable.Remove(arranged);
Context.SaveChanges();
var a = Context.Deletable.Count();

Context.Deletable.Count().Should().Be(1);
Context.Deletable.Count(e => e.Deleted == null).Should().Be(0);
Expand All @@ -41,14 +42,14 @@ public void SaveChanges_Deleting_SetsDeleted()
public void SaveChangesForUser_Deleting_SetsDeletedBy()
{
Context.Deletable.Count().Should().Be(1);
Context.Deletable.Count(e => e.DeletedBy == BaseDbContext.UnknownUser).Should().Be(1);
Context.Deletable.Count(e => e.DeletedBy == null).Should().Be(1);
var arranged = Context.Deletable.First();

Context.Deletable.Remove(arranged);
Context.SaveChanges(nameof(SaveChangesForUser_Deleting_SetsDeletedBy));

Context.Deletable.Count().Should().Be(1);
Context.Deletable.Count(e => e.DeletedBy == BaseDbContext.UnknownUser).Should().Be(0);
Context.Deletable.Count(e => e.DeletedBy == null).Should().Be(0);
Context.Deletable.Count(e => e.DeletedBy == nameof(SaveChangesForUser_Deleting_SetsDeletedBy)).Should().Be(1);
}

Expand Down
129 changes: 0 additions & 129 deletions test/E13.Common.Data.Db.Tests/BaseDbContext_IEntityTests.cs

This file was deleted.

Loading

0 comments on commit 26ed17f

Please sign in to comment.