views:

348

answers:

1

Background:

  • I have a win app and a web app and a shared class library.
  • In my class lib I have some static methods for SQL queries which pick up my SQL connection string
  • I store my SQL connection string in a Session variable since it is set at log in time where it is determined which database to use.
  • My class lib cannot access my session variables (yes, of course I can use HttpContext.Current..., but that wont work in my winapp)

Solution?

I envision a sort of solution where I have a class for my current user/context and when creating it I inject the preferred behaviour, something like this:

UserContex current = new UserContext();
current.SessionHandler = new AspNetSessionHandler();

However, I would like a static class which I could user without having to pass it along all the time and then it would get it's variables either from the session if used in a web app or from somewhere else (I'm not a winapp developer) if used in a winform.

I will try to conjure up this kind of thing, but it would be great if I found an already working solution and that's why I call on the shared collective madness of you guys

+1  A: 

Csla contains a similar setup using a static ApplicationContext class which is discussed in Rockford Lhotka's book Expert C# Business Objects...To deal with the connection string issue I would suggest creating a DataConnection class that returns a static connection string from the config file that way it doesn't matter if the connection string is coming from the Web.config or the App.config

public class DataConnection
{


    public static string NameOfConnection
    {
        get
        {
            return ConfigurationManager.ConnectionStrings["NameOfConnection"].ConnectionString;
        }
    }
}
Jon
Well, that's the thing. I need to read the connection string from my Session variables since it varies depending on who is logging in.
Mr W
You could move the logic that determines the connection string for the user into the DataConnection class that way it would work for both win app and web app
Jon