Five New Optimizely Certifications are Here! Validate your expertise and advance your career with our latest certification exams. Click here to find out more

smithsson68@gmail.com
Jul 6, 2011
  6002
(2 votes)

Session and Unit Of Work for Dynamic Data Store

I’ve created a couple of classes that make it even easier to work with the Dynamic Data Store:

DynamicDataStoreSession

This wraps the main functionality of a DynamicDataStore instance with the following additional benefits:

  • Thread Safe – Ensures all calls to a session instance are done from the same thread that created it and throws an exception if not. This is done because a DynamicDataStore instance is not thread safe but has no enforcement thus resulting in weird errors when used from multiple threads
  • Specify Type Only Once - When working with the DDS you need to specify the Type in the call to DynamicDataStoreFactory.CreateStore/GetStore and then again when calling Load / Find / Items etc. on a DynamicDataStore instance. This is because the Type passed to CreateStore/GetStore is used to derive the store's name and the Type passed to Load / Find / Items defines what container to load the properties from the store into. The DynamicDataStoreSession is a generic class so the Type you pass as a generic argument is used in all cases where needed. E.g.
    var session = new DynamicDataStoreSession<Person>();
    var people = from person in session.Items
                 where person.FirstName == "Jones"
                 select person;
  • Find requires no magic strings - The Find method of the DynamicDataStore takes a string property name and value (or collection of). Some people quite rightly don't like using 'magic' strings in this way. The DynamicDataStoreSession provides a Find method where property names can be specified by using a lambda expression (thanks to Anders Hattestad for this). It also has a LINQ style method chaining syntax. E.g.
    var session = new DynamicDataStoreSession<Person>();
    var people = session.Find()
                 .Where(p => p.LastName)
                 .Equals("Jones")
                 .And(p => p.FirstName)
                 .Equals("Mark")
                 .Go();
  • IoC / Mocking / Unit Testing - The DynamicDataStoreSession and associated find classes implement interfaces. They also allow constructor injection of a DynamicDataStoreFactory

DynamicDataStoreUnitOfWork

It has always been possible to implement ‘unit of work’ / transactions between DDS stores and external data sources. The DynamicDataStoreUnitOfWork class wraps this functionality up nicely and should be used like a TransactionScope in a using block.

class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public Identity AddressId { get; set; }
}

public class Address
{
    public string Line1 { get; set; }
    public string Line2 { get; set; }
    public string City { get; set; }
}

using (var work = new DynamicDataStoreUnitOfWork())
{
    var addressSession = work.GetSession<Address>();
    var addressId = addressSession.Save(new Address()
    {
        Line1 = "Main Street",
        City = "Chicago"
    });

    var personSession = work.GetSession<Person>();
    personSession.Save(new Person()
    {
        FirstName = "Paul",
        LastName = "Smith",
        AddressId = addressId
     });

    work.Complete();
}

You can get an assembly here or if you want the source code then download the EPiServer.Extensions.DynamicDataStore.1.0.nupkg from nuget.episerver.com.

Jul 06, 2011

Comments

Please login to comment.
Latest blogs
Removing a Segment from the URL in Optimizely CMS 12 using Partial Routing

Problem Statement In Optimizely CMS 12, dynamically generated pages inherit URL segments from their container pages. However, in certain cases, som...

Adnan Zameer | Mar 14, 2025 |

Optimizely Configured Commerce and Spire CMS - Figuring out Handlers

I recently entered the world of Optimizely Configured Commerce and Spire CMS. Intriguing, interesting and challenging at the same time, especially...

Ritu Madan | Mar 12, 2025

Another console app for calling the Optimizely CMS REST API

Introducing a Spectre.Console.Cli app for exploring an Optimizely SaaS CMS instance and to source code control definitions.

Johan Kronberg | Mar 11, 2025 |

Extending UrlResolver to Generate Lowercase Links in Optimizely CMS 12

When working with Optimizely CMS 12, URL consistency is crucial for SEO and usability. By default, Optimizely does not enforce lowercase URLs, whic...

Santiago Morla | Mar 7, 2025 |