-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataContextBase.cs
188 lines (161 loc) · 4.46 KB
/
DataContextBase.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
using System.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using Microsoft.EntityFrameworkCore.Storage;
using Nerosoft.Euonia.Domain;
namespace Nerosoft.Euonia.Repository.EfCore;
/// <inheritdoc cref="DbContext" />
public abstract class DataContextBase<TContext> : DbContext, IRepositoryContext
where TContext : DbContext, IRepositoryContext
{
/// <inheritdoc />
protected DataContextBase(DbContextOptions<TContext> options)
: base(options)
{
Id = Guid.NewGuid();
}
/// <summary>
/// Gets a value indicate whether the entry values are automatically set or not.
/// </summary>
protected abstract bool AutoSetEntryValues { get; }
/// <summary>
/// Gets the kind of the date time.
/// </summary>
protected virtual DateTimeKind DateTimeKind { get; } = DateTimeKind.Unspecified;
/// <inheritdoc />
public override int SaveChanges()
{
return SaveChanges(true);
}
/// <inheritdoc />
public override int SaveChanges(bool acceptAllChangesOnSuccess)
{
var entries = ChangeTracker.Entries();
SetEntryValues(entries);
var result = base.SaveChanges(acceptAllChangesOnSuccess);
return result;
}
#region Implementation of IRepositoryContext
/// <summary>
///
/// </summary>
public Guid Id { get; }
/// <summary>
///
/// </summary>
public virtual string Provider => Database.ProviderName;
/// <inheritdoc />
public IQueryable<TEntity> SetOf<TEntity>()
where TEntity : class
{
return Set<TEntity>();
}
/// <inheritdoc />
public IDbConnection GetConnection()
{
return Database.GetDbConnection();
}
/// <inheritdoc />
public IDbTransaction GetTransaction()
{
return Database.CurrentTransaction?.GetDbTransaction();
}
/// <inheritdoc />
public async Task RollbackAsync(CancellationToken cancellationToken = default)
{
await Database.RollbackTransactionAsync(cancellationToken);
}
#endregion
/// <inheritdoc cref="DbContext.SaveChangesAsync(CancellationToken)" />
public override async Task<int> SaveChangesAsync(CancellationToken cancellationToken = default)
{
return await SaveChangesAsync(true, cancellationToken);
}
/// <inheritdoc />
public override async Task<int> SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = default)
{
var entries = ChangeTracker.Entries();
SetEntryValues(entries);
var result = await base.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken);
return result;
}
/// <summary>
/// Sets the entry values.
/// </summary>
/// <param name="entries"></param>
protected virtual void SetEntryValues(IEnumerable<EntityEntry> entries)
{
if (!AutoSetEntryValues)
{
return;
}
foreach (var entry in entries)
{
var time = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Unspecified);
switch (entry.State)
{
case EntityState.Added:
if (entry.Entity is IHasCreateTime)
{
entry.CurrentValues[nameof(IHasCreateTime.CreateTime)] = time;
}
if (entry.Entity is IHasUpdateTime)
{
entry.CurrentValues[nameof(IHasUpdateTime.UpdateTime)] = time;
}
if (entry.Entity is ITombstone)
{
entry.CurrentValues[nameof(ITombstone.IsDeleted)] = false;
}
break;
case EntityState.Deleted:
if (entry.Entity is ITombstone)
{
entry.State = EntityState.Modified;
entry.CurrentValues[nameof(ITombstone.IsDeleted)] = true;
entry.CurrentValues[nameof(ITombstone.DeleteTime)] = time;
}
break;
case EntityState.Detached:
break;
case EntityState.Unchanged:
break;
case EntityState.Modified:
SetModifiedEntry(entry, time);
break;
default:
continue;
}
}
}
private static void SetModifiedEntry(EntityEntry entry, DateTime time)
{
if (entry.State != EntityState.Modified)
{
return;
}
// ReSharper disable once ConvertIfStatementToSwitchStatement
if (entry.Entity is ITombstone { IsDeleted: true })
{
entry.CurrentValues[nameof(ITombstone.IsDeleted)] = true;
entry.CurrentValues[nameof(ITombstone.DeleteTime)] = time;
return;
}
if (entry.Entity is IHasUpdateTime)
{
entry.CurrentValues[nameof(IHasUpdateTime.UpdateTime)] = time;
}
}
/// <inheritdoc />
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
foreach (var entityType in modelBuilder.Model.GetEntityTypes())
{
//other automated configurations left out
if (typeof(ITombstone).IsAssignableFrom(entityType.ClrType))
{
entityType.SetTombstoneQueryFilter();
}
}
}
}