Example #1
0
        public static IEnumerable <IDbDataParameter> BindingModelToParameters <TModel>(
            TModel model, IEnumerable <IDbModelPropertyStrategy> modelPropertyStrategies, IEnumerable <IDbDataParameter> Parameters)
        {
            List <IDbDataParameter> parameters = new List <IDbDataParameter>(Parameters);

            foreach (var parameter in parameters)
            {
                var propBindingQuery = modelPropertyStrategies
                                       .Where(c => c.GetParameterName() == (parameter.ParameterName.Substring(1)));

                if (propBindingQuery.Any())
                {
                    var            propertyBindingInfo = propBindingQuery.First();
                    PropertyInfo   modelProperty       = propertyBindingInfo.GetPropertyInfo();
                    object         value     = modelProperty.GetValue(model, null);
                    ITypeConverter converter = _sConverterFactory.GetConvertType(
                        (propertyBindingInfo.GetMapDbType() == DbType.Guid)
                        ? typeof(Guid)
                        : modelProperty.PropertyType);

                    if (value != null)
                    {
                        if (converter is StringConverter)
                        {
                            if (propertyBindingInfo.GetLength() != null)
                            {
                                if (value.ToString().Length > propertyBindingInfo.GetLength().Value)
                                {
                                    throw new InvalidOperationException("ERROR_MODEL_DATA_LENGTH_TOO_LONG");
                                }
                            }
                        }

                        if (converter is DateTimeConverter)
                        {
                            DateTime dtValue = (DateTime)value;
                            parameter.DbType = DbType.DateTime;
                            parameter.Size   = 8;

                            if (dtValue == DateTime.MinValue)
                            {
                                if (propertyBindingInfo.IsAutoGenerated() || propertyBindingInfo.IsAutoGeneratedAtUpdate())
                                {
                                    parameter.Value = DateTime.Now;
                                }
                                else
                                {
                                    if (dtValue < (new DateTime(1900, 1, 1, 0, 0, 0)))
                                    {
                                        throw new InvalidOperationException("E_DATETIME_RANGE_INVALID");
                                    }
                                    else
                                    {
                                        throw new InvalidOperationException("E_DATETIME_PARAMETER_NOT_PRESENT");
                                    }
                                }
                            }
                            else
                            {
                                if (propertyBindingInfo.IsAutoGeneratedAtUpdate())
                                {
                                    parameter.Value = DateTime.Now;
                                }
                                else
                                {
                                    parameter.Value = dtValue;
                                }
                            }

                            continue;
                        }

                        if (converter != null)
                        {
                            if (converter is EnumConverter)
                            {
                                parameter.Value = Convert.ToInt32(value);
                            }
                            else
                            {
                                parameter.Value = converter.Convert(value);
                            }

                            continue;
                        }
                        else
                        {
                            if (value.GetType().IsValueType)
                            {
                                parameter.Value = value;
                            }
                            else
                            {
                                if (value == null)
                                {
                                    parameter.Value = DBNull.Value;
                                }
                                else
                                {
                                    parameter.Value = value.ToString();
                                }
                            }
                        }
                    }
                    else
                    {
                        if (propertyBindingInfo.IsAllowNull())
                        {
                            if (propertyBindingInfo.GetMapDbType() != null)
                            {
                                parameter.DbType = propertyBindingInfo.GetMapDbType().Value;
                            }

                            parameter.Value = DBNull.Value;
                        }
                        else
                        {
                            if (converter is GuidConverter)
                            {
                                if (propertyBindingInfo.IsAutoGenerated() || propertyBindingInfo.IsAutoGeneratedAtUpdate())
                                {
                                    parameter.DbType = DbType.Guid;
                                    parameter.Size   = 16;
                                    parameter.Value  = Guid.NewGuid();
                                    continue;
                                }
                            }

                            if (converter is DateTimeConverter)
                            {
                                if (propertyBindingInfo.IsAutoGenerated() || propertyBindingInfo.IsAutoGeneratedAtUpdate())
                                {
                                    parameter.DbType = DbType.DateTime;
                                    parameter.Size   = 8;
                                    parameter.Value  = DateTime.Now;
                                    continue;
                                }
                            }

                            // for varbinary.
                            if (propertyBindingInfo.GetMapDbType() != null &&
                                propertyBindingInfo.GetMapDbType().Value == DbType.Binary)
                            {
                                parameter.Value = new byte[] { }; // empty byte array.
                                continue;
                            }

                            if (propertyBindingInfo.GetPropertyInfo().PropertyType.IsValueType)
                            {
                                parameter.Value = Activator.CreateInstance(
                                    propertyBindingInfo.GetPropertyInfo().PropertyType);
                            }
                            else
                            {
                                parameter.Value = string.Empty;
                            }
                        }
                    }
                }
            }

            return(parameters);
        }