Domanda

In the system there is a singleton for currently logged user (our own user not windows user) containing a shared instance.

In a few hundred data access class files this is used to set CreatebyID and EditbyID for each request sent to the database. Nearly all the classes inherit a single base although at the moment the two values are set in each class.

This all worked fine for the companies desktop applications for many years however when the same data access classes were used in a new web application the currently logged user was shared across all users sessions and could not be changed without causing issues.

How can I refactor the code without significantly re-writing the whole DAL and without passing in a current user (or User ID) into every instance or setting an EditbyID property on each class for every save.

È stato utile?

Soluzione

You could use a static property which get/set 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;
        }
    }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top