H
Hal Meyer
Okay... I've got a fairly straightforward web store, which keeps the obvious
in Session variables:
Customer object
Order object
I also, however, have a large class (which is also used by the WinForms app
that manages products/customers) which I keep in a Session variable. It
contains a SqlConnection, and am curious as to just how bad a practice this
would be considered. Pseudo-code below:
-- global.asax --
void Session_Start(object sender, EventArgs e)
{
Session["DataManager"] = new DataManager([...connection string
....]);
}
-- datamanager.cs --
//Field declaration
private SqlConnection _conn;
//Constructor
public DataManager(string connectionString)
{
_conn = new SqlConnection(connectionString);
}
//Get an object
public TypeA GetTypeA(int objectID)
{
_conn.Open();
[... TypeA object created from a SqlDataReader ...]
_conn.Close();
return objectA;
}
//Save an object
public void SaveTypeA(TypeA objectA)
{
_conn.Open();
[... Insert/Update SqlCommands executed inside a transaction...]
_conn.Close();
}
-- default.aspx --
DataManager dataMgr;
TypeA objectA;
void Page_Load(object sender, EventArgs e)
{
dataMgr = (DataManager)Session["DataManager"];
if(!Page.IsPostBack)
objectA = dataMgr.GetTypeA(objectID);
}
in Session variables:
Customer object
Order object
I also, however, have a large class (which is also used by the WinForms app
that manages products/customers) which I keep in a Session variable. It
contains a SqlConnection, and am curious as to just how bad a practice this
would be considered. Pseudo-code below:
-- global.asax --
void Session_Start(object sender, EventArgs e)
{
Session["DataManager"] = new DataManager([...connection string
....]);
}
-- datamanager.cs --
//Field declaration
private SqlConnection _conn;
//Constructor
public DataManager(string connectionString)
{
_conn = new SqlConnection(connectionString);
}
//Get an object
public TypeA GetTypeA(int objectID)
{
_conn.Open();
[... TypeA object created from a SqlDataReader ...]
_conn.Close();
return objectA;
}
//Save an object
public void SaveTypeA(TypeA objectA)
{
_conn.Open();
[... Insert/Update SqlCommands executed inside a transaction...]
_conn.Close();
}
-- default.aspx --
DataManager dataMgr;
TypeA objectA;
void Page_Load(object sender, EventArgs e)
{
dataMgr = (DataManager)Session["DataManager"];
if(!Page.IsPostBack)
objectA = dataMgr.GetTypeA(objectID);
}