System.ComponentModel.DataAnnotations.DisplayAttribute vs. CSLA RegisterProperty friendlyName argument

System.ComponentModel.DataAnnotations.DisplayAttribute vs. CSLA RegisterProperty friendlyName argument

Old forum URL:

Dane posted on Saturday, March 09, 2013

Are these two ways of setting the display name for a property interchangeable?

JonnyBee replied on Saturday, March 09, 2013


Assuming you use this property syntax:

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

The you will use the DefaultPropertyInfoFactory and this is the code:

    public Csla.PropertyInfo<T> Create<T>(Type containingType, string name, string friendlyName)
      if (string.IsNullOrWhiteSpace(friendlyName))
        friendlyName = GetFriendlyNameFromAttributes(containingType, name);
      return new Csla.PropertyInfo<T>(name, friendlyName);

And the GetFriendlyNameFromAttribute will look for

DisplayAttribute  (or)

So essentially - yes if you leave FriendlyName as non-specified, null or string.Empty then the DefaultPropertyInfoFactory will look for the Attributes. 

Dane replied on Monday, March 11, 2013

I'd been using RegisterProperty<string>(c => c.UserName, "User Name") for quite some time. My UI was in Silverlight and it was honoring the friendly name. However, I just started working on an MVC4 UI and I found that MVC is not honoring the friendly name setting. Html.LabelFor was simply returning a label with the property name, i.e. "UserName". I implemented a DisplayAttribute on the property set to "User Name" and that, of course, resolved the issue in the MVC UI. I'm now wondering if there are more places that one method will work and the other won't.

TSF replied on Tuesday, March 12, 2013

Not sure about where one method works or doesn't work, but I saw this example in Rocky's Using CSLA e-book a few times. Perhaps this is the preferred method?

public static readonly PropertyInfo<string> NameProperty = RegisterProperty<string>(c => c.Name);
[Display(Name = "Project name")]
public string Name
   get { return GetProperty(NameProperty); }      
   set { SetProperty(NameProperty, value); }

JonnyBee replied on Wednesday, March 13, 2013


DisplayAttribute and DisplayNameAttribute is standard attributes from Microsoft and may be used in multiple UI-frameworks, including ASP.NET MVC. For more details on usage you should search the MSDN documentation and UI frameworks. 

Csla has it's own PropertyInfo that holds a FriendlyName to be used for ex in messages in the rule engine.  So we have added support in CSLA to set the FriendlyName from DisplayName or DisplayAttribute if no value was provided explicitly in code. This is because we want CSLA to not be tied a specific feature or attribute in .NET yet provide support for these as good as possible. 

RockfordLhotka replied on Wednesday, March 13, 2013

Jonny is correct.

It is simply a matter of timing. CSLA was handling friendly names for years before the DataAnnotations namespace came along with formal support for the concept. CSLA integrates Microsoft's attributes, and I'd recommend using their technology now that it exists, because you get the automatic support from MVC (and other technologies), plus smooth integration into CSLA.

Copyright (c) Marimer LLC