473,398 Members | 2,380 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Deep copy between two different object.

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
6 3989
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
Hi Bart,

Thanks anyway. I'll check it.

Regards,
Allen

Apr 3 '06 #3
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

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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Tony Johansson | last post by:
Hello! I'm reading in a book about C++ and that is something that sound strange. It says "Pointers have reference-assignment semantics similar to those in Java. For example, after the...
2
by: Alex | last post by:
Entering the following in the Python shell yields >>> help(dict.copy) Help on method_descriptor: copy(...) D.copy() -> a shallow copy of D >>>
4
by: fperfect13 | last post by:
Hi, I wanted to perform a deep copy of an array. Searching on google I ran into different opinions : C# Interview Questions (http://blogs.wwwcoder.com/tsvmadhav/archive/2005/04/08/2882.aspx)...
13
by: ahaupt | last post by:
Hi all, I'm implementing the Clone() method through the ICloneable interface and don't quite know how deep I need to go for a deep copy. Example: class A: ICloneable { object _val;
2
by: bonk | last post by:
I have come across the need to distinguish between the creation of a deep and a shallow copy and with great interest I have read this article: ...
22
by: Steven Blair | last post by:
I need to perform a Deep Copy on an ArrayList. I wrote a small sample app to prove this could be done: ArrayList a = new ArrayList(); ArrayList b = new ArrayList(); a.Add("Hello"); b =...
13
by: blangela | last post by:
I have decided (see earlier post) to paste my Word doc here so that it will be simpler for people to provide feedback (by directly inserting their comments in the post). I will post it in 3 parts...
0
by: Tarik Monem | last post by:
I have been working on an all AJAX/DOM web site which is set to go live today and I thought I'd share my discoveries with all of you whom have helped me when I have encountered different issues along...
3
by: raylopez99 | last post by:
The "C# Cookbook" (O'Reilly / Jay Hilyard), section 3.26, is on deep cloning versus shallow cloning. The scanned pages of this book are found here: http://www.sendspace.com/file/mjyocg (Word...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.