0

I am using SavableModelBase in order to save/load configuration files to/from XML.

I have a case now that I have common properties that I want to refactor to a base class.

Something like:

class CommonConfig: SavableModelBase<CommonConfig>
{
    /// <summary>
    /// Gets or sets the property value.
    /// </summary>
    public string CommonPath
    {
        get { return GetValue<string>(CommonPathProperty); }
        set { SetValue(CommonPathProperty, value); }
    }

    /// <summary>
    /// Register the Name property so it is known in the class.
    /// </summary>
    public static readonly PropertyData CommonPathProperty = RegisterProperty("CommonPath", typeof(string), string.Empty);
}   

Then I want to create some specific configuration (e.g. SpecificConfig) that share properties with the common configuration. The problem that if I inherit CommonConfig the Save() function is not aware of the properties of SpecificConfig.

I guess I can use composition (SpecificConfig will have a property of type CommonConfig) but this does not looks/reads well.

Any suggestions?

Tomer Cagan
  • 1,078
  • 17
  • 31

2 Answers2

0

The serialization engine uses the GetType() to get the actual type. So you can use interfaces / base classes / whatever you want to actually call the Save() method. In the end it will get the properties of the actual live instance type (thus the inherited) and save all properties that belong to that object, including the base class properties.

In other words, this should work fine.

Geert van Horrik
  • 5,689
  • 1
  • 18
  • 32
  • I tried once and it seemed not to work. Maybe the problem was with the load? If I use SavanleModelBase the loaded type should be whatever is in the XML file? – Tomer Cagan Nov 05 '13 at 10:07
  • No, it must have a class to instantiate before it even looks in the xml file to see if it can be loaded. Please create a reproducable issue and upload it at http://www.catelproject.com/support/issue-tracker/, then I can take a look at it. – Geert van Horrik Nov 05 '13 at 14:04
  • Hi - didn't get back to it until now - I've found that if I use SpecificConfig.Load(...) it works but using SpecificConfig.Load(...) does not work - it this as expected? – Tomer Cagan Mar 06 '14 at 07:45
  • No, please create an issue. – Geert van Horrik Mar 06 '14 at 11:51
0

In the above scenario using:

SpecificConfig.Load(myFile, SerializationMode.Xml);

should return CommonConfig. Probably it returns null because the class in the file is actually SpecificConfig.

If instead I am using:

SpecificConfig.Load<SpecificConfig>(myFile, SerializationMode.Xml);

it works as expected.

Tomer Cagan
  • 1,078
  • 17
  • 31