This content is archived. See latest version here

Last updated: Mar 31 2014

Most APIs in EPiServer CMS returns read-only instances of objects, for example DataFactory.GetPage return a read-only PageData. To make changes to read-only instances a writable clone has to be created using the CreateWritableClone method.

The implementation has the following advantages:

  • Reduce memory consumption. All threads serving web requests will get the same instance of an object, this effectively reduces the amount of short-lived objects being created.
  • Improve performance. Returning shared read-only instances offer great performance.
  • Cleaner architecture. This simplifies the implementation significantly, it is not possible to make changes to an instance of an object shared with other threads.

All classes that have the read-only support implements the IReadOnly<T> interface which is defined as follows:

C#
public interface IReadOnly
    {
        void MakeReadOnly();
        bool IsReadOnly
        {
            get;
        }
    }

    public interface IReadOnly<T> : IReadOnly
    {
        T CreateWritableClone();
    }

To give some background to what is happening behind the scenes, the life cycle of a typical PageData object is as follows:

  1. Create the PageData object as mutable (which means writeable).
  2. Populate the PageData object with properties.
  3. Call the Core.PageData.MakeReadOnly method to ensure that any contained objects are made read-only. From here on, the object is now immutable for the remainder of its lifetime.
  4. Add the object to the cache.

Do you have feedback on this documentation? Send an email to documentation@episerver.com. For development-related questions and discussions, refer to our Forums on https://world.episerver.com/forum/