smithsson68@gmail.com
May 10, 2010
  7463
(0 votes)

Data Container Indifference in the Dynamic Data Store

The Dynamic Data Store (DDS) that shipped with EPiServer CMS 6 has a feature unofficially called “Data Container Indifference”. This is a feature that some people are going to love, and some hate.

Let me explain what I’m talking about. When a .NET object is saved to the DDS the object is reflected over and it’s public properties are extracted and mapped to a column in the database. The CLR Type of the object saved is saved but it is important to note that the DDS does not use serialization in the same way as, let’s say, the BinaryFormatter class in the .NET class library. What this essentially means is that the .NET object instance saved translates to a set of name value property pairs.

So where does the data container indifference come in then?

The DynamicDataStore class has 3 method groups for reading data from a store.

Methods that return instances of System.Object:

public abstract Object Load(Identity id);
public abstract IEnumerable<Object> LoadAll();
public abstract IEnumerable<Object> Find(string propertyName, object value);
public abstract IEnumerable<Object> Find(IDictionary<string, object> parameters);
public abstract IOrderedQueryable<Object> Items();

Methods that return instances of TResult:

public abstract TResult Load<TResult>(Identity id);
public abstract IEnumerable<TResult> LoadAll<TResult>();
public abstract IEnumerable<TResult> Find<TResult>(string propertyName, object value);
public abstract IEnumerable<TResult> Find<TResult>(IDictionary<string, object> parameters);
public abstract IOrderedQueryable<TResult> Items<TResult>();

Methods that return instance of EPiServer.Data.Dynamic.PropertyBag:

public abstract PropertyBag LoadAsPropertyBag(Identity id);
public abstract IEnumerable<PropertyBag> LoadAllAsPropertyBag();
public abstract IEnumerable<PropertyBag> FindAsPropertyBag(IDictionary<string, object> parameters);
public abstract IEnumerable<PropertyBag> FindAsPropertyBag(string propertyName, object value);
public abstract IOrderedQueryable<PropertyBag> ItemsAsPropertyBag();

This means that data saved to the DDS can be read back using any of the 3 method groups regardless of the CLR Type of the actual .NET object instance that was saved.

Some examples should help visualize what I’m talking about. Take the following class:

   1:   public class Person
   2:   {
   3:       public string FirstName { get; set; }
   4:       public string LastName { get; set; }
   5:       public DateTime DateOfBirth { get; set; }
   6:   }
 

Create a store in the DDS using the Person class as the definition for the store:

   1:  // Create the factory and the store
   2:  EPiServerDynamicDataStoreFactory factory = new EPiServerDynamicDataStoreFactory();
   3:   
   4:  // The definition of the store will be defined by the Person class
   5:  // i.e. it will have 3 properties, FirstName, LastName and DateOfBirth
   6:  DynamicDataStore store = factory.CreateStore(typeof(Person));
   7:   
   8:  // Save an instance of person
   9:  Person p = new Person()
  10:  {
  11:      FirstName = "John",
  12:      LastName = "Doe",
  13:      DateOfBirth = new DateTime(1972, 4, 21)
  14:  };
  15:   
  16:  store.Save(p);

As you would expect you can then read the person back using the following code:

   1:  // Read the person back using a Person class as the container
   2:  Person p2 = store.Find<Person>("LastName", "Doe").FirstOrDefault();

However, what you can also do is read the person back into a PropertyBag:

   1:  // Read the person back using a PropertyBag
   2:  PropertyBag bag = store.FindAsPropertyBag("LastName", "Doe").FirstOrDefault();

But even more surprising is that you can read it back with a completely different class:

class Person2
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateTime DateOfBirth { get; set; }
    public char Gender { get; set; }
}
 
   1:  // Read the person back using a Person2 class as the container
   2:  Person2 person2 = store.Find<Person2>("LastName", "Doe").FirstOrDefault();

What is happening here is that the stored properties are read into memory and then the DDS will map them against any matching properties (with same name and value type) on the Person2 class. In the above case FirstName, LastName and DateOfBirth will be successfully mapped but Gender will not and therefore have a default value for the CLR Type.

This mechanism could potentially be used as some kind of runtime polymorphism and even allows you to completely replace an entire class without affecting the data stored in the DDS.

The group of methods that return System.Object will always return an instance of the CLR Type that was actually used to save the properties to the DDS, in this case a Person class.

The source code for this example can be found here.

May 10, 2010

Comments

Please login to comment.
Latest blogs
Optimizely Commerce vs Composable Commerce: What Should You Do with CMS 13?

As organizations modernize their digital experience platforms, a common architectural question emerges: Should we continue using Optimizely Commerc...

Aniket | Mar 12, 2026

Missing Properties tool for Optimizely CMS

If you have been working with Optimizely CMS for a while you have probably accumulated some technical debt in your property definitions. When you...

Per Nergård (MVP) | Mar 10, 2026

AI Generated Optimizely Developer Newsletter

Updates in the Optimizely ecosystem are everywhere: blog posts, forums, release notes, NuGet packages, and documentation changes. This newsletter...

Allan Thraen | Mar 10, 2026 |

Lessons from Building Production-Ready Opal Tools

AI tools are becoming a normal part of modern digital platforms. With  Optimizely Opal , teams can build tools that automate real tasks across the...

Praful Jangid | Mar 7, 2026

My Takeaway from Optimizely Opal Agents in Action 2026 - What Agentic AI Means for the Future of Digital Marketing

I would like to share with you what stayed in my head after this amazing virtual event organized by Optimizely. Agents in Action 2026 , a live...

Augusto Davalos | Mar 6, 2026

From Vision to Velocity: Introducing the Optimizely MVP Technical Roundtable

Digital transformation is a two-sided coin. On one side, you have the high-level strategy, the business cases, the customer journeys, and the...

Patrick Lam | Mar 6, 2026