runtime errors might occur when converting 'String' to 'Csla.core.ipropertyInfo'

runtime errors might occur when converting 'String' to 'Csla.core.ipropertyInfo'

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


mrisher posted on Tuesday, November 30, 2010

This may sound like a silly question, but I am getting a runtime error when I get/set a property.  I am using Visual Studio 2010 and CSLA 4.0.  The CanReadProperty and the PropertyHasChanged calls are where it bombs.  I understand some things were changed somewhere around CSLA 3.5 and 4.0.  I have looked everywhere on the forum and can't find this answer.  Do I need to convert/cast my property name to type iPropertyInfo?  If so, how?  Here's my code. 

Public Property Description() As String
            Get
                CanReadProperty("Description", True)
                Return _description
            End Get
            Set(ByVal value As String)
                CanWriteProperty("Description", True)
                If value Is Nothing Then value = String.Empty
                If Not _description.Equals(value) Then
                    _description = value
                    PropertyHasChanged("Description")
                End If
            End Set
        End Property

JonnyBee replied on Tuesday, November 30, 2010

Your code looks like legacy code from Csla 2.x/3.x and will not be able to work with Csla 4 without a certain amount of changes. 

Sorry - there is no workaround for having your legacy code work in CSLA 4.0 without changing the property declarations.
You will also have o rewrite all of your custom validation rules and authorization rules to conform with new syntax and new rule engine.


I'd recommend to look at the new snippets and class templates that is included with Csla 4. You should also read the release notes for
all newer version from your original version to catch all breaking changes. These are available at http://www.lhotka.net/cslanet/Download.aspx

In newer versions of Csla the property declaration syntax has been greatly simplified so this is how a property should be declared now:
Public Shared ReadOnly DescriptionProperty As PropertyInfo(Of String) = RegisterProperty(Of String)(Function(c) c.Description)
Public Property Description() As String
Get
Return GetProperty(DescriptionProperty)
End Get
Set
SetProperty(DescriptionProperty, value)
End Set
End Property

or to use a private backing field:

Dim _description As String
Public Shared ReadOnly DescriptionProperty As PropertyInfo(Of String) = RegisterProperty(Of String)(Function(c) c.Description, RelationshipTypes.PrivateField)
Public Property Description() As String
Get
Return GetProperty(DescriptionProperty, _description)
End Get
Set
SetProperty(DescriptionProperty, _description, value)
End Set
End Property

I think that Structural Search and Replace function of R# may help you but I haven't tried this on VB.NET code.

JonnyBee replied on Wednesday, December 01, 2010

Hi

If you wish to do only minor/minimum of changes to your code then I would only upgrade to Csla 3.8.x.  
Your property declarations and rules should run in Csla 3.8.x without any major workover.

The inner workings of Csla 4 has changed so much that you will have to rewrite much of your properties, rules and authorization to make it run.

Copyright (c) Marimer LLC