Telerik OpenAccess Classic

Telerik OpenAccess ORM Send comments on this topic.
How to: Detach Objects from an Object Scope
Programmer's Guide > OpenAccess ORM Classic (Old API) > OpenAccess Tasks > Working with Objects > How to: Detach Objects from an Object Scope

Glossary Item Box

This documentation article is a legacy resource describing the functionality of the deprecated OpenAccess Classic only. The contemporary documentation of Telerik OpenAccess ORM is available here.

Persistent types generated by Telerik OpenAccess ORM support serialization. When you serialize an object to a binary stream, all related objects currently loaded into the object scope will also be serialized.

To detach and then attach persistent instances to an object context, the classes must meet one of the following conditions:

  • Expose public Version and Id fields
  • Implement the IDataObjectKey interface

In this example, a SerializeToBinary and SerializeToXml method queries for the Person object for the specified last name value, and returns a binary MemoryStream.

C# Copy Code
   [Serializable,Persistent(IdentityField="id",VersionField="version")]
   
public class Person
   {
       
private Guid id;
       
private int version;
       
private string name;
       
public string Name
       {
           get
           {
               
return name;
           }
           set
           { name = value;
           }
       }
       
public int Version
       {
           get {
return version; }
           set { version = value; }
       }
       
public Guid Id
       {
           get {
return id; }
           set { id = value; }
       }
      
   }
VB.NET Copy Code
   <Serializable, Persistent(IdentityField:="id",VersionField:="version")> _
   Public Class Person
  Private id_Renamed As Guid
  Private version_Renamed As Integer
  Private name_Renamed As String
  Public Property Name() As String
   Get
    Return name_Renamed
   End Get
   Set(ByVal value As String)
    name_Renamed = value
   End Set
  End Property
  Public Property Version() As Integer
   Get
    Return version_Renamed
   End Get
   Set(ByVal value As Integer)
    version_Renamed = value
   End Set
  End Property
  Public Property Id() As Guid
   Get
    Return id_Renamed
   End Get
   Set(ByVal value As Guid)
    id_Renamed = value
   End Set
  End Property
   End Class
A helper class named ObjectNetworkAttacher is used in this example. The class is a part of the Telerik OpenAccess ORM distributable and can be found under the [Program Files]\Telerik\OpenAccess ORM\src\Helper folder.
C# Copy Code
       public void SerializationExample()
       {
           
//Getting the scope OpenAccessNorthwindMapping
           
IObjectScope scope = ObjectScopeProvider1.GetNewObjectScope();
          
           
// Adding a new Person instance to the scope
           
scope.Transaction.Begin();
           Person person =
new Person();
           person.Name =
"John";
           person.Id = Guid.NewGuid();
           scope.Add(person);
           scope.Transaction.Commit();
           
// serializing the persistent instance
           
scope.Transaction.Begin();
           MemoryStream serialized = SerializeToStream(person);
           scope.Transaction.Commit();
           
// now we will dispose the scope for the purpose of this example
           
scope.Dispose();
           scope =
null;
           
// DESERIALIZE
          
           
//Getting the scope OpenAccessNorthwindMapping
           
scope = ObjectScopeProvider1.GetNewObjectScope();
           Person personNew = DeserializeFromStream(serialized);
           personNew.Name =
"changed";
           
// attach the deserialized object back to the scope
           
scope.Transaction.Begin();
           personNew = ObjectNetworkAttacher.AttachXML(scope, personNew)
as Person;
           scope.Transaction.Commit();
       }
       
private static MemoryStream SerializeToStream(Person person)
       {
           XmlSerializer serializer =
new XmlSerializer(typeof(Person));
               MemoryStream stream =
new MemoryStream();
           
try
           {        
            
serializer.Serialize(stream, person);
           }
           
catch (SerializationException ex)
           {
               
throw new ApplicationException("The object graph could not be serialized", ex);
           }
           
// Return the streamed object graph.
           
return stream;
       }
       
private static Person DeserializeFromStream(MemoryStream stream)
       {
           XmlSerializer serializer =
new XmlSerializer(typeof(Person));
           Person person;
           
try
           {        
               
stream.Seek(0, SeekOrigin.Begin);
               person = serializer.Deserialize(stream)
as Person;
            serializer.Serialize(stream, person);
           }
           
catch (SerializationException ex)
           {
               
throw new ApplicationException("The object graph could not be deserialized", ex);
           }
           
// Return the streamed object graph.
           
return person;
       }
VB.NET Copy Code
  Public Sub SerializationExample()
   'Getting the scope OpenAccessNorthwindMapping
   Dim scope As IObjectScope = ObjectScopeProvider1.GetNewObjectScope()
   ' Adding a new Person instance to the scope
   scope.Transaction.Begin()
   Dim person As New Person()
   person.Name = "John"
   person.Id = Guid.NewGuid()
   scope.Add(person)
   scope.Transaction.Commit()
   ' serializing the persistent instance
   scope.Transaction.Begin()
   Dim serialized As MemoryStream = SerializeToStream(person)
   scope.Transaction.Commit()
   ' now we will dispose the scope for the purpose of this example
   scope.Dispose()
   scope = Nothing
   ' DESERIALIZE
   'Getting the scope OpenAccessNorthwindMapping
   scope = ObjectScopeProvider1.GetNewObjectScope()
   Dim personNew As Person = DeserializeFromStream(serialized)
   personNew.Name = "changed"
   ' attach the deserialized object back to the scope
   scope.Transaction.Begin()
   personNew = TryCast(ObjectNetworkAttacher.AttachXML(scope, personNew), Person)
   scope.Transaction.Commit()
  End Sub
  Private Shared Function SerializeToStream(ByVal person As Person) As MemoryStream
   Dim serializer As New XmlSerializer(GetType(Person))
    Dim stream As New MemoryStream()
   Try
    serializer.Serialize(stream, person)
   Catch ex As SerializationException
    Throw New ApplicationException("The object graph could not be serialized", ex)
   End Try
   ' Return the streamed object graph.
   Return stream
  End Function
  Private Shared Function DeserializeFromStream(ByVal stream As MemoryStream) As Person
   Dim serializer As New XmlSerializer(GetType(Person))
   Dim person As Person
   Try
    stream.Seek(0, SeekOrigin.Begin);
    person = TryCast(serializer.Deserialize(stream), Person)
   Catch ex As SerializationException
    Throw New ApplicationException("The object graph could not be deserialized", ex)
   End Try
   ' Return the streamed object graph.
   Return person
  End Function