forked from aiska/BpmNet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcessTaskStore.cs
More file actions
128 lines (116 loc) · 3.99 KB
/
Copy pathProcessTaskStore.cs
File metadata and controls
128 lines (116 loc) · 3.99 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
using BPMNET.Core;
using Microsoft.EntityFrameworkCore;
using System;
using System.Linq;
namespace BPMNET.EntityCore
{
public class ProcessTaskStore : IDisposable
{
public DbSet<ProcessTask> Entities { get; private set; }
protected bool DisposeContext { private get; set; }
protected DbContext Context { get; private set; }
public ProcessTaskStore(DbContext context)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
Context = context;
Entities = Context.Set<ProcessTask>();
}
public ProcessTaskStore()
{
Context = new BpmDbContext();
DisposeContext = true;
Entities = Context.Set<ProcessTask>();
}
public ProcessTask FindById(Guid ProcessTaskId)
{
this.ThrowIfDisposed(_disposed);
ProcessTaskId.ThrowIfNull();
ProcessTask result;
//get from cache
result = Cache.Get(ProcessTaskId.ToString()) as ProcessTask;
if (result == null)
{
//get from db
return Entities.AsNoTracking().FirstOrDefault(t => t.ProcessTaskId.Equals(ProcessTaskId));
}
return result;
}
public void Create(ProcessTask item)
{
this.ThrowIfDisposed(_disposed);
item.ThrowIfNull();
Entities.Add(item);
SaveChanges();
Cache.Add(item.ProcessTaskId.ToString(), item);
}
public void Update(ProcessTask item)
{
this.ThrowIfDisposed(_disposed);
item.ThrowIfNull();
Entities.Update(item);
SaveChanges();
Cache.Set(item.ProcessTaskId.ToString(), item);
}
public void Delete(ProcessTask item)
{
this.ThrowIfDisposed(_disposed);
item.ThrowIfNull();
Entities.Remove(item);
SaveChanges();
Cache.Remove(item.ProcessTaskId.ToString());
}
private 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 ProcessTask)
{
// 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.ProcessTaskId == ((ProcessTask)entry.Entity).ProcessTaskId);
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();
}
}
#region Dispose ...
private bool _disposed;
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (DisposeContext && disposing && Context != null)
{
Context.Dispose();
}
_disposed = true;
Context = null;
Entities = null;
}
#endregion
}
}