Copy Properties From One Object To Another

by adrian vintu 3/27/2011 8:42:00 PM

Introduction

This article describes a method to automatically copy data from an object to another object with similar structures. This is similar to a deep_copy(source, destination) but with the possibility of returning a type that is different from the source.

Background

If you have worked with WCF services, you have noticed that versioning can lead to a lot of code duplication. My problem showed up when a service of version 2.0 was using almost the exact structures as in version 1.0 and I wanted to use the same service workflow code, but with different structures.

I needed a way to apply/convert values from one object to another so I created a method to automatically drill down the source object graph and copy the properties from one object to another. There are many ways to do this, but I designed a method that is generic enough and also leaves place for customization/fine tuning/hacking.

Basically, what I needed was the ability to do this automatically:

  service_version1.Customer.Name = service_version2.Customer.Name;
  service_version1.Customer.Address.Street = service_version2.Customer.Address.Street;
  //service_version1.Customer.Address.Zip = ? // Zip not available in service_version2, skipped

How to use

Although there are some gotchas, the basic usage is as follows.

Include files PropertiesCopier.cs and (optionally) PropertiesCopier.Checks.cs in your project.

Call PropertiesCopier.CopyProperties on your source and destination objects.

  PropertiesCopier.CopyProperties(sourceObject, destinationObject);
More...

DEX for Android Ten Thousand Downloads Contest - Close To Finish

by adrian vintu 3/27/2011 7:59:00 PM

 DEX for Android Ten Thousand Downloads Contest

Android Market has reported that DEX for Android has been downloaded more than 10.000 times! :)

I will however not close the contest this month, but rather extend it two more months; last entry possible will be the 31st of May 2011.

The Todo for the contest remains as it was.

What you have to do: write an email with at least three ideas (more ideas will increase your chances of winning) of applications that you would like me to build for you. It can be anything: a radio app, sports observer, social mining, anything.

What I will do: I, along with a jury, will carefully read your ideas and will choose the best ones.

What you will get: the top three ideas will receive a special prize filled with love :)

I wish you best of luck and thank you for being such good fans!

About Adrian Vintu

Adrian Vintu I am a computer software professional lately designing and programming .NET and Android applications.
Send mail

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008 - 2013

Sign in