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.



Popular posts from this blog

Advantages and Disadvantages of Using Microsoft Access

I've answered this question in some form or another far more times than I care to count.  Most often it's a question of "why do I need a fancy Web application when I can just build this myself in two days in Access.  I mean, the data's already in Excel."  So I figured I'd post out what I threw together, I know I've missed some points. Overview Microsoft Access is an ideal solution for relatively small datasets and a limited number of users. From the Microsoft Web site: “As a desktop database, Access is well suited for small, departmental applications. These applications may start as one user’s project. For example, an employee realizes that productivity can be increased if a paper-based process is automated with an Access application. Other users in the department recognize that they can take advantage of the application if additional features are added. As more features are added, more employees run the application. As time goes by, more and more Access

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 data, to

Beryllium Spheres

I'm sitting here at home watching The Shadow , easily one of the best movies made based on one of the best old time radio shows.  I hadn't picked up on this earlier, but the weapon used to destroy the city is none other than the same power source used to power the NSEA Protector in Galaxy Quest . I never knew Beryllium was so cool.  Now I want a sphere of my own. Anyone know of other places Beryllium Spheres are mentioned? Peace, +Tom