File tree Expand file tree Collapse file tree 2 files changed +40
-1
lines changed
AgileMapper.UnitTests/Configuration
AgileMapper/Api/Configuration Expand file tree Collapse file tree 2 files changed +40
-1
lines changed Original file line number Diff line number Diff 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}
Original file line number Diff line number Diff 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.
You can’t perform that action at this time.
0 commit comments