• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 374
  • Last Modified:

Class Module using Session State Access now also needs to be used by windows application

I have a class module called by a web application that will set its connection string based on a session state variable set by the web app when a user logs on (actually multiple class modules).  The connection string is set when the class is created.  I now need to use the same class module in a windows application.  

Best solution I've come up with so far is to test for the existance of the System.Web.HttpContext.Current object at initialization time of the class module.  If it exists then get the connection string from the session state variable, else read the connection string from the app.config for the windows app creating the class.

Does anyone have any other ideas?  I suppose I could pass the connection string when creating the class modules.
0
jeffhodgeson
Asked:
jeffhodgeson
  • 2
1 Solution
 
daveamourCommented:
Personally I would just read from the config file then this should work for an App.config and a Web.config without any change to the code.
Alternativeley have the client code which creates the class pass in the connection string into the constructor that way the class is indifferent to the client code which is better architecture.
0
 
jeffhodgesonAuthor Commented:
Reading from whichever config file does work fine except that there are 2 possible connection strings in the config file and the one to select is determined by the web app which creates these class modules.  This web app will set a session state variable for use by the class modules now.  The class modules do not have the needed information to determine which of the 2 connection strings to use.

For now I'm I'm checking for the existance of the System.Web.HttpContext.Current object.  If it exists I use it to get the connection string from the session state variable as the module is being consumed by the web app.  If the object does not exists the object is being consumed by the windows app and it just pulls in the primary connection string from the app.config file.

If System.Web.HttpContext.Current Is Nothing Then
   connString = ConfigurationManager.ConnectionStrings("ConnString1").ConnectionString
Else
   Dim currContext As HttpContext = System.Web.HttpContext.Current
   connString = currContext.Session("ConnString").ToString
End If
0
 
daveamourCommented:
This isn't good practice - much better to give the connection string to your class as a property of the class or in the constructor.  This leads to loose coupling.
What if you wanted to use your class in a Silverlight app or a mobile app in the future?
 
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now