|
' ___________________________________________________
'
' � Hi-Integrity Systems 2006. All rights reserved.
' www.hisystems.com.au - Toby Wicks
' ___________________________________________________
'
''' --------------------------------------------------------------------------------
''' <summary>
''' This class can be used to simplify the process of creating and returning late-bound
''' IDatabaseObject or DatabaseObject objects. This is is particularly useful in speeding
''' up load times of referenced objects by delaying loading the associated object or
''' objects until the property or method is actually called. This is opposed to loading
''' the object when the container object is loaded.
''' The object is loaded only on the first call to the Object property.
''' Subsequent calls to the Object property return the already loaded object.
''' If pobjDistinctValue is nothing (0 if an integer or "" if a string) then the Object
''' property will always return Nothing. Similarly if the default constructor is called
''' then the Object property will always return Nothing.
''' If IfNothingUseObjectByOrdinalFirst is true and the distinct value is nothing then the
''' first item in the collection will be returned.
''' </summary>
''' --------------------------------------------------------------------------------
Public Class ObjectReference
Private Enum DistinctValueDataTypeEnum
[String]
[Integer]
End Enum
Private pobjDistinctValue As Object = 0
Private pobjDatabase As Database
Private pobjCollection As IDatabaseObjects
Private pobjObject As IDatabaseObject
Private pbIfNothingUseObjectByOrdinalFirst As Boolean
''' --------------------------------------------------------------------------------
''' <summary>
''' If this constructor is used then the Object property will always return Nothing
''' which is useful for a new object that should always return Nothing for the associated object
''' </summary>
''' --------------------------------------------------------------------------------
Public Sub New()
End Sub
''' --------------------------------------------------------------------------------
''' <summary>
''' Initializes the object reference with the distinct value of the object reference
''' and the collection that contains the object reference. The object is not loaded
''' by the constructor - only when the Object property is called.
''' </summary>
''' <param name="objCollection">
''' The collection that contains the referenced object. The class' Mybase.Object
''' function is called to load the object.
''' </param>
''' <param name="objDistinctValue">
''' The distinct value (usually an identity or auto increment value) that identifies
''' the referenced object.
''' </param>
''' <param name="bIfNothingUseObjectByOrdinalFirst">
''' If the distinct value is nothing (0 if an integer or "" if a string) then the
''' ObjectByOrdinalFirst function is used to load the default object when the Object
''' function is first called. Subsequent calls return the already loaded object.
''' </param>
''' --------------------------------------------------------------------------------
Public Sub New( _
ByVal objCollection As DatabaseObjects, _
ByVal objDistinctValue As Object, _
Optional ByVal bIfNothingUseObjectByOrdinalFirst As Boolean = False)
Me.New(objCollection.Database, objCollection, objDistinctValue, bIfNothingUseObjectByOrdinalFirst)
End Sub
''' --------------------------------------------------------------------------------
''' <summary>
''' Initializes the object reference with the distinct value of the object reference
''' and the collection that contains the object reference. The object is not loaded
''' by the constructor - only when the Object property is called.
''' </summary>
''' <param name="objCollection">
''' The collection that contains the referenced object. The class' MyBase.Object
''' function is called to load the object.
''' </param>
''' <param name="objDistinctValue">
''' The distinct value (usually an identity or auto increment value) that identifies
''' the referenced object.
''' </param>
''' <param name="bIfNothingUseObjectByOrdinalFirst">
''' If the distinct value is nothing (0 if an integer or "" if a string) then the
''' ObjectByOrdinalFirst function is used to load the default object when the Object
''' function is first called. Subsequent calls return the already loaded object.
''' </param>
''' --------------------------------------------------------------------------------
Public Sub New( _
ByVal objDatabase As Database, _
ByVal objCollection As IDatabaseObjects, _
ByVal objDistinctValue As Object, _
Optional ByVal bIfNothingUseObjectByOrdinalFirst As Boolean = False)
If objCollection Is Nothing Or objDatabase Is Nothing Then
Throw New ArgumentNullException
End If
If objDistinctValue Is Nothing Then
Throw New ArgumentNullException("Distinct value cannot be Nothing")
End If
pobjDatabase = objDatabase
pobjCollection = objCollection
pobjDistinctValue = objDistinctValue
pbIfNothingUseObjectByOrdinalFirst = bIfNothingUseObjectByOrdinalFirst
End Sub
''' --------------------------------------------------------------------------------
''' <summary>
''' Sets/returns the referenced object.
''' When getting the value of this property on the first call the associated object
''' is loaded. If bIfNothingUseObjectByOrdinalFirst is true and the DistinctValue is
''' nothing then the default object is loaded.
''' Subsequent get calls will not load the object but return the already loaded object.
''' </summary>
''' --------------------------------------------------------------------------------
Public Overridable Property [Object]() As IDatabaseObject
Get
If pobjObject Is Nothing Then
If DistinctValueIsNothing() Then
If pbIfNothingUseObjectByOrdinalFirst Then
pobjObject = pobjDatabase.ObjectByOrdinalFirst(pobjCollection)
pobjDistinctValue = pobjObject.DistinctValue
End If
Else
pobjObject = pobjDatabase.Object(pobjCollection, pobjDistinctValue)
End If
End If
Return pobjObject
End Get
Set(ByVal Value As IDatabaseObject)
pobjObject = Value
If Value Is Nothing Then
DistinctValueSetToNothing()
Else
pobjDistinctValue = Value.DistinctValue
End If
End Set
End Property
''' --------------------------------------------------------------------------------
''' <summary>
''' This property can be used when saving the distinct value associated with this object
''' The Me.Object.DistinctValue could be used but this could cause an unnecessary loading
''' of the associated object when saving if the 'Property Get Object' hadn't been called
''' while the object was loaded.
''' </summary>
''' <example>
''' <code>
'''
''' Private pobjGroup As New ObjectReference
'''
''' <DatabaseObjects.FieldMapping("ProductGroupID")> _
''' Private Property GroupID() As Integer
''' Get
'''
''' Return pobjGroup.DistinctValue
'''
''' End Get
'''
''' Set(ByVal Value As Integer)
'''
''' pobjGroup = New ObjectReference(ProductGroups, Value)
'''
''' End Set
''' End Property
''' </code>
''' </example>
''' --------------------------------------------------------------------------------
Public ReadOnly Property DistinctValue() As Object
Get
'Get the distinct value for the default object if it hasn't been loaded yet
If pobjObject Is Nothing And pbIfNothingUseObjectByOrdinalFirst Then
pobjDistinctValue = Me.Object.DistinctValue
End If
DistinctValue = pobjDistinctValue
End Get
End Property
''' --------------------------------------------------------------------------------
''' <summary>
''' This property can be used when saving the object. Me.Object Is nothing could be
''' used but this could cause an unnecessary loading of the associated object when
''' saving if the 'Property Get Object' hadn't been called while the object was loaded.
''' If this property is true then DistinctValue will be either 0 or "" (depending
''' on the data type of the distinct value passed to the constructor).
''' </summary>
''' --------------------------------------------------------------------------------
Public ReadOnly Property ObjectIsNothing() As Boolean
Get
Return DistinctValueIsNothing()
End Get
End Property
Private Function DistinctValueIsNothing() As Boolean
Select Case DistinctValueDataType()
Case DistinctValueDataTypeEnum.Integer
Return CType(pobjDistinctValue, String) = String.Empty
Case DistinctValueDataTypeEnum.String
Return CType(pobjDistinctValue, Long) = 0
End Select
End Function
Private Sub DistinctValueSetToNothing()
Select Case DistinctValueDataType()
Case DistinctValueDataTypeEnum.Integer
pobjDistinctValue = String.Empty
Case DistinctValueDataTypeEnum.String
pobjDistinctValue = 0
End Select
End Sub
Private Function DistinctValueDataType() As DistinctValueDataTypeEnum
If TypeOf pobjDistinctValue Is String Then
Return DistinctValueDataTypeEnum.String
ElseIf _
TypeOf pobjDistinctValue Is Byte Or _
TypeOf pobjDistinctValue Is Short Or _
TypeOf pobjDistinctValue Is Integer Or _
TypeOf pobjDistinctValue Is Long Then
Return DistinctValueDataTypeEnum.Integer
Else
Throw New ArgumentException("DistinctValue data type is invalid")
End If
End Function
End Class
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.
A list of licenses authors might use can be found here
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.