/// <summary>
        /// Tries to set the value of the field with the given <paramref name="name"/>.
        /// Throws an exception if the field doesn't exists.
        /// </summary>
        /// <param name="name"></param>
        /// <param name="value"></param>
        public void SetFieldValue(string name, IValue value)
        {
            IRecordTypeField field = RecordType.GetField(name);

            // Validation: Is it a known field?
            if (field == null)
            {
                throw new SyneryException(String.Format("Field with name='{0}' doesn't exists in record '{1}'.", name, RecordType.FullName));
            }

            // Handle NULL values: Create a typed value by taking the SyneryType from the field definition
            if (value.Type == null)
            {
                value = new TypedValue(field.Type);
            }

            // Validation: Do the field type and the type of the given value match?
            if (field.Type != value.Type)
            {
                throw new SyneryException(String.Format("Field type '{0}' and value type '{1}' don't match. Field name='{2}'.",
                                                        field.Type.PublicName, value.Type.PublicName, name));
            }

            // OK! Set the field value

            _Data[name] = value;
        }
        /// <summary>
        /// Tries to get the value of the field with the given <paramref name="name"/>.
        /// Throws an exception if the field doesn't exists.
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public IValue GetFieldValue(string name)
        {
            if (_Data.ContainsKey(name))
            {
                // a value for the field is available

                return(_Data[name]);
            }
            else
            {
                // try to find the field

                IRecordTypeField field = RecordType.GetField(name);

                if (field == null)
                {
                    throw new SyneryException(String.Format("Field with name='{0}' doesn't exists in record '{1}'.", name, RecordType.FullName));
                }

                // no value has been set yet

                if (field.DefaultValue != null)
                {
                    // the field has a default value
                    return(field.DefaultValue);
                }
                else
                {
                    // the field value is still NULL
                    return(new TypedValue(field.Type));
                }
            }
        }
        /// <summary>
        /// Check whether the field with the given <paramref name="name"/> exists in the record type.
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public bool DoesFieldExists(string name)
        {
            IRecordTypeField field = RecordType.GetField(name);

            if (field != null)
            {
                return(true);
            }

            return(false);
        }
Example #4
0
 /// <summary>
 /// Removes the given <paramref name="field"/> from this record type.
 /// </summary>
 /// <param name="field"></param>
 /// <returns></returns>
 public bool DeleteField(IRecordTypeField field)
 {
     return(_Fields.Remove(field));
 }