AutoMapper is one of those amazing tools that I wish I was better with. It’s incredibly flexible, but I often throw in the towel and map things myself when it starts getting complicated. A recent pain point has been view models in a MvvmCross project.
MvvmCross is a nice C# implementation of the MVVM pattern. I use it on a daily basis to write mobile apps. It’s very common to use it with AutoMapper to map from Model -> View Model. However, my view models often have constructor dependencies, which AutoMapper can’t handle with the default configuration.
Solution (On GitHub)
MvvmCross is able to supply these dependencies. We just need to make sure everything is registered and tell AutoMapper how to get them. My demo is a simple app that displays different types of tiles.
You can see that TileViewModel has a dependency on ITileService. Because of this, AutoMapper can’t instantiate a TileViewModel. We need to let AutoMapper know how to supply this dependency, which we can do through configuration.
The default App.cs for MvvmCross already registers all interfaces that end with “Service”, so we just need to register the view model.
Now, we just need to tell AutoMapper to use the service locator when mapping to TileViewModel
And the magic is complete. Now, we can use AutoMapper as usual.
When we map to TileViewModel, each instance is resolved with Mvx.Resolve, which knows how to supply an implementation of ITileService. You can see a complete demo app at https://github.com/CuriousCurmudgeon/MvvmCrossAutoMapperSample