简体   繁体   中英

Design Pattern for XML Based Project Files in C#

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM