How to remove shared variables from legacy code

The system has a singleton for the current user (our user, not a Windows user) containing a shared instance.

In several hundred data access class files, this is used to set CreatebyID and EditbyID for each request sent to the database. Almost all classes inherit from the same base, although currently two values ​​are specified in each class.

This all worked great for company desktop applications for years, however, when the same data access classes were used in a new web application, the user who was currently logging in was split across all user sessions and could not be changed. without any problems.

How can I refactor the code without significantly rewriting the entire DAL and without passing the current user (or user id) to each instance or setting the EditbyID property for each class for each save.

+3


source to share


1 answer


You can use a static property that gets / sets a session variable via HttpContext.Current.Session

.

For example:



public class DAL
{
    public static int CreatebyID
    {
        get
        {
            return (int)HttpContext.Current.Session["CreatebyID"];
        }
        set
        {
            HttpContext.Current.Session["CreatebyID"] = value;
        }
    }
    public static int EditbyID
    {
        get
        {
            return (int)HttpContext.Current.Session["EditbyID"];
        }
        set
        {
            HttpContext.Current.Session["EditbyID"] = value;
        }
    }
}

      

+1


source







All Articles