473,503 Members | 2,148 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Serialization, XSD to class, and deep copying?

I have an XSD which I have generated a class from using the xsd.exe
tool. My XSD contains complex types within it, so that the generated
class has member variables which are of types that are also defined in
the XSD.

I deserialize XML data into instances of that class, and serialize back
to XML. So far so good.

However, in my code I have the concept of wanting to create new copies
of these deserialized objects and modifying some of the members of the
new copies. This obviously presents a problem because I don't want the
member variables referencing the same data.

In other words I *think* I want to do a deep copy, but everything I've
read on "C# copy constructors and ICloneable" seems to point to this
area being a route to no-where. Besides which, my generated classes
don't support ICloneable and I can't keep writing my own copy
constructors each time I generate the class.

So... is there a design pattern for this model that can help me
overcome these problems? It seems that I could make use of
serialization to create new copies, which is handy because my generated
classes, by nature, support serialization.

Any advice or references on "the right thing to do" would be much
appreciated.

Boz

Mar 2 '06 #1
4 3020
What makes you think that implementing "ICloneable" is a road to nowhere?
You can certainly add methods to your class. When it is serialized, the
method will not be there, but when it is deserialized, the method *will* be
there.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
A brute awe as you,
a Metallic hag entity, eat us.
<jj********@yahoo.com> wrote in message
news:11**********************@j33g2000cwa.googlegr oups.com...
I have an XSD which I have generated a class from using the xsd.exe
tool. My XSD contains complex types within it, so that the generated
class has member variables which are of types that are also defined in
the XSD.

I deserialize XML data into instances of that class, and serialize back
to XML. So far so good.

However, in my code I have the concept of wanting to create new copies
of these deserialized objects and modifying some of the members of the
new copies. This obviously presents a problem because I don't want the
member variables referencing the same data.

In other words I *think* I want to do a deep copy, but everything I've
read on "C# copy constructors and ICloneable" seems to point to this
area being a route to no-where. Besides which, my generated classes
don't support ICloneable and I can't keep writing my own copy
constructors each time I generate the class.

So... is there a design pattern for this model that can help me
overcome these problems? It seems that I could make use of
serialization to create new copies, which is handy because my generated
classes, by nature, support serialization.

Any advice or references on "the right thing to do" would be much
appreciated.

Boz

Mar 2 '06 #2
I said road to nowhere based on what I found on the net regarding deep
copying (copy constructor) for ICloneable and also item 28 in Effective
C# "Avoid ICloneable".

Further discussions in house have revealed a quite neat solution:
- derive a class from the XSD generated class
- implement ICloneable on the derived class
- our Clone() method uses serialization & deserialization create new
copy

Does this sound a reasonable solution? The only downsides I can see
might be in terms of performance, in that we're using serialization
twice and there's boxing/unboxing going on because Clone returns
System.Object.

Boz

PS Referenced posting regarding deep copy and ICloneable being road to
nowhere, from another MVP like yourself:
http://groups.google.co.uk/group/mic...e&rnum=5&hl=en

Mar 2 '06 #3
Hi Boz,
Further discussions in house have revealed a quite neat solution:
- derive a class from the XSD generated class
- implement ICloneable on the derived class
- our Clone() method uses serialization & deserialization create new
copy

Does this sound a reasonable solution? The only downsides I can see
I couldn't tell you whether it's a reasonable solution in this specific
case. After all, you're still implementing ICloneable, regardless of whether
you're implementing it in the base class or the derived class.

And ICloneable may be something which can be abused, but it is also useful.
Otherwise it wouldn't be there.
PS Referenced posting regarding deep copy and ICloneable being road to
nowhere, from another MVP like yourself:
Well, Boz, speaking as an MVP (and a human being), I would have to say that
one should not take anything told to one by anyone, MVP or not, as Gospel.
When I post a suggestion, I expect the person I'm responding to to do some
research and ensure that I'm on the right track. I have been wrong a time or
two, and who hasn't? What I hope for is that what I tell someone will be
enough to stimulate or move that person in the right direction.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
A brute awe as you,
a Metallic hag entity, eat us.
<jj********@yahoo.com> wrote in message
news:11**********************@t39g2000cwt.googlegr oups.com...I said road to nowhere based on what I found on the net regarding deep
copying (copy constructor) for ICloneable and also item 28 in Effective
C# "Avoid ICloneable".

Further discussions in house have revealed a quite neat solution:
- derive a class from the XSD generated class
- implement ICloneable on the derived class
- our Clone() method uses serialization & deserialization create new
copy

Does this sound a reasonable solution? The only downsides I can see
might be in terms of performance, in that we're using serialization
twice and there's boxing/unboxing going on because Clone returns
System.Object.

Boz

PS Referenced posting regarding deep copy and ICloneable being road to
nowhere, from another MVP like yourself:
http://groups.google.co.uk/group/mic...e&rnum=5&hl=en

Mar 2 '06 #4
Kevin,

I was hoping to talk through ideas to help me reach a decision on how
to achieve a "deep copy", but perhaps discussing that here is going to
be a road to nowhere :-)

Anyway, I've been scouring the net on this subject and its very clear
that the subject is not clear cut. Check out this debate on obseleting
ICloneable. It expresses the issues involved and the differing
opinions.

http://www.dotnet247.com/247referenc...03/125427.aspx

Boz

Mar 3 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
4477
by: Arthur | last post by:
A bit inspired by the decorator discussions, I'm trying to tackle something I had been avoiding. Essentially I am trying to create a non-destructive tranformation of an instance of a class - is...
1
6030
by: Jesper Denmark | last post by:
Hi, Is deep serialization possible using XML. Know any good tutorials?. By deep serialization I mean the ability that an object being serialized automatically serialize its members. E.g ...
2
5547
by: Jesper Denmark | last post by:
Hi, I've had a lot of trouble finding a good tutorial showing deep serialization in XML in C#. All samples I've found shows merely serialization of a single class. If I want to serialize members...
1
2614
by: Opa | last post by:
Hi I'm having problem serialization an object instance which contains a public property on the object type My object hierarchy is many levels deep, so for simplicty I created to following which...
1
2852
by: Opa | last post by:
Hi I'm having problem serialization an object instance which contains a public property on the object type My object hierarchy is many levels deep, so for simplicty I created to following which...
2
1464
by: fperfect13 | last post by:
The problem is like this: I have a class C1 which has a private field "private Graph graph;" accesed by a property(C1.Graph). The class Graph is serializable but when i pass to a method that...
1
2318
by: oDDskOOL | last post by:
I realized today that the Hashtable.Clone only produces a shallow copy... that makes me go mad that M$ doesn't even provide a deep copy ctor for the Hashtable class ! mighty tech ducks might...
15
2372
by: Jacques | last post by:
Hi I am an dotNet newby, so pardon my ignorance. I am looking for a method of saving/copying a managed class to a stream/file WITHOUT saving the object's state, eg. if I have a ref class with...
5
3988
by: Henry Jones | last post by:
I read a bit on Serialization and came up with the following definition: The System.Runtime.Serialization namespace contains classes that can be used for serializing and deserializing objects....
0
7353
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
7468
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5596
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5023
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...
0
4689
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3180
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3170
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
747
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
401
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.