Reputation: 715
I've read the AutoMapper docs and the similar threads but I'm either confused or stymied so I turn to my favorite group of experts ;)
I have a lookup table that I load into a Dictionary for use in a CustomResolver. keyValue1 + keyValue2 is my "key".
public class myLookUpEntity
{
public string keyValue1 { get; set; }
public string keyValue2 { get; set; }
public string CodeLookup { get; set; }
}
This loads into a Dictionary<string, myLookupEntity>. Load is just fine via an IOptions pattern to load the table into memory.
I attempt to map from two different source objects to the same destination object using something like this
.ForMember(dest => dest.targetField, opt => opt.MapFrom<MyLookupResolver, string>(src => src.sourceField1.PadRight(14)))
and
.ForMember(dest => dest.targetField, opt => opt.MapFrom<MyLookupResolver, string>(src => "".PadRight(10) + src.sourceField2.PadRight(4)))
The resulting lookup "key" is 14 characters passed to the resolver. Here's the resolver code
public class LookupTable
{
public IDictionary<string, myLookupEntity> LookupEntries { get; set; }
}
public class MyLookupResolver : IMemberValueResolver<object, object, string, string>
{
private readonly LookupTable myLookupTable;
public MyLookupResolver()
{
// This constructor is only used by XUnit Tests
this.myLookupTable = new LookupTable() { LookupEntries = new Dictionary<string, myLookUpEntity>() { } };
}
public MyLookupResolver(IOptions<LookupTable> myTable)
{
this.myLookupTable = myTable.Value;
}
public string Resolve(object source, object destination, string keyValue, string resultValue, ResolutionContext context)
{
resultValue = "";
if (myLookupTable.LookupEntries.TryGetValue(keyValue, out myLookupEntity value))
resultValue = value.CodeLookup;
return resultValue;
}
}
Only problem is, it doesn't work I cannot figure out how to pass two source values to the resolver and let it form the key. Also I am getting this exception
Error mapping types.
Mapping types: sourceObject1 -> destObject .sourceObject1 -> .destObject
Type Map configuration: sourceObject1 -> destObject .sourceObject1 -> .destObject
Destination Member: targetField
Hope this is enough detail to set me on the right path.
Upvotes: 0
Views: 33