NShape Programmer Tasks
Implementing the Load Method

<< Click to Display Table of Contents >>

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

NShape Programmer Tasks
Implementing the Load Method

Previous pageReturn to chapter overviewNext page

The load methods of the IEntity interface uses the write methods of IRepositoryReader to load the fields of the persisted object. The following rules must be observed:

Never read the id of the object, this is done by the repository.
Always read all persistent fields of the object. Do not skip optional fields etc.
The order of the fields to load must be the same as in GetPropertyDefinitions.

Implementing LoadFields

In order to load objects from a repository, just implement all load methods of the interface, preferably as explicit implementation:

void IEntity.LoadFields(IRepositoryReader reader, int version) {
 MyIntProperty = reader.ReadInt32();
 MyStringProperty = reader.ReadString();
 if (version >= 2)
         MyColorStyleProperty = reader.ReadColorStyle();
}

For all base classes of the NShape framework, there are Core methods of the IEntity methods because you cannot override explicit implementations. Override the appropriate Core methods in this case:

protected override void LoadFieldsCore(IRepositoryReader reader, int version) {
 base.LoadFieldsCore(reader, version);
 MyIntProperty = reader.ReadInt32();
 MyStringProperty = reader.ReadString();
 if (version >= 2)
         MyColorStyleProperty = reader.ReadColorStyle();
}

Implementing LoadInnerObjects

The same for the inner objects: Implement the IEntity interface explicitly or override the appropriate methods of the base class. If your class does not contain any complex objects or lists that have to be saved, you don't have to bother with this method.

A notable difference to the LoadFields implementation is the fact that inner objects can also be list of objects and therefore the loading has to be done in loops. In addition to that, loading inner objects has to be started with a call to BeginReadInnerObjects and ended with a call to EndReadInnerObjects of the given IRepositoryReader. Similarly, each loading of an inner object has to be surrounded by BeginReadInnerObject and EndReadInnerObject.

protected override void LoadInnerObjectsCore(string propertyName, IRepositoryReader reader, int version) {
 if (propertyName == "MyInnerObjects") {
         // Clear list of inner objects
         innerObjects.Clear();
         // Load inner objects
         reader.BeginReadInnerObjects();
         while (reader.BeginReadInnerObject()) {
                 int intValue = reader.ReadInt32();
                 string strValue = reader.ReadString();
                 InnerObjects.Add(new MyInnerObject(intValue, strValue);
                 reader.EndReadInnerObject();
         }
         reader.EndReadInnerObjects();
 } else base.LoadInnerObjectsCore(propertyName, reader, version);
}

See Also

Concepts: Persistency

Working with NShape: Adding persistency support