Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to translate from IDictionary to a generic IDictionary

Tags:

c#

generics

I've got an IDictionary field that I would like to expose via a property of type IDictionary<string, dynamic> the conversion is surprisingly difficult since I have no idea what I can .Cast<>() the IDictionary to.

Best I've got:

IDictionary properties;
protected virtual IDictionary<string, dynamic> Properties {
  get { 
        return _properties.Keys.Cast<string>()
              .ToDictionary(name=>name, name=> _properties[name] as dynamic); 
      }
    }
like image 250
George Mauer Avatar asked Jun 02 '10 22:06

George Mauer


2 Answers

If the underlying type of the IDictionary does not implement IDictionary<string, dynamic>, you cannot cast the object, period. If it does, a simple cast via (IDictionary<string, dynamic>)localVar will suffice.

If it's not, there are two things you can do:

  1. Copy the IDictionary to your generic type.
  2. Build an Adapter class that accepts the IDictionary as a dependency and implements the generic IDictionary you want, mapping calls from one to the other.

Edit: The sample code you've just posted will copy the dictionary every time it gets called! I will edit again in a moment with some suggested code.

Option 1

your sample code approach is solid as a means of copying the data, but the copy should be cached or you're going to copy lots of times. I'd suggest you put the actual translation code into a separate method and call that from your property the first time it's used. For example:

private IDictionary dataLayerProperties; 
private IDictionary<string, dynamic> translatedProperties = null;
protected virtual IDictionary<string, dynamic> Properties
{
    if(translatedProperties == null)
    {
        translatedProperties = TranslateDictionary(dataLayerProperties);        
    }  
    return translatedProperties;
}

public IDictionary<string, dynamic> TranslateDictionary(IDictionary values)
{
    return values.Keys.Cast<string>().ToDictionary(key=>key, key => values[key] as dynamic);            
}

Now, there are obvious cons to this approach... what if dataLayerProperties needs to be refreshed? You have to go setting translatedProperties to null again, etc.

Option 2

This is my preferred approach.

public class TranslatedDictionary : IDictionary<string, dynamic>
{
    private IDictionary Original = null;

    public TranslatedDictionary(IDictionary original)
    {
        Original = original;
    }
    public ICollection<string> Keys
    {
        get
        {
            return Original.Keys.Cast<string>().ToList();
        }
    }

    public dynamic this[string key]
    {
        get
        {
            return Original[key] as dynamic;
        }
        set
        {
            Original[key] = value;
        }
    }
    // and so forth, for each method of IDictionary<string, dynamic>
}

//elsewhere, using your original property and field names:
Properties = new TranslatedDictionary(properties);

Now, there are obvious cons to this approach as well, the most glaring is the fact that the Keys (and Value and anything else that returns ICollection on IDictionary has to return a new array for every call. But this still allows the most flexible approach, since it ensures the data is always up to date.

like image 96
Randolpho Avatar answered Oct 19 '22 03:10

Randolpho


Unless the backing type for the IDictionary instance already implements IDictionary<string,dynamic> (like Dictionary<string,dynamic>) then casting won't help you. The Cast<>() method is only useful for returning IEnumerable<T> values and normal casting isn't an option.

If providing the data in the form of IDictionary<string,dynamic> is important, then why not go ahead and store it as a Dictionary<string,dynamic> from the start?

like image 2
JaredPar Avatar answered Oct 19 '22 04:10

JaredPar