0

I currently have a class MyClass with a constructor that logs into a Web Service, returns a Session ID if it doesn't already have one, and saves it in a private static SomeWebService service attribute.

In my controller, a call would look like new MyClass().doSomething() which in turn would end up calling service.someWebServiceMethod() within MyClass.

Is it appropriate to continue creating a new MyClass for every call, seeing as I'm saving the session itself as static? Would it be more ideal to make SomeWebService service non-static and implement the Singleton pattern instead? Does it even matter?

I saw this thread as I was about to switch to Singleton and now I'm unsure which approach to take.

Community
  • 1
  • 1
Jtaylorapps
  • 5,680
  • 8
  • 40
  • 56
  • The question isn't whether it's appropriate, but whether it's appropriate in this case. If you don't have a compelling reason to, don't. In fact, I'd be concerned about storing that static value at all, although I don't know exactly what it's for. – Scott Hannen Jul 26 '16 at 18:22
  • Agreed, I phrased it this way after seeing the advice "never use a singleton on a database connection", so my question is "Can I use a singleton on a web service connection?" The reason that static value exists is because it stores a session for all future web service calls to use. If it wasn't static, it would have to have a new session ID generated for every call. – Jtaylorapps Jul 26 '16 at 18:42
  • What does the web service you're calling do with that session? – Scott Hannen Jul 26 '16 at 18:50
  • It uses the session ID stored within to authenticate requests for information via any other method than the Login() call. – Jtaylorapps Jul 26 '16 at 19:13

0 Answers0