An abstract base class to facilitate persistence in the ESRI framework
There are different ways in which .NET types can be persisted using the ESRI persistence framework. Personally, I use an “sbPersistable” abstract base class to make the persistence easier. The sbPersistable is an utility class that can be inherited by classes that need to persist and rehydrate themselves to a stream. It implements most of the common methods required for persistence and only requires the implementing classes to store and retrieve their properties from its PersistPropertySet property. A sample class that implements and uses sbPersistable to support persistence is shown below. In the example, the implementing class simply overrides the ToPropertySet() and the FromPropertySet() methods to achieve its persistence.
Public Class PersistenceSample
Inherits sbPersistable
Private _name As String
Private _age As Integer
Public Property Name() As String
Get
Return _name
End Get
Set(ByVal value As String)
_name = value
End Set
End Property
Public Property Age() As Integer
Get
Return _age
End Get
Set(ByVal value As Integer)
_age = value
End Set
End Property
Public Overloads Overrides Sub ToPropertySet()
MyBase.ToPropertySet()
PersistPropertySet.SetProperty(“Name”, _name)
PersistPropertySet.SetProperty(“Age”, _age)
End Sub
Public Overloads Overrides Sub FromPropertySet()
MyBase.FromPropertySet()
_name = DirectCast(PersistPropertySet.GetProperty(“Name”), String)
_age = DirectCast(PersistPropertySet.GetProperty(“Age”), Integer)
End Sub
End Class
The class stores all its properties into a “PropertySet” and persists and rehydrates the “PropertySet” as a whole instead of persisting and rehydrating each property individually. The “sbPersistable” class also implements a couple of other interfaces “IsbClassID” and “IsbClassInstanceID” that enable applications using the class to identify and compare objects even after persistence and rehydration. The “sbPersistable” class and other utility classes can be downloaded here.
”’ <summary>
”’ Provides access to members used to persist objects
”’ </summary>
<Guid(“658CE48F-D5D4-4aa5-BC0C-AD542326C69E”)> _
<ProgId(“Common.GIS.sbPersistable”), ClassInterface(ClassInterfaceType.None)> _
Public MustInherit Class sbPersistable
Implements IsbPersistable
Implements IsbClassID
Implements IsbClassInstanceID
Protected _persistPropertySet As IPropertySet = New PropertySet()
Private _classInstanceID As String = Guid.NewGuid().ToString()
Public Sub New()
_classInstanceID = Guid.NewGuid().ToString()
End Sub
#Region “IsbClassID Members”
Public ReadOnly Property ClassID() As String Implements IsbClassID.ClassID
Get
Return Me.GetType().GUID.ToString()
End Get
End Property
#End Region
#Region “IsbClassInstanceID Members”
Public ReadOnly Property ClassInstanceID() As String Implements IsbClassInstanceID.ClassInstanceID
Get
Return _classInstanceID
End Get
End Property
#End Region
#Region “IsbPersistable Members”
Public Property PersistPropertySet() As IPropertySet Implements IsbPersistable.PersistPropertySet
Get
Return _persistPropertySet
End Get
Set(ByVal value As IPropertySet)
_persistPropertySet = value
End Set
End Property
Public Overridable Sub ToPropertySet() Implements IsbPersistable.ToPropertySet
PersistPropertySet.SetProperty(“ClassInstanceID”, ClassInstanceID)
End Sub
Public Overridable Sub FromPropertySet() Implements IsbPersistable.FromPropertySet
_classInstanceID = DirectCast(PersistPropertySet.GetProperty(“ClassInstanceID”), String)
End Sub
#End Region
#Region “IPersistStream Members”
Public Sub GetClassID(ByRef pClassID As Guid) Implements IPersistStream.GetClassID, IPersist.GetClassID
pClassID = Me.GetType().GUID
End Sub
Public Sub GetSizeMax(ByRef pcbSize As ESRI.ArcGIS.esriSystem._ULARGE_INTEGER) Implements IPersistStream.GetSizeMax
pcbSize = New _ULARGE_INTEGER()
pcbSize.QuadPart = CType(Marshal.SizeOf(PersistPropertySet), ULong)
End Sub
Public Sub IsDirty() Implements IPersistStream.IsDirty
End Sub
Public Sub Load(ByVal pstm As ESRI.ArcGIS.esriSystem.IStream) Implements IPersistStream.Load
Try
PersistPropertySet = New PropertySetClass()
TryCast(PersistPropertySet, ESRI.ArcGIS.esriSystem.IPersistStream).Load(pstm)
FromPropertySet()
Catch ex As Exception
Throw New Exception(“An unexpected error occurred while rehydrating object from stream.”, ex)
End Try
End Sub
Public Sub Save(ByVal pstm As ESRI.ArcGIS.esriSystem.IStream, ByVal fClearDirty As Integer) Implements IPersistStream.Save
Try
ToPropertySet()
TryCast(PersistPropertySet, ESRI.ArcGIS.esriSystem.IPersistStream).Save(pstm, fClearDirty)
Catch ex As Exception
Throw New Exception(“An unexpected error occurred while persisting object to stream.”, ex)
End Try
End Sub
#End Region
End Class
Follow Me
Contact me
leave a comment