This is a migrated thread and some comments may be shown as answers.

ObjectNetworkAttacher vs. DataContractSerializer

6 Answers 176 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
NemFontos
Top achievements
Rank 1
NemFontos asked on 21 Mar 2009, 08:58 PM

Hi,

I'm writing some proof-of-concept code using ObjectNetworkAttacher and your oneexample.zip.

Instead of XmlSerializer (cause of IList bug) I'm using NetContractSerializer/DataContract serializer. The last one work well: I can deserialize and serialize objects with IList (modified from oneexample.zip):

  byte[] serializedObject = null;  
  using (MemoryStream stream = new MemoryStream())  
  {  
  DataContractSerializer serializer = new DataContractSerializer(typeof(B));  
  serializer.WriteObject(stream, b);  
  serializedObject = stream.GetBuffer();  
  } 

But.

When I try to commit my changes back to the database

            // deserialize b  
            MemoryStream stream2 = new MemoryStream(serializedObject);              
            DataContractSerializer serializer2 = new DataContractSerializer(typeof(B));  
            B deserializedB = (B)serializer2.ReadObject(stream2);  
 
            deserializedB.Name = "changed";  
 
            // attach   
            scope = ObjectScopeProvider1.GetNewObjectScope();  
            scope.Transaction.Begin();  
            B attachedB = (B) ObjectNetworkAttacher.AttachXML(scope, deserializedB);  
            scope.Transaction.Commit(); // throws exception

I've got "A conflicting object was added remotely and concurrently in the database." exception. ObjectNetworkAttacher thinks that this object is "remotenew".

I think there is a version member issue, because the version propery is 0 and not 1 - but why?

6 Answers, 1 is accepted

Sort by
0
NemFontos
Top achievements
Rank 1
answered on 21 Mar 2009, 09:14 PM

I found an interessing behavior

            scope.Transaction.Begin();  
 
            // serialize a;  
            MemoryStream stream = new MemoryStream();  
            DataContractSerializer serializer = new DataContractSerializer(typeof(B));  
            serializer.WriteObject(stream, b);  
            byte[] serializedObject = stream.GetBuffer();  
                          
            // log  
            using (StreamWriter sw = new StreamWriter("temp.xml"))  
            {  
                sw.Write(Encoding.ASCII.GetString(serializedObject));  
            }  
 
            // now we can forget the scope  
            scope.Transaction.Commit(); 

The version field is sometimes 0, and sometimes 1. (In the previous step I saved the object already.)

0
NemFontos
Top achievements
Rank 1
answered on 21 Mar 2009, 10:16 PM

Aha!

If I'm getting the value of .Version explicitly (i.e. int v = object.Version) then the value of this property stay there. If I'm only run the serializer on this object - the real value is missing! (I'm getting just default 0)

The question: how is possible toforce to fill the Version property? Some VEnhance trick?

0
Jan Blessenohl
Telerik team
answered on 23 Mar 2009, 08:10 AM
Hi NemFontos,
In the forward mapping wizard you can choose your new field as version field by selection the class in the tree.

You can also modify the persistent attribute by hand

[Telerik.OpenAccess.Persistent(VersionField="v"]

Regards,
Jan Blessenohl
the Telerik team

Check out Telerik Trainer , the state of the art learning tool for Telerik products.
0
NemFontos
Top achievements
Rank 1
answered on 23 Mar 2009, 12:31 PM

Hi Jan,

thank you. Sadly the value of "version" is still 0 when the DataContractSerializer reads it. (If I "cheat" with an explicit read, OA fills it).

Serialized objects:

<B>  
<alist>  
   <A>  
     <id>66d4daee-921b-47f7-9d52-ce9c6666ad79</id>  
     <name>Jan</name>  
     <version>0</version>  
     </A>  
   </alist>  
   <date>2009-03-22T00:43:07.377</date>  
   <id>30d89f0f-4727-4239-8053-80e84c85bd72</id>  
   <name>Bela</name>  
   <version>0</version>  
 </B> 

But b.Version == 1 is true in runtime (b is instance of B)

Is there any attribute/configuration to turn off the version lazy-loading?

Thank you,

NemFontos

0
Accepted
Jan Blessenohl
Telerik team
answered on 23 Mar 2009, 02:47 PM
Hi NemFontos,
Before you pass the object to the serializer you should call objectscope.Retrieve() on that object. This will load all values to the object.

All the best,
Jan Blessenohl
the Telerik team

Check out Telerik Trainer , the state of the art learning tool for Telerik products.
0
NemFontos
Top achievements
Rank 1
answered on 23 Mar 2009, 05:01 PM
Great, thank you!
Tags
General Discussions
Asked by
NemFontos
Top achievements
Rank 1
Answers by
NemFontos
Top achievements
Rank 1
Jan Blessenohl
Telerik team
Share this question
or