forked from aiska/BpmNet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseElementStore.cs
More file actions
170 lines (156 loc) · 5.87 KB
/
Copy pathBaseElementStore.cs
File metadata and controls
170 lines (156 loc) · 5.87 KB
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
using BPMNET.Core;
using Microsoft.EntityFrameworkCore;
using System;
using System.Linq;
using System.Threading.Tasks;
namespace BPMNET.EntityCore
{
public abstract class BaseElementStore<TEntity> : BaseStore<TEntity>
where TEntity : class, IBaseElement, new()
{
#region Constructor ...
public BaseElementStore(DbContext context) : base(context) { }
public BaseElementStore() : base() { }
#endregion
public override TEntity FindById(Guid Key)
{
this.ThrowIfDisposed(_disposed);
Key.ThrowIfNull();
TEntity result;
//get from cache
result = Cache.Get(Key.ToString()) as TEntity;
if (result == null)
{
//get from db
return Entities.AsNoTracking().FirstOrDefault(t => t.Key.Equals(Key));
}
return result;
}
public override async Task<TEntity> FindByIdAsync(Guid Key)
{
this.ThrowIfDisposed(_disposed);
Key.ThrowIfNull();
TEntity result;
//get from cache
result = Cache.Get(Key.ToString()) as TEntity;
if (result == null)
{
//get from db
return await Entities.AsNoTracking().FirstOrDefaultAsync(t => t.Key.Equals(Key)).ConfigureAwait(false);
}
return result;
}
public override void Create(TEntity item)
{
this.ThrowIfDisposed(_disposed);
item.ThrowIfNull();
Entities.Add(item);
SaveChanges();
Cache.Add(item.Key.ToString(), item);
}
public override async Task CreateAsync(TEntity item)
{
this.ThrowIfDisposed(_disposed);
item.ThrowIfNull();
Entities.Add(item);
await SaveChangesAsync();
Cache.Add(item.Key.ToString(), item);
}
public override void Update(TEntity item)
{
this.ThrowIfDisposed(_disposed);
item.ThrowIfNull();
Entities.Update(item);
SaveChanges();
Cache.Set(item.Key.ToString(), item);
}
public override async Task UpdateAsync(TEntity item)
{
this.ThrowIfDisposed(_disposed);
item.ThrowIfNull();
Entities.Update(item);
await SaveChangesAsync();
Cache.Set(item.Key.ToString(), item);
}
public override void Delete(TEntity item)
{
this.ThrowIfDisposed(_disposed);
item.ThrowIfNull();
Entities.Remove(item);
SaveChanges();
Cache.Remove(item.Key.ToString());
}
public override async Task DeleteAsync(TEntity item)
{
this.ThrowIfDisposed(_disposed);
item.ThrowIfNull();
Entities.Remove(item);
await SaveChangesAsync();
Cache.Remove(item.Key.ToString());
}
protected void SaveChanges()
{
try
{
// Attempt to save changes to the database
Context.SaveChanges();
}
catch (DbUpdateConcurrencyException ex)
{
foreach (var entry in ex.Entries)
{
if (entry.Entity is TEntity)
{
// Using a NoTracking query means we get the entity but it is not tracked by the context
// and will not be merged with existing entities in the context.
var databaseEntity = Entities.AsNoTracking().Single(p => p.Key == ((TEntity)entry.Entity).Key);
var databaseEntry = Context.Entry(databaseEntity);
foreach (var property in entry.Metadata.GetProperties())
{
// Update original values to
entry.Property(property.Name).OriginalValue = databaseEntry.Property(property.Name).CurrentValue;
}
}
else
{
throw new NotSupportedException("concurrency conflicts for " + entry.Metadata.Name);
}
}
// Retry the save operation
Context.SaveChanges();
}
}
protected async Task SaveChangesAsync()
{
try
{
// Attempt to save changes to the database
await Context.SaveChangesAsync().ConfigureAwait(false);
}
catch (DbUpdateConcurrencyException ex)
{
foreach (var entry in ex.Entries)
{
if (entry.Entity is TEntity)
{
// Using a NoTracking query means we get the entity but it is not tracked by the context
// and will not be merged with existing entities in the context.
var databaseEntity = Entities.AsNoTracking().Single(p => p.Key == ((TEntity)entry.Entity).Key);
var databaseEntry = Context.Entry(databaseEntity);
foreach (var property in entry.Metadata.GetProperties())
{
// Update original values to
entry.Property(property.Name).OriginalValue = databaseEntry.Property(property.Name).CurrentValue;
}
}
else
{
throw new NotSupportedException("concurrency conflicts for " + entry.Metadata.Name);
}
}
// Retry the save operation
await Context.SaveChangesAsync().ConfigureAwait(false);
}
}
}
}