generated from Avanade/avanade-template
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathODataItem.cs
159 lines (143 loc) · 9.25 KB
/
ODataItem.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
// Copyright (c) Avanade. Licensed under the MIT License. See https://github.com/Avanade/CoreEx
using CoreEx.Mapping;
using CoreEx.OData.Mapping;
using System;
using System.Collections.Generic;
namespace CoreEx.OData
{
/// <summary>
/// Provides a untyped <see cref="Attributes"/>-based (<see cref="IDictionary{TKey, TValue}"/>) <b>OData</b> item/entry.
/// </summary>
public class ODataItem
{
/// <summary>
/// Maps none or more <paramref name="items"/> into a corresponding <see cref="ODataItem"/> <see cref="IEnumerable{T}"/>.
/// </summary>
/// <param name="items">The OData items.</param>
/// <returns>The <see cref="ODataItem"/> <see cref="IEnumerable{T}"/>.</returns>
public static IEnumerable<ODataItem> MapODataItems(IEnumerable<IDictionary<string, object>> items)
{
foreach (var item in items)
{
yield return new ODataItem(item);
}
}
/// <summary>
/// Maps none or more <paramref name="items"/> into a corresponding <typeparamref name="T"/> <see cref="IEnumerable{T}"/> using the specified <paramref name="mapper"/>.
/// </summary>
/// <typeparam name="T">The resulting item <see cref="Type"/>.</typeparam>
/// <param name="mapper">The specific <see cref="IODataMapper{TSource}"/>.</param>
/// <param name="items">The OData items.</param>
/// <param name="operationType">The singluar <see href="https://en.wikipedia.org/wiki/Create,_read,_update_and_delete">CRUD</see> <see cref="OperationTypes"/> value being performed.</param>
/// <returns>The <typeparamref name="T"/> <see cref="IEnumerable{T}"/>.</returns>
public static IEnumerable<T> MapODataItems<T>(IODataMapper<T> mapper, IEnumerable<IDictionary<string, object>> items, OperationTypes operationType = OperationTypes.Unspecified)
{
foreach (var item in MapODataItems(items))
{
yield return mapper.MapFromOData(item, operationType)!;
}
}
/// <summary>
/// Creates an <see cref="ODataItem"/> from the specified <paramref name="value"/>.
/// </summary>
/// <typeparam name="T">The <paramref name="value"/> <see cref="Type"/>.</typeparam>
/// <param name="mapper">The specific <see cref="IODataMapper{TSource}"/>.</param>
/// <param name="value">The value.</param>
/// <param name="operationType">The singluar <see href="https://en.wikipedia.org/wiki/Create,_read,_update_and_delete">CRUD</see> <see cref="OperationTypes"/> value being performed.</param>
/// <returns>The resultant value.</returns>
public static ODataItem MapFrom<T>(IODataMapper<T> mapper, T value, OperationTypes operationType = OperationTypes.Unspecified)
{
var result = new ODataItem();
mapper.MapToOData(value, result, operationType);
return result;
}
/// <summary>
/// Creates an <see cref="ODataItem"/> from the specified <paramref name="value"/>.
/// </summary>
/// <typeparam name="T">The <paramref name="value"/> <see cref="Type"/>.</typeparam>
/// <param name="mapper">The specific <see cref="IMapper{TSource, TDestination}"/>.</param>
/// <param name="value">The value.</param>
/// <param name="operationType">The singluar <see href="https://en.wikipedia.org/wiki/Create,_read,_update_and_delete">CRUD</see> <see cref="OperationTypes"/> value being performed.</param>
/// <returns>The resultant value.</returns>
public static ODataItem? MapFrom<T>(IMapper<T, ODataItem> mapper, T? value, OperationTypes operationType = OperationTypes.Unspecified) => mapper.Map(value, operationType);
/// <summary>
/// Creates an <see cref="ODataItem"/> from the specified <paramref name="value"/>.
/// </summary>
/// <typeparam name="T">The <paramref name="value"/> <see cref="Type"/>.</typeparam>
/// <param name="mapper">The <see cref="IMapper{TSource, TDestination}"/> mapper.</param>
/// <param name="value">The value.</param>
/// <param name="operationType">The singluar <see href="https://en.wikipedia.org/wiki/Create,_read,_update_and_delete">CRUD</see> <see cref="OperationTypes"/> value being performed.</param>
/// <returns>The resultant value.</returns>
public static ODataItem? MapFrom<T>(IMapper mapper, T? value, OperationTypes operationType = OperationTypes.Unspecified) => mapper.Map<T, ODataItem>(value, operationType);
/// <summary>
/// Creates an <see cref="ODataItem"/> from the specified <paramref name="value"/>.
/// </summary>
/// <typeparam name="T">The <paramref name="value"/> <see cref="Type"/>.</typeparam>
/// <param name="client">The <see cref="ODataClient"/> mapper.</param>
/// <param name="value">The value.</param>
/// <param name="operationType">The singluar <see href="https://en.wikipedia.org/wiki/Create,_read,_update_and_delete">CRUD</see> <see cref="OperationTypes"/> value being performed.</param>
/// <returns>The resultant value.</returns>
public static ODataItem? MapFrom<T>(ODataClient client, T? value, OperationTypes operationType = OperationTypes.Unspecified) => MapFrom(client.Mapper, value, operationType);
/// <summary>
/// Initializes a new instance of the <see cref="ODataItem"/> class.
/// </summary>
public ODataItem() => Attributes = new Dictionary<string, object>();
/// <summary>
/// Initializes a new instance of the <see cref="ODataItem"/> class with the specified <paramref name="attributes"/>.
/// </summary>
/// <param name="attributes">The <see cref="IDictionary{TKey, TValue}"/>.</param>
public ODataItem(IDictionary<string, object> attributes) => Attributes = attributes;
/// <summary>
/// Gets the attributes/columns.
/// </summary>
public IDictionary<string, object> Attributes { get; private set; } = new Dictionary<string, object>();
/// <summary>
/// Gets or sets the value of the specified attribute.
/// </summary>
/// <param name="name">The name.</param>
/// <returns>The value.</returns>
public object? this[string name]
{
get => Attributes[name];
set
{
if (Attributes.ContainsKey(name))
Attributes[name] = value!;
else
Attributes.Add(name, value!);
}
}
/// <summary>
/// Maps the <see cref="ODataItem"/> to a <typeparamref name="T"/>.
/// </summary>
/// <typeparam name="T">The resulting value <see cref="Type"/>.</typeparam>
/// <param name="mapper">The specific <see cref="IODataMapper{TSource}"/>.</param>
/// <param name="operationType">The singluar <see href="https://en.wikipedia.org/wiki/Create,_read,_update_and_delete">CRUD</see> <see cref="OperationTypes"/> value being performed.</param>
/// <returns>The resultant value.</returns>
public T MapTo<T>(IODataMapper<T> mapper, OperationTypes operationType = OperationTypes.Unspecified) where T : class, new() => mapper.MapFromOData(this, operationType)!;
/// <summary>
/// Maps the <see cref="ODataItem"/> to a <typeparamref name="T"/>.
/// </summary>
/// <typeparam name="T">The resulting value <see cref="Type"/>.</typeparam>
/// <param name="mapper">The specific <see cref="IMapper{TSource, TDestination}"/>.</param>
/// <param name="operationType">The singluar <see href="https://en.wikipedia.org/wiki/Create,_read,_update_and_delete">CRUD</see> <see cref="OperationTypes"/> value being performed.</param>
/// <returns>The resultant value.</returns>
public T MapTo<T>(IMapper<ODataItem, T> mapper, OperationTypes operationType = OperationTypes.Unspecified) => mapper.Map(this, operationType)!;
/// <summary>
/// Maps the <see cref="ODataItem"/> to a <typeparamref name="T"/>.
/// </summary>
/// <typeparam name="T">The resulting value <see cref="Type"/>.</typeparam>
/// <param name="mapper">The <see cref="IMapper"/>.</param>
/// <param name="operationType">The singluar <see href="https://en.wikipedia.org/wiki/Create,_read,_update_and_delete">CRUD</see> <see cref="OperationTypes"/> value being performed.</param>
/// <returns>The resultant value.</returns>
public T MapTo<T>(IMapper mapper, OperationTypes operationType = OperationTypes.Unspecified) => mapper.Map<ODataItem, T>(this, operationType)!;
/// <summary>
/// Maps the <see cref="ODataItem"/> to a <typeparamref name="T"/>.
/// </summary>
/// <typeparam name="T">The resulting value <see cref="Type"/>.</typeparam>
/// <param name="client">The <see cref="ODataClient"/>.</param>
/// <param name="operationType">The singluar <see href="https://en.wikipedia.org/wiki/Create,_read,_update_and_delete">CRUD</see> <see cref="OperationTypes"/> value being performed.</param>
/// <returns>The resultant value.</returns>
public T MapTo<T>(ODataClient client, OperationTypes operationType = OperationTypes.Unspecified) => MapTo<T>(client.Mapper, operationType);
}
}