Why is PropertyInfo.FriendlyName read-only ?

Why is PropertyInfo.FriendlyName read-only ?

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


lukky posted on Wednesday, March 18, 2009

Hi,

Just wondering about the rationale behind having that property as read-only.

The reason I ask is that in a code generation scenario, the PropertyInfo declarations are most likely to be in the generated file, such that one can never customize it without risking seeing it overwritten. Some other threads suggested the use of a resource file, but I'd rather have the possibility to set the friendly name in a separate partial class definition.

My first idea was to simply modify the FriendlyName property in the csla source code, but then I decided to ask whether this would introduce problems down the road.

Thanks a lot.

RockfordLhotka replied on Wednesday, March 18, 2009

Sometimes the PropertyInfo<T> fields are public. In fact I've taken to usually making them public, as it makes it far easier to build your DAL using an ObjectFactory.

If they are public, then someone could change the property at any time while the app is running, leading to unpredictable results. In reality, the PropertyInfo<T> objects should be considered to be read-only tokens representing each property.

However, they don't actually have to be static fields. Nor do they need to be in your business class. The only real constraint is that they be initialized before any code tries to interact with an instance of your business object(s) - including deserialization of a data portal call.

In other words, it is totally realistic to have some type of initialization routine in your application that loads all the PropertyInfo<T> objects as the app starts up - perhaps using metadata - as long as this occurs before any attempted use of the business objects. And of course, the initialization would need to be run on both the client and app server (and intermediate web server in the 4-tier Silverlight scenario).

lukky replied on Thursday, March 19, 2009

Rocky,

I understand that making PropertyInfo.FriendlyName writeable could lead to unexpected results, but it's something I'm willing to live with. I mean, we're a small team of 2 coders, so we can establish that kind of rule rather easily (I hope).

The way I would use it if it were writeable would be within a static constructor in the "user" partial class file. In there I'd set the friendly name only for those properties that I want to change. That way, I could regenerate the "generated" file if the DB schema changes, and I wouldn't lose my changes.

I'm not sure I understand when you say that PropertyInfo<T> objects don't have to be in my BO class. Could you provide me with a minimalist example (or is there one in the book ?).

Thank you.

RockfordLhotka replied on Thursday, March 19, 2009

My problem is that while you may be willing to live with the risk, I’m not sure the other users of CSLA would be happy to accept it :)

 

 

Putting the RegisterProperty() calls outside the business class is very much an advanced scenario, and is not covered in the book. For most people it isn’t appropriate, but it does enable some interesting variations in some cases. It looks somewhat like this, though this is not the only possible approach:

 

public static class CustomerMeta

{

  public static PropertyInfo<string> NameProperty;

}

 

public class AppData

{

  public void InitializeApp()

  {

    CustomerMeta.NameProperty = RegisterProperty<string>(typeof(Customer), “Name”);

  }

}

 

[Serializable]

public class Customer : BusinessBase<Customer>

{

  public string Name

  {

    get { return GetProperty(CustomerMeta.NameProperty); }

    set { SetProperty(CustomerMeta.NameProperty, value); }

  }

}

 

The only catch with this, is that AppData.InitializeApp() must be run before Customer is used or you’ll get exceptions. And you must run InitializeApp() on the client and app server.

 

The value here, is that InitializeApp() can do the RegisterProperty() calls using metadata, perhaps in a way that you couldn’t do when calling RegisterProperty() as a static field initializer.

 

Rocky

lukky replied on Friday, March 20, 2009

RockfordLhotka:

My problem is that while you may be willing to live with the risk, I’m not sure the other users of CSLA would be happy to accept it :)



Ohh... I didn't mean that the CSLA code base should be changed, but that WE could change it on our side to make that property read + write :-)

I just wanted to know what pitfalls could come out of this, but since our scenario would involve writing to that property only in the context I described earlier (static constructor in user partial file), I think we can assume that it wouldn't cause undesired side effects.

Thanks Rocky for the sample code. Interesting. I'll try to see if we could use something like that instead.


RockfordLhotka replied on Thursday, March 19, 2009

My problem is that while you may be willing to live with the risk, I’m not sure the other users of CSLA would be happy to accept it :)

 

 

Putting the RegisterProperty() calls outside the business class is very much an advanced scenario, and is not covered in the book. For most people it isn’t appropriate, but it does enable some interesting variations in some cases. It looks somewhat like this, though this is not the only possible approach:

 

public static class CustomerMeta

{

  public static PropertyInfo<string> NameProperty;

}

 

public class AppData

{

  public void InitializeApp()

  {

    CustomerMeta.NameProperty = RegisterProperty<string>(typeof(Customer), “Name”);

  }

}

 

[Serializable]

public class Customer : BusinessBase<Customer>

{

  public string Name

  {

    get { return GetProperty(CustomerMeta.NameProperty); }

    set { SetProperty(CustomerMeta.NameProperty, value); }

  }

}

 

The only catch with this, is that AppData.InitializeApp() must be run before Customer is used or you’ll get exceptions. And you must run InitializeApp() on the client and app server.

 

The value here, is that InitializeApp() can do the RegisterProperty() calls using metadata, perhaps in a way that you couldn’t do when calling RegisterProperty() as a static field initializer.

 

Rocky

Copyright (c) Marimer LLC