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

Posted on 2009-04-22
Last Modified: 2012-05-06
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.
Question by:jeffhodgeson
    LVL 19

    Expert Comment

    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.

    Author Comment

    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
       Dim currContext As HttpContext = System.Web.HttpContext.Current
       connString = currContext.Session("ConnString").ToString
    End If
    LVL 19

    Accepted Solution

    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?

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Join & Write a Comment

    A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
    IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now