NShape Programmer Tasks
Providing the Entity Description

<< Click to Display Table of Contents >>

Navigation:  Programmer Tasks > Developing a New Shape Class > Adding Persistency Support >

NShape Programmer Tasks
Providing the Entity Description

Previous pageReturn to chapter overviewNext page

The persistency mechanism requires information on the names, the order and the data type of the shape type's properties. This information is created by the static method GetPropertyDefinitions. Due to the fact that static methods can neither be part of an interface nor overridden, the implementing class has to 'override' GetPropertyDefinitions using the "new" keyword.

The base class' property definitions have to be returned before the implementing class returns its own property definitions.

new public static IEnumerable<EntityPropertyDefinition> GetPropertyDefinitions(int version) {
 // Return all property definitions of the base class "MyBaseClass"
 foreach (EntityPropertyDefinition pi in MyBaseClass.GetPropertyDefinitions(version))
         yield return pi;
 // Return property definitions of this class
 yield return new EntityFieldDefinition("MyIntProperty", typeof(int));
 yield return new EntityFieldDefinition("MyStringProperty", typeof(string));
 if (version >= 2)        // Color Style property was added in version 2
         yield return new EntityFieldDefinition("MyColorStyleProperty", typeof(object));
 // Return inner object definitions of this class
 yield return new EntityInnerObjectsDefinition("MyInnerObjects", 
         "MyLibraryName.MyInnerObjectName", 
         new string[] { "InnerObjIntProperty", "InnerObjStringProperty" }, 
         new Type[] { typeof(int), typeof(string) }
 );
}

It is assumed here that MyBaseShape is the base class of this shape class. Note, that it is important that the order of the properties is absolutely identical to the order in which they are written and read in the SaveFields and SaveInnerObjects methods.

As you can see in the example above, there is a version parameter that specifies the version of the repository. Every time you add features to your shapes, you should keep in mind that there are repositories out there saved with older versions of your library. In order to maintain backwards compatibility, you should check the version parameter before reading the new properties.