smithsson68@gmail.com
Jan 15, 2010
  11798
(7 votes)

Using a DynamicDataStore instance correctly

I’ve recently seen a couple of examples of code where the Dynamic Data Store (being released as part of CMS 6) has been used in a multithreaded environment (web app for example) in a singleton pattern, i.e. a single instance of a DynamicDataStore is shared between all requests (read threads) in the application.

THIS IS BAD!

The default implementation (EPiServerDynamicDataStore) of the DynamicDataStore is not thread safe.

Let me just say that again

DYNAMIC DATA STORE IS NOT THREAD SAFE.

Think of it like an ADO.NET connection object:

- Obtain – Use – Throw Away

on the stack in a method.

The internals of the Dynamic Data Store are thread safe, that is all instances access shared data with thread synchronization. The cost of obtaining a DDS instance using the DynamicDataStoreFactory GetStore method is low due to all store metadata being cached in memory so there really is no need to hold DDS instances as member variables in a class.

An example of the recommended use pattern can be found below:

 

// Create and populate my object
Person p = new Person();
p.FirstName = "Micky";
p.LastName = "Mouse";

// Obtain a store instance
EPiServer.Data.Dynamic.DynamicDataStore store = GetStore(typeof(Person));

// save the object
store.Save(p);

static EPiServer.Data.Dynamic.DynamicDataStore GetStore(Type t)
{
  // GetStore will only return null the first time this method is called for a Type
  // In that case the ?? C# operator will call CreateStore
  return EPiServer.Data.Dynamic.DynamicDataStoreFactory.Instance.GetStore(t) ??
      EPiServer.Data.Dynamic.DynamicDataStoreFactory.Instance.CreateStore(t);
}
Jan 15, 2010

Comments

Jonathan Roberts
Jonathan Roberts Nov 20, 2014 06:28 PM

THis is great - can you get the data from the DDS when using a webservice? for example:

var store = DynamicDataStoreFactory.Instance.GetStore(typeof(CategoryItems));

var responses = from r in store.Items()
select r;

I use the exact code in a page and I get the data back but calling it from a webservice I get Zero items returned.

Jon

Please login to comment.
Latest blogs
How to add an Admin Mode add-on in Optimizely CMS12

How to add a new add-on with navigation and unified stylesheet

Bartosz Sekula | Jan 2, 2025 | Syndicated blog

Managing Your Graph Conventions

Recently, Optimizely released a Conventions API for manging how various fields on your CMS content are indexed by the Graph. This is an extremely...

Ethan Schofer | Dec 31, 2024

SaaS CMS and Visual Builder - Opticon 2024 Workshop Experience

Optimizely is getting SaaSy with us…. This year Optimizely’s conference Opticon 2024 took place in San Antonio, Texas. There were a lot of great...

Raj Gada | Dec 30, 2024

Copy Optimizely SaaS CMS Settings to ENV Format Via Bookmarklet

Do you work with multiple Optimizely SaaS CMS instances? Use a bookmarklet to automatically copy them to your clipboard, ready to paste into your e...

Daniel Isaacs | Dec 22, 2024 | Syndicated blog