Friday, 11 September 2015

A much better String to ValueType converter implementation in Csharp

There are situations when one needs to convert the string values to ValueTypes(int, guid, datetime, Enums, Nullables etc.). A situation of doing these conversion could be to read Settings store in Configuration file or in database and converting them to their respective value type.
I have seen people have writing if..else ladder to match the type and use the specific TypeConverter method from static class i.e. Convert.To<ValueType>();

Here’s a sample what was observed:

public object ConvertToValueType(string value, Type targetType)
{
    object result = null;
 
    bool isEmpty = string.IsNullOrWhiteSpace(value);
 
    if (targetType == typeof (string))
    {
        result = value;
    }
    else if (targetType == typeof (Int32) || targetType == typeof (int))
    {
        if (isEmpty)
        {
            result = 0;
        }
        else
        {
            result = Int32.Parse(value);
        }
    }
    else if (targetType == typeof (Guid))
    {
        if (isEmpty)
        {
            result = Guid.Empty;
        }
        else
        {
            result = new Guid(value);
        }
    }
    else if (targetType.IsEnum)
    {
        result = Enum.Parse(targetType, value);
    }
    else if (targetType == typeof (byte[]))
    {
        result = Convert.FromBase64String(value);
    }
    else if (targetType == typeof (DateTime))
    {
        if (isEmpty)
        {
            result = DateTime.MinValue;
        }
        else
        {
            result = Convert.ToDateTime(value);
        }
    }
 
    return result;
}

This solution would work and I can tell half of you have seen or done things like this. But look at this again. There will be an IF…Else condition for each type in the system.
The problem can be solved by using a System class System.ComponentModel.TypeDescriptor. Which provide underlying information of a Type. So the above complex statement chain for Conversion can be converted to something like this using an extension method accepting generic types:


private static bool TryConvertTo<T>(this string value, out object result)
        {
            var newType = typeof(T);
result = default(T);
            if (string.IsNullOrWhiteSpace(value))
            {
        // simply return. A default value is already provided to out parameter.
                return true;
            }
 
            try
            {
                var converter = TypeDescriptor.GetConverter(typeof(T));
                result = (T)converter.ConvertFromString(value);
                return true;
            }
            Catch(Exception exception) 
            {
          // Log this exception if required. 
               // throw new InvalidCastException(string.Format("Unable to cast the {0} to type {1}", value, newType, exception));
        return false;
            }
        }

I have created this method with “Try-Parse” pattern so that it doesn’t break the application but simply returns ture/false if conversion is successful or failed due to exception or any other reasons. I found this classes in one the solution of Asp.net. You can find the complete class create for this blog post here.
Thumb Rule: Before you decide to jump into changing the business logic, try writing some tests for complete coverage of existing converter and then apply the changes and see if it’s not breaking any previously running cases.
I have wrote couple of tests to see if it works. You can find those tests for string to Converter here. These are not complete tests to validate every conversion so use and test it by your own.

Thursday, 10 September 2015

Migrating to simpleInjector 3.0 with Caliburn Micro Bootstrap changes

A note, Before I write this blog I want to say thanks to all the contributors to the open source libraries those changed the life of developers by preventing writing of Boilerplate code. These libraries are not just helpers but they’re much more efficient than legacy code.
image

Why I’m writing this blog? Well, here’s the reason. I have been a great fan of SimpleInjector because of it’s performance and Caliburn.Micro for being lightweight and faster. I have been using them since the libraries are in their initial version and doing great job till date. Today I spent few hours creating a small application WPF and I was surprised everything was changed. It was whole new world ofcourse if you don’t do practice with coach you gonna miss a lot of stuff.

Let’s look at the changes in simple boootstrapper for WPF application. Since caliburn micro give a nice way to setup a Dependency injection by providing virtual methods in base class BootStrapperBase. Since the changes in SimpleInjector v3.0 for returning list of registered instances which now throws exception if none found. Here’s the complete code snippet for working bootstrap, if it would help someone out there struggling the same problem: