views:

106

answers:

1

I am developing a C# application where the majority of the code base is in C# class libraries. I want the application to support saving and loading of XML based project files and be able to determine if any modifications have occurred since the last save.

My current design idea is:

  1. Each class that needs to store settings implements IXmlSerializable.

  2. The application maintains a generic list of IXmlSerializable settings objects and calls ReadXml and WriteXml to save/load the project files.

  3. Each class that stores settings also maintains a Modified flag.

  4. The application can check if the project has been modified by enumerating the generic list of IXmlSerializable objects and checking the Modified flag on each. It can also clear the Modified flag on each after saves.

Is this a good design? Are there any better ones? Perhaps I also need to derive my own interface from IXmlSerializable to add the Modified flag?

thanks, Andy

+2  A: 

I think the concept you are going for is sound. I only have one concern and it might be me reading to much into your wording.

It sounds like you expect to not execute the WriteXml method if the instance modified flag is not set. If I have understood correctly, what I think you will find is that the resulting file will then not contain the non-modified settings. XmlSerialization is an all or nothing, it cannot update certain parts of the file.

So I think your flag is useful to determine if you need to save or not, but if you determine that you need to save the data you need to save the entire structure. In which case there is no need to implement IXmlSerializable on every class unless you have some custom serialization requirements that can't be addressed otherwise.

Chris Taylor
That is if you serialize the whole object tree at once, right?
Caspar Kleijne
Yes, my intention is to OR the Modified flags together and then determine if the project in it's entirety needs to be saved or not. Thanks for point this out.
Andy
@Caspar, that is correct. I assume (hope?) the serialization is all to a single XML file. To write parts of the graph individually would mean that multiple XmlSerializers are used, one of each root type to be written, this would result in a non-Well formed XML document. Unless there is some trick I am missing here.
Chris Taylor
@Andy, if that is the case. Do you need the IXmlSerializable for anything specific?
Chris Taylor
@Chris, ins uch cases, I would go for a BinaryFormatter and just dump the whole graph. XML seduces to manually change, so it is likely break faster than a binary blob.
Caspar Kleijne
@Casper, I believe the OP has confirmed that the intention is to write out the entire graph so XmlSerializer will be fine. Since the OP has indicated that the file should be human readable a BinaryFormatter does not seem appropriate, even the SoapFormatter would not be nice in light of this requirement.
Chris Taylor