Formatting address of different types
I have various classes which all contain address details, i.e. AddressLine1, AddressLine2, AddressLine3, Suburb, Town, etc.
On the front end, I nee开发者_如何学God to format the address in a particular way, i.e.
AddressLine1<br />
AddressLine2<br />
Suburb State Postcode
If AddressLine2 does not exist or empty, don't show it. Quite straight-forward. I am trying to determine the best way to show this info. At the moment, the Address property of each class calls a FormatAddress method which writes out the html string. This exists as a method of the class. My opinion is that any formatting should exist from the front-end control i.e. ascx etc. However, if these DetailClasses need to format the same address info, what would be the best option, also making it easier to maintain?
If the constituent parts are public properties, how about an extension method? This does not pollute the abstraction of an address in the classes themselves, but gives you easy, natural access to the functionality and readability.
public static class AddressExtensions
{
public static string ToHtmlString(this IAddress address)
{
// return the formatted html from address
}
}
It would seem that these formatting methods would be specific to a class, where each class can have its own specialized formatting.
It would be acceptable to have to formatting handled in the class itself.
Or you could create specialized formatting classes in the UI assembly that specializes per class, where the formatting class can be marked, lets say for instance by an attribute, identiying the type of data class it is specialized to format.
精彩评论