Try our conversational search powered by Generative AI!

smithsson68@gmail.com
Jul 6, 2011
  5745
(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
Optimizely and the never-ending story of the missing globe!

I've worked with Optimizely CMS for 14 years, and there are two things I'm obsessed with: Link validation and the globe that keeps disappearing on...

Tomas Hensrud Gulla | Apr 18, 2024 | Syndicated blog

Visitor Groups Usage Report For Optimizely CMS 12

This add-on offers detailed information on how visitor groups are used and how effective they are within Optimizely CMS. Editors can monitor and...

Adnan Zameer | Apr 18, 2024 | Syndicated blog

Azure AI Language – Abstractive Summarisation in Optimizely CMS

In this article, I show how the abstraction summarisation feature provided by the Azure AI Language platform, can be used within Optimizely CMS to...

Anil Patel | Apr 18, 2024 | Syndicated blog

Fix your Search & Navigation (Find) indexing job, please

Once upon a time, a colleague asked me to look into a customer database with weird spikes in database log usage. (You might start to wonder why I a...

Quan Mai | Apr 17, 2024 | Syndicated blog