Setting FriendlyName for Managed Properties

Setting FriendlyName for Managed Properties

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


John posted on Friday, August 15, 2008

I'm just starting to use Managed Properties in CSLA 3.5 and have a question.  Can I set the PropertyInfo.FriendlyName in the DataPortal_Create method?  I want to load the FriendlyName with a database value I retrieve during DataPortal_Create, but it is readonly.  I've read in other posts about subclassing PropertyInfo, but was wondering if I was missing something simpiler.  My goal is to have my Business Object fetch all of it's own data during a single trip across the DataPortal.  Thanks for your help.  

John replied on Sunday, August 17, 2008

Let me ask this in a different way.  Is there any way to set the value of the PropertyInfo.FriendlyName other than in the PropertyInfo constructor?  At the point in time my BO is instantiated (on the web server), I do not have the value that I wish to load into the FriendlyName.  I need my BO to travel across the DataPortal to obtain this value.  

I tried moving the call to RegisterProperty from the PropertyInfo field declaration into DataPortal_Create, but that resulted in an Exception (DataPortal.Create failed on the server).  I then tried adding a 2nd call to RegisterProperty - I left the original call in the field declaration and added the 2nd call in DataPortal_Create.  I was hoping this 2nd call would in effect "Re-register" the property with the new FriendlyName data that I obtaind from the DB.  The 2nd call seemed to succeed, but the underlying items in the FiledManager._propertyList did not change. 

Forgive me if this seems simplistic.  I know I must be missing something very basic.  How can change the value in FriendlyName after the property has been registered?  Thanks.

 

 

    

RockfordLhotka replied on Sunday, August 17, 2008

It is an advanced scenario, but it is possible to register the propertyinfo objects outside your business object. If done right, they could be registered before the creation of your first business object instance.

This would need to be done on both client and app server.

Notice that RegisterProperty() allows you to specify the type of business object that contains the specified property. You could create a totally different class that makes the RegisterProperty() calls, and not make them at all in the actual business class.

As long as you ensure that this other "definition class" was used before any business objects are created, you'd be able to use metadata in your RegisterProperty() calls.

ajj3085 replied on Monday, August 18, 2008

Hm... sounds like a pattern.  Strategy?

Cine replied on Tuesday, August 26, 2008

Is there a scenario for FriendlyName in anything but a single-language application?
I could understand if there was a overload that was a Func<string> so that it was possible to use localized strings for the name.
e.g. () => Resources.MyObjectsFriendlyName

ajj3085 replied on Wednesday, August 27, 2008

Why wouldn't that work right now?  One of the constructors takes a friendly name string, and Resources.MyObjectsFriendlyName is a string.

Cine replied on Wednesday, August 27, 2008

It would work fine... Except ALL users would get the language of the initial requestor :P

ajj3085 replied on Thursday, August 28, 2008

Are you sure?  I would think that since RegisterProperty is called on the client as well, the client copy would have the correct translation.  But I think that if the server tries to use FriendlyName, you're right that it would end up using the language of the first requester.

RockfordLhotka replied on Wednesday, August 27, 2008

In 3.5 and higher the friendly name is provided when the property is registered:

 

Private Shared NameProperty As PropertyInfo(Of String) = _

  RegisterProperty(New PropertyInfo(Of String)(“Name”, My.Resources.NamePropertyText))

 

Rocky

 

Copyright (c) Marimer LLC