-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathRefDataMapper.cs
More file actions
69 lines (61 loc) · 3.43 KB
/
RefDataMapper.cs
File metadata and controls
69 lines (61 loc) · 3.43 KB
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
// Copyright (c) Avanade. Licensed under the MIT License. See https://github.com/Avanade/CoreEx
using CoreEx.Entities;
using CoreEx.Mapping;
using CoreEx.RefData;
using System;
using System.Collections.Generic;
namespace CoreEx.Database.Extended
{
/// <summary>
/// Represents a dynamic <see cref="IReferenceData"/> query-only mapper.
/// </summary>
/// <typeparam name="TItem">The <see cref="IReferenceData"/> <see cref="Type"/>.</typeparam>
/// <typeparam name="TId">The <see cref="IReferenceData"/> <see cref="IIdentifier.Id"/> <see cref="Type"/>.</typeparam>
internal class RefDataMapper<TItem, TId> : DatabaseQueryMapper<TItem> where TItem : class, IReferenceData<TId>, new() where TId : IComparable<TId>, IEquatable<TId>
{
private readonly DatabaseColumns _cols;
private readonly string _idCol;
private readonly Action<DatabaseRecord, TItem>? _additionalProperties;
private Dictionary<string, int>? _fields;
/// <summary>
/// Initializes a new instanace of the <see cref="RefDataMapper{TItem, TId}"/>.
/// </summary>
/// <param name="database">The <see cref="IDatabase"/>.</param>
/// <param name="idColumnName">The <see cref="IReferenceData"/> <see cref="IIdentifier.Id"/> column name.</param>
/// <param name="additionalProperties"></param>
public RefDataMapper(IDatabase database, string? idColumnName = null, Action<DatabaseRecord, TItem>? additionalProperties = null)
{
_cols = database.DatabaseColumns;
_idCol = idColumnName ?? _cols.RefDataIdName;
_additionalProperties = additionalProperties;
}
/// <inheritdoc/>
public override TItem MapFromDb(DatabaseRecord dr, OperationTypes operationType = OperationTypes.Unspecified)
{
if (_fields == null)
{
_fields = [];
for (var i = 0; i < dr.DataReader.FieldCount; i++)
{
_fields.Add(dr.DataReader.GetName(i), i);
}
if (!_fields.ContainsKey(_idCol) || !_fields.ContainsKey(_cols.RefDataCodeName))
throw new InvalidOperationException($"The reference data query must return as a minimum the Id and Code columns as per the configured names.");
}
var item = new TItem()
{
Id = dr.GetValue<TId>(_idCol),
Code = dr.GetValue<string?>(_cols.RefDataCodeName),
Text = _fields.ContainsKey(_cols.RefDataTextName) ? dr.GetValue<string?>(_cols.RefDataTextName) : null,
Description = _fields.ContainsKey(_cols.RefDataDescriptionName) ? dr.GetValue<string?>(_cols.RefDataDescriptionName) : null,
SortOrder = _fields.ContainsKey(_cols.RefDataSortOrderName) ? dr.GetValue<int>(_cols.RefDataSortOrderName) : 0,
IsActive = _fields.ContainsKey(_cols.RefDataIsActiveName) && dr.GetValue<bool>(_cols.RefDataIsActiveName),
StartDate = _fields.ContainsKey(_cols.RefDataStartDateName) ? dr.GetValue<DateTime?>(_cols.RefDataStartDateName) : null,
EndDate = _fields.ContainsKey(_cols.RefDataEndDateName) ? dr.GetValue<DateTime?>(_cols.RefDataEndDateName) : null,
ETag = _fields.ContainsKey(_cols.ETagName) ? dr.GetRowVersion(_cols.ETagName) : null
};
_additionalProperties?.Invoke(dr, item);
return item;
}
}
}