generated from Avanade/avanade-template
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathChangeLogDatabaseMapper.cs
59 lines (50 loc) · 2.41 KB
/
ChangeLogDatabaseMapper.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
// Copyright (c) Avanade. Licensed under the MIT License. See https://github.com/Avanade/CoreEx
using CoreEx.Entities;
using CoreEx.Mapping;
using System;
namespace CoreEx.Database.Mapping
{
/// <summary>
/// Represents a <see cref="ChangeLog"/> <see cref="IDatabaseMapper"/>.
/// </summary>
public readonly struct ChangeLogDatabaseMapper : IDatabaseMapper<ChangeLog>
{
private static readonly Lazy<ChangeLogDatabaseMapper> _default = new(() => new(), true);
/// <summary>
/// Gets the default (singleton) instance.
/// </summary>
public static ChangeLogDatabaseMapper Default => _default.Value;
/// <inheritdoc/>
public ChangeLog? MapFromDb(DatabaseRecord record, OperationTypes operationType = OperationTypes.Unspecified)
{
if (OperationTypes.AnyExceptGet.HasFlag(operationType))
{
var changeLog = new ChangeLog
{
CreatedBy = record.GetValue<string?>(record.Database.DatabaseColumns.CreatedByName),
CreatedDate = record.GetValue<DateTime?>(record.Database.DatabaseColumns.CreatedDateName),
UpdatedBy = record.GetValue<string?>(record.Database.DatabaseColumns.UpdatedByName),
UpdatedDate = record.GetValue<DateTime?>(record.Database.DatabaseColumns.UpdatedDateName)
};
return ((IChangeLogAudit)changeLog).IsInitial ? null : changeLog;
}
return null;
}
/// <inheritdoc/>
public void MapToDb(ChangeLog? value, DatabaseParameterCollection parameters, OperationTypes operationType = OperationTypes.Unspecified)
{
if (value == null || !parameters.Database.EnableChangeLogMapperToDb)
return;
if (OperationTypes.AnyExceptUpdate.HasFlag(operationType))
{
parameters.Param(parameters.Database.DatabaseColumns.CreatedByName, value.CreatedBy);
parameters.Param(parameters.Database.DatabaseColumns.CreatedDateName, value.CreatedDate);
}
if (OperationTypes.AnyExceptCreate.HasFlag(operationType))
{
parameters.Param(parameters.Database.DatabaseColumns.UpdatedByName, value.UpdatedBy);
parameters.Param(parameters.Database.DatabaseColumns.UpdatedDateName, value.UpdatedDate);
}
}
}
}