-
Notifications
You must be signed in to change notification settings - Fork 0
/
DomainObjectDemoObject.cs
228 lines (189 loc) · 7.25 KB
/
DomainObjectDemoObject.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
using Microsoft.Extensions.Logging;
using ShtrihM.DemoServer.Processing.Api.Common;
using ShtrihM.DemoServer.Processing.Api.Common.Dtos.DemoObject;
using ShtrihM.DemoServer.Processing.Api.Common.Dtos.DemoObject.Update;
using ShtrihM.DemoServer.Processing.Common;
using ShtrihM.DemoServer.Processing.Model.Interfaces;
using ShtrihM.Wattle3.Common.Exceptions;
using ShtrihM.Wattle3.DomainObjects.DomainObjects;
using ShtrihM.Wattle3.DomainObjects.Interfaces;
using ShtrihM.Wattle3.Mappers.Primitives.MutableFields;
using System;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using ShtrihM.DemoServer.Processing.Generated.Interface;
using ShtrihM.Wattle3.DomainObjects;
using ShtrihM.Wattle3.DomainObjects.DomainObjectActivators;
using ShtrihM.Wattle3.Primitives;
using ShtrihM.Wattle3.DomainObjects.UnitOfWorkLocks;
using ShtrihM.Wattle3.DomainObjects.DomainObjects.BaseDomainObjects;
namespace ShtrihM.DemoServer.Processing.Model.DomainObjects.DemoObject;
[DomainObjectDataMapper]
// ReSharper disable once ClassNeverInstantiated.Global
public sealed class DomainObjectDemoObject : BaseDomainObjectMutableWithUpdateLock<DomainObjectDemoObject, IEntryPointContext<ICustomEntryPoint>>, IDomainObjectDemoObject, IDomainObjectActivatorPostCreate
{
#region Template - шаблон создания объекта
/// <summary>
/// Шаблон создания объекта <see cref="DomainObjectDemoObject"/>.
/// </summary>
public class Template : IDomainObjectTemplate
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
// ReSharper disable once ConvertToPrimaryConstructor
public Template(
string name,
bool enabled)
{
Name = name;
Enabled = enabled;
}
public readonly string Name;
public readonly bool Enabled;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ValueTask<IDomainObjectDemoObject> NewAsync(
ICustomEntryPoint entryPoint,
CancellationToken cancellationToken = default)
{
return entryPoint.CurrentUnitOfWork.NewAsync<IDomainObjectDemoObject>(this, cancellationToken);
}
}
#endregion
#region Изменяемы поля
[DomainObjectFieldValue]
private readonly MutableField<bool> m_enabled;
[DomainObjectFieldValue]
private readonly MutableFieldStringLimitedEx m_name;
#endregion
#region Конструкторы
// ReSharper disable once UnusedMember.Global
public DomainObjectDemoObject(
DemoObjectDtoActual data,
IEntryPointContext<ICustomEntryPoint> entryPointContext,
IDomainObjectUnitOfWorkLockService lockUpdate)
: base(entryPointContext, data, lockUpdate, false)
{
CreateDate = data.CreateDate.SpecifyKindLocal();
ModificationDate = data.ModificationDate.SpecifyKindLocal();
m_name = new MutableFieldStringLimitedEx(FieldsConstants.DemoObjectNameMaxLength, data.Name);
m_enabled = new MutableField<bool>(data.Enabled);
}
// ReSharper disable once UnusedMember.Global
public DomainObjectDemoObject(
long identity,
Template template,
IEntryPointContext<ICustomEntryPoint> entryPointContext,
IDomainObjectUnitOfWorkLockService lockUpdate)
: base(entryPointContext, identity, lockUpdate, true)
{
CreateDate = m_entryPointContext.TimeService.NowDateTime;
ModificationDate = CreateDate;
m_name = new MutableFieldStringLimitedEx(FieldsConstants.DemoObjectNameMaxLength, template.Name);
m_enabled = new MutableField<bool>(template.Enabled);
}
#endregion
public override Guid TypeId => WellknownDomainObjects.DemoObject;
public bool Enabled
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => m_enabled.Value;
}
public string Name
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => m_name.Value;
}
[DomainObjectFieldValue]
public DateTime CreateDate
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get;
}
[DomainObjectFieldValue]
public DateTime ModificationDate
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private set;
}
public ValueTask UpdateAsync(DemoObjectUpdate parameters, CancellationToken cancellationToken)
{
m_lockUpdate.Has(Identity);
var changed = false;
foreach (var field in parameters.Fields)
{
cancellationToken.ThrowIfCancellationRequested();
if (field is DemoObjectUpdateFieldValueOfName fieldValueOfName)
{
m_name.SetValue(fieldValueOfName.Name);
if (m_name.Changed)
{
changed = true;
}
continue;
}
if (field is DemoObjectUpdateFieldValueOfEnabled fieldValueOfEnabled)
{
m_enabled.SetValue(fieldValueOfEnabled.Enabled);
if (m_enabled.Changed)
{
changed = true;
}
continue;
}
throw new InternalException($"Неизвестный тип поля '{field.GetType().AssemblyQualifiedName}'.");
}
if (changed)
{
return DoUpdateAsync(cancellationToken);
}
return ValueTask.CompletedTask;
}
public DemoObjectInfo GetInfo()
{
var result =
new DemoObjectInfo
{
Id = Identity,
Name = Name,
Enabled = Enabled,
};
return result;
}
protected override ValueTask DoUpdateAsync(CancellationToken cancellationToken = default)
{
ModificationDate = m_entryPointContext.TimeService.NowDateTime;
return base.DoUpdateAsync(cancellationToken);
}
#region Аспект создания
public void PostCreate()
{
var identity = Identity;
var unitOfWork = m_entryPointContext.EntryPoint.CurrentUnitOfWork;
var domainBehaviour = unitOfWork.CreateDomainBehaviourWithСonfirmation();
unitOfWork.AddBehaviour(domainBehaviour);
domainBehaviour.SetFailAll(
() =>
{
var logger = m_entryPointContext.EntryPoint.LoggerFactory.CreateLogger<DomainObjectDemoObject>();
var messgae = $"Не удалось создать объект с идентификатором '{identity}'.";
logger.LogError(messgae);
Console.WriteLine(messgae);
});
domainBehaviour.SetSuccessfulAll(
() =>
{
var logger = m_entryPointContext.EntryPoint.LoggerFactory.CreateLogger<DomainObjectDemoObject>();
var messgae = $"Создан объект с идентификатором '{identity}'.";
logger.LogError(messgae);
Console.WriteLine(messgae);
});
}
public ValueTask PostCreateAsync(CancellationToken cancellationToken = default)
{
PostCreate();
return ValueTask.CompletedTask;
}
#endregion
}