示例#1
0
        private static List <string> CreateColumnTexts(object obj, Dictionary <string, object> parameters)
        {
            var temp = new List <string>();


            foreach (var key in parameters.Keys)
            {
                var classConfig = ClassConfigContainer.FindClassConfig2(obj);

                PropertyConfig propertyConfig = classConfig.GetPropertyConfig(key);


                if (propertyConfig == null)
                {
                    throw new EasylinkException(
                              "Error occurred when create create column texts. property name {0}  is not defined in the {1} mapping.",
                              key, obj.GetType().Name);
                }

                string item = string.Format("{0}={1}", propertyConfig.ColumnName, parameters[key]);

                temp.Add(item);
            }
            return(temp);
        }
示例#2
0
        private static string GetRecordId(object obj)
        {
            var isAuditable = Shared.IsAuditable(obj);

            if (isAuditable)
            {
                var classsConfig = ClassConfigContainer.FindClassConfig2(obj);

                if (classsConfig == null)
                {
                    throw new EasylinkException("class {0} mapping file is not found!", obj.GetType().Name);
                }


                var key = classsConfig.IdPropertyName;

                if (string.IsNullOrEmpty(key))
                {
                    throw new EasylinkException("class {0} mapping Id property is not set.", obj.GetType().Name);
                }


                var property = obj.GetType().GetProperty(key);

                var propertyValue = property.GetValue(obj, null);

                return(propertyValue.ToString());
            }


            throw new EasylinkException("Record key is not set.");
        }
示例#3
0
        private static string CreateUpdateAuditText(object obj, Dictionary <string, string> changes)
        {
            var temp = new List <string>();


            foreach (var key in changes.Keys)
            {
                var classConfig = ClassConfigContainer.FindClassConfig2(obj);

                PropertyConfig propertyConfig = classConfig.GetPropertyConfig(key);

                if (propertyConfig == null)
                {
                    throw new EasylinkException(
                              "Error occurred when create update audit text. property name {0}  is not defined in the {1} mapping.",
                              key, obj.GetType().Name);
                }

                string item = string.Format("{0} {1}", propertyConfig.ColumnName, changes[key]);

                temp.Add(item);
            }

            return("Updated " + string.Join(", ", temp) + ".");
        }
示例#4
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);
            }
        }
示例#5
0
        private object GetObjectId(object obj)
        {
            var classConfig = ClassConfigContainer.FindClassConfig2(obj);

            var idPropertyName = classConfig.IdPropertyName;

            var property = obj.GetType().GetProperty(idPropertyName);

            return(property.GetValue(obj, null));
        }
示例#6
0
        private static AuditBase CreateAuditRecordBase(object obj, DbOperation operation, string auditText)
        {
            var auditRecord = new AuditBase
            {
                RecordId      = GetRecordId(obj),
                TableName     = ClassConfigContainer.FindClassConfig2(obj).TableName,
                Description   = auditText,
                TimeStamp     = DateTime.Now,
                Operation     = operation.ToString(),
                ObjectToAudit = obj
            };


            return(auditRecord);
        }
示例#7
0
        private void NullifyIdProperty(object obj)
        {
            var classConfig = ClassConfigContainer.FindClassConfig2(obj);

            var idPropertyName = classConfig.IdPropertyName;

            var property = obj.GetType().GetProperty(idPropertyName);

            if (property.PropertyType == typeof(string) || property.PropertyType == typeof(int?) ||
                property.PropertyType == typeof(Int64?) || property.PropertyType == typeof(DateTime?))
            {
                property.SetValue(obj, null, null);
            }

            if (property.PropertyType == typeof(int) || property.PropertyType == typeof(long))
            {
                property.SetValue(obj, -1, null);
            }

            if (property.PropertyType == typeof(DateTime))
            {
                property.SetValue(obj, DateTime.MinValue, null);
            }
        }
示例#8
0
        internal static bool IsAuditable(object obj)
        {
            var classConfig = ClassConfigContainer.FindClassConfig2(obj);

            return(classConfig.Auditable);
        }