How to make a static class not so static?

I have a static class containing my database logic.

This class is used on a website, web services, and as part of a middleware component.

For each method in this class, I need a piece of contextual information from the caller. In the case of a website, this will be user information, for the web service and middleware component, this identifies the calling service.

I cannot save this value in the configuration, because it can be different for each user, and I do not always have httpcontext to get this.

I could easily add a new parameter for each method in this class, or I could change it from a static class so that it has one non-static property, but none of these solutions seem very elegant.

So, are there any other options that I have not considered?

+5
source share
2 answers

If all methods need some kind of state, it sounds just like you should instantiate and pass that state through the constructor.

Changing the design from a bunch of static methods to an instance will also facilitate class validation.

+6
source

I would add options. This does not seem ridiculous to me - you need contextual information, and the only way to get it in a static class is to pass it on.

+1

All Articles