Jonas Bergqvist
Feb 25, 2011
  5278
(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
        {
            get
            {
                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

Comments

Please login to comment.
Latest blogs
Opti ID overview

Opti ID allows you to log in once and switch between Optimizely products using Okta, Entra ID, or a local account. You can also manage all your use...

K Khan | Jul 26, 2024

Getting Started with Optimizely SaaS using Next.js Starter App - Extend a component - Part 3

This is the final part of our Optimizely SaaS CMS proof-of-concept (POC) blog series. In this post, we'll dive into extending a component within th...

Raghavendra Murthy | Jul 23, 2024 | Syndicated blog

Optimizely Graph – Faceting with Geta Categories

Overview As Optimizely Graph (and Content Cloud SaaS) makes its global debut, it is known that there are going to be some bugs and quirks. One of t...

Eric Markson | Jul 22, 2024 | Syndicated blog

Integration Bynder (DAM) with Optimizely

Bynder is a comprehensive digital asset management (DAM) platform that enables businesses to efficiently manage, store, organize, and share their...

Sanjay Kumar | Jul 22, 2024

Frontend Hosting for SaaS CMS Solutions

Introduction Now that CMS SaaS Core has gone into general availability, it is a good time to start discussing where to host the head. SaaS Core is...

Minesh Shah (Netcel) | Jul 20, 2024

Optimizely London Dev Meetup 11th July 2024

On 11th July 2024 in London Niteco and Netcel along with Optimizely ran the London Developer meetup. There was an great agenda of talks that we put...

Scott Reed | Jul 19, 2024