ASP.NET Dynamic Data 4 – UI Field Localization


In Dynamic Data for the .NET Framework 3.5 SP1, you manually had to localize the data fields at time of display, leading to some very hard-to-maintain code. This has all changed in Dynamic Data 4, or rather with the new System.ComponentModel.DataAnnotations.DisplayAttribute class, http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.displayattribute_properties(VS.100).aspx, which can be used with Silverlight 3 and the .NET Framework 4. In particular, it can be used with your data model, LINQ to SQL or Entity Data Model (ADO.NET Entity Framework) to annotate your fields in a metadata class:

Visual Basic

<ScaffoldTable(True)>
Public Class Brand_Metadata

    Public Property Name As Object
End Class

Partial Public Class Brand
End Class


Visual C#

[ScaffoldTable(true)]
public class Brand_Metadata
{
    [Display(ResourceType = typeof(Resources.DataStrings), Name = "Entity_Brand_Name", ShortName = "Entity_Brand_Name_Short")]
    public object Name
    {
        get;
        set;
    }
}

[MetadataType(typeof(Brand_Metadata))]
public partial class Brand
{
}

In the code example, it is the ResourceType property of the DisplayAttribute class that make it all happen. When it is set, and doesn’t evaluate to Nothing/null, such as when the resource file can’t be found, the Name and ShortName properties are treated as constants for retrieving a string from the resource file. The Resources.DataStrings bit refers to the Resources class that loads the resources in the DataStrings.resx file, which is located in the App_GlobalResources folder.

Advertisements