By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,152 Members | 2,039 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,152 IT Pros & Developers. It's quick & easy.

Deep copy between two different object.

P: n/a
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?

Apr 3 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Hi Allen,

Role modelling might be usefull. It will simplify your design and you
probably won't need to automate deep copying :
http://st-www.cs.uiuc.edu/users/hanm...ngs/riehle.pdf
and http://ootips.org/role-object.html.

Regards,
Bart

http://www.xenopz.com/blog/bartdeboeck/

Al******@gmail.com schreef:
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?


Apr 3 '06 #2

P: n/a
Hi Bart,

Thanks anyway. I'll check it.

Regards,
Allen

Apr 3 '06 #3

P: n/a
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.

Apr 3 '06 #4

P: n/a

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).



Al******@gmail.com wrote:
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?


Apr 3 '06 #5

P: n/a
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.

Apr 4 '06 #6

P: n/a
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.

Apr 4 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.