Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Performance for GridView - Where to store Data

Status
Not open for further replies.

tektipster79

Programmer
Oct 1, 2007
34
US
I have some data that I am "pivoting" and storing in a DataTable. The original subset of data needs to be filtered various ways, then pivoted and bound to a GridView. The data is display only, I don't need to allow for editing, etc. I want to make sure that I'm not creating copies of DataTables all over the place for the different users who are coming to the site. Should I
store the original DataSet in an Application variable and then create the DataTables off of that? I don't want to be hitting the database all the time since this data really doesn't change.
 
here's how I would approach the problem
Code:
interface IRepostistory
{
  DataTable GetData();
}

class Repository : IRepository
{
   public DataTable GetData()
   {
      //your sql commands here
      return new DataTable();
   }
}

interface ILogic
{
   DataTable GetPivotTable();
}

class Logic : ILogic
{
   private IRepository repository;

   public Logic(IRepository repository)
   {
       this.repository = repository
   }

   public DataTable GetPivotTable()
   {
        DataTable results = repository.GetData();
        //preform business logic;
        return resutls; //or a new DataTable, or whatever.
   }
}

class MyPage : Page
{
    override onLoad(EventArgs e)
    {
        if(!IsPostBack)
        {
            MyRepeater.DataSource = new Logic(new Repository()).GetPivotTable();
            MyRepeater.DataBind();
        }
    }
}
i would use a repeater because the data is read only, no need to carry the extra overhead of the gridview.

this would be my starting point. If I found there to be an unexceptable preformannce hit I would then determine where my bottleneck is (Logic or Repository) I would then create a cachable object. for this example I will use Repository as the bottleneck
Code:
public class CachedRepository
{
   private IRepository underlyingRepository;

   public CachedRepository(IRepository underlyingRepository)
   {
      this.underlyingRepository = underlyingRepository;
   }

   public GetData()
   {
      DataTable results = Cache["data"] as DataTable;
      if(results == null)
      {
         results = underlyingRepository.GetData();
         Cache["data"] = results;
      }
      return results;
   }
}
technically I solve it a little differently, because I let Windsor manage the state of my objects. that discussion is beyond the scope of this post, so for now we will just access the asp cache.

for the on load event we can not tweak it to use the new cached repository
Code:
class MyPage : Page
{
    override onLoad(EventArgs e)
    {
        if(!IsPostBack)
        {
            MyRepeater.DataSource = new Logic(new CacheRepository(new Repository())).GetPivotTable();
            MyRepeater.DataBind();
        }
    }
}

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top