开发者

How long does a dataset stay in memory in a winforms application

I have a question regarding datasets in windows forms applications.

Part 1:

Question:

1) Is a dataset hitting the database every time it is instantiated.

Part 2:

Assumptions:

  • The answer to my first question is true
  • That the dataset I'm working with has a relatively small amount of data, and the application itself wont have a high number of concurrent users.
  • Number of forms may be quite high but requiring the same dataset.

Question:

1) Is it a bad idea to store a static version of the dataset in memory.

Options (rate these possible solutions):

Option 1: Create a static class that acts as a container for the dataset.

Option 2: Create a dataset inside the form where its' needed, then create a property for any subsequent forms that depend on that dataset and passing the reference to that dataset to that particular form thereby preventing the GC from reallocating the memory (and reducing number of database calls).

Pseudo-code: This is parent form that instantiates the dataset.

public partial class ParentForm: Form
{
   private DataSet dataset;

   public ParentForm()
   {
   //logic that populates dataset
   }

   Button_Click(object sender, EventArgs e)
   {
       DependentOnDataSetForm dependent = new DependentOnDataSetForm(dataset);
   }
}

Pseudo-code: This is a form that is dependent on that same dataset.

public partial class DependentOnDataSetForm : Form
{
   private DataSet dataset;

   public DependentOnDataSetForm (DataSet dataset)
   {
      this.dataset = dataset;
   }

   // Another from that depends on the same dataset.
   Button_Click(object sender, EventArgs e)
   {
       DependentOnDataSetForm2 dependent = new DependentOnDataSetForm2(dataset);
   }
}

Bonus Question: (doesn't apply to my current situation, but good to know for future reference).

Is it a good/bad idea to create more than one Da开发者_如何学运维taSet.xsd file that is only responsible for certain data required by certain forms. For example (purely hypothetical): lets say I have an orders service that is responsible for filling orders, this particular service would need access to a orders table, customers table, shipping table, etc. So is creating a .xsd file that housed only the needed tables a good idea?


Answer to question #1 is no. A DataSet object is considered an ADO.NET disconnected object. Therefore it does not hit the database. You may instantiate a DataSet with results from an ADO Connected object (i.e. DataAdapter) but the DataSet itself does not connect to a database.

I would choose Option 2 but be aware it's a reference object and changes in one form may affect another.


  1. Dataset only hits DB when being populated by using Fill() on tables or DataAdapters.
  2. It is worth to store a single instance of your data cache (which DataSet actually is). Of course if you don't mind taking extra care with updates. DataSets are often used for GUI data binding and it needs to be kept in mind that whatever changes happen to this DataSet - those should happen in UI thread.


You can always call the Dispose() method on your dataset when you are done with it. Then when you want to use it again, you can instantiate a new instance of it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜