Try our conversational search powered by Generative AI!

smithsson68@gmail.com
Jan 15, 2010
  11525
(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
Optimizely and the never-ending story of the missing globe!

I've worked with Optimizely CMS for 14 years, and there are two things I'm obsessed with: Link validation and the globe that keeps disappearing on...

Tomas Hensrud Gulla | Apr 18, 2024 | Syndicated blog

Visitor Groups Usage Report For Optimizely CMS 12

This add-on offers detailed information on how visitor groups are used and how effective they are within Optimizely CMS. Editors can monitor and...

Adnan Zameer | Apr 18, 2024 | Syndicated blog

Azure AI Language – Abstractive Summarisation in Optimizely CMS

In this article, I show how the abstraction summarisation feature provided by the Azure AI Language platform, can be used within Optimizely CMS to...

Anil Patel | Apr 18, 2024 | Syndicated blog

Fix your Search & Navigation (Find) indexing job, please

Once upon a time, a colleague asked me to look into a customer database with weird spikes in database log usage. (You might start to wonder why I a...

Quan Mai | Apr 17, 2024 | Syndicated blog

The A/A Test: What You Need to Know

Sure, we all know what an A/B test can do. But what is an A/A test? How is it different? With an A/B test, we know that we can take a webpage (our...

Lindsey Rogers | Apr 15, 2024

.Net Core Timezone ID's Windows vs Linux

Hey all, First post here and I would like to talk about Timezone ID's and How Windows and Linux systems use different IDs. We currently run a .NET...

sheider | Apr 15, 2024