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
- public static DynamicDataStore Store
- {
- get
- {
- return DynamicDataStoreFactory.Instance.GetStore(typeof(ProductItem)) ??
- DynamicDataStoreFactory.Instance.CreateStore(typeof(ProductItem));
- }
- }
- public static DynamicDataStore Store
- {
- get
- {
- return DynamicDataStoreFactory.Instance.GetCreateEnsureStore(typeof(ProductItem));
- }
- }
Where the actually code is like this.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- namespace EPiServer.Data.Dynamic
- {
- public static class StoreExtensions
- {
- static object lockObject = new object();
- static Dictionary<Type, bool> done = new Dictionary<Type, bool>();
- public static DynamicDataStore GetCreateEnsureStore(this DynamicDataStoreFactory factory,Type type)
- {
- bool isFirstTime = true;
- lock (lockObject)
- if (!done.TryGetValue(type, out isFirstTime))
- isFirstTime = true;
- if (isFirstTime)
- {
- var result= factory.GetStore(type);
- if (result == null)
- result = factory.CreateStore(type);
- else
- {
- var def = result.StoreDefinition;
- def.Remap(type);
- def.CommitChanges();
- }
- lock (lockObject)
- {
- done[type] = false;
- }
- }
- return factory.GetStore(type);
- }
- }
- }
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.
Nice one, was wondering how to solve this the other day. :)
My previous method was to delete the store and create a new one...
So it’s a big improvement :)
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?
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
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();
}
That was a good tip. It felt wrong to always remap the store, but I didn't bother to look further.
Have added Mr Smith's code and uploaded it here
http://world.episerver.com/Code/Anders-Hattestad1/GetCreateEnsureStore/
Very nice. I was trying to do this, this weekend. Thanks for the generosity.