Exemplo n.º 1
0
        private DbValueConverter BuildNullableValueConverter(Type nullablePropertyType, DbValueConverter baseConv = null)
        {
            var         valueFromNullable = BuildNullableValueReader(nullablePropertyType);
            Type        colType;
            ConvertFunc propToColFunc;
            ConvertFunc colToPropFunc;

            if (baseConv == null)
            {
                colType = baseConv == null?nullablePropertyType.GetUnderlyingType() : baseConv.ColumnType;

                propToColFunc =
                    x => x == null || x == DBNull.Value ?
                    x :
                    x.GetType().IsValueType ? x : valueFromNullable(x);
                colToPropFunc = x => x;
            }
            else
            {
                colType       = nullablePropertyType.GetUnderlyingType();
                propToColFunc =
                    x => x == null || x == DBNull.Value ?
                    x :
                    x.GetType().IsValueType ? baseConv.PropertyToColumn(x) :
                    baseConv.PropertyToColumn(valueFromNullable(x));
                colToPropFunc = baseConv.ColumnToProperty;
            }
            var convObj = new DbValueConverter(colType, nullablePropertyType, colToPropFunc, propToColFunc);

            return(convObj);
        }
Exemplo n.º 2
0
 private void AddWithLock(DbValueConverter convertInfo)
 {
     lock (_lock) {
         var types = new TypeTuple(convertInfo.ColumnType, convertInfo.PropertyType);
         _converters[types] = convertInfo;
     }
 }
Exemplo n.º 3
0
        public DbValueConverter AddConverter(Type columnType, Type propertyType, ConvertFunc columnToProperty, ConvertFunc propertyToColumn)
        {
            var conv = new DbValueConverter(columnType, propertyType, columnToProperty, propertyToColumn);

            AddWithLock(conv);
            return(conv);
        }
Exemplo n.º 4
0
        private DbValueConverter BuildEnumValueConverter(Type columnType, Type enumType)
        {
            var         colToEnum   = BuildIntToEnum(columnType, enumType);
            var         enumToCol   = BuildEnumToInt(columnType, enumType);
            ConvertFunc entToDbFunc = x => Convert.ChangeType(x, columnType);
            var         convObj     = new DbValueConverter(columnType, enumType, (ConvertFunc)colToEnum, (ConvertFunc)enumToCol);

            return(convObj);
        }
Exemplo n.º 5
0
        protected DbValueConverter AddDefaultConverter(Type fromType, Type toType)
        {
            var conv = new DbValueConverter(fromType, toType, BuildImplicitConverter(fromType, toType), BuildImplicitConverter(toType, fromType));

            AddWithLock(conv);
            //Test
            var fromV = Activator.CreateInstance(fromType);
            var toV   = conv.ColumnToProperty(fromV);

            fromV = conv.PropertyToColumn(toV);
            return(conv);
        }