Skip to content

Commit 72ca032

Browse files
committed
Introduced AttributeDictionary support into TopicMappingService
Updated the private `MapAsync()` overload to identify if the model exposes a constructor with the `AttributeDictionary` (31ee953) and, if it does, populate it with `AsAttributeDictionary()` (a781356). In this case, bypass other constructor processing, and then bypass `SetPropertyAsync()` for all attributes mapped via the constructor. This, of course, relies on the implementer of the constructor to correctly set the properties. This is the core implementation of Issue #99.
1 parent a781356 commit 72ca032

1 file changed

Lines changed: 16 additions & 6 deletions

File tree

OnTopic/Mapping/TopicMappingService.cs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ public TopicMappingService(ITopicRepository topicRepository, ITypeLookupService
185185
var constructorInfo = typeAccessor.GetPrimaryConstructor();
186186
var parameters = constructorInfo?.GetParameters()?? Array.Empty<ParameterInfo>();
187187
var arguments = new object?[parameters.Length];
188+
var attributeArguments = (IDictionary<string, string?>)new Dictionary<string, string?>();
188189

189190
/*------------------------------------------------------------------------------------------------------------------------
190191
| Pre-cache entry
@@ -201,14 +202,23 @@ public TopicMappingService(ITopicRepository topicRepository, ITypeLookupService
201202
\-----------------------------------------------------------------------------------------------------------------------*/
202203
var parameterQueue = new Dictionary<int, Task<object?>>();
203204

204-
foreach (var parameter in parameters) {
205-
parameterQueue.Add(parameter.Position, GetParameterAsync(topic, associations, parameter, cache, attributePrefix));
205+
if (parameters.Length == 1 && parameters[0].ParameterType == typeof(AttributeDictionary)) {
206+
var attributes = topic.Attributes.AsAttributeDictionary(true);
207+
arguments[0] = attributes;
208+
attributeArguments = attributes;
206209
}
210+
else {
211+
212+
foreach (var parameter in parameters) {
213+
parameterQueue.Add(parameter.Position, GetParameterAsync(topic, associations, parameter, cache, attributePrefix));
214+
}
207215

208-
await Task.WhenAll(parameterQueue.Values).ConfigureAwait(false);
216+
await Task.WhenAll(parameterQueue.Values).ConfigureAwait(false);
217+
218+
foreach (var parameter in parameterQueue) {
219+
arguments[parameter.Key] = await parameter.Value.ConfigureAwait(false);
220+
}
209221

210-
foreach (var parameter in parameterQueue) {
211-
arguments[parameter.Key] = await parameter.Value.ConfigureAwait(false);
212222
}
213223

214224
/*------------------------------------------------------------------------------------------------------------------------
@@ -230,7 +240,7 @@ public TopicMappingService(ITopicRepository topicRepository, ITypeLookupService
230240
| Loop through properties, mapping each one
231241
\-----------------------------------------------------------------------------------------------------------------------*/
232242
var propertyQueue = new List<Task>();
233-
var mappedParameters = parameters.Select(p => p.Name);
243+
var mappedParameters = parameters.Select(p => p.Name).Union(attributeArguments.Select(a => a.Key));
234244

235245
foreach (var property in typeAccessor.GetMembers(MemberTypes.Property)) {
236246
if (!mappedParameters.Contains(property.Name, StringComparer.OrdinalIgnoreCase)) {

0 commit comments

Comments
 (0)