2

I have a problem with implementing an IsDirty mechanism with my XmlSerializer system.

This is how my serialization is called:

public OCLMEditorModel()
{
    DeSerialize();
}

public void Serialize()
{
    XmlSerializer x = new XmlSerializer(_ModelData.GetType());
    using (StreamWriter writer = new StreamWriter(_strPathModelDataXml))
    {
        x.Serialize(writer, _ModelData);
    }
}

public void DeSerialize()
{
    _ModelData = new OCLMModelData();
    XmlSerializer x = new XmlSerializer(_ModelData.GetType());
    using (StreamReader reader = new StreamReader(_strPathModelDataXml))
    {
        _ModelData = (OCLMModelData)x.Deserialize(reader);
    }
}

It reads and saves perfectly, no issues there. But it is the IsDirty flag I have issues with. Directly after the DeSerialize call ...

Set to true

Ass the IsDirty are set to true. Even though all we have done is read it in from the computer. Example properties:

public class MaterialItem
{
    [XmlAttribute]
    public string Setting
    {
        get { return _Setting; }
        set
        {
            _Setting = value;
            MarkDirty();
        }
    }
    private string _Setting;

    [XmlText]
    public string Material
    {
        get { return _Material; }
        set
        {
            _Material = value;
            MarkDirty();
        }
    }
    private string _Material;

    [XmlIgnore]
    public bool IsDirty { get { return _isDirty; } }
    private bool _isDirty;

    public void MarkClean()
    {
        _isDirty = false;
    }

    protected void MarkDirty()
    {
        _isDirty = true;
    }

    public MaterialItem()
    {
        MarkClean();
    }
}

Ideally, the flag should be false when we have just read it using XMLSerializer.

What am I doing wrong?

Thank you.

Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
  • 2
    When the object is being deserialized, it is setting the value on the property and in the property setter, `MarkDirty()` is being called which marks everything as dirty. – Metro Smurf Jul 03 '16 at 20:28
  • step thru the code and see where it's doing or not doing what you expect – MethodMan Jul 03 '16 at 20:29

1 Answers1

3

XmlSerializer doesn't work in any mysterious way.

It uses reflection, yes, but only to get the properties it can serialize/deserialize. Then it uses those properties to get/set the required values.

So during the deserialization those setters will be called, thus calling the MarkDirty method, thus marking your entities dirty.

There isn't anything you can change in XmlSerializer, but you can change your deserialization method, so it sets the entity clean just after deserializing it:

public void DeSerialize()
{
    _ModelData = new OCLMModelData();
    XmlSerializer x = new XmlSerializer(_ModelData.GetType());
    using (StreamReader reader = new StreamReader(_strPathModelDataXml))
    {
        _ModelData = (OCLMModelData)x.Deserialize(reader);
    }
    _ModelData.MarkClean();
}
Community
  • 1
  • 1
Eugene Podskal
  • 10,270
  • 5
  • 31
  • 53
  • @AndrewTruckle There is absolutely nothing wrong with adding additional information to the topic, but it is usually [frowned upon when the topic itself is changed drastically](http://meta.stackoverflow.com/questions/300966/dealing-with-solutions-that-end-with-a-new-error) - in such a case it is better to ask a new question, while linking to the current one to give the necessary context. – Eugene Podskal Jul 03 '16 at 20:36
  • 1
    It is not a new question ... :) Just updated it. Please see bottom. – Andrew Truckle Jul 03 '16 at 20:41
  • Thanks. I will stick with just making a MarkClean method which parses the read in data. Thank you. :) – Andrew Truckle Jul 03 '16 at 20:47
  • 1
    @AndrewTruckle Well, I wouldn't exactly agree that it is not a new question - initial question was about why does it happen, while now it is more about what are the alternatives. I'd rather asked a new question along the line of "Managing a single isdirty flag on top of objects hierarchy" - it would be more concise and can potentially be very interesting to answerers. – Eugene Podskal Jul 03 '16 at 20:48
  • We can delete these comments if you like. I removed it. :) – Andrew Truckle Jul 03 '16 at 20:49
  • @AndrewTruckle Still you can ask that potential question - it may give you some alternative ideas. – Eugene Podskal Jul 03 '16 at 20:50