This project has moved and is read-only. For the latest updates, please go here.

Proper way to serialize styles

Dec 27, 2012 at 8:49 PM
Edited Dec 28, 2012 at 11:30 AM


In my application I'm using a custom repository which has nothing common with the NShape's built-in one. My repository is a combination of MS SQL database and a set of .NET interface used to communicate with the database.

Model data is stored in a structured manner - almost every model property has a corresponding column in some certain table. Shape data - although also stored in database - is saved as a single XML, in one column of SQL XML type. This XML is formed by serializing array of classes inherited from ShapeInfo type (my custom type): PlanarShapeInfo, LinearShapeInfo etc. These ***ShapeInfo types are simply "facades" for built-in NShape's shape types, hiding shape data which I do not need to serialize. For example, ShapeInfo type currently contains only 2 properties - ShapeTypeName and ZOrder, - and the derived PlanarShapeInfo type currently contains only 3 properties - ModelId, Location, and Size.

Serialization (as well as further deserialization) worked fine until I decided to add style-related properties to the PlanarShapeInfo type (LineStyle, ColorStyle etc): this resulted in the following exception stack:

"There was an error reflecting type 'Dataweb.NShape.LineStyle'."

    "Cannot serialize member 'Dataweb.NShape.LineStyle.ColorStyle' of type 'Dataweb.NShape.IColorStyle', see inner exception for more details."

        "Cannot serialize member Dataweb.NShape.LineStyle.ColorStyle of type Dataweb.NShape.IColorStyle because it is an interface."

I guess that this problem could be easily solved by adding the [XmlInclude(typeof(ColorStyle))] attribute to the Dataweb.NShape.IColorStyle interface. But I cannot do that. Well, I can, of course, modify your source code, but obviously it's not a proper way to go.

What would You recommend in this situation?

Jan 2, 2013 at 10:59 PM

Well, having investigated your code for some time, I understand why this doesn't work and why everybody is still happy with that: you do not use .NET serialization - instead you use IEntity.SaveFields() method in conjunction with the GetPropertyDefinitions() method. As a result, you never care about are your objects serializable or not (from .NET point of view).

So instead of this topic, I created another one: Serializing an arbitrary shape to XML. I still need to save NShape stuff to XML! :)

Jan 30, 2013 at 5:08 PM


Is there a way to determine if built-in Style object has changed in run-time?

For example, if the built-in FillStyle "LightGray" hasn't changed - I want to store only it's name: it's enough to restore it properly in future. But if user changed its FillMode property from FillMode.Solid to FillMode.Pattern - I want to store at least this FillMode property (but the whole FillStyle object will be also OK).

.NET XmlSerializer stores only those properties that differ from their default values. But I couldn't find smth. similar in NShape's code.

Feb 1, 2013 at 7:55 AM
Edited Feb 1, 2013 at 7:57 AM
If it is enough to know whether a style was modified or not, handle the IRepository.StyleUpdated event.

If you need to find out which properties differ from the default values, you can create a temporary Design (which is filled with standard styles). Afterwards, you can compare the styles property by property.
Feb 2, 2013 at 12:29 PM
Thank you!