I’ve finally come to embrace AutoMapper after a long love-hate relationship over the years. The basic use-case was always useful but there were always edge-cases where it fell down for me. I’m usually thinking this was my fault as I was mis-using or misunderstanding.
In my current usage, I’ve come across the need to use
async along side mapping data to a domain object. Mapping a domain object isn’t just for DB calls (though, these can be async as well). While you might want to do crazier things like an HTTP call for data to map, my use-case for this is simple: a Redis cache. The cache contains things from the DB and the API is rightfully async.
AutoMapper Type Converters
AutoMapper does provide a way to custom build a type: TypeConverters. However, the API is sync and it seems unlikely that async support will come to this or similar or any APIs in AutoMapper. The work of maintaining sync and async code paths is non-trivial and requiring async code paths for simple mappings does seem silly.
I wish there was a good way to do this but there doesn’t seem to be. Enter AsyncMapper!
This is a library that sits on top of AutoMapper and aims to basically provide async versions of a Type Converter.
AsyncMapper first looks for it’s own interfaces for the requested mapping. If found, it uses that. If not found, it forwards to AutoMapper. Ideally, you’d still use AutoMapper inside AsyncMappings to do the grunt work of mapping as well.
My toy example should illustrate what I’m after.
The intent is this a just a small helper library for async operations but AutoMapper still is the primary way to map.
Look at Narochno.AsyncMapper and see how it looks and feels. There are a few things on the TODO but I wouldn’t grow more functionality into this. This library greatly assists my mapping organization that needs