Example #1
0
        public static MeasurementType GetOrAdd(this TableOperations <MeasurementType> measurementTypeTable, string name, string description = null)
        {
            MeasurementType measurementType = measurementTypeTable.QueryRecordWhere("Name = {0}", name);

            if ((object)measurementType == null)
            {
                measurementType             = new MeasurementType();
                measurementType.Name        = name;
                measurementType.Description = description ?? name;

                try
                {
                    measurementTypeTable.AddNewRecord(measurementType);
                }
                catch (Exception ex)
                {
                    // Ignore errors regarding unique key constraints
                    // which can occur as a result of a race condition
                    bool isUniqueViolation = ExceptionHandler.IsUniqueViolation(ex);

                    if (!isUniqueViolation)
                    {
                        throw;
                    }

                    return(measurementTypeTable.QueryRecordWhere("Name = {0}", name));
                }

                measurementType.ID = measurementTypeTable.Connection.ExecuteScalar <int>("SELECT @@IDENTITY");
            }

            return(measurementType);
        }
Example #2
0
        public MeasurementType GetMeasurementType(MeasurementType measurementType)
        {
            MeasurementType cachedMeasurementTypeLine;

            if ((object)measurementType == null)
            {
                return(null);
            }

            if (measurementType.ID == 0)
            {
                return(measurementType);
            }

            if (m_measurementTypes.TryGetValue(measurementType.ID, out cachedMeasurementTypeLine))
            {
                return(cachedMeasurementTypeLine);
            }

            m_measurementTypes.Add(measurementType.ID, measurementType);
            return(measurementType);
        }