CanReadProperty and Throwing Exceptions

CanReadProperty and Throwing Exceptions

Old forum URL: forums.lhotka.net/forums/t/2952.aspx


Marjon1 posted on Tuesday, May 29, 2007

I would like to get some feedback on what others using CSLA are doing for large scale applications.
We are developing a form based application using CSLA for the base objects, and the all our properties use the following:

Public Property Description() As String
    Get
        CanReadProperty("Description",True)
        Return _Description
    End Get
    Set(ByVal value As String)
        CanWriteProperty("Description", True)
        If _Description <> value Then
          _Description = value
          PropertyHasChanged()
        End If
    End Set
End Property



This does exactly what we would expect and throws an exception if the user does not have rights to read or write the relevant properites. However, when binding the object to a datasource to be presented on a form (with a readwriteauthorization control); the first property that the user doesn't have rights to causes an exception and no other properites are read. I would prefer not to have to put if statements throughout all properities.

How have others done this?

Thankyou in advance!

JoeFallon1 replied on Tuesday, May 29, 2007

I don't use it but if I did I think I would use it like this in order to "blank out" values they can't read and not allow sets. This avoids the exceptions.

Public Overridable Property Msgtype() As String
      Get
        If CanReadProperty("Msgtype") Then
          Return mMsgtype
        Else
          Return ""
        End If
      End Get

      Set(ByVal Value As String)
        If CanWriteProperty("Msgtype") Then
          If mMsgtype <> Value Then
            mMsgtype = Value
            PropertyHasChanged("Msgtype")
          End If
        End If
      End Set
    End Property

ajj3085 replied on Tuesday, May 29, 2007

Joe,

The RWAuth control should handle checking CanReadProperty and CanWriteProperty when databinding, so you should not need to have that code in the property setters and getters.

RockfordLhotka replied on Tuesday, May 29, 2007

Andy, unfortunately the RWAuth control doesn't solve the issue. Data binding is too agressive, and tries to read the values even though the value never makes it to the control.

If you are using the RWAuth control you are best off doing as Joe suggests and returning some dummy value (empty string, zero, etc).

It doesn't really matter what you return, because RWAuth will eat the value anyway, but the exception does give data binding indigestion in this case Sad [:(]

ajj3085 replied on Tuesday, May 29, 2007

Ack,  I saw code in RWAuth, and it had looked like it should handle the case, and have used it to lockout writing of properties, so I wasn't aware of the issues when reading is locked down.  Sorry for the misinformation.

Marjon1 replied on Tuesday, May 29, 2007

Thanks Rocky and everbody else who replied, I was afraid that this was the case.

Time to update my code templates again Angry [:@]


rdrunner replied on Thursday, May 31, 2007

Be happy that it is a only a Template ;)

ajj3085 replied on Tuesday, May 29, 2007

What are the databound controls that you are using?  Are you sure that every control which is bound has the ReadWriteAuth control enabled for it?

RWAuth should handle returning an empty string if the user cannot read a particular property, so an exception shouldn't be thrown at all. 

Copyright (c) Marimer LLC