troubleshooting Question

Should I use a Singleton or a cached object?

Avatar of ppastor
ppastor asked on
14 Comments2 Solutions506 ViewsLast Modified:
I am build ing an ASP.NET application and the pages and user controls interact with a library of custom business objects for getting data, etc...
In addition, I have designed things (so far) so that the business objects could be used by a winForms application if the client should decide to go down that path as well.
Finally, I am a big fan of declaring TraceSwitches in the application's config file (either web or app) because of the flexibility of being able to turn these switches on or off at runtime. The downside to these traceswitches is that you must instantiate a new TraceSwitch object when you want to determine the current switch setting. If your code uses a "reasonable" number of trace messages, it seems that instantiating a TraceSwitch object on every call could be an expensive proposition.

With that in mind, I have been considering options to minimize the number of TraceSwitch instantiations.

One thought was to create a "thread-safe" Singleton class in my business object library. This would statisfy my desire for something that could be called by the web app or the business tier, but I am concerned about performance. My understanding is that all requests within the aspnet_wp would be funneled through this instance, and that would probably be very detrimental.

Another option is to create an instance of the TraceSwitch object in the HttpCache. This should perform much better than the Singleton, and all of the UserControls and current aspx page could access this instance. The downside here is that I still do not have a solution for the business tier (unless I just assume that there is an HttpContext to hook into...but that breaks my hopes of porting to WinForms).

A solution to this is greatly appreciated!

Thank you,

Jesse Houwing
Scrum Trainer | Microsoft MVP | ALM Ranger | Consultant

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 2 Answers and 14 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 14 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros