using System; using System.Xml; namespace oodb.mapping { public abstract class FlatTypeMapping { public OODB OODB { get; } public Type NativeType { get; } public FlatTypeMapping(OODB oodb, Type nativeType) { OODB = oodb; NativeType = nativeType; } public XmlElement CreateEmptyValueElement(XmlDocument xmlDocument) { XmlElement value = xmlDocument.CreateElement("Value"); value.SetAttribute("Type", NativeType.FullName); return value; } public abstract XmlElement ToXml(XmlDocument xmlDocument, object value); public abstract object FromXml(XmlElement xmlValue); public abstract String ToText(object value); public abstract object FromText(string text); } public abstract class NaiveMapping : FlatTypeMapping { public NaiveMapping(OODB oodb, Type nativeType) :base(oodb,nativeType) { } public override object FromXml(XmlElement xmlValue) { if (!NativeType.FullName.Equals(xmlValue.GetAttribute("Type"))) throw new ArgumentException("Type != NativeType", nameof(xmlValue)); return FromText(xmlValue.InnerText); } public override XmlElement ToXml(XmlDocument xmlDocument, object value) { XmlElement valueElement = CreateEmptyValueElement(xmlDocument); if (value != null) valueElement.InnerText = ToText(value); return valueElement; } } }