Mr W
Mr W

Reputation: 676

Access User context from a class library

Background:

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

Upvotes: 0

Views: 2125

Answers (1)

Jon
Jon

Reputation: 655

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;
        }
    }
}

Upvotes: 1

Related Questions