- Home
- LLBLGen Pro
- LLBLGen Pro Runtime Framework
Best way to convert a db Y/N column into .net boolean?
Joined: 16-Sep-2009
I don't have a choice to alter the db, otherwise I would definitely do that.
If I have an IsActive column and its values would be Y or N, with the LLBLGen how would I convert that to a .net boolean in an entity? (I obviously don't need the syntax, but more of "where" to best do it)
Is it customary to make another partial class file and just tack on another property that handles the conversion from y/n to bool? What about the old property that would still be sitting there as string? I wouldn't want other developers to use this in .net.
Lastly, keep in mind that I'd have to make these changes for a boat load of entities. Any quicker ways to do it for several of them - or is there something in the LLBLGen designer that allows you to have a custom getter/setter function for the property? Especially when converting between data types.
Joined: 14-Dec-2003
Yes you have to write one of your own. Look in the sdk documentation. Here is the one I use for Y/N to bool.'
using System;
using System.ComponentModel;
namespace XXXX.Utility.TypeConverters
{
/// <summary>
/// Implementation of a BooleanChar1Converter. This converter uses 'Boolean' as its core type and converts char1 string values Y/N to
/// and from the boolean.
/// Any value other than Y/y is seen as false, and 0 is seen as false.
/// </summary>
/// <remarks>This implementation is targeted to be used as a converter which is instantiated through code, not through TypeDescriptor.GetConverter.
/// This means that this converter can be used in an attribute on a type, but the implementation can fail (but doesn't have to) in that
/// situation as it's not tested in that scenario. The core difference is that 'context' is ignored in the converter code.</remarks>
[Description("Converter with a core type System.Boolean, for mapping a field with a .NET type System.Boolean onto a database char 1 field")]
public class BooleanChar1Converter : TypeConverter
{
/// <summary>
/// Initializes a new instance of the <see cref="BooleanChar1Converter"/> class.
/// </summary>
public BooleanChar1Converter()
{
}
/// <summary>
/// Returns whether this converter can convert an object of the given type to the type of this converter (Boolean).
/// </summary>
/// <param name="context">Ignored</param>
/// <param name="sourceType">A <see cref="T:System.Type"/> that represents the type you want to convert from.</param>
/// <returns>
/// <see langword="true "/>if this converter can perform the conversion; otherwise, <see langword="false"/>.
/// </returns>
/// <remarks>Accepted types are: String</remarks>
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
// expect char(1).
switch(sourceType.FullName)
{
case "System.String":
return true;
default:
return false;
}
}
/// <summary>
/// Returns whether this converter can convert the object to the specified type.
/// </summary>
/// <param name="context">Ignored</param>
/// <param name="destinationType">A <see cref="T:System.Type"/> that represents the type you want to convert to.</param>
/// <returns>
/// <see langword="true "/>if this converter can perform the conversion; otherwise, <see langword="false"/>.
/// </returns>
/// <remarks>Accepted types are: string. True will be converted to Y, false will be
/// converted to N.</remarks>
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
// any integer type is accepted. No fractional types like float/double.
switch(destinationType.FullName)
{
case "System.String":
return true;
default:
return false;
}
}
/// <summary>
/// Converts the given object to the type of this converter (Boolean).
/// </summary>
/// <param name="context">Ignored</param>
/// <param name="culture">Ignored</param>
/// <param name="value">The <see cref="T:System.Object"/> to convert.</param>
/// <returns>
/// An <see cref="T:System.Object"/> that represents the converted value, which is of type boolean.
/// </returns>
/// <exception cref="T:System.NotSupportedException">The conversion could not be performed.</exception>
public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
{
bool toReturn = false;
switch(value.GetType().FullName)
{
case "System.String":
string s = ((string) value).ToUpperInvariant();
toReturn = (s == "Y");
break;
default:
return false;
}
return toReturn;
}
/// <summary>
/// Converts the given value object to the specified type
/// </summary>
/// <param name="context">Ignored</param>
/// <param name="culture">Ignored</param>
/// <param name="value">The <see cref="T:System.Object"/> to convert.</param>
/// <param name="destinationType">The <see cref="T:System.Type"/> to convert the <paramref name="value"/> parameter to.</param>
/// <returns>
/// An <see cref="T:System.Object"/> that represents the converted value. The value will be 1 if <paramref name="value"/> is true, otherwise 0
/// </returns>
/// <exception cref="T:System.ArgumentNullException">The <paramref name="destinationType"/> parameter is <see langword="null"/>.</exception>
/// <exception cref="T:System.NotSupportedException">The conversion could not be performed.</exception>
public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
{
if(value==null)
{
throw new ArgumentNullException("value", "Value can't be null");
}
if(! (value is bool))
{
var type = value.GetType();
//throw new ArgumentException(string.Format("Value isn't of type boolean, it is {0}",type ), "value");
return value;
}
if((bool)value)
{
return "Y";
}
return "N";
}
/// <summary>
/// Creates an instance of the Type that this <see cref="T:System.ComponentModel.TypeConverter"/> is associated with (bool)
/// </summary>
/// <param name="context">ignored.</param>
/// <param name="propertyValues">ignored.</param>
/// <returns>
/// An <see cref="T:System.Object"/> of type bool. It always returns 'true' for this converter.
/// </returns>
public override object CreateInstance(ITypeDescriptorContext context, System.Collections.IDictionary propertyValues)
{
return true;
}
}
}