An Introduction to the EPiServer Dynamic Data Store

Episerver provides a nice out of the box way for you to dynamically store your data.. called the dynamic data store.  The dynamic data store allows us, as developers, to store compile time and runtime data types.  This can be handy when you don’t know for sure the type of class you might need to store.

When using the Dynamic Data Store you’ll be using the EPiServer.Data and EPiServer.Data.Dynamic namespaces inside of EPiServer.Data.

Storing Compile Time Data Types

When we want to store a compile time class within the dynamic data store, you create a standard class/DTO/POCO and have it inherit from IDynamicData.

public class Review: IDynamicData
{
public Identity Id { get; set; }
public string PostedBy{ get; set; }
public string Review{ get; set; }
public ContentReference RelatedReviewPage { get; set; }
}

One thing to note when storing data using compile time objects, is that you’ll need an Identity property to store the data store Id that will be return after you save it, otherwise you’ll run into problems.

Storing Our Compile Time Object

public void CreateStore(Review review)
{
var store = DynamicDataStoreFactory.Instance.CreateStore(typeof(Review));
store.Save(review);
}

To add something to the store, we call the CreateStore method in DynamicDataStoreFactory.Instance.  This will ether create a new store, if it doesn’t already exist, or retrieve an existing one. We pass in our custom class to the stores save method and boom it’s stored.

Getting Our Reviews

public static IEnumerable<Review> GetReview(ContentReference productPageReference)
{
var store = DynamicDataStoreFactory.Instance.CreateStore(typeof(Review));
return store.Items<Review>().Where(x => x.RelatedReviewPage == productPageReference);
}

To get our newly stored object again we call the CreateStore method.  To get the store for the class we care about we pass in the classes type.  We then use the Items method to get the Reviews we are interested in and filter it by the data we want to match.

Removing Reviews

public void RemoveReview(Review review)
{
var store = DynamicDataStoreFactory.Instance.CreateStore(typeof(Review));
store.Delete(review.Id);
}

The code to remove an item from the store is very similar to the create method.  As the previous code snippets we create get a reference to the store and then pass in our object to the delete method.

Storing Runtime Data

So we’ve talked about how to store custom objects in the data store, but what happens if you just want to save some random data where you can’t inherit from IDynamicData?  In these circumstances, when you want to store run-time data, you will need to use something called a property bag.

What is a PropertyBag?

The PropertyBag is a class used to save and load a collection a name/value pairs into the dynamic data store.  You can perform the same type of operation as you can with compile time objects, so add, get and remove.  The only difference is everything’s wrapped within a Property Bag.  For this example I’m going to store a page name and the page Id into the store:

public Identity AddToStore(PageData page)
{
var propertyBag= new PropertyBag();
propertyBag.Add("PageId", page.ID.ToString());
propertyBag.Add("PageName", page.PageName);
var store = DynamicDataStoreFactory.Instance.CreateStore("PageStore",   propertyBag.GenerateTypeBag());
return store.Save(propertyBag);
}

As you can see, it’s pretty similar to the compile time approach.  In here we create a property bag and add properties using the Add method.  Underneath the hood this is just using a key/value pattern.

You add in whatever you want, create a store (NOTE I have to explicitly tell the store the name rather than just using typeof()) and call the GenerateTypeBag method within the property bag to add all the properties).

Getting Our Data

public string GetPageData(int pageID)
{
var store = (DynamicDataStore)DynamicDataStoreFactory.Instance.GetStore("PageStore");
if (store != null)
{
var pageProperties = store.FindAsPropertyBag("PageId", pageID);
if (pageProperties != null)
{
foreach (var item in pageProperties )
{
var pageName = item["PageName"] as string ?? string.Empty;
var pageId = item["PageId"] as string ?? string.Empty;
}
}
}
}

To get our data out of the store, we get a reference to the store by passing in the name, “PageStore”.  We then do a search using FindAsPropertyBag on the key that we created on the Add method, “PageId”.  We can then retrieve the data we want by passing in the key names into the return property bags items collection.

Updating the dynamica data store

public void UpdatePageData(int pageId, string newPageName)
{
var store = (DynamicDataStore)DynamicDataStoreFactory.Instance.GetStore("PageStore");
if (store != null)
{
var pageData = store.FindAsPropertyBag("PageId", currentPageID);
if (pageData != null)
{
foreach (varitem in pageData )
{
item["PageName"] = newPageName;
store.Save(item);
}
}
}
}

Updating the store is very similar to the Get method.  You pass in the stores name and retrieve the PropertyBag that matches the page ID, then update the values and re-save it.

Deleting the Dynamica Data Store

DynamicDataStoreFactory.Instance.DeleteStore("PageStore", true);

To delete the store you can use the DeleteStore method. Warning:  this will delete everything in there.

Before you start using the dynamic data store… READ THIS!

One word of caution is if you add/remove properties from a class that inherits from IDynamicData, or you add or remove from a Property Bag, you may run into issues.  If the dynamic data store is changed and Episerver has already stored something, it will throw a StoreInconsistancy exception if you try and access existing data within the store.  To get around this you can either start afresh and delete all your stored data, however, in production this might not be possible. Your other option would be to remap your property bag or add the AutomaticallyRemapStore attribute on your custom DDS object, like so:

[EPiServerDataStore(AutomaticallyRemapStore = true)]

Conclusion

We’ve now covered the two different ways to store data within the dynamic data store.  The code is pretty easy to store custom objects within the store or custom properties.  The main thing to keep in mind is you need an identifier, after that you’re all set.

In my example, I’m storing reviews, however, with the introduction of custom content providers, depending on your requirements, you may be better suited using that approach, if you have a similar situation.

Jon D Jones

Software Architect, Programmer and Technologist Jon Jones is founder and CEO of London-based tech firm Digital Prompt. He has been working in the field for nearly a decade, specializing in new technologies and technical solution research in the web business. A passionate blogger by heart , speaker & consultant from England.. always on the hunt for the next challenge

More Posts

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *