Jonas Bergqvist
Feb 25, 2011
  5339
(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
Optimizely Forms: You cannot submit this form because an administrator has turned off data storage.

Do not let this error message scare you, the solution is quite simple!

Tomas Hensrud Gulla | Oct 4, 2024 | Syndicated blog

Add your own tools to the Optimizely CMS 12 admin menu

The menus in Optimizely CMS can be extended using a MenuProvider, and using the path parameter you decide what menu you want to add additional menu...

Tomas Hensrud Gulla | Oct 3, 2024 | Syndicated blog

Integrating Optimizely DAM with Your Website

This article is the second in a series about integrating Optimizely DAM with websites. It discusses how to install the necessary package and code t...

Andrew Markham | Sep 28, 2024 | Syndicated blog

Opticon 2024 - highlights

I went to Opticon in Stockholm and here are my brief highlights based on the demos, presentations and roadmaps  Optimizely CMS SaaS will start to...

Daniel Ovaska | Sep 27, 2024

Required fields support in Optimizely Graph

It's been possible to have "required" properties (value must be entered) in the CMS for a long time. The required metadata haven't been reflected i...

Jonas Bergqvist | Sep 25, 2024

How to write a bespoke notification management system

Websites can be the perfect vehicle for notifying customers of important information quickly, whether it’s the latest offer, an operational message...

Nicole Drath | Sep 25, 2024