Exemple #1
0
        public void Insert(object obj)
        {
            string insertSql = ClassConfigContainer.FindInsertSql(obj.GetType());

            try
            {
                var classConfig = ClassConfigContainer.FindClassConfig2(obj);

                if (classConfig.NextIdOption == NextIdOption.Sequence)
                {
                    if (string.IsNullOrEmpty(classConfig.SequenceName))
                    {
                        throw new EasylinkException(
                                  "Class {0} mapping NextId option is Sequence, but no sequence name is found.",
                                  obj.GetType().Name);
                    }

                    SetObjectIdBeforeInsert(obj);
                }


                Command.Start();

                var propertyParameters =
                    ParametersCreator.CreatePropertyParameters(ParameterPrefix, obj, ref insertSql);

                var sqlParameters = ConvertPropertyParametersToSqlParameters(propertyParameters);


                insertSql = BeforeInsertingRecord(insertSql);


                var result = ExecuteScalar(insertSql, sqlParameters);

                if (classConfig.NextIdOption == NextIdOption.AutoIncrement)
                {
                    string idPropertyName;

                    var id = SetObjectIdAfterInsert(obj, result, out idPropertyName);

                    propertyParameters.Add(idPropertyName, id);
                }


                if (Shared.IsAuditable(obj))
                {
                    string auditText = Auditor.AuditInsert(obj, propertyParameters);


                    InsertAuditRecord(obj, DbOperation.Insert, auditText);
                }
            }

            catch (Exception ex)
            {
                throw CreateEasylinkException(insertSql, ex);
            }
        }