smithsson68@gmail.com
Jun 9, 2010
  10896
(2 votes)

Best (and fastest) ways to load items from the Dynamic Data Store

There are essentially 4 ways to load items from the Dynamic Data Store, depending upon what information you have and what you want back in response. Below I am going to detail the methods in (probable) speed order. Note the speed order depends how many items are fetched from the store.

Load

Usage Scenario: Load a single item using its id.

If you have the Identity of an item (or at least it’s Guid part) then this should be by far the fastest way to load a single item.

Example:

Identity id = ObtainId();
DynamicDataStore store = DynamicDataStoreFactory.Instance.GetStore(typeof(MyType));

if (store != null)
{
    MyType obj = store.Load<MyType>(id);
}

If the item is already in the DDS cache then then no database hit is required at all.

Find

Usage Scenario: Load one or more items that directly match one or more name/value pairs, without paging.

Example 1: Single match criteria

DynamicDataStore store = DynamicDataStoreFactory.Instance.GetStore(typeof(MyType));

if (store != null)
{
    IEnumerable<MyType> items = store.Find<MyType>("MyIntProperty", 34);
}

The ids of the matching items will be read from the database and then each item either read from the DDS cache or from the database. This is faster than using Linq because expressions are not used and a direct SQL statement can be generated and executed.

Example 2: Multi match criteria

DynamicDataStore store = DynamicDataStoreFactory.Instance.GetStore(typeof(MyType));

if (store != null)
{
    IEnumerable<MyType> items = store.Find<MyType>
    (
        new Dictionary<string, object>()
        {                    
            {"MyIntProperty", 34},
            {"MyStringProperty", "SomeValue"}
        }
    );
}

Linq (Items)

Usage Scenario: Load one or more items with complex parameter matching and paging. Items can also be projected into new types.

Example:

DynamicDataStore store = DynamicDataStoreFactory.Instance.GetStore(typeof(MyType));

if (store != null)
{
    var query = from item in store.Items<MyType>()
                where item.MyIntProperty > 33 &&
                item.MyStringProperty.Contains("omeval")
                select item;

    IEnumerable<MyType> items = query.Skip(100).Take(25).ToList();
}

The ids of the matching items will be read from the database and then each item either read from the DDS cache or from the database.

LoadAll

Usage Scenario: Load all items from a store without paging

Example:

DynamicDataStore store = DynamicDataStoreFactory.Instance.GetStore(typeof(MyType));

if (store != null)
{
    IEnumerable<MyType> items = store.LoadAll<MyType>();
}

The ids of the matching items will be read from the database and then each item either read from the DDS cache or from the database. If all items are required from a store then this should be marginally faster than using the Linq (Items) methods as LoadAll does not use expressions and a direct SQL statement can be generated and executed.

For loading items back as PropertyBag or types other than the type that was saved see my blog post about Data Container Indifference

Feedback and comments are as always very welcome.

Jun 09, 2010

Comments

Sep 21, 2010 10:33 AM

Very nice to see the different methods.
Is there a way to see all the different datastores that exists?

If you don't want to hardcode the property names like this
store.Find("MyIntProperty", 34);
you could use
store.Find(MembersOf(s=>s.MyIntProperty), 34);

public static class MembersOf
{
public static string GetName(Expression> expr)
{
var node = expr.Body as MemberExpression;
if (object.ReferenceEquals(null, node))
throw new InvalidOperationException("Expression must be of member access");
return node.Member.Name;
}
}

/ Anders Hattestad

Sep 21, 2010 10:33 AM

MembersOf.GetName(s=>s.MyIntProperty)
/ Anders Hattestad

Frederik Vig
Frederik Vig Sep 21, 2010 10:33 AM

@Anders: EPiServer.Data.Dynamic.StoreDefinition.GetAll();

Please login to comment.
Latest blogs
Opti ID overview

Opti ID allows you to log in once and switch between Optimizely products using Okta, Entra ID, or a local account. You can also manage all your use...

K Khan | Jul 26, 2024

Getting Started with Optimizely SaaS using Next.js Starter App - Extend a component - Part 3

This is the final part of our Optimizely SaaS CMS proof-of-concept (POC) blog series. In this post, we'll dive into extending a component within th...

Raghavendra Murthy | Jul 23, 2024 | Syndicated blog

Optimizely Graph – Faceting with Geta Categories

Overview As Optimizely Graph (and Content Cloud SaaS) makes its global debut, it is known that there are going to be some bugs and quirks. One of t...

Eric Markson | Jul 22, 2024 | Syndicated blog

Integration Bynder (DAM) with Optimizely

Bynder is a comprehensive digital asset management (DAM) platform that enables businesses to efficiently manage, store, organize, and share their...

Sanjay Kumar | Jul 22, 2024

Frontend Hosting for SaaS CMS Solutions

Introduction Now that CMS SaaS Core has gone into general availability, it is a good time to start discussing where to host the head. SaaS Core is...

Minesh Shah (Netcel) | Jul 20, 2024

Optimizely London Dev Meetup 11th July 2024

On 11th July 2024 in London Niteco and Netcel along with Optimizely ran the London Developer meetup. There was an great agenda of talks that we put...

Scott Reed | Jul 19, 2024