Skip to main content

AutoMapper from a List to Class Properties

I just found AutoMapper, a tool I wish I'd had years ago. This can take any of you classes and translate them into another class. This would have been especially useful when I've had to get an object out of and EntityFramework, and add in additional properties before display, which happens quite often. The code for this is pretty simple, an well defined on their site.

I came across AutoMapper from my wife last week, and found it to be a perfect fit for on of my current issues. We're getting data from a Web service that has an Object and a lit of properties with their values, like the following.


I needed to easy map each property to my display object type. There is the option of using reflection (Looping through Object's properties in C# is an excellent example), and looping over each of the properties in my destination object, and filling them from values in the source object. Honestly, I did head down this path a bit, but realized I needed a more flexible solution.

It turns out our Web service may change in the next year or two where it returns properties in the same way my display object uses properties, but may have different names. AutoMapper makes it easy to put the translation logic in one place, and changes in the future will be minimal, and all take place in one location.

I'm now using the following to make the mapping work pretty easily:


The AutoMapper configuration should occur I one location, such as application start. Once we switch the Web service, the mapping can remove the specific property names, and only cover the properties where the display property name is different from the data source.

Peace.

Comments

Popular posts from this blog

Using an Array of Objects in C++

 I've been programming for years (over 35 at this point, which is crazy  to think about). My career right now is much more Software Architecture, and much less Software Developer, but I still get some time to write out GraphQL APIs in TypeScript, Vue 3 UIs, GitLab pipelines, and just generally making "big" decisions and helping make them a reality. It's nice every now and then to come across different articles and ideas that get me to remember life in college when I was using C++. Who would have thought C++ was the "hot new thing" right now (though I suppose it's more like Rust and Go, both great languages as well). One of the things I find frustrating with most technical posts is where they focus on the "how do I build an app" and not so much on "how do I do this one slightly useful thing". I figured I'd throw one together what was front of mind, using user attributes for permissions (i.e., Attribute Based Access Control - ABAC) ...

Red-Gate SQL Compare

Every now and then I come across a program that becomes so ingrained in my daily work that I hardly know how I'd get by without it.  I'll probably break down a couple over the next few days, but for database work, I have never found anything as good as Red Gate's SQL Compare and SQL Data Compare .  Essentially these tools let you compare two SQL Server databases (all objects, users, permissions, functions, diagrams, anything) and update changes to whichever database you want.  This is amazingly useful for deploying database changes to a test or production environment (do it to production with ridiculous care, even though it will generate a SQL Script for you and run all updates in one transaction), and making sure everything is synchronized. For releases we can just generate the compare script, confirm that the changes match the updates we want to go out, and store it all in one place with the release details.  This is true for both the structure and the d...

Kids Activities

I find myself often in a situation where it's some morning, I have the kids for the afternoon, and I'm not sure what to do with them. We could go to a movie, or play Legos, but living near Washington, DC, I want the kids to love the museums as much as I do, or to see what else is going on. This Sunday, while my wife was travelling, I took the kids to the Chocolate Festival in Old Town Fairfax. I didn't even know there wad an Old Town Fairfax, much less a chocolate festival. It was okay overall, but the best was seeing any type of chocolate you could imagine, and letting the kids pick something for themselves and their teacher. For finding cheap or free stuff going on nearby with the kids, I have to say About.com has consistently been the best. I tried si.edu (the Smithsonian Website) which is also good, but a little hard to navigate, partly because they have so much going on. At About I did a search of what to do with my kids this weekend, and a bunch of items came ...