Imagine I have a class MyTestClass. And I need an instance of this Type throughout my whole web application.
Now there are several possibilities to accomplish this.
1. Make MyTestClass static, make it contain static methods only
Probably the most performant solution. I'm not feeling lucky about using static fields though. Thread safety? What if my static class contained a static System.Collections.Queue? Good idea? Bad idea? Better idea?
What's the best way to write into a Queue from many different threads at the same time anyway...
2. Make MyTestClass a singleton
Good. However I don't really like the idea of checking for an instance of a Type every time I call a method. Performance is an issue. Imagine for example a logging class.
Would it make any sense to store a reference to my singleton instance somewhere to access it directly? I'd prefer something like HttpContext.Current.MyTestClass.
Is this a common approach? It should be possible using the decorator pattern I guess... probably not that easily - I didn't look into it yet.
Any suggestions? :-)
View Complete Post