2 Replies - 11296 Views - Last Post: 17 September 2012 - 10:05 AM

#1 CharlieMay   User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1734
  • View blog
  • Posts: 5,710
  • Joined: 25-September 09

I'd like to hear your ideas on this.

Posted 17 September 2012 - 08:41 AM

I posted this here because I'm not really looking for any code but more your thoughts on if this is the wrong path in what I'm doing.

I have created a couple of classes that I am serializing to XML and storing in a database so that I can bring them back out as needed as the object. I have this working fine. But then I got this idea to create a method to .Serialize in the class instead of passing the object to another routine that performs the serialization.
Public Class LaborEntry
    Public Property Items As New List(Of LaborItem)

    Public Function Serialize() As String
        Dim xs As XmlSerializer = New XmlSerializer(MyBase.GetType())
        Dim sw As New StringWriter
        xs.Serialize(sw, Me)
        Return sw.ToString
    End Function
End Class

Public Class LaborItem
    Public Property Description As String
    Public Property ManHours As Double

    Public Sub New()

    End Sub

    Public Sub New(desc As String, qty As Double)
        _Description = desc
        _ManHours = qty
    End Sub
End Class
This works great. I can
Dim le as New LaborEntry
Dim li as LaborItem
li = New LaborItem("Batching", 2.5)
li = New LaborItem("Pouring", 4)


'Now I can just use something like
TextBox1.Text = le.Serialize
cmd.Parameters.AddWitValue("@LaborInfo", le.Serialize)

And then I got a hair-brained idea, why not add a DeSerialize Method and came up with this:
   Public Function Deserialize(xml As String) As LaborEntry
        Dim xs As New XmlSerializer(MyBase.GetType())
        Dim sr As New StringReader(xml)
        Return CType(xs.Deserialize(sr), LaborEntry)
    End Function

This is inside the LaborEntry Class and it works but it doesn't feel right. To use it I have to do something like:
Dim LaborInfo as New LaborEntry
LaborInfo = LaborInfo.Deserialize(TextBox1.Text)

At this point. LaborInfo's properties holds all the information from the XML string that is in TextBox1.Text (in the application, I would pass the field from the database that has this information).

Of course, I've not done any error checking or try...catch routines as I am just testing this code to see if it's practical.

Is this a good OOP solution for what I'm doing or what do you suggest?

Is This A Good Question/Topic? 0
  • +

Replies To: I'd like to hear your ideas on this.

#2 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6536
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: I'd like to hear your ideas on this.

Posted 17 September 2012 - 09:01 AM

The biggest design flaws I see is that this is still very tightly bound to the GUI. And the serializing is done within the class. So if you make a new class you have to import (copy/paste) your serialize/deserialize code into to.

Two main tenants of OOP is to separate GUI from data and to not duplicate code. You've broken both of those.

It would be better to make a single class outside of the entire project that does serialize/deserialize then you can send any of your objects to it. One method for each direction. One place to update if you make a change. This could be part of your own utility namespace. A project that you use in all your projects for common utilitarian needs like this. After all, you're going to be using serialization in your next project, and the next one, and the next one. So if you make a project/namespace of CharlieMay and put this sort of thing in it you can use it for the rest of your life without actually duplicating code. It then grows to:

And so on. You just build up your own namespace much like the .NET framework. There's nothing magic about a namespace/framework, just putting in the time to build it.

I wrote this article to better cover the separation of the data and GUI. Hope it helps.
Separating data from GUI - PLUS - serializing the data to XML
Was This Post Helpful? 1
  • +
  • -

#3 AdamSpeight2008   User is offline

  • MrCupOfT
  • member icon

Reputation: 2298
  • View blog
  • Posts: 9,535
  • Joined: 29-May 08

Re: I'd like to hear your ideas on this.

Posted 17 September 2012 - 10:05 AM

eclipsed4utoo has written a C# snippet generic Generic XML Serialization Methods. It wouldn't take much to convert and make an extension method.
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1