As part of the refactoring work for my stored procedure framework project, I needed to extract the conversion of CLR Types to `SqlDbTypes` out into a dedicated helper class. this has given me a simple mapper class which I'd like to share.
The class is a static class with a single public member `GetSqlDbTypeFromClrType
` which takes a `Type
` and returns a `SqlDbType
`. Most of the common DataTypes have been covered but feel free to take the code and extend as you feel fit.
using System;
using System.Collections.Generic;
using System.Data;
namespace Dibware.StoredProcedureFramework.Helpers
{
public static class ClrTypeToSqlDbTypeMapper
{
#region Constructors
static ClrTypeToSqlDbTypeMapper()
{
CreateClrTypeToSqlTypeMaps();
}
#endregion
#region Public Members
public static SqlDbType GetSqlDbTypeFromClrType(Type clrType)
{
if (!_clrTypeToSqlTypeMaps.ContainsKey(clrType))
{
throw new ArgumentOutOfRangeException("clrType", @"No mapped type found for " + clrType);
}
SqlDbType result;
_clrTypeToSqlTypeMaps.TryGetValue(clrType, out result);
return result;
}
#endregion
#region Private Members
private static void CreateClrTypeToSqlTypeMaps()
{
_clrTypeToSqlTypeMaps = new Dictionary<Type, SqlDbType>
{
{typeof (Boolean), SqlDbType.Bit},
{typeof (Boolean?), SqlDbType.Bit},
{typeof (Byte), SqlDbType.TinyInt},
{typeof (Byte?), SqlDbType.TinyInt},
{typeof (String), SqlDbType.NVarChar},
{typeof (DateTime), SqlDbType.DateTime},
{typeof (DateTime?), SqlDbType.DateTime},
{typeof (Int16), SqlDbType.SmallInt},
{typeof (Int16?), SqlDbType.SmallInt},
{typeof (Int32), SqlDbType.Int},
{typeof (Int32?), SqlDbType.Int},
{typeof (Int64), SqlDbType.BigInt},
{typeof (Int64?), SqlDbType.BigInt},
{typeof (Decimal), SqlDbType.Decimal},
{typeof (Decimal?), SqlDbType.Decimal},
{typeof (Double), SqlDbType.Float},
{typeof (Double?), SqlDbType.Float},
{typeof (Single), SqlDbType.Real},
{typeof (Single?), SqlDbType.Real},
{typeof (TimeSpan), SqlDbType.Time},
{typeof (Guid), SqlDbType.UniqueIdentifier},
{typeof (Guid?), SqlDbType.UniqueIdentifier},
{typeof (Byte[]), SqlDbType.Binary},
{typeof (Byte?[]), SqlDbType.Binary},
{typeof (Char[]), SqlDbType.Char},
{typeof (Char?[]), SqlDbType.Char}
};
}
private static Dictionary<Type, SqlDbType> _clrTypeToSqlTypeMaps;
#endregion
}
}
The tests I have written for this class are shown below.
using System;
using System.Data;
using System.Text;
using Dibware.StoredProcedureFramework.Helpers;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Dibware.StoredProcedureFramework.Tests.UnitTests.Helpers
{
[TestClass]
public class ClrTypeToSqlTypeMapperTests
{
[TestMethod]
public void GetSqlDbTypeFromClrType_WhenGivenBooleanType_ReturnsBitSqlDbType()
{
Type value = typeof (Boolean);
const SqlDbType expectedSqlDbType = SqlDbType.Bit;
SqlDbType actual = ClrTypeToSqlDbTypeMapper.GetSqlDbTypeFromClrType(value);
Assert.AreEqual(expectedSqlDbType, actual);
}
[TestMethod]
public void GetSqlDbTypeFromClrType_WhenGivenNullableBooleanType_ReturnsBitSqlDbType()
{
Type value = typeof(Boolean?);
const SqlDbType expectedSqlDbType = SqlDbType.Bit;
SqlDbType actual = ClrTypeToSqlDbTypeMapper.GetSqlDbTypeFromClrType(value);
Assert.AreEqual(expectedSqlDbType, actual);
}
[TestMethod]
public void GetSqlDbTypeFromClrType_WhenGivenByteType_ReturnsTinyIntSqlDbType()
{
Type value = typeof (Byte);
const SqlDbType expectedSqlDbType = SqlDbType.TinyInt;
SqlDbType actual = ClrTypeToSqlDbTypeMapper.GetSqlDbTypeFromClrType(value);
Assert.AreEqual(expectedSqlDbType, actual);
}
[TestMethod]
public void GetSqlDbTypeFromClrType_WhenGivenNullableByteType_ReturnsTinyIntSqlDbType()
{
Type value = typeof(Byte?);
const SqlDbType expectedSqlDbType = SqlDbType.TinyInt;
SqlDbType actual = ClrTypeToSqlDbTypeMapper.GetSqlDbTypeFromClrType(value);
Assert.AreEqual(expectedSqlDbType, actual);
}
[TestMethod]
public void GetSqlDbTypeFromClrType_WhenGivenStringType_ReturnsNVarCharSqlDbType()
{
Type value = typeof (String);
const SqlDbType expectedSqlDbType = SqlDbType.NVarChar;
SqlDbType actual = ClrTypeToSqlDbTypeMapper.GetSqlDbTypeFromClrType(value);
Assert.AreEqual(expectedSqlDbType, actual);
}
[TestMethod]
public void GetSqlDbTypeFromClrType_WhenGivenDateTimeType_ReturnsDateTimeSqlDbType()
{
Type value = typeof (DateTime);
const SqlDbType expectedSqlDbType = SqlDbType.DateTime;
SqlDbType actual = ClrTypeToSqlDbTypeMapper.GetSqlDbTypeFromClrType(value);
Assert.AreEqual(expectedSqlDbType, actual);
}
[TestMethod]
public void GetSqlDbTypeFromClrType_WhenGivenNullableDateTimeType_ReturnsDateTimeSqlDbType()
{
Type value = typeof(DateTime?);
const SqlDbType expectedSqlDbType = SqlDbType.DateTime;
SqlDbType actual = ClrTypeToSqlDbTypeMapper.GetSqlDbTypeFromClrType(value);
Assert.AreEqual(expectedSqlDbType, actual);
}
[TestMethod]
public void GetSqlDbTypeFromClrType_WhenGivenInt16Type_ReturnsSmallIntSqlDbType()
{
Type value = typeof (Int16);
const SqlDbType expectedSqlDbType = SqlDbType.SmallInt;
SqlDbType actual = ClrTypeToSqlDbTypeMapper.GetSqlDbTypeFromClrType(value);
Assert.AreEqual(expectedSqlDbType, actual);
}
[TestMethod]
public void GetSqlDbTypeFromClrType_WhenGivenNullableInt16Type_ReturnsSmallIntSqlDbType()
{
Type value = typeof(Int16?);
const SqlDbType expectedSqlDbType = SqlDbType.SmallInt;
SqlDbType actual = ClrTypeToSqlDbTypeMapper.GetSqlDbTypeFromClrType(value);
Assert.AreEqual(expectedSqlDbType, actual);
}
[TestMethod]
public void GetSqlDbTypeFromClrType_WhenGivenInt32Type_ReturnsIntSqlDbType()
{
Type value = typeof (Int32);
const SqlDbType expectedSqlDbType = SqlDbType.Int;
SqlDbType actual = ClrTypeToSqlDbTypeMapper.GetSqlDbTypeFromClrType(value);
Assert.AreEqual(expectedSqlDbType, actual);
}
[TestMethod]
public void GetSqlDbTypeFromClrType_WhenGivenNullableInt32Type_ReturnsIntSqlDbType()
{
Type value = typeof(Int32?);
const SqlDbType expectedSqlDbType = SqlDbType.Int;
SqlDbType actual = ClrTypeToSqlDbTypeMapper.GetSqlDbTypeFromClrType(value);
Assert.AreEqual(expectedSqlDbType, actual);
}
[TestMethod]
public void GetSqlDbTypeFromClrType_WhenGivenInt64Type_ReturnsBigIntSqlDbType()
{
Type value = typeof (Int64);
const SqlDbType expectedSqlDbType = SqlDbType.BigInt;
SqlDbType actual = ClrTypeToSqlDbTypeMapper.GetSqlDbTypeFromClrType(value);
Assert.AreEqual(expectedSqlDbType, actual);
}
[TestMethod]
public void GetSqlDbTypeFromClrType_WhenGivenNullableInt64Type_ReturnsBigIntSqlDbType()
{
Type value = typeof(Int64?);
const SqlDbType expectedSqlDbType = SqlDbType.BigInt;
SqlDbType actual = ClrTypeToSqlDbTypeMapper.GetSqlDbTypeFromClrType(value);
Assert.AreEqual(expectedSqlDbType, actual);
}
[TestMethod]
public void GetSqlDbTypeFromClrType_WhenGivenDecimalType_ReturnsDecimalSqlDbType()
{
Type value = typeof(Decimal);
const SqlDbType expectedSqlDbType = SqlDbType.Decimal;
SqlDbType actual = ClrTypeToSqlDbTypeMapper.GetSqlDbTypeFromClrType(value);
Assert.AreEqual(expectedSqlDbType, actual);
}
[TestMethod]
public void GetSqlDbTypeFromClrType_WhenGivenNullableDecimalType_ReturnsDecimalSqlDbType()
{
Type value = typeof(Decimal?);
const SqlDbType expectedSqlDbType = SqlDbType.Decimal;
SqlDbType actual = ClrTypeToSqlDbTypeMapper.GetSqlDbTypeFromClrType(value);
Assert.AreEqual(expectedSqlDbType, actual);
}
[TestMethod]
public void GetSqlDbTypeFromClrType_WhenGivenDoubleType_ReturnsFloatSqlDbType()
{
Type value = typeof(Double);
const SqlDbType expectedSqlDbType = SqlDbType.Float;
SqlDbType actual = ClrTypeToSqlDbTypeMapper.GetSqlDbTypeFromClrType(value);
Assert.AreEqual(expectedSqlDbType, actual);
}
[TestMethod]
public void GetSqlDbTypeFromClrType_WhenGivenNullableDoubleType_ReturnsFloatSqlDbType()
{
Type value = typeof(Double?);
const SqlDbType expectedSqlDbType = SqlDbType.Float;
SqlDbType actual = ClrTypeToSqlDbTypeMapper.GetSqlDbTypeFromClrType(value);
Assert.AreEqual(expectedSqlDbType, actual);
}
[TestMethod]
public void GetSqlDbTypeFromClrType_WhenGivenSingleType_ReturnsRealSqlDbType()
{
Type value = typeof(Single);
const SqlDbType expectedSqlDbType = SqlDbType.Real;
SqlDbType actual = ClrTypeToSqlDbTypeMapper.GetSqlDbTypeFromClrType(value);
Assert.AreEqual(expectedSqlDbType, actual);
}
[TestMethod]
public void GetSqlDbTypeFromClrType_WhenGivenNullableSingleType_ReturnsRealSqlDbType()
{
Type value = typeof(Single?);
const SqlDbType expectedSqlDbType = SqlDbType.Real;
SqlDbType actual = ClrTypeToSqlDbTypeMapper.GetSqlDbTypeFromClrType(value);
Assert.AreEqual(expectedSqlDbType, actual);
}
[TestMethod]
public void GetSqlDbTypeFromClrType_WhenGivenTimeSpanType_ReturnsTimeSqlDbType()
{
Type value = typeof (TimeSpan);
const SqlDbType expectedSqlDbType = SqlDbType.Time;
SqlDbType actual = ClrTypeToSqlDbTypeMapper.GetSqlDbTypeFromClrType(value);
Assert.AreEqual(expectedSqlDbType, actual);
}
[TestMethod]
public void GetSqlDbTypeFromClrType_WhenGivenGuidType_ReturnsUniqueIdentifierSqlDbType()
{
Type value = typeof (Guid);
const SqlDbType expectedSqlDbType = SqlDbType.UniqueIdentifier;
SqlDbType actual = ClrTypeToSqlDbTypeMapper.GetSqlDbTypeFromClrType(value);
Assert.AreEqual(expectedSqlDbType, actual);
}
[TestMethod]
public void GetSqlDbTypeFromClrType_WhenGivenNullableGuidType_ReturnsUniqueIdentifierSqlDbType()
{
Type value = typeof(Guid?);
const SqlDbType expectedSqlDbType = SqlDbType.UniqueIdentifier;
SqlDbType actual = ClrTypeToSqlDbTypeMapper.GetSqlDbTypeFromClrType(value);
Assert.AreEqual(expectedSqlDbType, actual);
}
[TestMethod]
public void GetSqlDbTypeFromClrType_WhenGivenByteArrayType_ReturnsBinarySqlDbType()
{
Type value = typeof (Byte[]);
const SqlDbType expectedSqlDbType = SqlDbType.Binary;
SqlDbType actual = ClrTypeToSqlDbTypeMapper.GetSqlDbTypeFromClrType(value);
Assert.AreEqual(expectedSqlDbType, actual);
}
[TestMethod]
public void GetSqlDbTypeFromClrType_WhenGivenNullableByteArrayType_ReturnsBinarySqlDbType()
{
Type value = typeof(Byte?[]);
const SqlDbType expectedSqlDbType = SqlDbType.Binary;
SqlDbType actual = ClrTypeToSqlDbTypeMapper.GetSqlDbTypeFromClrType(value);
Assert.AreEqual(expectedSqlDbType, actual);
}
[TestMethod]
public void GetSqlDbTypeFromClrType_WhenGivenCharArrayType_ReturnsCharSqlDbType()
{
Type value = typeof (Char[]);
const SqlDbType expectedSqlDbType = SqlDbType.Char;
SqlDbType actual = ClrTypeToSqlDbTypeMapper.GetSqlDbTypeFromClrType(value);
Assert.AreEqual(expectedSqlDbType, actual);
}
[TestMethod]
public void GetSqlDbTypeFromClrType_WhenGivenNullableCharArrayType_ReturnsCharSqlDbType()
{
Type value = typeof(Char?[]);
const SqlDbType expectedSqlDbType = SqlDbType.Char;
SqlDbType actual = ClrTypeToSqlDbTypeMapper.GetSqlDbTypeFromClrType(value);
Assert.AreEqual(expectedSqlDbType, actual);
}
[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void GetSqlDbTypeFromClrType_WhenGivenUnexpectedType_THEN()
{
Type value = typeof(StringBuilder);
ClrTypeToSqlDbTypeMapper.GetSqlDbTypeFromClrType(value);
}
}
}
Gist
The full class and tests code is available at my `ClrTypeToSqlDbTypeMapper for C# .Net` Gist.