tags:

views:

28

answers:

2

I have this DB Table of 'Settings' that's like:

MopedID, Name, Value

I want to make an object in my Base Controller that is populated with attributes from whatever is stored there.

So if it's:

32, "EnableHyperCanon", "True"

The object at run time when I access it from any Controllers that inherit from the Base will have access to MopedSettings.EnableHyperCanon which would be the string value "True".

The object would only ever have attributes that it find for ID 32.

Any idea?

EDIT: Maybe a better idea would be MopedSettings["EnableHyperCanon"] is there a way to create an object that works this way?

A: 

If you're running on .NET 4.0, you can use ExpandoObject.

Stephen Cleary
A: 

To make your object indexable:

class MopedSettings
{
    private Dictionary<string,object> _dic;
    public object this[string s] { get { return _dic[s]; }; set { _dic[s] = value; } }
}

I hope this helps.

FallingBullets
thanks, VS doesn't like some of your semicolons though
shogun
Strangely I get an object reference is null error when trying to populate an instance of this object:List<CompanySetting> allSettings = (from cs in db.CompanySettings where cs.CompanyID == company.CompanyID select cs).ToList(); foreach (CompanySetting cs in allSettings) { companySettings[cs.Name] = cs.Value; }
shogun
yeah, you'll have to initialize the Dictionary (`_dic = new Dictionary<string,object>();`), and the semicolon after the `get{}` statement isn't needed.
FallingBullets