Exemplo n.º 1
0
        public void Do()
        {
            if (targetPropertyExpression != null)
            {
                PropertyInfo property = ExpressionParser.GetPropertyInfo(targetPropertyExpression);

                if (!property.CanWrite)
                {
                    return;
                }

                P value = create();

                bool success = true;

                if (autoMapping)
                {
                    success = ModelMapper <P> .Map(value, row, validColumnNames, prefixColumnName, columnNamePropertyMap, ignoreCase);
                }

                if (success)
                {
                    DynamicMethodBuilder <T, P> .CreateSetMethod(property)(model, value);

                    if (mapAction != null)
                    {
                        mapAction(value);
                    }
                }
            }
            else if (targetListPropertyExpression != null)
            {
                PropertyInfo property = ExpressionParser.GetPropertyInfo(targetListPropertyExpression);

                if (!property.CanWrite || !property.CanRead)
                {
                    return;
                }

                Func <T, List <P> > getMethod = DynamicMethodBuilder <T, List <P> > .CreateGetMethod(property);

                List <P> list = getMethod(model);

                if (list == null)
                {
                    list = new List <P>();
                    DynamicMethodBuilder <T, List <P> > .CreateSetMethod(property)(model, list);
                }

                P value;

                if (list.Count != 0 && row.PrevRow != null && uniqueColumnNames.Length > 0 && row.Compare(row.PrevRow, uniqueColumnNames))
                {
                    value = list[list.Count - 1];

                    if (mapAction != null)
                    {
                        mapAction(value);
                    }
                }
                else
                {
                    value = create();

                    bool success = true;

                    if (autoMapping)
                    {
                        success = ModelMapper <P> .Map(value, row, validColumnNames, prefixColumnName, columnNamePropertyMap, ignoreCase);
                    }

                    if (success)
                    {
                        list.Add(value);

                        if (mapAction != null)
                        {
                            mapAction(value);
                        }
                    }
                }
            }
        }
Exemplo n.º 2
0
        protected virtual int Execute(DataRowState rowState, T entity)
        {
            Dictionary <string, object> columnValueMap = new Dictionary <string, object>();

            if (this.autoMapping)
            {
                foreach (var property in typeof(T).GetProperties())
                {
                    if (!property.CanRead)
                    {
                        continue;
                    }

                    DbType?dbType = dagentKernel.GetDbType(property.PropertyType);
                    string columnName;
                    if (dbType.HasValue && columnNamePropertyMap.TryGetColumnName <T>(property, out columnName))
                    {
                        columnValueMap[columnName] = DynamicMethodBuilder <T> .CreateGetMethod(property)(entity);
                    }
                }
            }

            UpdateRow updateRow = new UpdateRow(columnValueMap);

            this.mapAction(updateRow, entity);

            List <Parameter> valueParameters = new List <Parameter>();

            foreach (string columnName in updateRow.ColumnNames)
            {
                object value = updateRow[columnName];

                if (value == null)
                {
                    value = DBNull.Value;
                }

                valueParameters.Add(new Parameter(columnName, value));
            }

            if (parameters != null)
            {
                foreach (Parameter parameter in parameters)
                {
                    valueParameters.Add(parameter);
                }
            }

            string sql = "";

            if (rowState == DataRowState.Added)
            {
                sql = dagentKernel.GetInsertSql(tableName, this.primaryKeys, updateRow.ColumnNames);
            }
            else if (rowState == DataRowState.Modified)
            {
                sql = dagentKernel.GetUpdateSql(tableName, this.primaryKeys, updateRow.ColumnNames)
                      + (string.IsNullOrEmpty(this.where) ? string.Empty : string.Format(" and ({0}) ", where));
            }
            else if (rowState == DataRowState.Deleted)
            {
                sql = dagentKernel.GetDeleteSql(tableName, this.primaryKeys)
                      + (string.IsNullOrEmpty(this.where) ? string.Empty : string.Format(" and ({0}) ", where));
            }

            using (TransactionScope tarnsactionScope = new TransactionScope(dagentKernel))
            {
                DbCommand command = dagentKernel.CreateDbCommand(sql);

                ParameterConverter.SetParamters(command, valueParameters.ToArray(), dagentKernel.CreateDbParameter);

                int rtn = command.ExecuteNonQuery();

                tarnsactionScope.Complete();

                return(rtn);
            }
        }