/
ExposedType-TDomain.cs
242 lines (201 loc) · 8.4 KB
/
ExposedType-TDomain.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
namespace Olive.Entities.Replication
{
public abstract partial class HardDeletableExposedType<TDomain> : ExposedType<TDomain> where TDomain : class, IEntity
{
public override bool IsSoftDeleteEnabled => false;
}
public abstract partial class ExposedType<TDomain> : ExposedType where TDomain : class, IEntity
{
Type domainType;
ILogger Logger;
public override Type DomainType => domainType ?? (domainType = GetType().BaseType.GenericTypeArguments.Single());
protected virtual string QueueUrlConfigKey => string.Empty;
IEventBusQueue Queue
{
get
{
if (QueueUrlConfigKey.HasValue())
return EventBus.Queue(Config.GetOrThrow(QueueUrlConfigKey));
else
return EventBus.Queue(QueueUrl);
}
}
internal override void Start()
{
Logger = Log.For(this);
DependenciesEnitityTypes = Dependencies?.Select(p => p.EntityType)
.Union(typeof(TDomain))
.Distinct().ToArray();
GlobalEntityEvents.InstanceSaved.Handle(x => OnInstanceSaved(x.Entity));
GlobalEntityEvents.InstanceDeleted.Handle(x => OnInstanceDeleted(x));
}
async Task OnInstanceSaved(IEntity item)
{
Logger.Debug("Instance saved. Initiating publish checks.");
if (!DependenciesEnitityTypes.Contains(item.GetType()))
{
Logger.Debug("Publish aborted: " + item.GetType().Name + " is not of type " + DomainType.Name);
return;
}
if (item is TDomain entity)
{
await Publish(entity);
}
else if (item != null)
{
await
(await FindRelationsOf(item))
.ExceptNull()
.Do(async p => await Publish(p));
}
}
async Task OnInstanceDeleted(GlobalDeleteEventArgs eventArg)
{
Logger.Debug("Instance saved. Initiating publish checks.");
if (!DependenciesEnitityTypes.Contains(eventArg.EntityType))
{
Logger.Debug("Publish aborted: " + eventArg.EntityType.Name + " is not of type " + DomainType.Name);
return;
}
if (eventArg.EntityType == typeof(TDomain))
{
await Publish(eventArg.Entity as TDomain, toDelete: true);
}
else
{
await
(await FindRelationsOf(eventArg.Entity))
.ExceptNull()
.Do(async p => await Publish(p));
}
}
async Task<IEnumerable<TDomain>> FindRelationsOf(IEntity item)
{
var selectedTriggers = Dependencies.Where(t => t.EntityType == item.GetType());
return await selectedTriggers.SelectManyAsync(async t =>
{
try
{
return (await t.Function.Invoke(item)) ?? new List<TDomain>();
}
catch
{
return new List<TDomain>();
}
})?.Distinct()?.ToList();
}
async Task Publish(TDomain entity, bool toDelete = false)
{
if (!Filter(entity) || !(await FilterAsync(entity)))
{
Logger.Debug("Skipped publishing the " + entity.GetType().Name + " record of " + entity.GetId() + ". It does not match the filter.");
return;
}
Logger.Debug("Publishing the " + entity.GetType().Name + " record of " + entity.GetId());
if (!toDelete)
await Queue.Publish(await ToMessage(entity));
else
await Queue.Publish(ToDeleteMessage(entity));
}
public ExposedPropertyInfo Expose<T>(Expression<Func<TDomain, T>> field)
{
var result = new ExposedPropertyInfo(field.GetProperty());
Fields.Add(result);
return result;
}
public CustomExposedField Expose<TProperty>(string title, Func<TDomain, TProperty> valueProvider)
{
return Expose(new CustomExposedField(title, typeof(TProperty), x => Task.FromResult<object>(valueProvider((TDomain)x))));
}
public CustomExposedField Expose<TProperty>(string title, Func<TDomain, Task<TProperty>> valueProvider)
{
return Expose(new CustomExposedField(title, typeof(TProperty),
async x =>
{
var value = valueProvider((TDomain)x);
if (value == null) return null;
return await value;
}));
}
public CustomExposedField Expose(CustomExposedField field)
{
Fields.Add(field);
return field;
}
public void ExposeEverything()
{
var properties = DomainType.GetProperties()
.Where(x => x.CanRead && x.CanWrite)
.Where(x => x.DeclaringType.Assembly == DomainType.Assembly)
.ToArray();
foreach (var p in properties)
{
if (p.PropertyType == typeof(Guid?) && p.Name.EndsWith("Id") && properties.Any(x => x.Name ==
p.Name.TrimEnd(2))) continue;
Fields.Add(new ExposedPropertyInfo(p));
}
}
internal override async Task UploadAll()
{
var database = Context.Current.Database();
var totalCount = await database.Of(typeof(TDomain)).Count();
var log = Log.For(this);
log.Warning($"Uploading {totalCount} records of {typeof(TDomain).FullName} to the queue...");
var pageSize = 10000;
var allPages = Enumerable.Range(0, (int)Math.Ceiling(totalCount / (decimal)pageSize));
//using (var scope = database.CreateTransactionScope())
//{
await allPages.ForEachAsync(10, async (pageIndex) =>
{
var query = database.Of(typeof(TDomain));
query.OrderBy("ID");
query.PageSize = pageSize;
query.PageStartIndex = pageIndex * pageSize;
await UploadPage(await query.GetList());
});
// scope.Complete();
//}
log.Warning($"Finished uploading {totalCount} records of {typeof(TDomain).FullName} to the queue.");
}
async Task UploadPage(IEnumerable<IEntity> toUpload)
{
var list = await toUpload.SelectAsync(GetMessage)
.ExceptNull();
await list.Chop(10).DoAsync(async (messages, _) =>
{
try { await Queue.PublishBatch(messages); }
catch (Exception ex)
{
Log.For(this).Error(ex,
$"Failed to publish the event bus messages for a batch of {typeof(TDomain).FullName}");
}
});
}
async Task<IEventBusMessage> GetMessage(IEntity item)
{
IEventBusMessage result = null;
try { result = await ToMessage(item); }
catch (Exception ex)
{
Log.For(this).Error(ex, $"Failed to create an event bus message for {item.GetType().FullName} with ID of {item.GetId()}");
}
return result;
}
/// <summary>
/// If it returns false for any given record, it will not be published in the replication queue.
/// Warning: This will not unpublish or modify a record that was previously published, and which no longers meet the filter criteria.
/// </summary>
protected virtual Task<bool> FilterAsync(TDomain record) => Task.FromResult(true);
/// <summary>
/// If it returns false for any given record, it will not be published in the replication queue.
/// Warning: This will not unpublish or modify a record that was previously published, and which no longers meet the filter criteria.
/// </summary>
protected virtual bool Filter(TDomain record) => true;
}
}