In this tutorial, you will learn about the Episerver dynamic data store and how you can use it to store custom data within the Epsierver database. Episerver provides a easy out-of-the-box way for you to dynamically store your data, the dynamic data store 🏪. The dynamic data store allows us, as developers, to store compile time and run-time data types. This can be handy when you don't know for sure the type of class you might need to store at compile time. When using the Dynamic Data Store, you will be using code found within EPiServer.Data and EPiServer.Data.Dynamic namespaces, found within the EPiServer.Data assembly which you get access to this when you installed the CMS 🔥🔥🔥 Want to learn more, read on 💥

Storing Compile Time Data Types

At want to store a compile-time class within the dynamic data store, you first need to create a standard classDTO/POCO (depending on your naming preferences. Within this custom class, you need to inherit from IDynamicData. This can be done, like this:

One important thing to note when storing data using compile-time objects is that you'll need to include an Identity property. This ID will be returned after you save it, without it, you'll run into problems 😔

How To Store Compile Time Objects Within The Episerver Dynamic Data Store

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.

To add something to the store, we will need to call the CreateStore() method (found in DynamicDataStoreFactory.Instance). This will either create a new store if it doesn't already exist, or retrieve an existing one. We pass in our custom class into the stores Save() method and boom it's stored 💥.

Getting Data Out Of The Dynamic Data Store

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

The code to remove an item from the store is very similar to the create method. As in the previous code snippets, we create a reference to the store and pass in our object to the Delete() method.

Storing Runtime Data

What happens if you just want to save some simple data (like a key/pair) and you don't care about storing a whole object or creating something extra that implements the IDynamicData interface just for some simple data? In these circumstances, you will need to use something called a PropertyBag 👜👜👜

What is a PropertyBag?

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

As you can see, it's pretty similar to the compile-time approach. 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 and call the GenerateTypeBag() method within the property bag to add all the properties.

NOTE You have to explicitly tell the store the name rather than just using generic and typeof()

Getting Key/Value Data

To get data out of the dynamic data store, first create a reference to the store by passing in the name. As seen on Line3, I pass the name PageStore into GetStore(). Next, you need to find the data using the FindAsPropertyBag() method. Pass in the key whose value you want to be returned, in this example that is PageId. The data can be returned by querying the key within the property bags items collection.

Updating the Dynamic Datastore

The approach to updating the value is very similar to the Get() method. Get the store by passing in the stores' name. Retrieve the item from the PropertyBag, then update the value and re-save it using Save().

Deleting An Item From The Dynamic Data Store

To delete the store you can use the DeleteStore method. Warning: this can not be undone and will delete all data within the store ❌

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

One word of caution before you go off and use the dynamic data store in anger 😠. If you add/remove properties onto a class that inherits from IDynamicData, or you add or remove properties from 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 the next time you try and access existing data within the store. To get around this you can either delete all your stored data, which is not ideal. The other option would be to remap your property bag by adding the AutomaticallyRemapStoreattribute onto your custom DDS object, like so:

We've now covered the two different ways to store data within the dynamic data store. As you can see, the code to add custom objects into the store is simple enough. The main thing to keep in mind is you need an Identifier, after that you're all set. Happy Coding 🤘