What is my choice in this situation? Thanks.

  • Thread starter Thread starter davidw
  • Start date Start date
D

davidw

I created my own WebControl, I am thinking a way to put some logics in the
class to a seperated class, and it will be accessed by all instances of my
WebControl, the idea is that the class doesn;t need to be created everytime,
and data in it can be retrieved from database one time only. What can I
achieve this? static method in class? multi-thread class? or cache?


Thanks
 
make it singleton and put into the cache

something like

public class CachedLogic
{
private static CachedLogic m_instance;

public static CachedLogic Instance
{
get
{
if ( m_instance == null)
m_instance = new CachedLogic();
return ( m_instance );
}
}
}

And then in the Web page or in the Web Service you can use Application cache
e.g
CachedLogic logic;
logic = Application["MyCachedLogic"] ;
if ( logic == null )
{
logic = CachedLogic.Instance;
this.Application["MyCachedLogic"] = logic;
}
 
Vadym Stetsyak said:
make it singleton and put into the cache

something like

public class CachedLogic
{
private static CachedLogic m_instance;

public static CachedLogic Instance
{
get
{
if ( m_instance == null)
m_instance = new CachedLogic();
return ( m_instance );
}
}
}

And then in the Web page or in the Web Service you can use Application cache
e.g
CachedLogic logic;
logic = Application["MyCachedLogic"] ;
if ( logic == null )
{
logic = CachedLogic.Instance;
this.Application["MyCachedLogic"] = logic;
}

davidw said:
I created my own WebControl, I am thinking a way to put some logics in the
class to a seperated class, and it will be accessed by all instances of my
WebControl, the idea is that the class doesn;t need to be created everytime,
and data in it can be retrieved from database one time only. What can I
achieve this? static method in class? multi-thread class? or cache?


Thanks
 
Vadym Stetsyak said:
make it singleton and put into the cache

something like

public class CachedLogic
{
private static CachedLogic m_instance;

public static CachedLogic Instance
{
get
{
if ( m_instance == null)
m_instance = new CachedLogic();
return ( m_instance );
}
}
}

That's not a thread-safe singleton pattern - there are simpler patterns
which *are* thread-safe, however. See
http://www.pobox.com/~skeet/csharp/singleton.html
 
Back
Top