Centralised DataSet

My mainform opens a form as modal on which I have dropped two tables. However every time the form is shown, which is frequent, the dataset etc has to be initialized and loaded, and this causes a delay of a couple of seconds before the for opens.

So I thought I could centralise the DataSet and tables etc so they could remain in memory throughout the app running.

One way I have done this was to drop these tables onto the main form and delete the navigator and dataview thus leaving the DataSet etc. I then created public fields in the modal form and when I show the form I set these fields to point to the equivilent fields in the main form.

Is this the best way of doing things?

[714 byte] By [Chardiot] at [2008-1-2]
# 1

I would create a managerial class that exposes the data set as a (static) property. When the property is called the first time it loads the data set. All forms (including the main form) then use the property as their input rather than using a data source. This eliminates the overhead. Of course the managerial class can use a data source internally (it doesn't have to have a UI to use a data source) but you'll have to set up the code manually since the designer won't help you. It is pretty straightforward though.

However unless all your forms are showing variantions on a theme you might want to evaluate why you are loading so much data to begin with. If you are showing information from table A then you really shouldn't be loading tables B, C and D. This just slows things down. A better approach is to load only the data you need. This will speed things up. If you still have performance problems then caching may help. Also be aware that data sets have a high memory and bandwidth overhead so unless you want to edit all the data then a data reader might be a faster, albeit readonly, approach.

Michael Taylor - 4/30/07

http://p3net.mvps.org

TaylorMichaelL at 2007-9-13 > top of Msdn Tech,.NET Development,.NET Framework Data Access and Storage...
# 2

Here is an example :

public partial class Form4 : Form

{

public Form4()

{

InitializeComponent();

}

private void Form4_Load(object sender, EventArgs e)

{

DataTable Source = DataProvider.DefaultSource.DataSource;

dataGridView1.DataSource = Source;

}

}

public class DataProvider

{

private static DataProvider mDefaultSource = new DataProvider();

public static DataProvider DefaultSource

{

get { return mDefaultSource; }

set { mDefaultSource = value; }

}

private DataTable mDataSource;

public DataTable DataSource

{

get { return mDataSource; }

set { mDataSource = value; }

}

public DataProvider()

{

mDataSource = new DataTable("Data Source");

DataSource.Columns.Add(new DataColumn("First Name", typeof(String)));

DataSource.Columns.Add(new DataColumn("Last Name", typeof(String)));

Int32 Index = 0;

Int32 Length = 15;

for (Index = 0; Index < Length; Index++)

{

DataSource.Rows.Add(String.Format("First {0}", Index + 1), String.Format("Last {0}", Index + 1));

}

String FilePath = @"C:\text.txt";

if (!File.Exists(FilePath))

{

File.Create(FilePath);

}

}

}

decyclone at 2007-9-13 > top of Msdn Tech,.NET Development,.NET Framework Data Access and Storage...
# 3

Thankyou for the replies - I will have to study them before I understand them.

What is a "managerial class "?

Chardiot at 2007-9-13 > top of Msdn Tech,.NET Development,.NET Framework Data Access and Storage...
# 4

A managerial class is simply a class (generally static) that manages other types. It is similar to a factory class if you follow design patterns but instead of necessarily creating instances of other types it provides support for working with other types. For example we might have an Employee class that represents an employee in a company. Rather than pollute the class with the functionality needed to retrieve the employee from a database, add new employees, update existing employees, etc. we create a managerial class that handles this for us. This gives us a clean Employee class. Depending on one's style and need the managerial class is often combined with the class it manages. For example:

Code Snippet

public class Employee
{

public int Id { ... }

public string FirstName { ... }

public string LastName { ... }

...

public void Update ( ) { Employees.Update(this); }

public void Delete ( ) { Employees.Delete(Id); }

...
}

public static class Employees
{

public static Employee[] GetEmployees ( ) { ... }

public static Employee GetEmployee ( int id ) { ... }

public static Employee GetEmployee ( string name ) { ... }

public static void Add ( Employee emp ) { ... }

public static void Update ( Employee emp ) { ... }

public static void Delete ( int id ) { ... }

...
}

Some people name their managerial class -Manager. I prefer to append a -s to the end. The nice thing about managerial classes is that you can embed some additional logic about how instances are managed without to much work. For example I could easily add caching to the managerial class for performance reasons and no one would care. Even better though is the fact that I've separated management and storage of the data from the actual business object.

Again, you can combine the managerial class with the class it manages (and I do so a lot myself) but if the management aspect starts to get complex then you should definitely create a separate class. In the .NET framework managerial classes have a tendency to be combined with the class they manage (i.e. Screen, ServiceController, Process).

Michael Taylor - 5/2/07

http://p3net.mvps.org

TaylorMichaelL at 2007-9-13 > top of Msdn Tech,.NET Development,.NET Framework Data Access and Storage...
# 5
I will keep on reading it till I understand - lol
Chardiot at 2007-9-13 > top of Msdn Tech,.NET Development,.NET Framework Data Access and Storage...

.NET Development

Site Classified