/
DomainObjectDemoDelayTask.cs
142 lines (118 loc) · 5.03 KB
/
DomainObjectDemoDelayTask.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
using System;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using ShtrihM.DemoServer.Processing.Common;
using ShtrihM.DemoServer.Processing.Generated.Interface;
using ShtrihM.DemoServer.Processing.Model.DomainObjects.Common;
using ShtrihM.DemoServer.Processing.Model.DomainObjects.DemoDelayTask.Scenarios;
using ShtrihM.DemoServer.Processing.Model.Interfaces;
using ShtrihM.Wattle3.Common.Exceptions;
using ShtrihM.Wattle3.DomainObjects.DomainObjects;
using ShtrihM.Wattle3.DomainObjects.Interfaces;
using ShtrihM.Wattle3.Json.Extensions;
using ShtrihM.Wattle3.Mappers.Primitives.MutableFields;
namespace ShtrihM.DemoServer.Processing.Model.DomainObjects.DemoDelayTask;
[DomainObjectDataMapper(WellknownMappersAsText.DemoDelayTask, DomainObjectDataTarget.Create, DomainObjectDataTarget.Update)]
public sealed class DomainObjectDemoDelayTask : BaseDomainObjectMutable<DomainObjectDemoDelayTask>, IDomainObjectDemoDelayTask
{
#region Template - шаблон создания объекта DemoDelayTask
/// <summary>
/// Шаблон создания объекта <see cref="DomainObjectDemoDelayTask"/>.
/// </summary>
public class Template : IDomainObjectTemplate
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
// ReSharper disable once ConvertToPrimaryConstructor
public Template(
DemoDelayTaskScenario scenario,
DateTimeOffset? startDate)
{
Scenario = scenario;
StartDate = startDate;
}
public readonly DemoDelayTaskScenario Scenario;
public readonly DateTimeOffset? StartDate;
}
#endregion
#region Изменяемы поля
[DomainObjectFieldValue(DomainObjectDataTarget.Create, DomainObjectDataTarget.Update, DtoFiledName = nameof(DemoDelayTaskDtoChanged.Available))]
private MutableField<bool> m_available;
#endregion
#region Конструкторы
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public DomainObjectDemoDelayTask(
DemoDelayTaskDtoActual data,
ICustomEntryPoint entryPoint)
: base(entryPoint, data)
{
m_available = new MutableField<bool>(data.Available);
CreateDate = data.CreateDate;
ModificationDate = data.ModificationDate;
Scenario = data.Scenario;
StartDate = data.StartDate;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public DomainObjectDemoDelayTask(
long identity,
Template template,
ICustomEntryPoint entryPoint)
: base(entryPoint, identity)
{
m_available = new MutableField<bool>(true);
CreateDate = entryPoint.TimeService.Now;
ModificationDate = CreateDate;
Scenario = template.Scenario.ToJsonText();
StartDate = template.StartDate;
}
#endregion
public override Guid TypeId => WellknownDomainObjects.DemoDelayTask;
[DomainObjectFieldValue(DomainObjectDataTarget.Create, DomainObjectDataTarget.Update)]
public DateTimeOffset CreateDate
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get;
}
[DomainObjectFieldValue(DomainObjectDataTarget.Create, DomainObjectDataTarget.Update)]
public DateTimeOffset ModificationDate
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private set;
}
[DomainObjectFieldValue(DomainObjectDataTarget.Create, DomainObjectDataTarget.Update)]
public string Scenario
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get;
}
[DomainObjectFieldValue(DomainObjectDataTarget.Create, DomainObjectDataTarget.Update)]
public DateTimeOffset? StartDate
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get;
}
public async ValueTask<(bool IsCompleted, CancellationToken? CommitCancellationToken)> ProcessAsync(bool isRemoved, CancellationToken cancellationToken)
{
var scenario = Scenario.FromJson<DemoDelayTaskScenario>();
if (scenario is DemoDelayTaskScenarioAsDelay scenarioAsDelay)
{
Console.WriteLine($"[{m_entryPoint.TimeService.NowDateTime:O}] DemoDelayTask.Id:{Identity} - Начало ожидания '{scenarioAsDelay.Delay}' ...");
await Task.Delay(scenarioAsDelay.Delay, cancellationToken).ConfigureAwait(false);
Console.WriteLine($"[{m_entryPoint.TimeService.NowDateTime:O}] DemoDelayTask.Id:{Identity} - Конец ожидания.");
}
else
{
throw new InternalException($"Неизвестный тип сценария '{scenario.GetType().Assembly}'.");
}
m_available.SetValue(false);
await DoUpdateAsync(cancellationToken).ConfigureAwait(false);
return (true, null);
}
protected override ValueTask DoUpdateAsync(CancellationToken cancellationToken = default)
{
ModificationDate = m_entryPoint.TimeService.NowDateTime;
return base.DoUpdateAsync(cancellationToken);
}
}