Don't miss out Virtual Happy Hour this Friday (April 26).

Try our conversational search powered by Generative AI!

Anders Hattestad
Jan 12, 2011
  6760
(4 votes)

Dynamic Data Store and new properties on Types

When you use access the data store you get your object with the properties from the first time you created the store. If you have added properties on thje type the definition in the store is not updated, and your object will be missing values from the new properties.

That's a bit hassle, since I guess most of you will add new properties after the first time the store is created.

So instead of accessing the store like this

Code Snippet
  1. public static DynamicDataStore Store
  2. {
  3.     get
  4.     {
  5.         return DynamicDataStoreFactory.Instance.GetStore(typeof(ProductItem)) ??
  6.                DynamicDataStoreFactory.Instance.CreateStore(typeof(ProductItem));
  7.     }
  8. }
I have created myself and extension method so
Code Snippet
  1. public static DynamicDataStore Store
  2. {
  3.     get
  4.     {
  5.         return DynamicDataStoreFactory.Instance.GetCreateEnsureStore(typeof(ProductItem));
  6.     }
  7. }

Where the actually code is like this.

Code Snippet
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5.  
  6. namespace EPiServer.Data.Dynamic
  7. {
  8.     public static class StoreExtensions
  9.     {
  10.         static object lockObject = new object();
  11.         static Dictionary<Type, bool> done = new Dictionary<Type, bool>();
  12.         public static DynamicDataStore GetCreateEnsureStore(this DynamicDataStoreFactory factory,Type type)
  13.         {
  14.             bool isFirstTime = true;
  15.             lock (lockObject)
  16.                 if (!done.TryGetValue(type, out isFirstTime))
  17.                     isFirstTime = true;
  18.             if (isFirstTime)
  19.             {
  20.                 var result= factory.GetStore(type);
  21.                 if (result == null)
  22.                     result = factory.CreateStore(type);
  23.                 else
  24.                 {
  25.                     var def = result.StoreDefinition;
  26.                     def.Remap(type);
  27.                     def.CommitChanges();
  28.                 }
  29.                 lock (lockObject)
  30.                 {
  31.                     done[type] = false;
  32.                 }
  33.                 
  34.             }
  35.             return factory.GetStore(type);
  36.         }
  37.  
  38.     }
  39. }

This code will on the first time it get accessed, checks if the store exits, and if not creates it. And if it exists, it will be remap to the type before it returns the store.

Jan 12, 2011

Comments

Erik Nordin Wahlberg
Erik Nordin Wahlberg Jan 12, 2011 09:25 PM

Nice one, was wondering how to solve this the other day. :)

Anders Hattestad
Anders Hattestad Jan 12, 2011 10:51 PM

My previous method was to delete the store and create a new one...
So it’s a big improvement :)

Jan 13, 2011 09:19 AM

Interesting code, but I can't figure out why you use the lockObject? Is it only to make it thread safe or what is its purpose?

Anders Hattestad
Anders Hattestad Jan 13, 2011 10:20 AM

Just to make it thread safe,
I thought that could be a good idea, but I quess most of the times this isn't a problem.
I lock when I do a read, but are not sure if that's necessery. Did google it, and was some contradiction statements about it

smithsson68@gmail.com
smithsson68@gmail.com Jan 13, 2011 03:29 PM

You can also check if the store actually needs remapping like this:

var def = result.StoreDefinition;
if (!def.ValidateAgainstMappings(typeof(type), false))
{
def.ReMap(typeof(type));
def.CommitChanges();
}

Anders Hattestad
Anders Hattestad Jan 13, 2011 05:39 PM

That was a good tip. It felt wrong to always remap the store, but I didn't bother to look further.

Anders Hattestad
Anders Hattestad Jan 13, 2011 06:00 PM

Have added Mr Smith's code and uploaded it here
http://world.episerver.com/Code/Anders-Hattestad1/GetCreateEnsureStore/

Joshua Folkerts
Joshua Folkerts Jan 31, 2011 02:43 PM

Very nice. I was trying to do this, this weekend. Thanks for the generosity.

Please login to comment.
Latest blogs
Solving the mystery of high memory usage

Sometimes, my work is easy, the problem could be resolved with one look (when I’m lucky enough to look at where it needs to be looked, just like th...

Quan Mai | Apr 22, 2024 | Syndicated blog

Search & Navigation reporting improvements

From version 16.1.0 there are some updates on the statistics pages: Add pagination to search phrase list Allows choosing a custom date range to get...

Phong | Apr 22, 2024

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