Jonas Bergqvist
Dec 3, 2013
  3814
(3 votes)

Batch update with lambda expression in DDS (EPiServer 7.5)

Introduction

When Dynamic Data Store was introduced in CMS 6, developers could directly use a LINQ provider to receive data from their stores. The LINQ provider has got some small improvements sense CMS 6, but there haven’t been any new big features.

I’m currently working in the Commerce team in Stockholm, and we’re using the DDS to store versions of catalog content. In some scenarios, for example when changing supported languages in the catalog, we had to update a lot of versions in DDS. Before 7.5, the only way to do this was to load all the objects, change the property, and save them all. This isn’t very good, so we added a new feature to DDS to solve the problem for us.

I’m now happy to present the first version of batch update in DDS. The batch update only supports simple scenarios in this first version, but can probably make some developers life a little bit easier.

Update<T>

In the DynamicDataStore class, we have now added a typed method called “Update<T>”, similar to “Items<T>”. The big difference is that “Items<T>” returns an IOrderedQueryable<T> object, which is a LINQ interface, while “Update<T>” returns an interface called “IUpdateQueryable<T>”. The later interface is an EPiServer interface, and contains tree methods, “Set”, “Where”, and “Execute”.

  public interface IUpdateQueryable<TStore>
  {
    IUpdateQueryable<TStore> Set<TUpdate>(Expression<Func<TStore, TUpdate>> property, TUpdate value);

    IUpdateQueryable<TStore> Set<TUpdate>(Expression<Func<TStore, TUpdate>> property, Expression<Func<TStore, TUpdate>> value);

    IUpdateQueryable<TStore> Where(Expression<Func<TStore, bool>> predicate);

    int Execute();
  }

Set

The set method can be used to update specific properties. There are two overloads, one that sets a property to a constant value, and one that set the value using another property. Currently, the set method only supports the inline types in DDS, and types that uses type handlers (ITypeHandler) to map the type to an inline type.

Inpline types are:

  • System.Byte (and arrays of)
  • System.Int16
  • System.Int32
  • System.Int64
  • System.Enum
  • System.Single
  • System.Double
  • System.DateTime
  • System.String
  • System.Char (and arrays of)
  • System.Boolean
  • System.Guid
  • EPiServer.Data.Identity

Set property to constant value

The following line will update “MyString” property of all objects in the store:

Store.Update<MyObject>().Set(x => x.MyString, ”Hello World”).Execute();

Set property using a property

The following line will increase the “MyInt” property of all objects in the store:

Store.Update<MyObject>().Set(x => x.MyInt, y => y.MyInt + 1).Execute();

Only inline types directly on the store are supported

We only support inline types directly on the object. This is a limitation in this first version. If you have an object stored in DDS, which contains a reference type with inline types, you will not be able to do like this (trying to do this will result in an exception):

Update<T>().Set(x => x.MyComplecObject.MyString, “Hello World”).Execute(). 

Where

In most case, the update operation should only be done for some objects in the store. By using the “Where” method, just in the same way as LINQ, it’s possible to accomplish this.

Store.Update<MyObject>().Set(x => x.MyString, ”Hello World”).Where(x => x.MyInt == 3).Execute();

Execute

The execute method will, just as the name say, execute the query. The method will return number of updated rows.

Dec 03, 2013

Comments

Justin Le
Justin Le Dec 4, 2013 08:02 AM

Really nice!

Please login to comment.
Latest blogs
How I Fixed DLL Conflicts During EPiServer CMS Upgrade to .NET Framework 4.8.1

We had a CMS solution of EPiServer 11.26.0, which was built on .NET Framework 4.7.1. We needed to update the target framework from .NET Framework...

calimat | Dec 12, 2024

Custom form element view in Optimizely CMS 12

Do you want full control over the form element markup? Create your own views!

Tomas Hensrud Gulla | Dec 11, 2024 | Syndicated blog

How to Elevate Your Experimentation - Opticon workshop experience

As a non-expert in the field of experimentation, I’d like to share my feedback on the recent Opticon San Antonio workshop session titled "How to...

David Ortiz | Dec 11, 2024

Persisting a Strawberry Shake GraphQL Client for Optimizely's Content Graph

A recent CMS project used Strawberry Shake to generate an up-to-date C# GraphQL client at each build. But what happens to the build if the GraphQL...

Nicholas Sideras | Dec 11, 2024 | Syndicated blog

Opti ID with Secure Cookies And Third Party AddOns

Opti ID has revolutionised access to the Optimizely One suite and is now the preferred authentication method on all PAAS CMS websites that I build....

Mark Stott | Dec 9, 2024

AsyncHelper can be considered harmful

.NET developers have been in the transition to move from synchronous APIs to asynchronous API. That was boosted a lot by await/async keyword of C#...

Quan Mai | Dec 4, 2024 | Syndicated blog