Jonas Bergqvist
Feb 25, 2011
(1 votes)

Typed “Find” method in Dynamic data store

On the morning train to work, I played around with the DDS, and made a couple of extension methods to fix something I’ve been irritated about for a while.

The “typed” find method in the DDS takes the name of a property, and the value the property must have to receive the object from the store. This isn’t that nice, and I’m the one to blame. Therefore, I’ve created the following two extensions:

public static IEnumerable<TStore> Find<TStore, TFind>(this DynamicDataStore store, Expression<Func<TStore, TFind>> property, TFind value)
        string propertyName = MultipleFind<TStore>.GetPropertyName<TFind>(property);
        return store.Find<TStore>(propertyName, value);
public static IEnumerable<TStore> Find<TStore>(this DynamicDataStore store, MultipleFind<TStore> find)
    return store.Find<TStore>(find.Dictionary);

Those extension methods uses the following class that I needed to create:

    public class MultipleFind<TStore>
        private Dictionary<string, object> _find;
        public MultipleFind()
            _find = new Dictionary<string, object>();
        public void Add<TFind>(Expression<Func<TStore, TFind>> property, TFind value)
            _find.Add(GetPropertyName<TFind>(property), value);
        public static string GetPropertyName<TFind>(Expression<Func<TStore, TFind>> property)
            MemberExpression member = property.Body as MemberExpression;
            if (member == null)
                throw new NotSupportedException("Find method only works directly against properties at the moment");
            return member.Member.Name;
        public Dictionary<string, object> Dictionary
                return new Dictionary<string, object>(_find);

Those can be used like this:

var result = _personStore.Find<Person, int>(p => p.ShoeSize, 40);
MultipleFind<Person> findings = new MultipleFind<Person>();
findings.Add(p => p.ShoeSize, 40);
findings.Add(p => p.FirstName, "Jonas");
var result2 = _personStore.Find<Person>(findings);

Compared with:

var result = personStore.Find<Person>("ShoeSize", 40);
Dictionary<string, object> search = new Dictionary<string, object>();
search.Add("ShoeSize", 40);
search.Add("FirstName", "Jonas");
var result2 = personStore.Find<Person>(search);
Feb 25, 2011


Please login to comment.
Latest blogs
Content Delivery API – The Case of the Duplicate API Refresh Token

Creating a custom refresh provider to resolve the issues with duplicate tokens in the DXC The post Content Delivery API – The Case of the Duplicate...

David Lewis | Sep 29, 2022 | Syndicated blog

New Optimizely certifications - register for beta testing before November 1st

In January 2023, Optimizely is making updates to the current versions of our certification exams to make sure that each exam covers the necessary...

Jamilia Buzurukova | Sep 28, 2022

Optimizely community meetup - Sept 29 (virtual + Melbourne)

Super excited to be presenting this Thursday the 29th of September at the Optimizely community meetup. For the full details and RSVP's see the...

Ynze | Sep 27, 2022 | Syndicated blog

Preview multiple Visitor Groups directly while browsing your Optimizely site

Visitor groups are great - it's an easy way to add personalization towards market segments to your site. But it does come with it's own set of...

Allan Thraen | Sep 26, 2022 | Syndicated blog

The Report Center is finally back in Optimizely CMS 12

With Episerver.CMS.UI 12.12.0 the Report Center is finally re-introduced in the core product.

Tomas Hensrud Gulla | Sep 26, 2022 | Syndicated blog

Dynamic Route in ASP.NET Core When MapDynamicControllerRoute Does Not Work

Background Creating one of the add-on for Optimizely I had to deal with challenge to register dynamically route for the API controller. Dynamic rou...

valdis | Sep 25, 2022 | Syndicated blog