I'm having a hard time with my design in the serialization of objects.
Let my show you my scenario. I've got a general Configuration
class and contains three properties:
public sealed class Configuration
{
public Configuration(string name, Levels level, ConfigurationSpec spec)
{
this.Name = name;
this.Level = level;
this.Spec = spec;
}
public string Name { get; set; }
public Levels Level { get; set; }
public ConfigurationSpec Spec { get; set; }
}
The last property is imporant, because it's an abstract class and it can derived from others classes:
public class ConfigurationSpec { }
public class BinaryConfiguration : ConfigurationSpec
{
public Range<int> Range1 { get; set; }
public Range<int> Range2 { get; set; }
public BinaryConfiguration()
{
this.Range1 = new Range<int>();
this.Range2 = new Range<int>();
}
public BinaryConfiguration(Range<int> range1, Range<int> range2)
{
this.Range1 = range1;
this.Range2 = range2;
}
}
public class Range<T> where T : IComparable<T>
{
private T _min;
private T _max;
public Range()
{
}
public Range(T min, T max)
{
this.Min = min;
this.Max = max;
}
public T Min
{
get { return _min; }
set { _min = value; }
}
public T Max
{
get { return _max; }
set { _max = value; }
}
All of this contain the ConfigurationSpec
class. And the real problem is, I'm gonna add many Specs
derived from ConfigurationSpec
, I mean hundreds.
<Configuration>
<!-- Maybe here it'll be good specify the type -->
<ConfigurationSpec>
<Range1 X="2" Y="4" />
<Range2 X="5" Y="10" />
</ConfigurationSpec>
</Configuration>
And I want to know how can I write and read all of this classes from a XmlFile for the serialization.
If you have any doubt, please let me know
As long as your Configuration
class has a reference to the ConfigurationSpec
it will correctly serialize/deserialize all the object they are needed.
Talking about ConfigurationSpec
only the type referenced by the Configuration
will be serializated(when you serialize an instance of Configuration
).
In other word if you got 5 different ConfigurationSpec
only the one you will pass to the Configuration
constructor will be serializeted