Source/NFX/DataAccess/CRUD/Row.cs
/// <summary>
/// Converts field value to the type specified by Schema.FieldDef. For example converts GDID->ulong or ulong->GDID.
/// This method can be overridden to perform custom handling of types,
///  for example one can assign bool field as "Si" that would convert to TRUE.
/// This method is called by SetFieldValue(...) before assigning actual field buffer
/// </summary>
/// <param name="fdef">Field being converted</param>
/// <param name="value">Value to convert</param>
/// <returns>Converted value before assignment to field buffer</returns>
Converts field value to the type specified by Schema.FieldDef. For example converts GDID->ulong or ulong->GDID.
This method can be overridden to perform custom handling of types,
for example one can assign bool field as "Si" that would convert to TRUE.
This method is called by SetFieldValue(...) before assigning actual field buffer
Converts field value to the type specified by Schema . FieldDef . For example converts GDID - > ulong or ulong - > GDID . This method can be overridden to perform custom handling of types for example one can assign bool field as " Si " that would convert to TRUE . This method is called by SetFieldValue ( ... ) before assigning actual field buffer
public virtual object ConvertFieldValueToDef(Schema.FieldDef fdef, object value)
            {
              if (value==DBNull.Value) value = null;
              if (value == null) return null;
              var tv = value.GetType();
              if (tv != fdef.NonNullableType && !fdef.NonNullableType.IsAssignableFrom(tv))
              {
                  if (value is ObjectValueConversion.TriStateBool)
                  {
                    var tsb = (ObjectValueConversion.TriStateBool)value;
                    if (tsb==ObjectValueConversion.TriStateBool.Unspecified)
                      value = null;
                    else
                      value = tsb==ObjectValueConversion.TriStateBool.True;
                    return value;
                  }
                  if (fdef.NonNullableType==typeof(ObjectValueConversion.TriStateBool))
                  {
                    var nb = value.AsNullableBool();
                    if (!nb.HasValue)
                      value = ObjectValueConversion.TriStateBool.Unspecified;
                    else
                      value = nb.Value ? ObjectValueConversion.TriStateBool.True : ObjectValueConversion.TriStateBool.False;
                    return value;
                  }
                  if (fdef.NonNullableType==typeof(NFX.DataAccess.Distributed.GDID))
                  {
                      if (tv==typeof(byte[]))
                        value = new Distributed.GDID((byte[])value);
                      else if (tv==typeof(string))
                      {
                        var sv = (string)value;
                        if (sv.IsNotNullOrWhiteSpace())
                          value = Distributed.GDID.Parse((string)value);
                        else
                          value = fdef.Type == typeof(Distributed.GDID?) ? (Distributed.GDID?)null : Distributed.GDID.Zero;
                      }
                      else
                        value = new Distributed.GDID(0, (UInt64)Convert.ChangeType(value, typeof(UInt64)));
                      return value;
                  }
                  if (tv==typeof(Distributed.GDID))
                  {
                      if (fdef.NonNullableType==typeof(byte[]))
                      {
                        value = ((Distributed.GDID)value).Bytes;
                      }
                      else if (fdef.NonNullableType==typeof(string))
                      {
                        value = value.ToString();
                      }
                      else
                      {
                        var gdid = (Distributed.GDID)value;
                        if (gdid.Era!=0)
                          throw new CRUDException(StringConsts.CRUD_GDID_ERA_CONVERSION_ERROR.Args(fdef.Name, fdef.NonNullableType.Name));
                        value = gdid.ID;
                      }
                      return value;
                  }
                  if (fdef.NonNullableType.IsEnum)
                  {
                      value = value.AsString().AsType(fdef.NonNullableType);
                      return value;
                  }
                  value = Convert.ChangeType(value, fdef.NonNullableType);
              }
              return value;
            }
public virtual object ConvertFieldValueToDef ( Schema . FieldDef fdef , object value ) { if ( value == DBNull . Value ) value = null ; if ( value == null ) return null ; var tv = value . GetType ( ) ; if ( tv != fdef . NonNullableType && ! fdef . NonNullableType . IsAssignableFrom ( tv ) ) { if ( value is ObjectValueConversion . TriStateBool ) { var tsb = ( ObjectValueConversion . TriStateBool ) value ; if ( tsb == ObjectValueConversion . TriStateBool . Unspecified ) value = null ; else value = tsb == ObjectValueConversion . TriStateBool . True ; return value ; } if ( fdef . NonNullableType == typeof ( ObjectValueConversion . TriStateBool ) ) { var nb = value . AsNullableBool ( ) ; if ( ! nb . HasValue ) value = ObjectValueConversion . TriStateBool . Unspecified ; else value = nb . Value ? ObjectValueConversion . TriStateBool . True : ObjectValueConversion . TriStateBool . False ; return value ; } if ( fdef . NonNullableType == typeof ( NFX . DataAccess . Distributed . GDID ) ) { if ( tv == typeof ( byte [ ] ) ) value = new Distributed . GDID ( ( byte [ ] ) value ) ; else if ( tv == typeof ( string ) ) { var sv = ( string ) value ; if ( sv . IsNotNullOrWhiteSpace ( ) ) value = Distributed . GDID . Parse ( ( string ) value ) ; else value = fdef . Type == typeof ( Distributed . GDID ? ) ? ( Distributed . GDID ? ) null : Distributed . GDID . Zero ; } else value = new Distributed . GDID ( 0 , ( UInt64 ) Convert . ChangeType ( value , typeof ( UInt64 ) ) ) ; return value ; } if ( tv == typeof ( Distributed . GDID ) ) { if ( fdef . NonNullableType == typeof ( byte [ ] ) ) { value = ( ( Distributed . GDID ) value ) . Bytes ; } else if ( fdef . NonNullableType == typeof ( string ) ) { value = value . ToString ( ) ; } else { var gdid = ( Distributed . GDID ) value ; if ( gdid . Era != 0 ) throw new CRUDException ( StringConsts . CRUD_GDID_ERA_CONVERSION_ERROR . Args ( fdef . Name , fdef . NonNullableType . Name ) ) ; value = gdid . ID ; } return value ; } if ( fdef . NonNullableType . IsEnum ) { value = value . AsString ( ) . AsType ( fdef . NonNullableType ) ; return value ; } value = Convert . ChangeType ( value , fdef . NonNullableType ) ; } return value ; }
Converts field value to the type specified by Schema.FieldDef.
Converts field value to the type specified by Schema . FieldDef .
// 20150224 DKh, addedEra to GDID. Only GDIDS with ERA=0 can be converted to/from INT64 //20151103 DKh GDID support for byte[] //20160504 Spol GDID support for string // 20161026 Serge: handle values of enumerated field types //Types Differ
Converted value before assignment to field buffer
Converted value before assignment to field buffer
fdef
Schema.FieldDef
Field being converted
Field being converted
value
object
Value to convert
Value to convert