Skip to main content

Readonly Properties

Some properties stored in ordinary readonly fields never change after initialization. They nevertheless must be initialized in a special way for the Document Model Framework to keep track of them.

Overview

Some properties of a node are never changed. For example, the node that represents the root of the document holds a reference to the data manager and to a collection of pages. Such properties are stored in ordinary read only fields but the document model framework needs to know of them. Thus such fields must be initialized in a special way.

Consider a node that represents a page. It holds a reference to a collection of visualizations. Such properties typically contain other document nodes which are then owned by the node. In those cases the document model framework needs to know of them in order to manage ownership. The pattern is as follows.

Implementation Pattern

public class Example : DocumentNode
{
    public new class PropertyNames : DocumentNode.PropertyNames
    {
        public static readonly PropertyName ChildNode = CreatePropertyName("ChildNode");
    }

    private readonly SomeNode childNode;

    public SomeNode ChildNode
    {
        get { return this.childNode; }
    }

    public Example(SomeNode someNode)
    {
        CreateReadOnlyProperty(
           PropertyNames.ChildNode, 
           someNode, 
           out this.childNode);
    }

    internal Example(SerializationInfo info, StreamingContext context) : base(info, context)
    {
        DeserializeReadOnlyProperty(
           info, 
           context, 
           PropertyNames.ChildNode, 
           out this.childNode);
    }

    protected override void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        base.GetObjectData(info, context);

        SerializeReadOnlyProperty(
           info, 
           context, 
           PropertyNames.ChildNode, 
           this.childNode);
    }
}