I am looking for an efficient way to automatically format data fields in an entity - ideally using attributes.
We need to generate a PDF file from the data model. We want to ensure consistency in the deliverable, so we're looking to apply some formatting rules to certain data fields (dates, phone numbers, zip codes, etc..). Of course, I could write custom attributes and formatting code, but I'd rather not re-invent the wheel. I see a lot of promise using DataAnnotations (especially the DisplayFormat attribute), but I can't seem to find any built-in classes that work with those attributes.
How do I do this in a non-UI (i.e. non-MVC) context?
Here's an example of what we're looking for:
public class MyDataModel
{
[PhoneNumber]
public string PhoneNumber { get; set; }
public void FormatData()
{
//Invoke some .NET or other method changes the value of PhoneNumber into a desired format, i.e. (888)555-1234 based on its decorations.
}
}
I am also open to solutions that create a "view" of the data rather than update the original object, that is:
MyDataModel formatted = original.FormatData();
Whatever requires the least amount of code is ideal.