Object mapping. It's a thing that people do, usually when they want to move data between "persistence" (database) objects and "domain" (client-facing) objects. There are a number of tools out there that do this quite well because it's such a common pattern, especially in .NET land where I tend to live.

Well now there's one more.

LeanMapper.Net is my own entry into the object mapper field. The goal was partially an educational one, since I thought it would be an interesting exercise in working with reflection in a practical manner (spoilers: it actually wasn't!), but it was also borne out of some niggles that I had about the mapper we were using around the office previously. In particular, we found ourselves writing lots of boilerplate code to configure our objects, and that got me to thinking: can this be done without all of that?

So LeanMapper.Net was born, with the goal of writing a functional object mapping library that was simple to use and minimized boilerplate code. The other folks around the office finally laid eyes on it the other day, and it's actually earned a spot in our brand-new web app. Whoo!

Most of the simplicity comes from the fact that pre-configuration is absolutely not required. If you have really simple objects - which many people do - just map them! LeanMapper will figure out how to get everything from one side to the other without any hand-holding, and you can get on to other, more important things. If you've got more complex objects then you can still put together a "custom" configuration using a nice fluent-style API. Checkout the docs on GitHub for all the sweet details.

So about that reflection thing...

I mentioned above (spoilers!) that I originally dove into the mapper with a reflection-based design, and I in fact got almost all of the features working how I wanted with that original algorithm. Once I ran some tests on it, though, I quickly realized that it wasn't very fast. Assuming that there must surely be a better way of doing things, I took a put my Google-fu skills to work and landed on a promising refactor candidate: Expression Trees.

I feel like it's almost an injustice to say that expression trees are "pretty cool"; it's basically metaprogramming with a sweet API, and object mapping is about as practical a use as I think I'll ever find for that kind of design. The speed gains were enormous, with the generated methods running orders of magnitude faster than the reflection-based code. On top of that, I got to learn a new (to me) technology and put it to work in the context of a real problem to be solved.

But don't take my word for it. Try out LeanMapper.NET for yourself! It's on NuGet right now, supports .NET 4.5.2+ and .NET Core 1.0, and is distributed under the MIT license. Think you can improve it? Send me a pull request!