I Need A Dip

Sunday, January 29, 2006

Singleton pattern on the web

I would love some feed back so please don't hold back.

I have a webservice that is responsible for formatting tons of information that is then queried by other applications (agents) that utilize it (the info packets).

All this "formatting" and logic I am talking about needs to be done in a centralized place and accessed by the agents. That is why we chose to go with a web service. This formatting and logic also needs to be done and held in memory so the agents don't have to wait for it. So we decided to implement a sort of singleton pattern. What do you guys think?

   1:  public class PacketManager 
   2:  { 
   3:      private static object lockObject = new Object(); 
   4:      private const string CACHE_MANAGER = "Mngr:PacketManager"; 
   5:      private List<InfoPacket> _preparedPackets; 
   6:   
   7:      private PacketManager() { initFunction(); } 
   8:   
   9:      public static InfoPacket GetInfoPacket() { return Instance.getInfoPacket(); } 
  10:   
  11:      public InfoPacket getInfoPacket() { ......some code ..... return this._preparedPackets[0]; } 
  12:   
  13:      private static PacketManager Instance 
  14:      { 
  15:          get {
  16:              lock(lockObject){
  17:              
  18:                  PacketManager oManager;
  19:              
  20:                  object o = System.Web.HttpContext.Current.Application.Get(CACHE_MANAGER);
  21:                  if(o==null){
  22:                      oManager = new PacketManager();
  23:                      System.Web.HttpContext.Current.Application.Add(CACHE_MANAGER,oManager);
  24:                  } else oManager = (PacketManager)o;
  25:                  
  26:                  return oManager;                
  27:              }
  28:                  
  29:          }
  30:      }
  31:  } 


Here is more information on the Singleton Pattern: http://www.yoda.arachsys.com/csharp/singleton.html

0 Comments:

Post a Comment

<< Home