Ejemplo n.º 1
0
        protected DynamicParameters CreateDynParameters(object obj, Object parameters)
        {
            DynamicParameters dynParam;
            PropertyList      properties = PropertyInfoHelper.GetProperties(obj);

            //Custom parameters
            if (parameters != null)
            {
                dynParam = new DynamicParameters(parameters);
            }
            else if (properties.Any() && properties.KeyProperty != null)
            {
                dynParam = new DynamicParameters();
                foreach (PropertyInfo dataProp in properties.DataProperties)
                {
                    dynParam.Add(dataProp.Name, dataProp.GetValue(obj, null));
                }

                foreach (PropertyInfo dataObjProp in properties.DataObjectProperties)
                {
                    dynamic dataObj = dataObjProp.GetValue(obj, null);
                    if (dataObj != null)
                    {
                        PropertyList propObjproperties = PropertyInfoHelper.GetProperties(dataObj);

                        if (propObjproperties.KeyProperty != null)
                        {
                            dynParam.Add(string.Format("{0}{1}", propObjproperties.KeyProperty.ReflectedType.Name, propObjproperties.KeyProperty.Name), propObjproperties.KeyProperty.GetValue(dataObj, null));
                        }
                    }
                }
            }
            else
            {
                //Fallback, use all properties from the object as parameters.
                dynParam = new DynamicParameters(obj);
            }

            //Get key field on object

            if (properties.KeyProperty != null)
            {
                //Looked in Dapper source to see that it overwrites existing parameters =)
                dynParam.Add(properties.KeyProperty.Name, properties.KeyProperty.GetValue(obj, null), direction: ParameterDirection.InputOutput);
            }

            return(dynParam);
        }