views:

93

answers:

4

This is not properly a question but something more like a thought I had recently. I'm taking XmlAttribute to XmlSerialize a class as an example: you can set attributes to a class to choose which properties should be serialized, but the same thing can be done quite easy by implementing a teorical interface IXmlSerializable (it does exist something similar, I don't remember) and by overloading a method "Serialize" for that class which just call Serialize on properties you want to serialize (this.myProp1.Serialize()), same for Deserialize

So what I'm basically saying: isn't Attribute method a bit redundant? (I like it actually, but I don't find it logically different from an interface)

Thanks for any answer, as I've said this is just a thought... hopefully someone will find it interesting

Update 1: Well I explained myself in a wrong way, what I'm asking is "why should I choose attribute instead of an Interface (or opposite)", not exactly this specific case (I took serialization because was the first thing that pop out in my mind), by the way thanks for your answer because they are very interesting

+1  A: 

The programmatic method of implementing the interface may give a bit more control (and likely more speed), but is harder to create and maintain than the attribute method. I mostly use attributes.

Steven Sudit
This answer appears to be correct, so why was it downvoted?
Steven Sudit
+8  A: 

From the comments and downvote, maybe I should highlight my main point here: something that can save me hours of work (per type) and horrible code complexity is very much not redundant, but very, very welcome.


"quite easy"? OK; I'm pretty experienced at serialization, but the implementation for that is not what I call easy. Quite the contrary, in fact.

If you don't want to use attributes, there is an overload for XmlSerializer that allows you to configure it at runtime.

But I shudder whenever I hear "implement IXmlSerializable". The attribute approach is very quick and easy:

[XmlRoot("foo"), XmlType("foo")]
[XmlInclude(typeof(SuperFoo))]
public class Foo {
    public string X {get;set;}

    [XmlAttribute("y")]
    public int? Y {get;set;}

    [XmlElement("item")]
    public List<string> Items {get;set;}
}
public class SuperFoo : Foo {}

I challenge you to write a robust implementation of IXmlSerializable for that very simple example in under 2 hours... and remember that every line you write is a line you have to maintain.

Marc Gravell
@Marc, I was going to say the same thing, but I thought maybe I was stupid. Any time I've attempted to dive into IXmlSerializable, it seemed pretty darn labor-intensive.
Mike Mooney
@serhio - let me spell it out then; something that saves me many hours of work *per class* is very much not redundant.
Marc Gravell
You're probably not serious about that challenge. And as I'm sure you know (but it needes to be clear) the advantage of implementing the interface is that you can directly control the process. That is to say, you can version your XML structure far easier, and transition it between data structure changes. With the attribute method, you cannot.
Noon Silk
@silky - re directly contolling it; IMO you don't need that feature for the attribute approach to be useful; saving time is reason enough. I didn't touch on that aspect because it seems unnecessary if the question is demonstrating that the attribute approach isn't redundant. I would rate it "not redundant" even if the two were feature-identical.
Marc Gravell
@Marc: Sure, agreed. (I didn't down vote you by the way).
Noon Silk
@silky - and re transitioning versions; there are other ways of handling that too, that don't involve writing all the serialization code manually.
Marc Gravell
@sikly: How "the advantage of implementing the interface is that you *control* the process", the advantage of attributes is that you *don't control* it.
serhio
@Marc Right, I didn't say anything to the contrary. @serhio: Your comment is not highlighting a contradiction.
Noon Silk
@silky: No I highlighted that the *control* itself is not a good or bad thing. It can be or not desirable.
serhio
@serhio I don't know if there is some sort of translation problem here, but I don't know what you're talking about. Either way, I suspect the point is beyond meaningless to discuss, and I expect that if there is something you wish to resolve regarding this it can be the target of thought/research/new thread/other form of discussion. No more purpose to discussing it in comments here, IMHO.
Noon Silk
@silky: "I don't know what you're talking about." About control(customization) of the serialization, man. Sometimes you need, sometimes not to customize. Is it still unclear?
serhio
+1  A: 

You can select properties to (not) serialize with attributes. Implementation of interface is serialization by code.

serhio
+4  A: 

Well, from the best I can tell, they are logically different.

Implementing IXmlSerializable directly impacts the class itself, because you are adding an interface and one or more methods into the implementation of the class. In essence, You are making your own class directly responsibly for the it's serialization.

However, adding the XmlAttribute attributes does not directly impact the functionality of the class, instead you are just decorating it with attributes so that XmlSerializer can carry out the actual serialization functiohality. In this case, you are deferring the serialization to the XmlSerializer class, and providing just enough metadata about your class for XmlSerailizer to do it's work.

This is why I prefer the latter attribute approach. When I'm writing a class, I want it to be serializable, but the last thing I care about is the specifics of the implementation, so I always start with thaqt approach and 99% of the time it works fine with very little work. However, if you did need more fine-grain control over the serialization, the implement the IXmlSerializable interface and write your own serialization code.

Mike Mooney