开发者

Confuse about which object should i assign the function to

i have the following two class which convert object into xml string

开发者_如何学运维should i do something like

class Person
{

   public string GetXml()
   {
       //return a xml string
   }  
}

or it is better to create another class which accept the person as a parameter and convert it into XML something like

class PersonSerializer 
{         
     public string Serialize(Person person)
     {
           // return a xml string
     }

 }

Thanks


The question to ask: What does a person know about XML?

The answer: nothing

So, third vote for a separate serializer.


Generally, the Serialize method should be on the class you want to serialize; that way, it can access all the private member variables that other classes can't access, and generally do things more efficiently


There is already a mechanism in .NET for XML serialisation of objects, have a look at this article for details on the attributes you can use to declaratively mark the aspects of your class you want to be serialised.

Your original question is actually asking whether to embed serialisation information into the class to which it pertains or to place it into a separate, though associated class. The advantage of the first approach is that the serialisation code is able to access private members directly and is tightly coupled with the class. The disadvantage is the serialisation code clouds the actual logic of the class — this becomes more apparent if you add binary serialisation too.

There is a actually a mechanism, called serialization surrogates, in .NET for separating serialisation logic out to a separate class. See part 3 of this article for details.


2nd option. You can create a generic serializer that can serialize different object/classes. Keep your classes as simple only, do what it should do. Serializing is not what a Person should do.


To be complete, you should use the IXmlSerializable interface. I.e:

class Person : IXmlSerializable
{
    #region IXmlSerializable Members

    public System.Xml.Schema.XmlSchema GetSchema()
    {
        // Provide Schema
        return null;
    }

    public void ReadXml(System.Xml.XmlReader reader)
    {
        // Read XML into Object
    }

    public void WriteXml(System.Xml.XmlWriter writer)
    {
        // Write XML here
    }

    #endregion

    // Added as example to what I have said below
    public override string ToString()
    {
        // Make XML String
        return "XML STRING";
    }
}

In response to your comment to TomTom:

yes i agree but i have seen something similar on .net classes itself Example: int int1 = 1; int1.ToString();

What you have seen here is overriding of the ToString() method. I updated the code above to illustrate its usage.


The .NET XmlSerializer can serialize any type. The serialized elements are all pulic read/write properties. Thus, the PersonXmlSerializer already exists ;-)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜