473,416 Members | 1,850 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,416 software developers and data experts.

How do you serialize object references?

I understand the concept.

Serialization of a class will add all the sub-objects of the class to
the stream if there are also serializible.

So say I have:

[Serialize]
class Author
{
public string Name = string.empty;
void Author(author)
{
Name = author;
}
}

[Serialize]
class Book
{
public Author author = null;
void Book(string authorname)
{
author = new Author(authorname);
}
}

void Main()
{
Book mybook = new book("John Smith");
// here I would open a stream and serialize the book collection.
}
Now, when I read back the stream and populate the book collection there
is indeed a reference to the author, since it saved the information,
however, what if I tried this:

void Main()
{
Book mybook = new book("John Smith");
Book mybook2 = mybook;
// here I would open a stream and serialize the book collection.
// then I read back both books, which should both contain a ref
// to the single author entry.
}

upon reading back the books from the stream there are TWO copies of
Author, not just a reference to the single entry I started with

So, if I change the author name on one book, it will not update the
second book.

Is there a way to have the object's runtime references persist between
sessions given that they are saved/serialized to a stream and read
back?

It would seem that the serialization process copies each object
reference by value to the stream when serializing. Is there a work
around or is this something beyond the capabilites of serialization?

Nov 16 '05 #1
2 8850
The SoapFormatter and BinaryFormatter classes support serializing and
deserializing an entire object graph. In your case, the graph would consist
of multiple books with references to the same author. The built-in
serialization/deserialization logic detects and handles this sort of
situation. The problem you're running into is that you are serializing your
books separately from your authors, thus giving the serialization logic no
point to detect that one of your authors is shared among multiple books.

My solution to this sort of situation is typically, at the application
level, to create a "UserDocument" class which is a simple container for all
the data that the user might be working with in the application. In your
case it would be something like:

[Serializable]
class UserDocument {
Books[] books;
Authors[] authors;
}

That's the thing you serialize, thus allowing the formatter to analyze your
books and your authors together, to detect the re-use of an author, and save
him only once. (and, even better, recreate him only once during
deserialization).

The sequence of actions your application then takes is something like this:

1. Create a UserDocument object at app load time, in response to file->new,
etc.:
myDocument = new UserDocument();
2. the user does stuff, turning myDocument into something worth saving.
3. BinaryFormatter b = new BinaryFormatter(new
Stream(mySaveFileDialog.Filename));
b.Serialize(theUserDocument);

The only other caveat I would offer, although you didn't ask explicitly, is
to make sure that if your Book and Author types use events to communicate
with each other and with your application's UI, make sure to mark the event
fields with [field:NonSerialized]; frequently the event objects your data
objects are using contain refererences to non-serializable classes
(typically, your main Form class), by virtue of holding references to the
event handlers that have subscribed to them. If the Serialize() method
encounters one of these, it will throw a run-time error because you're
trying to serialize a non-serializable class. Then, of course, you'll need
to create a mechanism to re-subscribe the event proper event handlers when
the object graph is restored from its serialized form. There's an interface
you implement in order to get a callback after deserialization happens so
you can fix up the values of any non-serialized fields, IDeserialization or
something like that, but my reference books are not where I am right now so
you'll have to look that one up for yourself.

<fi***@onarom.com> wrote in message
news:11**********************@c13g2000cwb.googlegr oups.com...
I understand the concept.

Serialization of a class will add all the sub-objects of the class to
the stream if there are also serializible.

So say I have:

[Serialize]
class Author
{
public string Name = string.empty;
void Author(author)
{
Name = author;
}
}

[Serialize]
class Book
{
public Author author = null;
void Book(string authorname)
{
author = new Author(authorname);
}
}

void Main()
{
Book mybook = new book("John Smith");
// here I would open a stream and serialize the book collection.
}
Now, when I read back the stream and populate the book collection there
is indeed a reference to the author, since it saved the information,
however, what if I tried this:

void Main()
{
Book mybook = new book("John Smith");
Book mybook2 = mybook;
// here I would open a stream and serialize the book collection.
// then I read back both books, which should both contain a ref
// to the single author entry.
}

upon reading back the books from the stream there are TWO copies of
Author, not just a reference to the single entry I started with

So, if I change the author name on one book, it will not update the
second book.

Is there a way to have the object's runtime references persist between
sessions given that they are saved/serialized to a stream and read
back?

It would seem that the serialization process copies each object
reference by value to the stream when serializing. Is there a work
around or is this something beyond the capabilites of serialization?

Nov 16 '05 #2

"Marcos Stefanakopolus" <ta*******@hotmail.com> wrote in message
news:%2***************@TK2MSFTNGP15.phx.gbl...
The SoapFormatter and BinaryFormatter classes support serializing and
<fi***@onarom.com> wrote in message
news:11**********************@c13g2000cwb.googlegr oups.com...
Serialization of a class will add all the sub-objects of the class to
the stream if there are also serializible.


Interesting question and a great answer.

- Michael S
Nov 16 '05 #3

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

Similar topics

5
by: David Sworder | last post by:
Hi, I've created a UserControl-derived class called MyUserControl that is able to persist and subsequently reload its state. It exposes two methods as follows: public void Serialize(Stream...
5
by: andrewcw | last post by:
I have an object to serialize. TextWriter writer = new StreamWriter("test.xml"); serializer.Serialize(writer,obj); writer.Close(); but below does not, why ?? I have a file that I will have...
3
by: Pol Bawin | last post by:
A class has a private field of type IWizard (An interface) and a public property to access it. When I try to serialize the Geometry class in XML, i have an error but it works in Binary Can...
10
by: Dan | last post by:
All I Am Attempting To Serialize An Object To An XML File. Here Is The Code For That public string SaveNewSurvey( MutualSurveyObject mso_TempObject, int i_JobID ) { string s_RootFileName;...
3
by: MAY | last post by:
Hi, I have a problem about serialize the form controls. I wrote a test program to test serialize a from but fail (->An unhandled exception of type...
1
by: Dominic | last post by:
I am using XmlSerializer to serialize my object. The object has various public members which are object references. This all works fine. However, one member (mShape) is declared as a reference...
3
by: Jerry | last post by:
Hi, I have a class like the following: class A { private B _b; A (B b) { _b = b; } ...
7
by: Ben Amada | last post by:
I've created a class that I need to store in ViewState. However when I try to store it in ViewState, I get the following error: "The type 'solution.pe2' must be marked as Serializable or have a...
8
by: cd~ | last post by:
I can provide a test app, the news server won't allow me to post the files because they are too large (93KB and 1.2KB) I downloaded the ESRI ArcXml schema and generated the classes from the...
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:
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
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
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,...
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...
0
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...

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.