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.
Comments