Reference Counting in VB.net

beaniiman

New member
Joined
Mar 6, 2005
Messages
1
Programming Experience
10+
I am kinda new to what I am trying to do here so maybe someone knows if it is even possible.

I am writing a data access layer that caches a certain amount of objects (to limit database access). I need to find out how many references to the objects in the cache exist as more items are added so I can eliminate objects that are not being used from the cache as the cache reaches its size limit.

Can you even find out how many references to a .net object there are. Am I wasting my time. I am currently using weak refferences to keep the cache a manageable size but from what I understand about them is that I will not be able to use IS to compare my objects now because the ones returned from the cache may be different from the ones referenced elsewhere in the program. I know that the weak refferences make new objects when needed so I am guessing this is the case. I guess I just need to know if you can get a refference count on an object or a better way to limit my cache size while maintaing the ability to us IS for object comparison.

Any help is appreciated.

Beaniiman
 
How about having shared varaible for the count in each object, that will be incremented, having decremented in the Dispose() (u should implement IDisposabe and make sure to call it when you want to destroy it..
 
The *best* way is to create a object factory class. Everytime you need a new reference to one of these objects, use the facotry class to request it. The factor ycan maintian an internal list (array or a hashtable) tha holds each instance. When the max # is reached, destroy the older one, create a new one, and add it to the end of the list.

About this point, you're probably going? Huh? A "Factory Class?" WTF is THAT?

Well, let me show you. Keep in mind, I'm doing this off the top of my head and this is a very BASIC example.

VB.NET:
Public Class ConnectionFactory

    Private Const MAXCONNECTIONS = 5
    Private Shared _Connections As Hashtable
    Private Shared _CurrIndex As Integer

    Public Shared Function RequestConnection(ByVal ConnectionString As String) As OleDb.OleDbConnection

        'First pass in we need to init the hashtable
        If _Connections Is Nothing Then
            _Connections = New Hashtable
            _CurrIndex = 1
        End If

        'If the number of objects in our hashtable is already at the limit, remove the first item in the list.
        If _CurrIndex > MAXCONNECTIONS Then
            _CurrIndex = 1
            _Connections.Remove(_CurrIndex)
        End If

        'Create the new object
        Dim newConn As New OleDb.OleDbConnection
        newConn.ConnectionString = ConnectionString
        'Add it to the hashtable
        _Connections.Add(_CurrIndex, newConn)
        'Increment our counter
        _CurrIndex += 1
        'Return the new item instance
        Return newConn

    End Function

End Class
[/class]

And you can call it like this:
[code]
Dim myConnection As OleDb.OleDbConnection = ConnectionFactory.RequestConnection("")


-tg
 
Back
Top