views:

263

answers:

7

I am looking for opinions and best coding practices.

I need to get info from a database with a query such as SELECT this, that FROM MyDB (returning 2 fields).

  1. Would it be wrong to use a hashtable for temporary storage?

  2. Is there a better way to accomplish the same thing?

  3. What about if I am returning more than 2 fields, would it be best to step up to a DataSet or something?

I am looking for simple efficiency.

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        GetData();

        foreach (DictionaryEntry de in ht)
        {
            DoSomething(Convert.ToInt32(de.Key), de.Value.ToString());

            DoMore(Convert.ToInt32(de.Key), de.Value.ToString());

            //etc...
        }
    }

    Hashtable ht = new Hashtable();

    private void GetData()
    {
        //connect to DB and use reader to fill hashtable ht with results..
    }

    private void DoSomething(int key, string value)
    {
        //do something with hashtable ht data
    }

    private void DoMore(int key, string value)
    {
        //do something else with hashtable ht data
    }
}
A: 

Have you considered a DictionaryEntry for just one key/value pair or using a domain object with two named properties?

Abboq
A: 

What you are proposing is locally caching the entire table in your program. This has advantages and disadvantages:

Advantages:

  • Faster for lots of small queries, needs to communicate with the database only once.
  • You get a unchanging copy of the data without needing to hold a transaction open.
  • You can still access your data even if the database goes offline.

Disadvantages:

  • You don't get the live data.
  • Slow the first time, or on application startup, as it must read the entire table.
  • Uses a lot of memory on the client.
  • Updating your local copy won't automatically update the data in the database.

Conclusion:

Usually you shouldn't do this, you should just request the database for the values you want when you need them. But there are times where it might make sense.

Mark Byers
A: 

That will depends on how many records do you have on your table, and how 'random' the hash keys will be. You want to avoid keys being hashed to the same slot to avoid collisions, and thus performance degradation.

Freddy
+1  A: 

Your best bet is creating a real simple data object.

private class MyData
{
    public int Id { get; set; }
    // Etc
}

Next, use the strongly typed dictionary class.

Dictionary<int, MyData> myDbCache = new Dictionary<int, MyData>();
ChaosPandion
A: 

I would reccomend you to choose Dictionary<string,string> rather than HashTable,if you need to store key/par values.

A: 

dictionary is cool ;)

Swoosh
A: 

If your data needs to be accessed from several pages, which is probably the case, you can store them in a static field (indeed using a Dictionary) so it needs to be loaded once for the whole application (vs once per user).

Here encapsulated in a singleton:

public class Globals
{
  private Context _current;
  private Dictionary<int, string> _dbValues;

  public Dictionary<int, string> DbValues
  {
    get
    {
      if (_dbValues == Null)
      {
        // ... Load our data here
      }
      return _dbValues;
    }
    set
    {  
  }

  public Globals Current
  {
    get
    {
      if(_current == Null)
        _current = Context();
      return _current;
    }
  }    

  private Globals()
  {  }


}

// Can be used this way
var value = Globals.Current.DbValues[key1];
Benoit Vidis
thanks for the example, the Dictionary looks like the way to go.
dkirk