Click here to Skip to main content
Click here to Skip to main content
Articles » Database » Database » Databases » Downloads
 
Add your own
alternative version

Object-Oriented database design with the DatabaseObjects library

, 31 Jan 2007
Demonstrates creating object-oriented database systems with the DatabaseObjects library.
databaseobjects_demo_net.zip
VB.NET
demo
source
Northwind
NorthwindDB
bin
Northwind.vbproj.user
AxInterop.SHDocVw.dll
DatabaseObjects.dll
Interop.SHDocVw.dll
Northwind.exe
NorthwindDatabase.dll
NorthwindDatabase.vbproj.user
Attributes
bin
Database
DatabaseObjects.vbproj.user
Misc
SQL
DatabaseObjects.dll
Data
mysql_northwind.zip
Documentation
DatabaseObjects.chm
databaseobjects_demo_vb6.zip
VB6
demo
regsvr32.exe
source
Northwind
Northwind.vbg
NorthwindDB
ControlAnchor.cls
frmMain.frm
frmMain.frx
frmOrder.frm
frmOrder.frx
frmProducts.frm
frmProducts.frx
frmProductSearch.frm
frmProductSearch.frx
frmProductSearchExtended.frm
frmProductSearchExtended.frx
frmSuppliers.frm
frmSuppliers.frx
Northwind.exe
northwind.RES
Northwind.vbp
Categories.cls
Category.cls
Data.bas
LateBoundObject.cls
NorthwindDatabase.cls
NorthwindDatabase.OBJ
NorthwindDB.dll
NorthwindDB.vbp
Order.cls
OrderDetail.cls
OrderDetails.cls
Orders.cls
Product.cls
Products.cls
ProductSearch.cls
Supplier.cls
Suppliers.cls
bin
Database.cls
DBO.vbp
GlobalDatabase.cls
GlobalDatabaseObjects.cls
IDatabaseObject.cls
IDatabaseObjects.cls
IGlobalDatabaseObjects.cls
ISQLStatement.cls
modMisc.bas
SQLAlterTable.cls
SQLAutoIncrementValue.cls
SQLCondition.cls
SQLConditionFieldCompare.cls
SQLConditionInSelect.cls
SQLConditions.cls
SQLConditionSelect.cls
SQLCreateIndex.cls
SQLCreateTable.cls
SQLDelete.cls
SQLDropIndex.cls
SQLDropTable.cls
SQLFieldValue.cls
SQLFieldValues.cls
SQLIndexExists.cls
SQLIndexField.cls
SQLIndexFields.cls
SQLInsert.cls
SQLSelect.cls
SQLSelectField.cls
SQLSelectFields.cls
SQLSelectGroupByField.cls
SQLSelectGroupByFields.cls
SQLSelectOrderByField.cls
SQLSelectOrderByFields.cls
SQLSelectTable.cls
SQLSelectTableJoin.cls
SQLSelectTableJoinCondition.cls
SQLSelectTableJoinConditions.cls
SQLSelectTableJoins.cls
SQLSelectTables.cls
SQLTableExists.cls
SQLTableField.cls
SQLTableFields.cls
SQLUpdate.cls
SQLUpdateCopyField.cls
SQLUpdateCopyFields.cls
DatabaseObjects_vb6.dll
mysql_northwind.zip
images
templates
databaseobjects_classdiagram.jpg
logo.jpg
databaseobjects_src_net.zip
DatabaseObjects.vbproj.user
DatabaseObjects.dll
databaseobjects_src_vb6.zip
Database.cls
DBO.vbp
GlobalDatabase.cls
GlobalDatabaseObjects.cls
IDatabaseObject.cls
IDatabaseObjects.cls
IGlobalDatabaseObjects.cls
ISQLStatement.cls
modMisc.bas
SQLAlterTable.cls
SQLAutoIncrementValue.cls
SQLCondition.cls
SQLConditionFieldCompare.cls
SQLConditionInSelect.cls
SQLConditions.cls
SQLConditionSelect.cls
SQLCreateIndex.cls
SQLCreateTable.cls
SQLDelete.cls
SQLDropIndex.cls
SQLDropTable.cls
SQLFieldValue.cls
SQLFieldValues.cls
SQLIndexExists.cls
SQLIndexField.cls
SQLIndexFields.cls
SQLInsert.cls
SQLSelect.cls
SQLSelectField.cls
SQLSelectFields.cls
SQLSelectGroupByField.cls
SQLSelectGroupByFields.cls
SQLSelectOrderByField.cls
SQLSelectOrderByFields.cls
SQLSelectTable.cls
SQLSelectTableJoin.cls
SQLSelectTableJoinCondition.cls
SQLSelectTableJoinConditions.cls
SQLSelectTableJoins.cls
SQLSelectTables.cls
SQLTableExists.cls
SQLTableField.cls
SQLTableFields.cls
SQLUpdate.cls
SQLUpdateCopyField.cls
SQLUpdateCopyFields.cls
DatabaseObjects_vb6.dll
' ___________________________________________________
'
'  � 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
    ''' 
    ''' &lt;DatabaseObjects.FieldMapping("ProductGroupID")&gt; _
    ''' 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.

License

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

Share

About the Author

Toby Wicks

United States United States
No Biography provided

| Advertise | Privacy | Mobile
Web03 | 2.8.140827.1 | Last Updated 31 Jan 2007
Article Copyright 2005 by Toby Wicks
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid