Simple EPiServer Dynamic Data Store (DDS) layer

This is part of a short article series that have been laying around as drafts for some time, so I figured it was time to actually do something with it.

Here is a simple wrapper used for making dynamic data store code a bit easier to unit test.

public interface IDdsService<T>
{
  Guid Save(T item);
  T Load(Guid id);
  T[] Find(string propertyName, object value);
  void Delete(Identity id);
  void DeleteAll();
  IOrderedQueryable<T> GetAll();
  T[] LoadAll();
}

The default implementation of the interface is also rather straight forward.

public class DdsService<T> : IDdsService<T> where T : BaseData
{
  private readonly string _storeName;

  private DynamicDataStore GetStore()
  {
    if (string.IsNullOrWhiteSpace(_storeName))
    {
      return DynamicDataStoreFactory.Instance.GetStore(typeof(T)) ?? DynamicDataStoreFactory.Instance.CreateStore(typeof(T));
    }

    return DynamicDataStoreFactory.Instance.GetStore(_storeName) ?? DynamicDataStoreFactory.Instance.CreateStore(typeof(T));
  }

  public DdsService()
  {
    _storeName = null;
  }

  public DdsService(string storeName)
  {
    _storeName = storeName;
  }

  public virtual Guid Save(T item)
  {
    using (DynamicDataStore store = GetStore())
    {
      return store.Save(item).ExternalId;
    }                   
  }    

  public virtual T Load(Guid id)
  {
    using (DynamicDataStore store = GetStore())
    {
      return store.Load<T>(id);
    }
  }

  public virtual T[] Find(string propertyName, object value)
  {
    using (DynamicDataStore store = GetStore())
    {
      return store.Find<T>(propertyName, value).ToArray();
    }            
  }

  public virtual void Delete(Identity id)
  {
    using (DynamicDataStore store = GetStore())
    {
      store.Delete(id);
    }
  }

  public virtual void DeleteAll()
  {
    using (DynamicDataStore store = GetStore())
    {
      store.DeleteAll();
    }
  }

  public virtual IOrderedQueryable<T> GetAll()
  {
    using (DynamicDataStore store = GetStore())
    {
      return store.Items<T>();
    }
  }

  public virtual T[] LoadAll()
  {
    using (DynamicDataStore store = GetStore())
    {
      return store.LoadAll<T>().ToArray();
    }
  }
}