ObjectNetworkAttacher vs. DataContractSerializer

Thread is closed for posting
7 posts, 1 answers
  1. NemFontos
    NemFontos avatar
    10 posts
    Member since:
    Sep 2012

    Posted 21 Mar 2009 Link to this post

    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?

  2. NemFontos
    NemFontos avatar
    10 posts
    Member since:
    Sep 2012

    Posted 21 Mar 2009 Link to this post

    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.)

  3. NemFontos
    NemFontos avatar
    10 posts
    Member since:
    Sep 2012

    Posted 21 Mar 2009 Link to this post

    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?

  4. Jan Blessenohl
    Admin
    Jan Blessenohl avatar
    707 posts

    Posted 23 Mar 2009 Link to this post

    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.
  5. NemFontos
    NemFontos avatar
    10 posts
    Member since:
    Sep 2012

    Posted 23 Mar 2009 Link to this post

    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

  6. Answer
    Jan Blessenohl
    Admin
    Jan Blessenohl avatar
    707 posts

    Posted 23 Mar 2009 Link to this post

    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.
  7. NemFontos
    NemFontos avatar
    10 posts
    Member since:
    Sep 2012

    Posted 23 Mar 2009 Link to this post

    Great, thank you!
Back to Top