-
Notifications
You must be signed in to change notification settings - Fork 68
/
ServiceBase.cs
477 lines (444 loc) · 30.6 KB
/
ServiceBase.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
using Peasy.Extensions;
namespace Peasy
{
/// <summary>
/// Serves as the base class for business services and represents an <see cref="ICommand"/> factory.
/// </summary>
/// <typeparam name="T">Represents a domain object or resource and can be any type that implements <see cref="IDomainObject{TKey}"/>.</typeparam>
/// <typeparam name="TKey">Represents an identifier for a domain object or resource and can be any type.</typeparam>
public abstract class ServiceBase<T, TKey> : IService<T, TKey> where T : IDomainObject<TKey>, new()
{
/// <inheritdoc cref="IDataProxy{T, TKey}"/>
protected readonly IDataProxy<T, TKey> _dataProxy;
/// <inheritdoc cref="IDataProxy{T, TKey}"/>
protected IDataProxy<T, TKey> DataProxy => _dataProxy;
/// <summary>
/// Initializes a new service instance with a required data proxy.
/// </summary>
/// <param name="dataProxy">The data proxy abstraction to be consumed by commands.</param>
public ServiceBase(IDataProxy<T, TKey> dataProxy)
{
_dataProxy = dataProxy;
}
/// <summary>
/// Performs initialization logic.
/// </summary>
/// <remarks>
/// <para>The first method invoked within the execution pipeline of the command returned by <see cref="GetByIDCommand"/>, triggered by <see cref="CommandBase.ExecuteAsync"/>.</para>
/// <para>Override this method to perform initialization logic before rule validations are invoked by the command returned by <see cref="GetByIDCommand"/>.</para>
/// </remarks>
/// <param name="id">The id of the resource to retrieve.</param>
/// <param name="context">Serves as shared state between all pipeline methods invoked by the command returned by <see cref="GetByIDCommand"/>.</param>
/// <returns>An awaitable <see cref="Task"/>.</returns>
protected virtual Task OnGetByIDCommandInitializationAsync(TKey id, ExecutionContext<T> context)
{
return Task.FromResult<object>(null);
}
/// <summary>
/// Performs initialization logic.
/// </summary>
/// <remarks>
/// <para>The first method invoked within the execution pipeline of the command returned by <see cref="GetAllCommand"/>, triggered by <see cref="CommandBase.ExecuteAsync"/>.</para>
/// <para>Override this method to perform initialization logic before rule validations are invoked by the command returned by <see cref="GetAllCommand"/>.</para>
/// </remarks>
/// <param name="context">Serves as shared state between all pipeline methods invoked by the command returned by <see cref="GetAllCommand"/>.</param>
/// <returns>An awaitable <see cref="Task"/>.</returns>
protected virtual Task OnGetAllCommandInitializationAsync(ExecutionContext<T> context)
{
return Task.FromResult<object>(null);
}
/// <summary>
/// Performs initialization logic.
/// </summary>
/// <remarks>
/// <para>The first method invoked within the execution pipeline of the command returned by <see cref="InsertCommand"/>, triggered by <see cref="CommandBase.ExecuteAsync"/>.</para>
/// <para>Override this method to perform initialization logic before rule validations are invoked by the command returned by <see cref="InsertCommand"/>.</para>
/// </remarks>
/// <param name="resource">The resource to insert.</param>
/// <param name="context">Serves as shared state between all pipeline methods invoked by the command returned by <see cref="InsertCommand"/>.</param>
/// <returns>An awaitable <see cref="Task"/>.</returns>
protected virtual Task OnInsertCommandInitializationAsync(T resource, ExecutionContext<T> context)
{
return Task.FromResult<object>(null);
}
/// <summary>
/// Performs initialization logic.
/// </summary>
/// <remarks>
/// <para>The first method invoked within the execution pipeline of the command returned by <see cref="UpdateCommand"/>, triggered by <see cref="CommandBase.ExecuteAsync"/>.</para>
/// <para>Override this method to perform initialization logic before rule validations are invoked by the command returned by <see cref="UpdateCommand"/>.</para>
/// </remarks>
/// <param name="resource">The resource to update.</param>
/// <param name="context">Serves as shared state between all pipeline methods invoked by the command returned by <see cref="UpdateCommand"/>.</param>
/// <returns>An awaitable <see cref="Task"/>.</returns>
protected virtual Task OnUpdateCommandInitializationAsync(T resource, ExecutionContext<T> context)
{
return Task.FromResult<object>(null);
}
/// <summary>
/// Performs initialization logic.
/// </summary>
/// <remarks>
/// <para>The first method invoked within the execution pipeline of the command returned by <see cref="DeleteCommand"/>, triggered by <see cref="CommandBase.ExecuteAsync"/>.</para>
/// <para>Override this method to perform initialization logic before rule validations are invoked by the command returned by <see cref="DeleteCommand"/>.</para>
/// </remarks>
/// <param name="id">The id of the resource to delete.</param>
/// <param name="context">Serves as shared state between all pipeline methods invoked by the command returned by <see cref="DeleteCommand"/>.</param>
/// <returns>An awaitable <see cref="Task"/>.</returns>
protected virtual Task OnDeleteCommandInitializationAsync(TKey id, ExecutionContext<T> context)
{
return Task.FromResult<object>(null);
}
/// <summary>
/// Returns a list of business rules to be executed within the execution pipeline of the command returned by <see cref="GetByIDCommand"/>.
/// </summary>
/// <remarks>
/// <para>This is the second method invoked within the execution pipeline of the command returned by <see cref="GetByIDCommand"/>, triggered by <see cref="CommandBase.ExecuteAsync"/>.</para>
/// <para>Override this method to supply business rules for invocation by the command returned by <see cref="GetByIDCommand"/>.</para>
/// <para>The successful invocation of the returned rules will determine whether or not to proceed with command pipeline execution.</para>
/// </remarks>
/// <param name="id">The id of the resource to retrieve.</param>
/// <param name="context">Serves as shared state between all pipeline methods invoked by the command returned by <see cref="GetByIDCommand"/>.</param>
/// <returns>An awaitable list of <see cref="IRule"/></returns>
protected virtual Task<IEnumerable<IRule>> OnGetByIDCommandGetRulesAsync(TKey id, ExecutionContext<T> context)
{
return Task.FromResult(Enumerable.Empty<IRule>());
}
/// <summary>
/// Returns a list of business rules to be executed within the execution pipeline of the command returned by <see cref="GetAllCommand"/>.
/// </summary>
/// <remarks>
/// <para>This is the second method invoked within the execution pipeline of the command returned by <see cref="GetAllCommand"/>, triggered by <see cref="CommandBase.ExecuteAsync"/>.</para>
/// <para>Override this method to supply business rules for invocation by the command returned by <see cref="GetAllCommand"/>.</para>
/// <para>The successful invocation of the returned rules will determine whether or not to proceed with command pipeline execution.</para>
/// </remarks>
/// <param name="context">Serves as shared state between all pipeline methods invoked by the command returned by <see cref="GetAllCommand"/>.</param>
/// <returns>An awaitable list of <see cref="IRule"/></returns>
protected virtual Task<IEnumerable<IRule>> OnGetAllCommandGetRulesAsync(ExecutionContext<T> context)
{
return Task.FromResult(Enumerable.Empty<IRule>());
}
/// <summary>
/// Returns a list of business rules to be executed within the execution pipeline of the command returned by <see cref="InsertCommand"/>.
/// </summary>
/// <remarks>
/// <para>This is the second method invoked within the execution pipeline of the command returned by <see cref="InsertCommand"/>, triggered by <see cref="CommandBase.ExecuteAsync"/>.</para>
/// <para>Override this method to supply business rules for invocation by the command returned by <see cref="InsertCommand"/>.</para>
/// <para>The successful invocation of the returned rules will determine whether or not to proceed with command pipeline execution.</para>
/// </remarks>
/// <param name="resource">The resource to insert.</param>
/// <param name="context">Serves as shared state between all pipeline methods invoked by the command returned by <see cref="InsertCommand"/>.</param>
/// <returns>An awaitable list of <see cref="IRule"/></returns>
protected virtual Task<IEnumerable<IRule>> OnInsertCommandGetRulesAsync(T resource, ExecutionContext<T> context)
{
return Task.FromResult(Enumerable.Empty<IRule>());
}
/// <summary>
/// Returns a list of business rules to be executed within the execution pipeline of the command returned by <see cref="UpdateCommand"/>.
/// </summary>
/// <remarks>
/// <para>This is the second method invoked within the execution pipeline of the command returned by <see cref="UpdateCommand"/>, triggered by <see cref="CommandBase.ExecuteAsync"/>.</para>
/// <para>Override this method to supply business rules for invocation by the command returned by <see cref="UpdateCommand"/>.</para>
/// <para>The successful invocation of the returned rules will determine whether or not to proceed with command pipeline execution.</para>
/// </remarks>
/// <param name="resource">The resource to update.</param>
/// <param name="context">Serves as shared state between all pipeline methods invoked by the command returned by <see cref="UpdateCommand"/>.</param>
/// <returns>An awaitable list of <see cref="IRule"/></returns>
protected virtual Task<IEnumerable<IRule>> OnUpdateCommandGetRulesAsync(T resource, ExecutionContext<T> context)
{
return Task.FromResult(Enumerable.Empty<IRule>());
}
/// <summary>
/// Returns a list of business rules to be executed within the execution pipeline of the command returned by <see cref="DeleteCommand"/>.
/// </summary>
/// <remarks>
/// <para>This is the second method invoked within the execution pipeline of the command returned by <see cref="DeleteCommand"/>, triggered by <see cref="CommandBase.ExecuteAsync"/>.</para>
/// <para>Override this method to supply business rules for invocation by the command returned by <see cref="DeleteCommand"/>.</para>
/// <para>The successful invocation of the returned rules will determine whether or not to proceed with command pipeline execution.</para>
/// </remarks>
/// <param name="id">The id of the resource to delete.</param>
/// <param name="context">Serves as shared state between all pipeline methods invoked by the command returned by <see cref="DeleteCommand"/>.</param>
/// <returns>An awaitable list of <see cref="IRule"/></returns>
protected virtual Task<IEnumerable<IRule>> OnDeleteCommandGetRulesAsync(TKey id, ExecutionContext<T> context)
{
return Task.FromResult(Enumerable.Empty<IRule>());
}
/// <summary>
/// Combines the results of <see cref="OnGetByIDCommandValidateID"/> and <see cref="OnGetByIDCommandGetRulesAsync"/> to generate potential list of <see cref="ValidationResult"/>.
/// </summary>
/// <remarks>Override this method to manipulate the creation of a list of <see cref="ValidationResult"/>.
/// <para>Example: You might want to verify that no validation errors exist before invoking the potentially expensive business rules.</para>
/// </remarks>
/// <param name="id">The id of the resource to retrieve.</param>
/// <param name="context">Serves as shared state between all pipeline methods invoked by the command returned by <see cref="GetByIDCommand"/>.</param>
/// <returns>An awaitable list of <see cref="ValidationResult"/></returns>
protected virtual async Task<IEnumerable<ValidationResult>> OnGetByIDCommandPerformValidationAsync(TKey id, ExecutionContext<T> context)
{
var validationErrors = OnGetByIDCommandValidateID(id, context);
var rules = await OnGetByIDCommandGetRulesAsync(id, context);
return validationErrors.Concat(await rules.ValidateAllAsync());
}
/// <summary>
/// Invokes <see cref="OnGetAllCommandGetRulesAsync"/> to generate potential list of <see cref="ValidationResult"/>.
/// </summary>
/// <remarks>Override this method to manipulate the creation of a list of <see cref="ValidationResult"/>.
/// <para>Example: You might want to verify that no validation errors exist before invoking the potentially expensive business rules.</para>
/// </remarks>
/// <param name="context">Serves as shared state between all pipeline methods invoked by the command returned by <see cref="GetAllCommand"/>.</param>
/// <returns>An awaitable list of <see cref="ValidationResult"/></returns>
protected virtual async Task<IEnumerable<ValidationResult>> OnGetAllCommandPerformValidationAsync(ExecutionContext<T> context)
{
var rules = await OnGetAllCommandGetRulesAsync(context);
return await rules.ValidateAllAsync();
}
/// <summary>
/// Combines the results of <see cref="OnInsertCommandValidateObject"/> and <see cref="OnInsertCommandGetRulesAsync"/> to generate potential list of <see cref="ValidationResult"/>.
/// </summary>
/// <remarks>Override this method to manipulate the creation of a list of <see cref="ValidationResult"/>.
/// <para>Example: You might want to verify that no validation errors exist before invoking the potentially expensive business rules.</para>
/// </remarks>
/// <param name="resource">The resource to insert.</param>
/// <param name="context">Serves as shared state between all pipeline methods invoked by the command returned by <see cref="InsertCommand"/>.</param>
/// <returns>An awaitable list of <see cref="ValidationResult"/></returns>
protected virtual async Task<IEnumerable<ValidationResult>> OnInsertCommandPerformValidationAsync(T resource, ExecutionContext<T> context)
{
var validationErrors = OnInsertCommandValidateObject(resource, context);
var rules = await OnInsertCommandGetRulesAsync(resource, context);
return validationErrors.Concat(await rules.ValidateAllAsync());
}
/// <summary>
/// Combines the results of <see cref="OnUpdateCommandValidateObject"/> and <see cref="OnUpdateCommandGetRulesAsync"/> to generate potential list of <see cref="ValidationResult"/>.
/// </summary>
/// <remarks>Override this method to manipulate the creation of a list of <see cref="ValidationResult"/>.
/// <para>Example: You might want to verify that no validation errors exist before invoking the potentially expensive business rules.</para>
/// </remarks>
/// <param name="resource">The resource to update.</param>
/// <param name="context">Serves as shared state between all pipeline methods invoked by the command returned by <see cref="UpdateCommand"/>.</param>
/// <returns>An awaitable list of <see cref="ValidationResult"/></returns>
protected virtual async Task<IEnumerable<ValidationResult>> OnUpdateCommandPerformValidationAsync(T resource, ExecutionContext<T> context)
{
var validationErrors = OnUpdateCommandValidateObject(resource, context);
var rules = await OnUpdateCommandGetRulesAsync(resource, context);
return validationErrors.Concat(await rules.ValidateAllAsync());
}
/// <summary>
/// Combines the results of <see cref="OnDeleteCommandValidateId"/> and <see cref="OnDeleteCommandGetRulesAsync"/> to generate potential list of <see cref="ValidationResult"/>.
/// </summary>
/// <remarks>Override this method to manipulate the creation of a list o <see cref="ValidationResult"/>.
/// <para>Example: You might want to verify that no validation errors exist before invoking the potentially expensive business rules.</para>
/// </remarks>
/// <param name="id">The id of the resource to delete.</param>
/// <param name="context">Serves as shared state between all pipeline methods invoked by the command returned by <see cref="DeleteCommand"/>.</param>
/// <returns>An awaitable list of <see cref="ValidationResult"/></returns>
protected virtual async Task<IEnumerable<ValidationResult>> OnDeleteCommandPerformValidationAsync(TKey id, ExecutionContext<T> context)
{
var validationErrors = OnDeleteCommandValidateId(id, context);
var rules = await OnDeleteCommandGetRulesAsync(id, context);
return validationErrors.Concat(await rules.ValidateAllAsync());
}
/// <summary>
/// Performs additional business logic and data proxy interaction.
/// </summary>
/// <remarks>
/// <para>This is the third and final method invoked within the execution pipeline of the command returned by <see cref="GetByIDCommand"/>, triggered by <see cref="CommandBase.ExecuteAsync"/>.</para>
/// <para>Override this method to invoke custom business logic and data proxy interaction.</para>
/// <para>This method is only invoked based on the successful validation of all configured validation and business rules.</para>
/// </remarks>
/// <param name="id">The id of the resource to retrieve.</param>
/// <param name="context">Serves as shared state between all pipeline methods invoked by the command returned by <see cref="GetByIDCommand"/>.</param>
/// <returns>An awaitable resource returned from <see cref="Peasy.ISupportGetByID{T, TKey}.GetByIDAsync"/> of <see cref="DataProxy"/>.</returns>
protected virtual async Task<T> OnGetByIDCommandValidationSuccessAsync(TKey id, ExecutionContext<T> context)
{
return await _dataProxy.GetByIDAsync(id);
}
/// <summary>
/// Performs additional business logic and data proxy interaction.
/// </summary>
/// <remarks>
/// <para>This is the third and final method invoked within the execution pipeline of the command returned by <see cref="GetAllCommand"/>, triggered by <see cref="CommandBase.ExecuteAsync"/>.</para>
/// <para>Override this method to invoke custom business logic and data proxy interaction.</para>
/// <para>This method is only invoked based on the successful validation of all configured validation and business rules.</para>
/// </remarks>
/// <param name="context">Serves as shared state between all pipeline methods invoked by the command returned by <see cref="GetAllCommand"/>.</param>
/// <returns>An awaitable resource list returned from <see cref="Peasy.ISupportGetAll{T}.GetAllAsync"/> of <see cref="DataProxy"/>.</returns>
protected virtual async Task<IEnumerable<T>> OnGetAllCommandValidationSuccessAsync(ExecutionContext<T> context)
{
return await _dataProxy.GetAllAsync();
}
/// <summary>
/// Performs additional business logic and data proxy interaction.
/// </summary>
/// <remarks>
/// <para>This is the third and final method invoked within the execution pipeline of the command returned by <see cref="InsertCommand"/>, triggered by <see cref="CommandBase.ExecuteAsync"/>.</para>
/// <para>Override this method to invoke custom business logic and data proxy interaction.</para>
/// <para>This method is only invoked based on the successful validation of all configured validation and business rules.</para>
/// </remarks>
/// <param name="resource">The resource to insert.</param>
/// <param name="context">Serves as shared state between all pipeline methods invoked by the command returned by <see cref="InsertCommand"/>.</param>
/// <returns>An awaitable updated representation of the resource resulting from a call to <see cref="Peasy.ISupportInsert{T}.InsertAsync"/> of <see cref="DataProxy"/>.</returns>
protected virtual async Task<T> OnInsertCommandValidationSuccessAsync(T resource, ExecutionContext<T> context)
{
return await _dataProxy.InsertAsync(resource);
}
/// <summary>
/// Performs additional business logic and data proxy interaction.
/// </summary>
/// <remarks>
/// <para>This is the third and final method invoked within the execution pipeline of the command returned by <see cref="UpdateCommand"/>, triggered by <see cref="CommandBase.ExecuteAsync"/>.</para>
/// <para>Override this method to invoke custom business logic and data proxy interaction.</para>
/// <para>This method is only invoked based on the successful validation of all configured validation and business rules.</para>
/// </remarks>
/// <param name="resource">The resource to update.</param>
/// <param name="context">Serves as shared state between all pipeline methods invoked by the command returned by <see cref="UpdateCommand"/>.</param>
/// <returns>An awaitable updated representation of the resource resulting from a call to <see cref="Peasy.ISupportUpdate{T}.UpdateAsync"/> of <see cref="DataProxy"/>.</returns>
protected virtual async Task<T> OnUpdateCommandValidationSuccessAsync(T resource, ExecutionContext<T> context)
{
return await _dataProxy.UpdateAsync(resource);
}
/// <summary>
/// Performs additional business logic and data proxy interaction.
/// </summary>
/// <remarks>
/// <para>This is the third and final method invoked within the execution pipeline of the command returned by <see cref="DeleteCommand"/>, triggered by <see cref="CommandBase.ExecuteAsync"/>.</para>
/// <para>Override this method to invoke custom business logic and data proxy interaction.</para>
/// <para>This method is only invoked based on the successful validation of all configured validation and business rules.</para>
/// </remarks>
/// <param name="id">The id of the resource to delete.</param>
/// <param name="context">Serves as shared state between all pipeline methods invoked by the command returned by <see cref="DeleteCommand"/>.</param>
/// <returns>An awaitable <see cref="Task"/> resulting from a call to <see cref="Peasy.ISupportDelete{T}.DeleteAsync"/> of <see cref="DataProxy"/>.</returns>
protected virtual Task OnDeleteCommandValidationSuccessAsync(TKey id, ExecutionContext<T> context)
{
return _dataProxy.DeleteAsync(id);
}
/// <summary>
/// Generates a potential list of <see cref="ValidationResult"/> based on the supplied resource.
/// </summary>
/// <remarks>
/// <para>Invoked by <see cref="OnGetByIDCommandPerformValidationAsync"/>.</para>
/// <para>Override this method to manipulate the creation of a list of <see cref="ValidationResult"/>.</para>
/// </remarks>
/// <param name="id">The id of the resource to retrieve.</param>
/// <param name="context">Serves as shared state between all pipeline methods invoked by the command returned by <see cref="GetByIDCommand"/>.</param>
/// <returns>A list of <see cref="ValidationResult"/></returns>
protected virtual IEnumerable<ValidationResult> OnGetByIDCommandValidateID(TKey id, ExecutionContext<T> context)
{
yield break;
}
/// <summary>
/// Performs validation against the supplied resource by validating property values applied with attributes of type <see cref="ValidationAttribute"/>.
/// </summary>
/// <remarks>
/// <para>Invoked by <see cref="OnInsertCommandPerformValidationAsync"/>.</para>
/// <para>Override this method to suppress or further manipulate resource validation based on attributes of type <see cref="ValidationAttribute"/>.</para>
/// </remarks>
/// <param name="resource">The resource to insert.</param>
/// <param name="context">Serves as shared state between all pipeline methods invoked by the command returned by <see cref="InsertCommand"/>.</param>
/// <returns>A list of <see cref="ValidationResult"/></returns>
protected virtual IEnumerable<ValidationResult> OnInsertCommandValidateObject(T resource, ExecutionContext<T> context)
{
return resource.Validate();
}
/// <summary>
/// Performs validation against the supplied resource by validating property values applied with attributes of type <see cref="ValidationAttribute"/>.
/// </summary>
/// <remarks>
/// <para>Invoked by <see cref="OnUpdateCommandPerformValidationAsync"/>.</para>
/// <para>Override this method to suppress or further manipulate resource validation based on attributes of type <see cref="ValidationAttribute"/>.</para>
/// </remarks>
/// <param name="resource">The resource to update.</param>
/// <param name="context">Serves as shared state between all pipeline methods invoked by the command returned by <see cref="UpdateCommand"/>.</param>
/// <returns>A list of <see cref="ValidationResult"/></returns>
protected virtual IEnumerable<ValidationResult> OnUpdateCommandValidateObject(T resource, ExecutionContext<T> context)
{
return resource.Validate();
}
/// <summary>
/// Performs validation against the supplied id.
/// </summary>
/// <remarks>
/// <para>Invoked by <see cref="OnDeleteCommandPerformValidationAsync"/>.</para>
/// <para>Override this method to manipulate the creation of a list of <see cref="ValidationResult"/>.</para>
/// </remarks>
/// <param name="id">The id of the resource to delete.</param>
/// <param name="context">Serves as shared state between all pipeline methods invoked by the command returned by <see cref="DeleteCommand"/>.</param>
/// <returns>A list of <see cref="ValidationResult"/></returns>
protected virtual IEnumerable<ValidationResult> OnDeleteCommandValidateId(TKey id, ExecutionContext<T> context)
{
yield break;
}
/// <summary>
/// Helper function that wraps supplied rules in an awaitable list.
/// </summary>
/// <remarks>Use this method to improve readability of rule configuration.</remarks>
/// <param name="rules">A list of rules to wrap in an awaitable list.</param>
/// <returns>An awaitable list of rules.</returns>
protected virtual Task<IEnumerable<IRule>> TheseRules(params IRule[] rules)
{
return Task.FromResult<IEnumerable<IRule>>(rules);
}
/// <inheritdoc cref="ISupportGetByIDCommand{T, TKey}.GetByIDCommand"/>
public virtual ICommand<T> GetByIDCommand(TKey id)
{
var context = new ExecutionContext<T>();
return new ServiceCommand<T>
(
initializationMethod: () => OnGetByIDCommandInitializationAsync(id, context),
validationMethod: () => OnGetByIDCommandPerformValidationAsync(id, context),
validationMethodUsesTheseRules: () => OnGetByIDCommandGetRulesAsync(id, context),
executeMethod: () => OnGetByIDCommandValidationSuccessAsync(id, context)
);
}
/// <inheritdoc cref="ISupportGetAllCommand{T}.GetAllCommand"/>
public virtual ICommand<IEnumerable<T>> GetAllCommand()
{
var context = new ExecutionContext<T>();
return new ServiceCommand<IEnumerable<T>>
(
initializationMethod: () => OnGetAllCommandInitializationAsync(context),
validationMethod: () => OnGetAllCommandPerformValidationAsync(context),
validationMethodUsesTheseRules: () => OnGetAllCommandGetRulesAsync(context),
executeMethod: () => OnGetAllCommandValidationSuccessAsync(context)
);
}
/// <inheritdoc cref="ISupportInsertCommand{T}.InsertCommand"/>
public virtual ICommand<T> InsertCommand(T resource)
{
var context = new ExecutionContext<T>();
return new ServiceCommand<T>
(
initializationMethod: () => OnInsertCommandInitializationAsync(resource, context),
validationMethod: () => OnInsertCommandPerformValidationAsync(resource, context),
validationMethodUsesTheseRules: () => OnInsertCommandGetRulesAsync(resource, context),
executeMethod: () => OnInsertCommandValidationSuccessAsync(resource, context)
);
}
/// <inheritdoc cref="ISupportUpdateCommand{T}.UpdateCommand"/>
public virtual ICommand<T> UpdateCommand(T resource)
{
var context = new ExecutionContext<T>();
return new ServiceCommand<T>
(
initializationMethod: () => OnUpdateCommandInitializationAsync(resource, context),
validationMethod: () => OnUpdateCommandPerformValidationAsync(resource, context),
validationMethodUsesTheseRules: () => OnUpdateCommandGetRulesAsync(resource, context),
executeMethod: () => OnUpdateCommandValidationSuccessAsync(resource, context)
);
}
/// <inheritdoc cref="ISupportDeleteCommand{TKey}.DeleteCommand"/>
public virtual ICommand DeleteCommand(TKey id)
{
var context = new ExecutionContext<T>();
return new ServiceCommand
(
initializationMethod: () => OnDeleteCommandInitializationAsync(id, context),
validationMethod: () => OnDeleteCommandPerformValidationAsync(id, context),
validationMethodUsesTheseRules: () => OnDeleteCommandGetRulesAsync(id, context),
executeMethod: () => OnDeleteCommandValidationSuccessAsync(id, context)
);
}
}
}