Does ReadProperty() work with properties with backing fields?

Does ReadProperty() work with properties with backing fields?

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


CodeSmartNotHard posted on Saturday, February 07, 2009

Hi,

I'm using the propertyinfo property pattern in csla 3.6.  When I create a property without a private backing field the ReadProperty method works fine.  When the property has a private backing field, the ReadProperty doesn't return anything.  Should this work and I am just doing something wrong?

Here's the two property declarations

        private static PropertyInfo<string> CustomerNameProperty = RegisterProperty(new PropertyInfo<string>("CustomerName", "Customer Name"));
        private string _customerName = CustomerNameProperty.DefaultValue;
        public string CustomerName
        {
            get { return GetProperty(CustomerNameProperty, _customerName); }
            set { SetProperty(CustomerNameProperty, ref _customerName, value); }
        }

        private static PropertyInfo<string> AddressProperty = RegisterProperty(new PropertyInfo<string>("Address", "Address"));
        public string Address
        {
            get { return GetProperty(AddressProperty); }
            set { SetProperty(AddressProperty, value); }
        }

Here's where I'm trying to call ReadProperty()

cm.Parameters.AddWithValue("@CustomerName", ReadProperty(CustomerNameProperty));
cm.Parameters.AddWithValue("@Address", ReadProperty(AddressProperty));

Thanks in advance for any help.

Mike

RockfordLhotka replied on Sunday, February 08, 2009

No, ReadProperty() doesn't work with private backing fields. There's no reason to use ReadProperty() in such a case, because you already have access to the private field - so ReadProperty() would be useless overhead.

ReadProperty() exists so you can read a managed backing field without the authorization check that occurs with GetProperty(). But with a private backing field, you can access the field directly, which obviously doesn't trigger the authorization check.

CodeSmartNotHard replied on Sunday, February 08, 2009

That makes sense.  Thanks Rocky!

Mike 

JasonG replied on Wednesday, March 24, 2010

I'm experiencing a problem with the ReadProperty method. This does make total sense, but i don't understand what my problem is.

I'm using the managed backing fields...

private static PropertyInfo<string> FirstNameProperty =RegisterProperty(new PropertyInfo<string>("FirstName"));

 

 

public string FirstName

{get { return

GetProperty(FirstNameProperty); }

 

 

set { SetProperty(FirstNameProperty, value

); }

And in the  DataPortal_Update()

cmd.Parameters.Add(

"@First_Name", OleDbType.VarChar).Value = ReadProperty(FirstNameProperty);

If i update the First Name property with a new value, the ReadProperty() returns the old value.

Do you have any suggestions?

thanks.

 

RockfordLhotka replied on Wednesday, March 24, 2010

The following code works as expected - printing out "default", "Rocky" and "Rocky". So there's something else going on in your code beyond a simple bug in ReadProperty().

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Csla;

namespace ConsoleApplication1
{
  [Serializable]
  public class Test : BusinessBase<Test>
  {
    private static PropertyInfo<string> NameProperty = RegisterProperty<string>(c => c.Name, "Name", "default");
    public string Name
    {
      get { return GetProperty(NameProperty); }
      set { SetProperty(NameProperty, value); }
    }

    public string GetName()
    {
      return ReadProperty(NameProperty);
    }
  }

  class Program
  {
    static void Main(string[] args)
    {
      var obj = new Test();
      Console.WriteLine(obj.Name);
      obj.Name = "Rocky";
      Console.WriteLine(obj.Name);
      Console.WriteLine(obj.GetName());
      Console.ReadLine();
    }
  }
}

JasonG replied on Thursday, March 25, 2010

I'm sure the problem is with my implementation, but its odd that the public FirstName property returns the updated value but the ReadProperty() method returns the old value. Also the object that is return from the .Save() contains the updated values, but its the old values are saved when DataPortal_Update() is called.

Can you think of anything that could cause that problem?

I really appreciate your help. 

 

JasonG replied on Thursday, March 25, 2010

Man, don't i feel stupid!   Problem solved...

ajj3085 replied on Thursday, March 25, 2010

Can you share what the problem was?

JasonG replied on Thursday, March 25, 2010

It was nothing to do with the framework. there were a number of things happening..

 first my testing data was the same as the property names, so when i was debugging i assumed the values were not updating, but the values i was seeing were the property names.. secondly, my oledb.execute command wasn't working due to the parameters not in the correct order.  

 

 

Copyright (c) Marimer LLC