Skip to content

Commit f7c42f6

Browse files
committed
Support for configuring specific-value-type dictionary mapping
1 parent 21741fb commit f7c42f6

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

AgileMapper.UnitTests/Configuration/WhenConfiguringDictionaryMapping.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,5 +391,35 @@ public void ShouldConditionallyMapToDerivedTypes()
391391
((MysteryCustomerViewModel)mysteryCustomerResult).Report.ShouldBe("Very good!");
392392
}
393393
}
394+
395+
[Fact]
396+
public void ShouldConditionallyMapToDerivedTypesFromASpecificValueTypeDictionary()
397+
{
398+
using (var mapper = Mapper.CreateNew())
399+
{
400+
mapper.WhenMapping
401+
.FromDictionariesWithValueType<string>()
402+
.ToANew<CustomerViewModel>()
403+
.If(s => s.Source["Report"].Length > 10)
404+
.MapTo<MysteryCustomerViewModel>();
405+
406+
var source = new Dictionary<string, string>
407+
{
408+
["Name"] = "Customer",
409+
["Report"] = "Too short!"
410+
};
411+
var customerResult = mapper.Map(source).ToANew<CustomerViewModel>();
412+
413+
customerResult.ShouldBeOfType<CustomerViewModel>();
414+
customerResult.Name.ShouldBe("Customer");
415+
416+
source["Report"] = "Plenty long enough!";
417+
var mysteryCustomerResult = mapper.Map(source).ToANew<CustomerViewModel>();
418+
419+
mysteryCustomerResult.ShouldBeOfType<MysteryCustomerViewModel>();
420+
mysteryCustomerResult.Name.ShouldBe("Customer");
421+
((MysteryCustomerViewModel)mysteryCustomerResult).Report.ShouldBe("Plenty long enough!");
422+
}
423+
}
394424
}
395425
}

AgileMapper/Api/Configuration/MappingConfigStartingPoint.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,10 +261,19 @@ public InstanceConfigurator<TObject> InstancesOf<TObject>() where TObject : clas
261261
/// <summary>
262262
/// Configure how this mapper performs mappings from source Dictionary{string, T} instances.
263263
/// </summary>
264-
/// <returns>A DictionaryConfigurator with which to continue the configuration.</returns>
265264
public DictionaryConfigurator<object> FromDictionaries
266265
=> new DictionaryConfigurator<object>(MappingConfigInfo.AllSourceTypes(_mapperContext));
267266

267+
/// <summary>
268+
/// Configure how this mapper performs mappings from source Dictionary{string, TValue} instances.
269+
/// </summary>
270+
/// <typeparam name="TValue">
271+
/// The type of values contained in the Dictionary to which the configuration will apply.
272+
/// </typeparam>
273+
/// <returns>A DictionaryConfigurator with which to continue the configuration.</returns>
274+
public DictionaryConfigurator<TValue> FromDictionariesWithValueType<TValue>()
275+
=> new DictionaryConfigurator<TValue>(MappingConfigInfo.AllSourceTypes(_mapperContext));
276+
268277
/// <summary>
269278
/// Configure how this mapper performs mappings from the source type specified by the given
270279
/// <paramref name="exampleInstance"/>. Use this overload for anonymous types.

0 commit comments

Comments
 (0)