private DbValueConverter BuildNullableValueConverter(Type nullablePropertyType, DbValueConverter baseConv = null) { var valueFromNullable = BuildNullableGetValueFunc(nullablePropertyType); Type colType; ConvertFunc propToColFunc; ConvertFunc colToPropFunc; if (baseConv == null) { colType = baseConv == null?nullablePropertyType.GetUnderlyingStorageClrType() : baseConv.ColumnType; propToColFunc = x => x == null || x == DBNull.Value ? DBNull.Value : x.GetType().IsValueType ? x : valueFromNullable(x); colToPropFunc = x => x; } else { colType = nullablePropertyType.GetUnderlyingStorageClrType(); propToColFunc = x => x == null || x == DBNull.Value ? DBNull.Value : x.GetType().IsValueType ? baseConv.PropertyToColumn(x) : baseConv.PropertyToColumn(valueFromNullable(x)); colToPropFunc = baseConv.ColumnToProperty; } var convObj = new DbValueConverter(colType, nullablePropertyType, colToPropFunc, propToColFunc); return(convObj); }
public DbValueConverter AddConverter(Type columnType, Type propertyType, ConvertFunc columnToProperty, ConvertFunc propertyToColumn) { var conv = new DbValueConverter(columnType, propertyType, columnToProperty, propertyToColumn); AddConverter(conv); return(conv); }
private DbValueConverter BuildEnumValueConverter(Type columnType, Type enumType) { var colToEnum = BuildIntToEnumFunc(columnType, enumType); var enumToCol = BuildEnumToIntFunc(columnType, enumType); var convObj = new DbValueConverter(columnType, enumType, (ConvertFunc)colToEnum, (ConvertFunc)enumToCol); return(convObj); }
protected DbValueConverter AddDefaultConverter(Type fromType, Type toType) { var conv = new DbValueConverter(fromType, toType, BuildImplicitConverter(fromType, toType), BuildImplicitConverter(toType, fromType)); AddConverter(conv); //Test var fromV = Activator.CreateInstance(fromType); var toV = conv.ColumnToProperty(fromV); fromV = conv.PropertyToColumn(toV); return(conv); }
}//method private void AddConverter(DbValueConverter converter) { var types = new TypeTuple(converter.ColumnType, converter.PropertyType); _converters[types] = converter; }