Serialization


Author
Message
Edhy Rijo
E
StrataFrame VIP (4.7K reputation)StrataFrame VIP (4.7K reputation)StrataFrame VIP (4.7K reputation)StrataFrame VIP (4.7K reputation)StrataFrame VIP (4.7K reputation)StrataFrame VIP (4.7K reputation)StrataFrame VIP (4.7K reputation)StrataFrame VIP (4.7K reputation)StrataFrame VIP (4.7K reputation)
Group: StrataFrame Users
Posts: 2.4K, Visits: 23K
Thanks a lot Trent, that will get me started.

Edhy Rijo

Trent Taylor
Trent Taylor
StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)
Group: StrataFrame Developers
Posts: 6.6K, Visits: 6.9K
Well, it would just be a matter of creating your header in a string format, etc.  Then place that header at the beginning.  For example, I might have an enum that tells me the contents of the byte array in the first element.  In this case, a "1" would indicate a business object.  You could make this as long as you need since you would be in control of the header.  The second part of the header may indicate, in bytes, the length of the serialized BO.

Dim header As String = "1|"
Dim bo() As Byte
Dim mem As New System.IO.MemoryStream()
Dim headerInBytes() As Byte

'-- Serialize the BO to a byte array
bo = MyBO.SerializeToByteArray()

'-- Create the header
header &= bo.Length.ToString()

'-- Place an ending tag on the header
header &= "|"

headerInBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(header)

'-- Create the full byte array
mem.Write(headerInBytes, 0, headerinBytes.Length)

'-- Add the BO to the byte array
mem.Write(bo, 0, bo.Length)

'-- Convert the stream into a byte array
mem.Seek(0, Begin)
Dim r() As Byte = mem.ToArray()

So when you get the byte array on the server side (or whereever) you already know what to expect in the header.  So you can parse it out of the array (convert it back into a string or whatever you expect) and then you know what to do with the rest of the array.

Edhy Rijo
E
StrataFrame VIP (4.7K reputation)StrataFrame VIP (4.7K reputation)StrataFrame VIP (4.7K reputation)StrataFrame VIP (4.7K reputation)StrataFrame VIP (4.7K reputation)StrataFrame VIP (4.7K reputation)StrataFrame VIP (4.7K reputation)StrataFrame VIP (4.7K reputation)StrataFrame VIP (4.7K reputation)
Group: StrataFrame Users
Posts: 2.4K, Visits: 23K
Trent L. Taylor (03/15/2009)
Hmmm....it is going to be a lot mroe difficult to add it at the end as you will not know the length of the serialized BO.  That is why a header would work much better as you would know what to expect that tells you what is further down the stream.

Hi again,

Thanks, but could you provide a quick sample code on how to add this byte to the header?  sorry, but I am really lost here w00t

Edhy Rijo

Trent Taylor
Trent Taylor
StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)
Group: StrataFrame Developers
Posts: 6.6K, Visits: 6.9K
Hmmm....it is going to be a lot mroe difficult to add it at the end as you will not know the length of the serialized BO.  That is why a header would work much better as you would know what to expect that tells you what is further down the stream.
Edhy Rijo
E
StrataFrame VIP (4.7K reputation)StrataFrame VIP (4.7K reputation)StrataFrame VIP (4.7K reputation)StrataFrame VIP (4.7K reputation)StrataFrame VIP (4.7K reputation)StrataFrame VIP (4.7K reputation)StrataFrame VIP (4.7K reputation)StrataFrame VIP (4.7K reputation)StrataFrame VIP (4.7K reputation)
Group: StrataFrame Users
Posts: 2.4K, Visits: 23K
Trent L. Taylor (03/14/2009)
Sure. If I were going to do this I would put it in the front of the byte array with any information that will tell me where the serialized BO starts, etc. So the beginning of the byte array would be a "header" that contained whatever information that you needed. This is why I mentioned that resetting the memory stream was not a safe thing to do as other people may be using the memory stream for other things.


Yes, but can you give me a sample code of how to add the info to the byte array? in my case I will enter the code at the end of the array and the communication library will take care of removing it from the transferred stream.

Edhy Rijo

Trent Taylor
Trent Taylor
StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)
Group: StrataFrame Developers
Posts: 6.6K, Visits: 6.9K
Sure.  If I were going to do this I would put it in the front of the byte array with any information that will tell me where the serialized BO starts, etc.  So the beginning of the byte array would be a "header" that contained whatever information that you needed.  This is why I mentioned that resetting the memory stream was not a safe thing to do as other people may be using the memory stream for other things.
Edhy Rijo
E
StrataFrame VIP (4.7K reputation)StrataFrame VIP (4.7K reputation)StrataFrame VIP (4.7K reputation)StrataFrame VIP (4.7K reputation)StrataFrame VIP (4.7K reputation)StrataFrame VIP (4.7K reputation)StrataFrame VIP (4.7K reputation)StrataFrame VIP (4.7K reputation)StrataFrame VIP (4.7K reputation)
Group: StrataFrame Users
Posts: 2.4K, Visits: 23K
Trent,

Another question about serialization, when we use this command:

pszOutMemByte = Me.bizConnectedClients1.SerializeToByteArray()

Is there a way to add some extra data to the pszOutMemByte variable?  With my communication library, I need to add and End Of Line character to the byte stream and have not found a way to do this, so the communication event will trigger when all data have arrive, so the question is how do I add these character to this byte array pszOutMemByte?



Edhy Rijo

Trent Taylor
Trent Taylor
StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)
Group: StrataFrame Developers
Posts: 6.6K, Visits: 6.9K
Since this my first time working with a MemoryStream, I wonder, should the BO.DeserializeBusinessObject(mem) method take care of repositioning the passed stream to the 0 position?

No.  This could cause all types of issues.  This is the responsibility of the developer.  The same issue would come up with a file.  Most of the time you are not trying to do all of this within the same segment of code, the difference here is that you wrote to it then turned right around and read from it...so in this instance you have to reset the pointer.

Edhy Rijo
E
StrataFrame VIP (4.7K reputation)StrataFrame VIP (4.7K reputation)StrataFrame VIP (4.7K reputation)StrataFrame VIP (4.7K reputation)StrataFrame VIP (4.7K reputation)StrataFrame VIP (4.7K reputation)StrataFrame VIP (4.7K reputation)StrataFrame VIP (4.7K reputation)StrataFrame VIP (4.7K reputation)
Group: StrataFrame Users
Posts: 2.4K, Visits: 23K
Trent L. Taylor (03/12/2009)
Yup, it worked.  You have to reposition the stream back to the 0 position, but it works like it should.

Trent, thanks a lot for testing this, the missing part was the code to reposition the stream back to the 0 position.

Dim mem As New System.IO.MemoryStream()
Me.CustomerBO1.SerializeToStream(mem)
mem.Seek(0, IO.SeekOrigin.Begin)
Dim desObj As CustomerBO = MicroFour.StrataFrame.Business.BusinessLayer.DeserializeBusinessObject(mem)

Since this my first time working with a MemoryStream, I wonder, should the BO.DeserializeBusinessObject(mem) method take care of repositioning the passed stream to the 0 position?

Edhy Rijo

Trent Taylor
Trent Taylor
StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)
Group: StrataFrame Developers
Posts: 6.6K, Visits: 6.9K
Yup, it worked.  You have to reposition the stream back to the 0 position, but it works like it should.

Dim mem As New System.IO.MemoryStream()
Me.CustomerBO1.SerializeToStream(mem)
mem.Seek(0, IO.SeekOrigin.Begin)
Dim desObj As CustomerBO = MicroFour.StrataFrame.Business.BusinessLayer.DeserializeBusinessObject(mem)


GO

Merge Selected

Merge into selected topic...



Merge into merge target...



Merge into a specific topic ID...




Similar Topics

Reading This Topic

Login

Explore
Messages
Mentions
Search