예제 #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ObjectRecord" /> class.
        /// </summary>
        /// <param name="recordSetDefinition">The table definition.</param>
        /// <param name="randomData">if set to <see langword="true" /> fills columns with random data; otherwise fills them with their default values.</param>
        /// <param name="nullProbability">The probability of a column's value being set to SQL null (0.0 for no nulls) -
        /// this is only applicable is <see paramref="randomData" /> is set to <see langword="true" /> [Defaults to 0.1 = 10%].</param>
        /// <param name="columnGenerators">The column generators is an array of functions that generate a value for each column, if the function is
        /// <see langword="null" /> for a particular index then a random value is generated, if it is not null then the function is used.  The function takes
        /// the current row number as it's only parameter and must return an object of the correct type for the column.</param>
        /// <param name="rowNumber">The optional row number to pass to the generator.</param>
        /// <exception cref="System.ArgumentException">Thrown if the number of column generators exceeds the number of columns in the record set definition.</exception>
        /// <remarks></remarks>
        public ObjectRecord([NotNull] RecordSetDefinition recordSetDefinition, bool randomData = false,
                            double nullProbability = 0.1,
                            Func <int, object>[] columnGenerators = null, int rowNumber = 1)
        {
            _recordSetDefinition = recordSetDefinition;
            int columnCount = recordSetDefinition.FieldCount;

            _columnValues = new object[columnCount];

            if ((columnGenerators != null) &&
                (columnGenerators.Length > recordSetDefinition.FieldCount))
            {
                throw new ArgumentException(
                          "The number of column generators must not exceed the number of columns in the record set definition.",
                          "columnGenerators");
            }

            for (int c = 0; c < columnCount; c++)
            {
                // Check if we have a generator
                if ((columnGenerators != null) &&
                    (columnGenerators.Length > c) &&
                    (columnGenerators[c] != null))
                {
                    // Use generator to get value
                    this[c] = columnGenerators[c](rowNumber);
                }
                else if (randomData)
                {
                    // Generate random value.
                    this[c] = recordSetDefinition[c].GetRandomValue(nullProbability);
                }
                else
                {
                    // Just set to default value (no need to revalidate so set directly).
                    _columnValues[c] = recordSetDefinition[c].DefaultValue;
                }
            }
        }
예제 #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ObjectRecord" /> class.
        /// </summary>
        /// <param name="recordSetDefinition">The table definition.</param>
        /// <param name="columnValues">The column values.</param>
        /// <remarks>
        /// If the number of column values supplied is less than the number of columns then the remaining columns are set to
        /// their equivalent default value.
        /// </remarks>
        public ObjectRecord([NotNull] RecordSetDefinition recordSetDefinition, [NotNull] params object[] columnValues)
        {
            int length  = columnValues.Length;
            int columns = recordSetDefinition.FieldCount;

            if (length > columns)
            {
                throw new ArgumentException(
                          string.Format(
                              "The number of values specified '{0}' cannot exceed the number of expected columns '{1}'.",
                              length, columns), "columnValues");
            }

            _recordSetDefinition = recordSetDefinition;
            _columnValues        = new object[recordSetDefinition.FieldCount];

            // Import values or set to null.
            for (int i = 0; i < columns; i++)
            {
                SetValue(i, i < length ? columnValues[i] : _recordSetDefinition[i].DefaultValue);
            }
        }