views:

44

answers:

4

I have a class instance which is created by using Activator.CreateInstance() method. That class instance contains helper methods which are frequently used throughout the application. In order to avoid creating the instance multiple times, I'm just thinking about implementing an appropriate caching mechanism.

The following consideration should be taken into account:

1) I can't use static class and methods.

2) There are around 6 instances (1 instance per class) per App Domain.

Your suggestion would be much appreciated!

+1  A: 

If you want to avoid creating it multiple times, then don't use the ASP.Net cache object. The cache object specifically does not guarantee that anything you put in it will remain there. In fact it's one of the first things to be cannibalized if the server needs to free up resources.

A better option would be to use the HttpApplicationState object, which should be used to store objects that need to be globally accessible to all sessions. It also has built in thread safety if you access it properly.

The code to do it is as follows:

HttpContext.Current.Application.Lock();
HttpContext.Current.Application["myObject"] = myObject;
HttpContext.Current.Application.Unlock();

Utilizing it is just

var myObject = (MyObject)HttpContext.Current.Application["myObject"];
womp
"A better option would be to use the HttpApplicationState object" - IMHO even better would be a static instance, using any of the standard .NET synchronization mechanisms. Strongly-typed, so no need to cast and not tied to ASP.NET. I would only use the HttpApplicationState object for backwards compatibility with migrated classic ASP code.
Joe
+1 Thanks for your suggestion. I will try to figure it out.
Kthurein
A: 

Ad 1) How about a static container for your instance? Along the lines of a singleton pattern?

Ad 2) 6 singletons or one static generic singleton class.

P.S.: I guess the static restriction is meant only for the helper class itself?

P.P.S.: Using HttpContext.Current.Application would be pretty much the same approach, except slower.

Jaroslav Jandek
+1  A: 

Use a singleton pattern:

class MySingleton {

     private static MySingleton instance;

     public MySingleton {
          if(instance != null)
              // One already created, the only call to this
              // should come through Activator
              throw...
          instance = this;
     }

     public static MySingleton GetInstance() {
          if(instance == null) instance = new MySingleton();
          return instance;
     }

}

The activator uses the public constructor. Then you can still retrieve the instance through GetInstance().

Mau
+1 It could be a viable solution. Let me try to figure it out.
Kthurein
A: 

Sounds like a case for a dependency injection container. No matter which one you pick, they all have support for caching like a singleton, and it will do the Activator.CreateInstance part for you.

I like NInject for it's simplicity.

dave thieben