Thursday, 29 July 2010 20:44 by
SyntaxC4
In my early years as a Developer I was using DotNetNuke and often had large Data Access Layers (DALs) to abstract the database away from my application. I quickly found myself checking to ensure that a value returning from the Database wasn’t NULL (\0), so I created a helper class that had a number of methods for each data type I interacted with. This created was done similar to this code below:
public static double ConvertNullDouble(object field)
{
if (field != DBNull.Value)
return Convert.ToDouble(field);
return 0; // or if my application had Defaults Global.DefaultDouble
}
This seemed like a great solution at the time, but is quite a lot of code, as you are writing a method for every…single…datatype that your application interacts with. Being in a Small Business you didn’t really get the ability to refactor code that often, so once it was written you stuck by it.
Moving along in my career I stepped into Environment that already had full DALs in place so I would mimic what the previous developer did in order to keep the code base easily maintainable, as there is never a lack of fun trying to understand more than one persons point of view across a piece of code.
Tonight I was scanning through twitter and noticed Ben Alabaster [@BenAlabaster] asking a quite a unique question on twitter. So I piped in with my two cents, and then the conversation sparked beyond twitter to msn, then eventually to Skype. Here is the unique Single Helper Method we came up with:
For .NET 2.0, a static method:
public static T ConvertDBNullToSafeOrDefaultTypeValue<T>(object field)
{
if (field != DBNull.Value)
return (T)Convert.ChangeType(field, typeof(T));
return default(T);
}
For .NET 3.5, an Extension method:
public static T ConvertDBNullToSafeOrDefaultTypeValue<T>(this object field)
{
if (field != DBNull.Value)
return (T)Convert.ChangeType(field, typeof(T));
return default(T);
}
This solution provides a source code friendlier way of returning values from a database and validating the data against your business rules [you can replace default(T) is a Generic way of handling your applications default values].
Happy Coding!
Wednesday, 28 July 2010 11:49 by
SyntaxC4
When it comes to providing a more usable interface for your user, I find that labels are the key. Being able to style a label as an error message and position it where you need it for your user is great, the added functionality being, if the user clicks on the label it lands their cursor nicely in the corresponding field [provided you have filled in the ‘for’ attribute].
When it comes to ASP.NET MVC, Microsoft made the choice to go with a <span> tag instead of the <label>. I’m not sure why they made this decision, and please feel free to comment below if I'm breaking an html rule here, but a label for error validation just seems to be a no brainer to me.
If you feel the same here is a snippet of Code I created to be change a ValidationMessageFor Extension Method into a Method that provides the label error messaging.
using System;
using System.Linq.Expressions;
using System.Text.RegularExpressions;
using System.Web.Mvc;
using System.Web.Mvc.Html;
public static class ValidationExtensions
{
public static MvcHtmlString ValidationMessageLabelFor<TModel, TProperty>(this HtmlHelper<TModel> html, Expression<Func<TModel, TProperty>> expression, string errorClass = "error")
{
string elementName = ExpressionHelper.GetExpressionText(expression);
MvcHtmlString normal = html.ValidationMessageFor(expression);
if (normal != null)
{
string newValidator = Regex.Replace(normal.ToHtmlString(), @"<span([^>]*)>([^<]*)</span>", string.Format("<label for=\\"{0}\\" $1>$2</label>", elementName), RegexOptions.IgnoreCase);
if (!string.IsNullOrWhiteSpace(errorClass))
newValidator = newValidator.Replace("field-validation-error", errorClass);
return MvcHtmlString.Create(newValidator);
}
return null;
}
}
I know that Regular Expression can cause some performance issues, and I don’t call myself a RegEx Master, so if you find a more efficient way to do this please Contact Me, and I will be sure to post the code change.
Remember: “friends don’t let friends __doPostBack();”
Happy Coding!