Bidirectional
Trung worked for a Microsoft and .NET framework shop that used AutoMapper to simplify object mapping between tiers. Their application's mapping configuration was performed at startup, as in the following C# snippet:
public void Configure(ConfigurationContext context){AutoMapper.Mapper.CreateMap().AfterMap(Map); ...},>
where the AfterMap() method's Map delegate was to map discrepancies that AutoMapper couldn't.
One day, a senior dev named Van approached Trung for help. He was repeatedly getting AutoMapper's "Missing type map configuration or unsupported mapping. Mapping types Y -> X ..." error.
Trung frowned a little, wondering what was mysterious about this problem. "You're ... probably missing mapping configuration for Y to X," he said.
"No, I'm not!" Van pointed to his monitor, at the same code snippet above.
Trung shook his head. "That mapping is one-way, from X to Y only. You can create the reverse mapping by using the Bidirectional() extension method. Here ..." He leaned over to type in the addition:
AutoMapper.Mapper.CreateMap().AfterMap(Map).Bidirectional();,>
This resolved Van's error. Both men returned to their usual business.
A few weeks later, Van approached Trung again, this time needing help with refactoring due to a base library change. While they huddled over Van's computer and dug through compilation errors, Trung kept seeing strange code within multiple AfterMap() delegates:
void Map(X src, Y desc){desc.QueueId = src.Queue.Id;src.Queue = Queue.GetById(desc.QueueId);...}
"Wait a minute!" Trung reached for the mouse to highlight two such lines and asked, "Why is this here?"
"The mapping is supposed to be bidirectional! Remember?" Van replied. "I'm copying from X to Y, then from Y to X."
Trung resisted the urge to clap a hand to his forehead or mutter something about CS101 and variable-swapping-not that this "swap" was necessary. "You realize you'd have nothing but X after doing that?"
The quizzical look on the senior developer's face assured Trung that Van hadn't realized any such thing.
Trung could only sigh and help Van trudge through the delegates he'd "fixed," working out a better mapping procedure for each.
[Advertisement] BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!