views:

513

answers:

3

I'm looking for a consistent way to structure my use of formatting strings throughout a large web application, and I'm looking for recommendations or best practices on which way to go.

Up until now I've had a static class that does some common formatting e.g.

Formatting.FormatCurrency

Formatting.FormatBookingReference

I'm not convinced that this is the way to go though, I'd prefer to use the standard way of formatting strings within .NET directly and use:

amount.ToString("c")

reference.ToString("000000")

Id use IFormattable and ICustomFormatter for some of our more complicated data structures, but I'm struggling what to do about the more simple existing objects that we need to format (in this case Int32 but also DateTime).

Do I simply define constants for "c" and "000000" and use them consistently around the whole web app or is there a more standard way to do it?

+12  A: 

One option is to use a helper class with extension methods like

public static class MyWebAppExtensions
{
    public static string FormatCurrency(this decimal d)
    {
        return d.ToString("c");
    }
}

Then anywhere you have a decimal value you do

Decimal d = 100.25;
string s = d.FormatCurrency();
GeekyMonkey
Thanks for your opinion GeekyMonkey - that is one scenario I had thought of but just didnt know if it was a 'standard' way of doing things. This this how you do it in your code base?
Kieran Benton
+5  A: 

I agree with GeekyMonkey's suggestion, with one alteration:

Formatting is an implementation detail. I would suggest ToCurrencyString to keep with the To* convention and its intent.

Bryan Watts
+2  A: 

This answer can be combined with GeekyMonkey's answer.

First of all, in ASP.NET you have the possibility to set the culture and UI culture in web.config using the globalization element. The resourceProviderFactoryType attribute is your friend when you have special formatting needs.

Another possibility is to create a subclass of the ASP.NET Page class and override the InitializeCulture method. Here you can change the culture and UI culture which is stored in the current thread handling the HTTP request.

A quick example:

protected override void InitializeCulture()
{
    Thread.CurrentThread.CurrentCulture = ...;
    Thread.CurrentThread.CurrentUICulture = ...;

    Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern = "dd-MM-yyyy";
    ...
}

For those worried about ASP.NET doing "random" thread switching:

ASP.NET ensures that, even if they switch your thread, the CurrentPrincipal and culture properties from the original thread carry forward to the new thread. This is automatic, and you don’t need to worry about losing those values. Whew!

Source: ASP.NET thread switching

Jonas Kongslund
I like this method too. I couldn't think of the specifics off the top of my head or I would have mentioned it.
GeekyMonkey
Is it really safe to use Thread.CurrentThread in the context of an ASP.NET app? Ive always been warned to stay clear of this because the thread can change under your feet in that kind of hosted environment.
Kieran Benton
Kieran, it is true that the thread can be changed under your feet but information about the principal and culture is automatically moved to the new thread handling the request. I have updated my answer with some information about this.
Jonas Kongslund