The EntitySpaces Community

Share and learn about the EntitySpaces Architecture.
Welcome to The EntitySpaces Community Sign in | Join | Help
in
Home Forums Photos

Sharing a Couple of Serialization Helpers

Last post 12-23-2007, 3:32 PM by TrevorW. 2 replies.
Sort Posts: Previous Next
  •  12-23-2007, 2:05 PM 7332

    Sharing a Couple of Serialization Helpers

    I thought I would share these helpers for serializing objects and queries using ES. I use the BinarySerializer for querries and the XMLSerializer is for serialzing the domain objects.

    BINARY:

           public class BinarySerializer {

     

                  private BinarySerializer() {}

     

                  #region Serialize

                  /// <summary>

                  /// Serializes the supplied object.

                  /// </summary>

                  /// <param name="objectToSerialize">The object to serialize.</param>

                  /// <returns>Returns a byte array representing the object.</returns>

                  public static byte[] Serialize(Object objectToSerialize) {

                         BinaryFormatter bf = new BinaryFormatter();

                         using (MemoryStream ms = new MemoryStream()) {

                               bf.Serialize(ms, objectToSerialize);

                                return ms.ToArray();

                         }

                  }

                  #endregion

     

                  #region Deserialize<T>

                  /// <summary>

                  /// Deserializes the supplied byte array.

                  /// </summary>

                  /// <typeparam name="T">The type of cast to be perfomed when deserializing.</typeparam>

                  /// <param name="serializedObject">The serialized object.</param>

                  /// <returns>Returns the type T specified.</returns>

                  public static T Deserialize<T>(byte[] serializedObject) {

                         BinaryFormatter bf = new BinaryFormatter();

                         using (MemoryStream ms = new MemoryStream(serializedObject)) {

                               return (T)bf.Deserialize(ms);

                         }

                  }

                  #endregion

           }

    XML:

     

           public class XMLSerializer {

     

                  #region Constructors

     

                  #region XMLSerializer()

                  /// <summary>

                  /// Initializes a new instance of the <see cref="T:XMLSerializer"/> class.

                  /// </summary>

                  public XMLSerializer() { }

                  #endregion

     

                  #endregion

     

                  #region Static Methods

     

                  #region static string SaveToString(Object objectToSave)

                  /// <summary>

                  /// Saves or serializes the object into an XML string.

                  /// </summary>

                  /// <param name="objectToSave">The object to save.</param>

                  /// <returns>Returns an XML String representing the object.</returns>

                  public static string SaveToString(Object objectToSave) {

                         //Create serializer object using the type name of the Object to serialize.

                         XmlSerializer xmlSerializer = new XmlSerializer(objectToSave.GetType());

                         using (StringWriter writer = new StringWriter()) {

                               xmlSerializer.Serialize(writer, objectToSave);

                               return writer.ToString();

                         }

                  }

                  #endregion

     

                  #region static T LoadFromString<T>(string serializedXml)

                  /// <summary>

                  /// Loads the object T from the supplied XML string.

                  /// </summary>

                  /// <typeparam name="T"></typeparam>

                  /// <param name="serializedXml">The serialized XML.</param>

                  /// <returns>Returns the type of object T.</returns>

                  public static T LoadFromString<T>(string serializedXml) {

                         XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));

                         using (StringReader reader = new StringReader(serializedXml)) {

                               return (T)xmlSerializer.Deserialize(reader);

                         }

                  }

                  #endregion

     

                  #endregion

     

           }

     

  •  12-23-2007, 2:35 PM 7334 in reply to 7332

    Re: Sharing a Couple of Serialization Helpers

    Very cool, post a snippet too on how you use them ...Yes

    EntitySpaces | Twitter | BLOG | Please honor our Software License
  •  12-23-2007, 3:32 PM 7335 in reply to 7334

    Re: Sharing a Couple of Serialization Helpers

    Okay,

    *Note all examples are based on the database having a table named, 'AppUser' - the logic applies for ES types - just the names change. 

    Example 1 (BinarySerializer - Serializing an ES query - Client-Side):

    ** FIRST WE CREATE THE QUERY (note we also join a second table named InternalLocation within the query)...

    BusinessObjects.AppUserQuery users = new BusinessObjects.AppUserQuery("u");

    BusinessObjects.InternalLocationQuery locations = new BusinessObjects.InternalLocationQuery("l");

    users.InnerJoin(locations).On(users.AssignedLocation == locations.Oid);

    users.Where(locations.Name.Equal("Portland"));

     

     ** HERE WE USE THE BINARY HELPER TO SERIALIZE THE QUERY

    byte[] serializedUsers = BinarySerializer.Serialize(users);

    Example 2 (BinarySerializer - Deserializing an ES query - Server-Side):

                  public void GetAppUserListByQuery(byte[] query) {

                      ** HERE WE DESERIALIZE THE QUERY SERVER-SIDE USING THE BINARY HELPER

                      BusinessObjects.AppUserQuery deserializedUsers = BinarySerializer.Deserialize<BusinessObjects.AppUserQuery>(query);

                      BusinessObjects.AppUserCollection coll = new AppUserCollection();

                      if (coll.Load(deserializedUsers)) {

                          BusinessObjects.AppUserCollectionProxyStub emps = new AppUserCollectionProxyStub(coll);

                           callback.SendUserList(requestId, XMLSerializer.SaveToString(emps));

                      }
                  
    }

    Example 3 (XMLSerializer - Serializing an ES Collection - Server-Side):

                  public void GetAppUserListByQuery(byte[] query) {
                      BusinessObjects.AppUserQuery deserializedUsers = BinarySerializer.Deserialize<BusinessObjects.AppUserQuery>(query);

                      BusinessObjects.AppUserCollection coll = new AppUserCollection();

                      if (coll.Load(deserializedUsers)) {

                          BusinessObjects.AppUserCollectionProxyStub emps = new AppUserCollectionProxyStub(coll);
                          ** HERE WE USE THE XMLSERIALIZER SERVER-SIDE

                           callback.SendUserList(XMLSerializer.SaveToString(emps));

                      }
                  
    }

    Example 4 (XMLSerializer - Deserializing an ES Collection - client-Side):

                  public void SendUserList(Guid requestId, string users) {
                         ** HERE WE USE THE XMLSERIALIZER TO DESERIALIZE THE COLLECTION

                         Client.AppUserCollectionProxyStub deserializedUsers = XMLSerializer.LoadFromString<Client.AppUserCollectionProxyStub>(users);

                         foreach (Client.AppUserProxyStub user in deserializedUsers.Collection) {

                               MessageBox.Show(string.Format("User: {0}", user.FirstName));

                         }

                  }

    I hope the examples are easy to understand, I tried to keep them as simple as possible.

View as RSS news feed in XML