Values not accessable in object after LoadProperty

Values not accessable in object after LoadProperty

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


Roger_wgnr posted on Thursday, March 03, 2011

I am just getting started using CSLA and have an issue that I am sure is because I am overlooking something that I need to do when using LoadProperty when performing a data fetch.

I am using CSLA 3.7.1 and am having an issue where the object properties are not updating.
I am working in Windows Forms.

This is a sample of what my code lookks like

Public Class Person
   Inherits BusinessBase(Of Person)

#Region "Business Methods"

   Private Shared FirstNameProperty As PropertyInfo(OF System.String) = RegisterProperty(New PropertyInfo(Of System.String)("FirstName", "First Name"))
   Public Property FirstName() As String
      <System.Runtime.CompilerServices.MethodImpl(Runtime.CompilerServices.MethodImplOptions.NoInlining)> _
      Get
          Return GetProperty(Of String)(FirstNameProperty)
      End Get
      Set(ByVal value As String)
          SetProperty(Of String)(FirstNameProperty, value)
      End Set
   End Property

..Many More Propertys set up like this...
#End Region

Then I have code similar to this to fetch and load the data

Private Overloads Sub DataPortall_Fetch(ByVal ToFind as Criteria)
   Dim _data As Employees = New Employees
   _data.LoadByPrimaryKey(ToFind.ID)
   With _data
       LoadProperty(FirstNameProperty, FName)
       ...Load remaining properties this same way....

   End With
End Sub

The Fetch is called by a method similar to the following

Public Shared Function GetEmployee(ByVal Id as Integer) As Person
   If Not CanGetObject() Then
      Throw New System.Security.SecurityException("Not Authorized")
   End If
   Return DirectCast(DataPortal.Fetch(OF Person)(New Criteria(Id), Person)
End Function

In the code I have the following
dim thePerson As Person = Person.GetEmployee(HisID)

lblFName.Text = thePerson.FirstName
..Set More UI controls

Now what is strange is that When I run in debug If I break after dim thePerson and Check the value of thePerson.FirstName it is a blank string.
However If I look at thePerson Object in the Debugger and Scroll down to the FirstName it will be present.
I can then check the value of thePerson.FirstName and it will be populated.

This makes me think ther is something I need to call to get the properties to update in the UI or Object.

Any Ideas.....I have been hung up on this for a day now.

RockfordLhotka replied on Thursday, March 03, 2011

I'd put a breakpoint at the bottom of the DataPortal_Fetch and see if the properties have their values at that point.

A couple (unrelated) comments:

  1. You don't need the NoInlining directive - that's counter-productive in your code
  2. You don't need to cast the result of DataPortal.Fetch(Of T) - the result is already of type T

 

JonnyBee replied on Thursday, March 03, 2011

And it could also be an issue with Visual Studio.

I have seen that on several occations when the debugger/visualizers misses on which object the variable points to and is unable to show the correct value.

To read more about the inner workings of a visualizer: http://msdn.microsoft.com/en-us/library/e2zc529c.aspx

Roger_wgnr replied on Friday, March 04, 2011

I will check the DataPortal Fetch (I have made some changes that provided a work around so that I could continue).

Since most of the data will be read only within the current use I added Local Backing Fields. However, the GetProperty(Of String)(FirstNameProperty, _fName) was still returning an empty string so I changed the return to return the backing field. I am not happy with this but it allows me to continue while I figure out the problem.

As to your comments - This is what happens when you take over someone elses code. I had already corrected the cast (realized that it was already type correct after my post). I was unsure of the NoInlining directive as I have never used it before and was unsure of when and if it should be used. As per your comments I have removed it from the code.

Could you explain when you would want to use the NoInlining directive?

RockfordLhotka replied on Friday, March 04, 2011

If you are using private backing fields then your property declaration is wrong, along with the getproperty, setproperty, and loadproperty calls. They all need to make use of that private field. In 3.7 you should never need noinlining.

Roger_wgnr replied on Friday, March 04, 2011

I guess I should have provided more information.

My property declarations now

 Private Shared FirstNameProperty As PropertyInfo(Of String) = RegisterProperty(Of String)(New PropertyInfo(Of String)("FirstName", "First Name"))
    Private Shared _fName As String = FirstNameProperty.DefaultValue
    Public Property FirstName() As String
        Get
            Return GetProperty(Of String)(FirstNameProperty, _fName)
        End Get
        Set(ByVal value As String)
            SetProperty(Of String)(FirstNameProperty, _fName, value)
        End Set
    End Property

I also changed the DataFetch to use SetProperty instead of LoadProperty.

JonnyBee replied on Friday, March 04, 2011

LoadProperty in Csla 3.7 should ONLY be used with managed backing fields.

If you use private backing fields then use the private field in your DataPortal_XYZ methods.

This is the reason we added the RelationshipTypes.PrivateField in Csla 4.x - so we could really check if it is a private field or managed field.

Roger_wgnr replied on Thursday, March 10, 2011

Still having an issue with this after having reinstalled .NET 3.5 SP1 and VS2008 Professional.

Could there be something I am missing?

Some reference or somthing that should have been done with the CSLA installation that was not done?

Grasping at straws since I have another issue that reared its ugly head it my other post.

ajj3085 replied on Thursday, March 10, 2011

In your DataPortal_Fetch, don't call SetProperty or LoadProperty.   Just set your _backingField directly.

Roger_wgnr replied on Friday, March 04, 2011

Ok I set a breakpoint at the end of the dataportal fetch.

I had changed to use SetProperty(FirstNameProperty, _fName, .FNAME) after adding the backing fields instead of LoadProperty. However the same behavior exists. When I checked the Properties after reaching the breakPoint I have  the following results

FNAME = "KEITH"

_fName = "KEITH"

FirstNameProperty = ""

FirstNameProperty.DefaultValue = ""

FirstNameProperty.Index = -1

This is really a headache ... I do not think it is within VS since If I run the program externally the values are not returned to the Controls. It may be a case of something is wrong in the Copy of the CSLA I have here....Maybe......Possibly......Perhaps.....

Copyright (c) Marimer LLC