tags:

views:

101

answers:

1

I am trying to setup a few fixes in some code that have caught me out, and am trying to get some exceptions to spit out if the developer trys to access a property where rules haven't been met.

nb. Lots of this class has been omitted for Clarity.

myobject = new POSTerminalList(mCommonManagers)

' This should throw an error which it does but is out of scope. 
Log.Write(myobject.Current.Description)

Currently I'm throwing an exception, but this loses the calling function (which is really where the problem exists).

Is there some way to protect a property like this so that if the developer incorrectly uses the property, that you know where to look rather than in this class.

Whilst this code is VB.Net i'm interested in the Design approach rather than language so a C# implemention would also work for me.

<DebuggerDisplay("{mCode}", Name:="{mDescription}")> _
Public Class POSTerminalList
Inherits SortedList(Of Integer, POSTerminal)

    Private mCurrentTerminal As POSTerminal
    Private mCurrentTerminalNumber As Integer

    Private Sub New()
        mFullList = New SortedList(Of Integer, POSTerminal)
    End Sub

    Public Sub New(ByVal theManagers As IPSBusiness.CommonManagers)
        Me.New()
    End Sub

    ''Internal thread that is assessing things and calling this method.
    Private Sub SetCurrentTerminal()
        If mCurrentTerminalNumber = 0 Then
            mCurrentTerminal = Nothing
        Else
            If Me.ContainsKey(mCurrentTerminalNumber) Then
                mCurrentTerminal = me.Item(mCurrentTerminalNumber)
            Else
                mCurrentTerminal = Nothing
            End If
        End If
    End Sub

    Public ReadOnly Property Current() As POSTerminal
        Get
            If mCurrentTerminal Is Nothing Then 
                Throw New POSTerminalMissingException(
                    "Attempt to use the Current Terminal without a " +
                    "valid current terminal. This is a developer error.")
            Return mCurrentTerminal
        End Get
    End Property

End Class
+2  A: 

First, I would recommend that you don't throw the exception through the Description property. While you consider it out of scope, it's really the Current property that is failing, and by exposing that through the Description property, you end up misdirecting the consumer of the code into thinking that something else is wrong.

That being said, if you really wanted to do this, you should basically expose proxies to the POSTTerminal class, where the properties delegate calls to the same properties on the POSTTerminal instance exposed by Current. This way, when the properties on the proxies are called, if they fail, the call stack will run through the property on the proxy.

casperOne
Is there an example or link that I can view for creating a proxy like this?
Paul Farry
@Paul Farry: It's a simple proxy pattern implementation: http://en.wikipedia.org/wiki/Proxy_pattern
casperOne
thanks i think i've got it.. I've created an Interface (IPOSTerminal) and my normal class now Implements that. I've also created a proxy class implemting IPOSTerminal. All the properties inside the proxy just throw an exception, and mCurrent terminal is set as either the proxy OR the real class and if it attempted to use the properties it just errors out. But inspecting the .Current works as stated. Is that the correct way to go?
Paul Farry