Deep copy between two different object.

  • Thread starter Thread starter AllenGnr
  • Start date Start date
A

AllenGnr

There are two objects, instants of two different class, but they has
same schema.

Class ClientDivision
{
public string Name
{
get {}; set {};
}

public string Manager
{
get {}; set {};
}
}

Class ClientPerson
{
public string Name
{
get {}; set {};
}

public int Age
{
get {}; set {};
}

public Division div
{
get {}; set {};
}
}

Class ServerDivision
{
public string Name
{
get {}; set {};
}

public string Manager
{
get {}; set {};
}
}

Class ServerPerson
{
public string Name
{
get {}; set {};
}

public int Age
{
get {}; set {};
}

public Division div
{
get {}; set {};
}
}

I have a instant of ClientPerson and need to build a ServerPerson with
the value from ClientPerson. The following code is what I do that job
now.

ServerPerson sPerson = new ServerPerson();
sPerson.Name = cPerson.Name;
sPerson.Age = cPerson.Age;
sPerson.Div = new ServerDivision();
sPerson.Div.Name = cPerson.Div.Name;
sPerson.Div.Manager = cPerson.Div.Manager;

Like you see, all value pass statement wrote by my hand. It's ok when
the class's struct is simple. But will be a large work when it's
complex. In last few days, I even wrote about 300 lines of code just
for copy the value. As you know, nobody will like coding in this way.

So, I'm thinking to find or write a "Deep copy" like tool to do that
job for me. Is anyone has seen a tool like this?
 
Hi Bart,

Our system is already designed and I can't modify it. So I just can
use a automate deep copy tool to simplify my job.
By the way, thanks for concert.
 
Here is some vb.net .. which is a translation I made from some C# code.
but I can't find the c# code at the moment.

Imports System.Reflection
Imports System.IO
Imports System.Collections.Specialized

'the next loop is basically a cloning mechanism.
Dim prop As PropertyInfo
For Each prop In AssemblyProperties
If prop.CanWrite Then ' only update properties which
can be written to
Dim indivReportProperty As PropertyInfo =
newObject.GetType().GetProperty(prop.Name)
If Not (indivReportProperty Is Nothing) Then
'handle the case where the objects are not perfect copies of one
another
'this sets the "new object" property to the
getvalue of the "old object"
indivReportProperty.SetValue(newObject,
prop.GetValue(newObject, Nothing), Nothing)
End If
End If
Next prop


The translation back to c# shoudl be simple enough I think.

This assumes the exact same contract between the 2 objects, or it won't
set them.
The .CanWrite is a good test, and got rid of some issues on some
readonly properties (derived properties to be exact).
 
Thanks for reply this thread.

Your solution is not enough, if all propertys is ValueType, yes, your
code doing well.
But if there are custom types such as classes I difined, some kind of
arraylist, generic collection,
I don't think this code can handle.
 
I wouldn't try for a systemic solution here. It would probably too
obscure and complicated for easy maintenance.

In this case, since the system is already designed, I would take the
simple, straightforward solution:

public class ClientPerson
{
...

public ServerPerson ServerPerson
{
get
{
ServerPerson result = new ServerPerson();
result.Name = this._name;
result.Age = this._age;
result.Division = this._division.ServerDivision;
return result;
}
}
}

public class ClientDivision
{
...

public ServerDivision ServerDivision
{
get
{
ServerDivision result = new ServerDivision();
result.Name = this._name;
result.Manager = this._manager;
return result;
}
}
}

Please note that I'm in no way condoning the original design, which is
rife with problems. Nor do I particularly like deep copies like this,
because it's pretty easy to create infinite recursion if you're not
careful.

However, given what you've been given to work with, I think that the
above solution has the advantage of being very simple and easy to
maintain (apart from lurking infinite recursion problems). For me, ease
of maintenance means dollar savings because junior programmers don't
introduce as many bugs.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top