views:

42

answers:

2

Currently i have some interfaces (stripped down for here):

public interface IJobGroup
{
    string Name { get; }
    IEnumerable<IJobItem> Jobs { get; }
}

public interface IJobItem
{
    string Name { get; }
    void Start();
    event EventHandler Finished;
}

Also i made some implementations of these interfaces. But now i'd like to serialize my JobGroup. So far i already made some tries with DataContractSerializer and XMLSerializer and added all the needed attributes to my implementations of the above interfaces.

So far, so good. But i'd like to get two things solved with my needed solution:

  1. Enforce everyone who implements my interface to make his class serializable to xml.
  2. If i serialize a IJobGroup, i'd like to see everything in the resulting xml file in clear text, not as Base64 coded data. In that case someone is able to change the xml file by hand (if he knows what he's doing).

Some ideas, which doesn't work very well:

  • ISerializable

    This doesn't really enforce to serialize an object. It is rather used to implement a custom serializer instead of using the default usage of attributes.

  • property within interfaces that encapsulated the serialization (e.g. byte[] State)

    I could add another byte array property State to the interface, which will be get and set the serialized data. Maybe this would have the advantage that every item could implement its very own process of how it will serialize something. But the drawback would be that the big xml file contains the byte[] as Base64 coded chunk of data, which can't be read by human easily.

So, maybe someone has an idea or could point me into the right direction.

A: 

The default XML Serialization doesn't support interfaces by default, but you might be able to derive from XmlSerializer and build something using that framework?

Rowland Shaw
Instead of building my own i hoped there is already something existing.
Oliver
A: 

How about using abstract classes? I don't think you can enforce behavior with interfaces... interfaces are more contract-oriented than anything else...

code4life
This would be a possible way. But i (and my colleagues) like the contract oriented way of an interface. So why isn't there a good way for a contract that says "Make my Serializable"?
Oliver
That's a good question. But I think you've come to the point in your problem where you're going to have to make the decision to either build your own implemention of the ISerializable, your own custom serializer, or abstract classes. Not pretty choices, that's for sure...!
code4life
Even if i don't like the answer, it's the one i have to accept. ;-)
Oliver