I believe there are always places in which you can improve efficiency. Some things stand out more than other, and usually the easy things get worked on last. I wanted to share with you a little trick to help out with managing data from the V (view) to the C (Controller).

My primary pattern for architecture is using MVC but not the ASP.Net MVC. Our company also does iPhone apps so we get exposed to other coding languages. The great thing about this is that you get to pick and choose what you like about each language. The MVC pattern that I follow is derived from that on the iPhone and this little trick comes in very handy in both objective-c and .net.

The point in time in which you are moving data from the user interface (View) into your Controller class a significant amount of time savings that can be gained. By implementing some nice extension methods (categories in the iPhone world) that can reduce the amount of code you put out.

Let me show you:

Extend the TextBox object to include one these so you don’t have to look for nulls

public static Decimal TextAsDecimal(this TextBox txt)
{
	decimal value = 0;
 
	if (!string.IsNullOrEmpty(txt.Text.Trim()))
	{ decimal.TryParse(txt.Text.Trim(), out value); }
 
	return value;
}

public static Decimal? TextAsDecimalNull(this TextBox txt)
{
	decimal value = 0;
	decimal? emptyValue = null;
 
	if (!string.IsNullOrEmpty(txt.Text.Trim()))
	{
	    decimal.TryParse(txt.Text.Trim(), out value);
 
	    return value;
	}
	else
	{ return emptyValue; }
 
}

Implement this so you don’t have to worry about setting the value from your storage to your TextBox

public static void TextSetFromDecimalNumber(this TextBox txt, Decimal? number)
{
	if (number.HasValue)
	{ txt.Text = number.Value.ToString(PageBase.DecimalFormatString); }
}

This is just a few examples that I usually implement, but I also do this for more data type handling and for more controls.  I usually have an extensions.cs file in each one of my projects and carrying them with me to whatever project I do. That way I hit the ground running. So take some of these concepts and implement your own extensions and start reducing the amount of code that you write. Let me know if you have any questions and I try and help you out.

Part 2


Josh T.  Let me know if you have any questions and I will more than happy to help out. Thanks for reading.


Tags: , ,