Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Caching strategy - usage on both WinForms and ASP.NET

Posted on 2009-05-11
5
Medium Priority
?
374 Views
Last Modified: 2012-05-06
Hi

I have a Class which i chose to implement like attached in my Winforms Application.
This works very and gives a great performance.

The problem now however is that we are building an ASP.NET application where i would like to reuse this code.
However the web application could be accessing different Data on each request, meaning the data stored in this Singleton would be invalid and the correct data should be loaded.

How can i implement this in a way where i get the performance from using singleton in my Windows Application but on my Web application i would like nothing to be stored for more than the current request

The Idea behind doing this is to enable very fast lazy loading on objects refering to these ShiftGroups.
Should i do this in some other way ?
using System;
using System.ComponentModel;
using System.Linq;
using System.Data.SqlClient;
namespace FlexyBook.Business.Shift
{
    public class ShiftGroups
    {
        static BindingList<ShiftGroup> m_loaded_groups;
        public static BindingList<ShiftGroup> GetShiftGroups()
        {
 
            if (m_loaded_groups == null)
            {
                LoadGroups();
            }
            return m_loaded_groups;
        }
        public static ShiftGroup GetShiftGroup(int groupID)
        {
            return GetShiftGroups().Where(mGroup => mGroup.ID == groupID).FirstOrDefault();
        }
        private static void LoadGroups()
        {
            m_loaded_groups = new BindingList<ShiftGroup>();
 
            using (SqlConnection cnx = Data.Connection)
            {
                cnx.Open();
                using (SqlCommand command = new SqlCommand("SELECT * FROM vagtgruppe_db;", cnx))
                {
                    SqlDataReader reader = command.ExecuteReader();
                    while (reader.Read())
                    {
                        int id = (int)reader["vagtgruppe_id"];
                        string name = (string)reader["vagtgruppe_navn"];
                        int area = (int)reader["vagtgruppe_area"];
                        int order = (int)reader["vagtgruppe_order"];
                        bool doPrint = ((int)reader["vagtgruppe_print"]) == 1;
                        ShiftGroup mGroup = new ShiftGroup(name, area, id, order, doPrint);
                        m_loaded_groups.Add(mGroup);
                    }
                    reader.Close();
                }
            }
        }
    }
}

Open in new window

0
Comment
Question by:mSchmidt
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 3

Expert Comment

by:OblivionSY
ID: 24355348
When i have implemented caching for both windows and web, I have generally implemented a new class for example "CacheHandler" and put a mehod in this class to GetShiftGroups.

This class then decides whether to serve up a windows cached item or a web cached item.

If the group list depends on the user (ie each web user has a different set of groups) this information will need to be passed into the CacheHandler.

Assuming there are not hundreds of users, you could store the lists in session which would be catered for each user automatically

Hope that helps
0
 

Author Comment

by:mSchmidt
ID: 24355402
That was what i had in mind, however could you show how you implemented this ?
0
 
LVL 3

Expert Comment

by:OblivionSY
ID: 24355648
It can be as complex as you want it to be really. In my implementation I created an Interface which defined all the methods a cache handler was suppose to be able to do (insert and retrieve) then created two implementations of this interface, one for windows and one for web. My cache handler merely instantiated the correct implementation class based on whether it was for windows or web. Your application just works with the interface, so doesnt care how the data is returned, merely that whatever implements those interfaces will guarantee to return something.

This could be incoporated into one file if you wanted to. How large scale is the project?

One file is simpler, but you have lots of "if online, then x else y" stuff all over the place. But the interface options (although IMO better) does take a little bit of setting up
0
 

Author Comment

by:mSchmidt
ID: 24355952
Ahh okay,

How did you check whether your application was online or not ?
0
 
LVL 3

Accepted Solution

by:
OblivionSY earned 2000 total points
ID: 24356925
System.Web.HttpContext (i think it is there), it will be null in a windows application.

Might be the .Current property. Sorry, not got access to Visual Studio at the moment.

It will need access to system.web even if its used in the windows environment (same code for both)
0

Featured Post

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question