Extending PropertyInfo for custom metadata: Does this look right?

Extending PropertyInfo for custom metadata: Does this look right?

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


JCardina posted on Monday, April 11, 2011

I want to store data access metadata with my properties in my read only lists which are displayed on a grid and the properties are used to build sql sort and filter based on users choices in the grid. 

I used to use attributes in csla 1.x for this but PropertyInfo seems to be the way to go now.

I couldn't find an example so winged it with a simplified test, does the following look correct?

//Custom property info class

public class ListPropertyInfo<T> : PropertyInfo<T>  
    {
        public string sqlColumnName { get; set; }

        public ListPropertyInfo(string Name, string SqlColumnName)
            : base(Name)
        {
            sqlColumnName = SqlColumnName;
        }
    }

 

//Using it to declare a property in my read only list business object

 public static readonly Utility.ListPropertyInfo<string> NameProperty = (Utility.ListPropertyInfo<string>)RegisterProperty(new Utility.ListPropertyInfo<string>("Name", "MySqlColumnName"));


        public string Name
        {
            get { return GetProperty(NameProperty); }
            set { LoadProperty(NameProperty, value); }
        }

 

 

//Getting the sql column name information from the property in my business object consuming code

 string SqlColumnName = ((Utility.ListPropertyInfo<string>)AddressFieldPickListInfo.NameProperty).sqlColumnName;

 

This works but it seems clunky, am I doing this correctly?

JonnyBee replied on Monday, April 11, 2011

Hi,

Well - An elegant solution would be to use your own:

* PropertyInfoFactory
* PropertyInfo type with a property for SqlColumnName.
* a custom SqlColumnAttribute on the property for SqlColumn name.

And in the custom propertyInfoFactory read the attribute and set the value into your own PropertyInfo<T>

This is close to how FriendlyName works today - if empty or null try to read the DisplayName attribute on the property.

In CslaContrib you can look at PropertyInformationFactory and PropertyInformationUsingOriginalvalue in CustomFieldData project.

So, your code would look someting like this:

    public static readonly PropertyInfo<string> NameProperty = RegisterProperty<string>(c => c.Name);

    [SqlColumn("CustomerName")]
    public string Name
    {
      get { return GetProperty(NameProperty); }
      set { SetProperty(NameProperty, value); }
    }

and the custom PropertyInfoFactory would look for the SqlColumn attribute  to set the custom ListPropertyInfo<T>.SqlColumnName.

JCardina replied on Monday, April 11, 2011

Hi Johnny, that looks interesting, I'll definitely keep it in mind however what I actually need to do is much more complex and only applies to my read only lists, everything else can use the native PropertyInfo therefore I'm just trying to determine if this procedure is correct as I outlined it before I get into alternatives.

I'd appreciate it if anyone can confirm that before I get into alternatives.

JonnyBee replied on Monday, April 11, 2011

If it's much more complex then I'd try to use another approach and not put all that into PropertyInfo and rather create your own metadata type and methods in your own ReadOnlyBase intermediate class for exposing it to the UI. 

You should still be able to use Attributes on you properties.

 

 

JCardina replied on Monday, April 11, 2011

JonnyBee
If it's much more complex then I'd try to use another approach and not put all that into PropertyInfo and rather create your own metadata type and methods in your own ReadOnlyBase intermediate class for exposing it to the UI

I considered that but Rocky has mentioned previously that one of the possible uses for a custom PropertyInfo class would be to add database column information if that's a requirement which is exactly what I need to do so it sounds ideal. 

It's not a *lot* more complex than that, basically there is the display and value database column info to associate with the field and also an attribute that suggests how it's displayed in the UI, i.e. as a link or a button or a text or numeric etc.

This allows me to make a generic grid list UI element that adapts itself to display various lists.  I have dozens of different read only lists to display in my app and I fit most of them into one generic grid and just make custom ones for edge cases.

JonnyBee
You should still be able to use Attributes on you properties.

I used to but I don't really need to now.  It's not a requirement at all and ultimately I'm going to write code to automatically generate the new code from the old code so it's not a matter of me *wanting* to use attributes.

Jonny, did you see any glaring mistakes in the code I posted earlier?  (I appreciate your suggestions but I'd like to know that first and foremost before I start looking at alternatives just to be sure I'm correct in my understanding of making and using a subclassed PropertyInfo.)

 

 

 

Copyright (c) Marimer LLC