开发者

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 appli开发者_运维知识库cation 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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜