Serialization And Deserialization with Extensions

Have you played with extensions? Recently I had the task of doing some serialization and deserialization of some objects except I did not want it to be open to all objects. I wanted to limit it down to only objects that implemented a certain interface. Let's check out the code below. Extensions start with a signiture simliar to how we do parameters however the different by using the "this" keyword. This means that only objects that implement this interface, "IEntityBase" can use this extension. Serializing objects allows me to do a couple of things. First, to pass messages where I can control the format of XML, it is already done for me. Second, it allows me to persist the information form non .Net code through some process, and then deserialize so I can do more processing through code. The need for the below code originated with handling messages through service broker. Since the messages are XML and I have control of the format, this was an easy way to pass the objects. "Well cool Bayer, but what about complexed objects"? Taken care of! It handles them too!

Now I can write code like this...

The XML produced from the above code will look like this

<WebGoLive xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <TriggeredFrom>NotSupplied</TriggeredFrom>
  <ProductID>000155</ProductID>
  <GoLiveDate>2008-12-22T10:57:21.7362381-05:00</GoLiveDate>
</WebGoLive>

Cool, so how do I get back the object? Well this code is a little more generalized since I need the "type" of object to deserialize. No need to wrap into an extension, but there are no limitations. Here is the code

So putting it all to together will look like this

 

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Posted by: BayerWhite
Posted on: 12/22/2008 at 3:36 AM
Actions: E-mail | Kick it! | DZone it! | del.icio.us
Post Information: Permalink | Comments (15) | Post RSSRSS comment feed

Comments

Comments are closed