Serializing Foreign Keys

Serializing Foreign Keys

Old forum URL: forums.lhotka.net/forums/t/3065.aspx


pbevis posted on Thursday, June 21, 2007

Hi there,

I'm a bit of a CSLA newbie so I'm looking for some advise on the best way to model foreign key relationships with regard to serialization.

Say I have an Order BO which has a collection of OrderLine BOs.  Each OrderLine BO will need a reference to the parent Order BO in order to access some of the order properties like OrderID when it comes to persisting the OrderLine to the database.

This seems reasonable to me and should work when the Data Portal is local to the client but what happens when the Data Portal is on a remote server and the Order needs to be serialised?  I assume that I would need to mark the OrderLine's reference to the Order BO as non-serializable to avoid an infinite serialization loop.  However, does that mean that when the OrderLine reaches the remote server it no longer has a reference to the parent Order BO (As it was not sent to the server)?

I suppose that I could pass all the required information to the OrderLine when it is created so that it does not need a reference to Order but that’s surely moving away from OO design?

Does anyone have any advice on how to solve this problem?

Thanks in advance

Paul

dlutz52 replied on Thursday, June 21, 2007

Paul --

On page 124 of the VB version of Rocky's book, you will find this discussed. I do not know the page number for the C# version of his book. Just look for "parent objects" in the index.

Essentially, the child's member variable that holds the reference to the parent object is marked as <NonSerialized()> and will not be included in the serialization process.

Hope this helps.

David Lutz

pbevis replied on Friday, June 22, 2007

Hi David,

Thanks for your reply, that makes perfect sense now, I was unaware of the OnDeserialised attribute which solves my problem.

Cheers,

Paul

P.S I found the section on page 120 - 121 of the C# book. 

Copyright (c) Marimer LLC