R
RSH
I am struggling with a concept I'm not really finding a lot of in depth
articles on but Im sure others have had similar issues.
What is the best practice when it comes to persisting custom created objects
during a user's session?
I have created several objects that when run with an Out of Process session
state they cause the dreaded "Unable to serialize the session state. Please
note that non-serializable objects or MarshalByRef objects are not permitted
when session state mode is 'StateServer' or 'SQLServer'. " exception.
In the past I have created a base object that contained all of the
serializable properties, then create a concrete class underneath and
basically stripped the base class off and saved it in a session variable and
then "bolted" it back up when needed on other pages (.net 1.1). But now
that I am at the point where I need to develop an application I am wondering
how to deal with custom objects and persisting them properly.
Im struggling using OOP techniques for the web. How are other people
handling these techniques?
This is an example of the class that wont serialize, but I need to reference
it throughout the user's session:
Thanks,
Ron
<Serializable()> Public Class CompanyBase
#Region "Member Variables"
Private m_ID As System.Int32
Private m_OfficeID As System.String
Private m_CompanyID As System.String
Private m_AliasID As System.String
Private m_CompanyName As System.String
Private m_DataSource As DataSourceType
Private m_oData As BaseDataClass
#End Region
#Region "Member Properties"
Public Property ID() As System.Int32
Get
Return m_ID
End Get
Set(ByVal Value As System.Int32)
m_ID = Value
End Set
End Property
Public Property OfficeID() As System.String
Get
Return m_OfficeID
End Get
Set(ByVal Value As System.String)
m_OfficeID = Value
m_CompanyDataset.Tables(0).Rows(0).Item("OfficeID") = Value
End Set
End Property
Public Property CompanyID() As System.String
Get
Return m_CompanyID
End Get
Set(ByVal Value As System.String)
m_CompanyID = Value
End Set
End Property
Public Property AliasID() As System.String
Get
Return m_AliasID
End Get
Set(ByVal Value As System.String)
m_AliasID = Value
End Set
End Property
Public Property CompanyName() As System.String
Get
Return m_CompanyName
End Get
Set(ByVal Value As System.String)
m_CompanyName = Value
End Set
End Property
#End Region
#Region "Constructor"
Public Sub New()
End Sub
Public Sub New(ByVal companyID As String)
m_CompanyID = companyID
m_DataSource = DataSourceType.SQLServer
DataSourceBuilder()
End Sub
#End Region
#Region "Member Methods"
Private Sub DataSourceBuilder()
If m_DataSource = DataSourceType.XML Then
m_File = HttpContext.Current.Request.PhysicalApplicationPath & Me.CompanyID
& "_Company.xml"
m_oData = DataFactory.GetDataClass(m_File, DataSourceType.XML)
m_oData.ConnectionString = m_File
m_oData.PrimaryKey = "ID"
ElseIf m_DataSource = DataSourceType.Access Then
m_File = HttpContext.Current.Request.PhysicalApplicationPath & "Global.mdb"
m_oData = DataFactory.GetDataClass(m_File, DataSourceType.Access)
m_oData.ConnectionString = m_File
m_oData.PrimaryKey = "ID"
m_oData.SQLString = "select * from companies WHERE CompanyID = '" &
m_CompanyID & "'"
ElseIf m_DataSource = DataSourceType.SQLServer Then
m_oData = DataFactory.GetDataClass("Global", DataSourceType.SQLServer)
m_oData.SQLString = "select * from Global.dbo.companies WITH(NOLOCK) WHERE
CompanyID = '" & m_CompanyID & "'"
m_oData.PrimaryKey = "ID"
End If
m_CompanyDataset = m_oData.GetDataSet()
BindProperties()
End Sub
Private Sub BindProperties()
Dim oDr As DataRow
If Not CompanyID Is Nothing Then
If m_CompanyDataset.Tables(0).Rows.Count > 0 Then
oDr = m_CompanyDataset.Tables(0).Rows(0)
ID = IIf(Not IsDBNull(oDr("ID")), oDr("ID"), Nothing)
OfficeID = IIf(Not IsDBNull(oDr("OfficeID")), oDr("OfficeID"), Nothing)
CompanyID = IIf(Not IsDBNull(oDr("CompanyID")), oDr("CompanyID"), Nothing)
AliasID = IIf(Not IsDBNull(oDr("AliasID")), oDr("AliasID"), Nothing)
CompanyName = IIf(Not IsDBNull(oDr("CompanyName")), oDr("CompanyName"),
Nothing)
End If
End If
End Sub
Public Sub UpdateDataSet()
Dim oDr As DataRow
If m_CompanyDataset.Tables(0).Rows.Count > 0 Then
oDr = m_CompanyDataset.Tables(0).Rows.Find(ID)
oDr("OfficeID") = IIf(OfficeID <> Nothing, OfficeID, DBNull.Value)
oDr("CompanyID") = IIf(CompanyID <> Nothing, CompanyID, DBNull.Value)
oDr("AliasID") = IIf(AliasID <> Nothing, AliasID, DBNull.Value)
oDr("CompanyName") = IIf(CompanyName <> Nothing, CompanyName, DBNull.Value)
End If
End Sub
Public Function GetOutput() As String
Dim sb As New StringBuilder
For Each prop As PropertyInfo In Me.GetType.GetProperties
sb.Append(prop.Name & " - " & prop.GetValue(Me, Nothing) & "<br>")
Next
Return sb.ToString
End Function
Public Sub SaveChanges()
UpdateDataSet()
m_oData.SaveDataSet(m_CompanyDataset)
End Sub
#End Region
End Class
articles on but Im sure others have had similar issues.
What is the best practice when it comes to persisting custom created objects
during a user's session?
I have created several objects that when run with an Out of Process session
state they cause the dreaded "Unable to serialize the session state. Please
note that non-serializable objects or MarshalByRef objects are not permitted
when session state mode is 'StateServer' or 'SQLServer'. " exception.
In the past I have created a base object that contained all of the
serializable properties, then create a concrete class underneath and
basically stripped the base class off and saved it in a session variable and
then "bolted" it back up when needed on other pages (.net 1.1). But now
that I am at the point where I need to develop an application I am wondering
how to deal with custom objects and persisting them properly.
Im struggling using OOP techniques for the web. How are other people
handling these techniques?
This is an example of the class that wont serialize, but I need to reference
it throughout the user's session:
Thanks,
Ron
<Serializable()> Public Class CompanyBase
#Region "Member Variables"
Private m_ID As System.Int32
Private m_OfficeID As System.String
Private m_CompanyID As System.String
Private m_AliasID As System.String
Private m_CompanyName As System.String
Private m_DataSource As DataSourceType
Private m_oData As BaseDataClass
#End Region
#Region "Member Properties"
Public Property ID() As System.Int32
Get
Return m_ID
End Get
Set(ByVal Value As System.Int32)
m_ID = Value
End Set
End Property
Public Property OfficeID() As System.String
Get
Return m_OfficeID
End Get
Set(ByVal Value As System.String)
m_OfficeID = Value
m_CompanyDataset.Tables(0).Rows(0).Item("OfficeID") = Value
End Set
End Property
Public Property CompanyID() As System.String
Get
Return m_CompanyID
End Get
Set(ByVal Value As System.String)
m_CompanyID = Value
End Set
End Property
Public Property AliasID() As System.String
Get
Return m_AliasID
End Get
Set(ByVal Value As System.String)
m_AliasID = Value
End Set
End Property
Public Property CompanyName() As System.String
Get
Return m_CompanyName
End Get
Set(ByVal Value As System.String)
m_CompanyName = Value
End Set
End Property
#End Region
#Region "Constructor"
Public Sub New()
End Sub
Public Sub New(ByVal companyID As String)
m_CompanyID = companyID
m_DataSource = DataSourceType.SQLServer
DataSourceBuilder()
End Sub
#End Region
#Region "Member Methods"
Private Sub DataSourceBuilder()
If m_DataSource = DataSourceType.XML Then
m_File = HttpContext.Current.Request.PhysicalApplicationPath & Me.CompanyID
& "_Company.xml"
m_oData = DataFactory.GetDataClass(m_File, DataSourceType.XML)
m_oData.ConnectionString = m_File
m_oData.PrimaryKey = "ID"
ElseIf m_DataSource = DataSourceType.Access Then
m_File = HttpContext.Current.Request.PhysicalApplicationPath & "Global.mdb"
m_oData = DataFactory.GetDataClass(m_File, DataSourceType.Access)
m_oData.ConnectionString = m_File
m_oData.PrimaryKey = "ID"
m_oData.SQLString = "select * from companies WHERE CompanyID = '" &
m_CompanyID & "'"
ElseIf m_DataSource = DataSourceType.SQLServer Then
m_oData = DataFactory.GetDataClass("Global", DataSourceType.SQLServer)
m_oData.SQLString = "select * from Global.dbo.companies WITH(NOLOCK) WHERE
CompanyID = '" & m_CompanyID & "'"
m_oData.PrimaryKey = "ID"
End If
m_CompanyDataset = m_oData.GetDataSet()
BindProperties()
End Sub
Private Sub BindProperties()
Dim oDr As DataRow
If Not CompanyID Is Nothing Then
If m_CompanyDataset.Tables(0).Rows.Count > 0 Then
oDr = m_CompanyDataset.Tables(0).Rows(0)
ID = IIf(Not IsDBNull(oDr("ID")), oDr("ID"), Nothing)
OfficeID = IIf(Not IsDBNull(oDr("OfficeID")), oDr("OfficeID"), Nothing)
CompanyID = IIf(Not IsDBNull(oDr("CompanyID")), oDr("CompanyID"), Nothing)
AliasID = IIf(Not IsDBNull(oDr("AliasID")), oDr("AliasID"), Nothing)
CompanyName = IIf(Not IsDBNull(oDr("CompanyName")), oDr("CompanyName"),
Nothing)
End If
End If
End Sub
Public Sub UpdateDataSet()
Dim oDr As DataRow
If m_CompanyDataset.Tables(0).Rows.Count > 0 Then
oDr = m_CompanyDataset.Tables(0).Rows.Find(ID)
oDr("OfficeID") = IIf(OfficeID <> Nothing, OfficeID, DBNull.Value)
oDr("CompanyID") = IIf(CompanyID <> Nothing, CompanyID, DBNull.Value)
oDr("AliasID") = IIf(AliasID <> Nothing, AliasID, DBNull.Value)
oDr("CompanyName") = IIf(CompanyName <> Nothing, CompanyName, DBNull.Value)
End If
End Sub
Public Function GetOutput() As String
Dim sb As New StringBuilder
For Each prop As PropertyInfo In Me.GetType.GetProperties
sb.Append(prop.Name & " - " & prop.GetValue(Me, Nothing) & "<br>")
Next
Return sb.ToString
End Function
Public Sub SaveChanges()
UpdateDataSet()
m_oData.SaveDataSet(m_CompanyDataset)
End Sub
#End Region
End Class