Exemplo n.º 1
0
        /// <summary>
        /// Retrieves a Dictionary with name and value
        /// for all object properties matching the given criteria.
        /// </summary>
        protected static PropertyContainer ParseProperties(T obj)
        {
            var propertyContainer = new PropertyContainer();

            var typeName      = typeof(T).Name;
            var validKeyNames = new[] { "Id",
                                        string.Format("{0}Id", typeName), string.Format("{0}_Id", typeName) };

            var properties = typeof(T).GetProperties();

            foreach (var property in properties)
            {
                // Skip reference types (but still include string!)
                if (property.PropertyType.IsClass && property.PropertyType != typeof(string))
                {
                    continue;
                }

                // Skip methods without a public setter
                if (property.GetSetMethod() == null)
                {
                    continue;
                }

                // Skip methods specifically ignored
                if (property.IsDefined(typeof(DapperIgnore), false))
                {
                    continue;
                }

                // Skip property with attribute "PrimaryKey"
                if (property.GetCustomAttribute(typeof(PrimaryKey)) != null)
                {
                    continue;
                }

                var name  = property.Name;
                var value = typeof(T).GetProperty(property.Name).GetValue(obj, null);

                if (property.IsDefined(typeof(DapperKey), false) || validKeyNames.Contains(name))
                {
                    propertyContainer.AddId(name, value);
                    propertyContainer.AddValue(name, value);
                }
                else
                {
                    propertyContainer.AddValue(name, value);
                }
            }

            return(propertyContainer);
        }
Exemplo n.º 2
0
        private static PropertyContainer ParseProperties <T>(T obj)
        {
            try
            {
                var propertyContainer = new PropertyContainer();

                var typeName      = typeof(T).Name;
                var validKeyNames = new[] { "Id",
                                            string.Format("{0}Id", typeName), string.Format("{0}_Id", typeName) };

                var properties = typeof(T).GetProperties();
                foreach (var property in properties)
                {
                    // Skip reference types (but still include string!)
                    if (property.PropertyType.IsClass && property.PropertyType != typeof(string))
                    {
                        continue;
                    }

                    // Skip methods without a public setter
                    if (property.GetSetMethod() == null)
                    {
                        continue;
                    }

                    // Skip methods specifically ignored
                    if (property.IsDefined(typeof(DapperIgnore), false))
                    {
                        continue;
                    }

                    if (property.PropertyType.IsAbstract)
                    {
                        continue;
                    }

                    var name  = property.Name;
                    var value = typeof(T).GetProperty(property.Name).GetValue(obj, null);

                    if (property.IsDefined(typeof(DapperKey), false) || validKeyNames.Contains(name))
                    {
                        propertyContainer.AddId(name, value);
                    }
                    else
                    {
                        propertyContainer.AddValue(name, value);
                    }
                }

                return(propertyContainer);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
Exemplo n.º 3
0
        private static PropertyContainer ParseProperties <T>(T obj)
        {
            PropertyContainer          propertyContainer = new PropertyContainer();
            IEnumerable <PropertyInfo> properties        = typeof(T).GetProperties().Where(c => c.DeclaringType == typeof(T) || c.IsDefined(typeof(DapperKeyAttribute), false));

            foreach (PropertyInfo property in properties)
            {
                // Skip reference types (but still include string!)
                if (property.PropertyType.GetTypeInfo().IsInterface || (property.PropertyType.GetTypeInfo().IsClass&& property.PropertyType != typeof(string)))
                {
                    continue;
                }

                // Skip methods without a public setter
                if (property.GetSetMethod() == null)
                {
                    continue;
                }

                // Skip methods without db type defined
                if (!property.IsDefined(typeof(DapperTypeAttribute), false))
                {
                    continue;
                }

                object value             = typeof(T).GetProperty(property.Name).GetValue(obj, null);
                DbType dbType            = ((DapperTypeAttribute)property.GetCustomAttribute(typeof(DapperTypeAttribute))).DbType;
                bool   isKey             = property.IsDefined(typeof(DapperKeyAttribute), false);
                bool   isCriticalCommand = property.IsDefined(typeof(DapperCriticalRestrictionAttribute), false);

                if (isKey && isCriticalCommand)
                {
                    throw new InvalidOperationException("Critical restriction cannot be set on Key.");
                }

                if (isKey)
                {
                    propertyContainer.AddKey(property.Name, value, dbType, ((DapperKeyAttribute)property.GetCustomAttribute(typeof(DapperKeyAttribute))).Identity);
                }
                else
                {
                    propertyContainer.AddValue(property.Name, value, dbType, isCriticalCommand);
                }

                if (isCriticalCommand)
                {
                    propertyContainer.AddCriticalRestriction(property.Name, ((DapperCriticalRestrictionAttribute)property.GetCustomAttribute(typeof(DapperCriticalRestrictionAttribute))).SqlOperation);
                }
            }
            return(propertyContainer);
        }
        /// <summary>
        /// Retrieves a Dictionary with name and value
        /// for all object properties matching the given criteria.
        /// </summary>
        private static PropertyContainer ParseProperties <U>(U obj)
        {
            var propertyContainer = new PropertyContainer();

            var typeName      = typeof(U).Name;
            var validKeyNames = new[] { "Id",
                                        string.Format("{0}Id", typeName), string.Format("{0}_Id", typeName) };

            //var properties = typeof(U).GetProperties();
            var properties = obj.GetType().GetProperties();

            foreach (var property in properties)
            {
                var name  = property.Name;
                var value = obj.GetType().GetProperty(property.Name).GetValue(obj, null);
                if (validKeyNames.Contains(name))
                {
                    propertyContainer.AddId(name, value);
                }
                else
                {
                    propertyContainer.AddValue(name, value);
                }
                //// Skip reference types (but still include string!)
                //if (property.PropertyType.IsClass && property.PropertyType != typeof(string))
                //    continue;

                //// Skip methods without a public setter
                //if (property.GetSetMethod() == null)
                //    continue;

                //// Skip methods specifically ignored
                //if (property.IsDefined(typeof(DapperIgnore), false))
                //    continue;

                //var name = property.Name;
                ////var value = typeof(U).GetProperty(property.Name).GetValue(obj, null);
                //var value = obj.GetType().GetProperty(property.Name).GetValue(obj, null);

                //if (property.IsDefined(typeof(DapperKey), false) || validKeyNames.Contains(name))
                //{
                //    propertyContainer.AddId(name, value);
                //}
                //else
                //{
                //    propertyContainer.AddValue(name, value);
                //}
            }

            return(propertyContainer);
        }
        //TODO: Cache the properties (without values, of course)
        protected static PropertyContainer ParseProperties(TEntity entity)
        {
            var propertyContainer = new PropertyContainer();

            var type = typeof(TEntity);

            var typeName      = type.Name;
            var validKeyNames = new[]
            {
                "Id",
                string.Concat(typeName, "Id"),
                string.Concat(typeName, "_Id")
            };

            var properties = type.GetProperties();

            foreach (var property in properties)
            {
                // Skip reference types (but still include string!)
                if (property.PropertyType.GetTypeInfo().IsClass&& property.PropertyType != typeof(string))
                {
                    continue;
                }

                // Skip methods without a public setter
                if (property.GetSetMethod() == null)
                {
                    continue;
                }

                // Skip methods specifically ignored
                if (property.IsDefined(typeof(DapperIgnore), false))
                {
                    continue;
                }

                var name  = property.Name;
                var value = type.GetProperty(property.Name).GetValue(entity, null);

                if (property.IsDefined(typeof(DapperKey), false) || validKeyNames.Contains(name))
                {
                    propertyContainer.AddId(name, value);
                }
                else
                {
                    propertyContainer.AddValue(name, value);
                }
            }

            return(propertyContainer);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Retrieves a Dictionary with name and value
        /// for all object properties matching the given criteria.
        /// </summary>
        private static PropertyContainer ParseProperties <T>(T obj)
        {
            var propertyContainer = new PropertyContainer();

            var typeName      = typeof(T).Name;
            var validKeyNames = new[] { "Id",
                                        $"{typeName}Id", $"{typeName}_Id" };

            var properties = typeof(T).GetProperties();

            foreach (var property in properties)
            {
                // Skip reference types (but still include string!)
                if (property.PropertyType.IsClass && property.PropertyType != typeof(string))
                {
                    continue;
                }

                // Skip methods without a public setter
                if (property.GetSetMethod() == null)
                {
                    continue;
                }

                // Skip methods specifically ignored
                if (property.IsDefined(typeof(Dapper.Contrib.Extensions.ComputedAttribute), false))
                {
                    continue;
                }

                var name  = property.Name;
                var value = typeof(T).GetProperty(property.Name).GetValue(obj, null);

                if (property.IsDefined(typeof(Dapper.Contrib.Extensions.KeyAttribute), false) || validKeyNames.Contains(name))
                {
                    propertyContainer.AddId(name, value);
                }
                else
                {
                    propertyContainer.AddValue(name, value);
                }
            }

            return(propertyContainer);
        }
Exemplo n.º 7
0
        private static PropertyContainer ParseProperties <T>(T obj)
        {
            PropertyContainer propertyContainer = new PropertyContainer();

            PropertyInfo[] properties = typeof(T).GetProperties();
            foreach (PropertyInfo property in properties)
            {
                // Skip reference types (but still include string!)
                if (property.PropertyType.GetTypeInfo().IsInterface || (property.PropertyType.GetTypeInfo().IsClass&& property.PropertyType != typeof(string)))
                {
                    continue;
                }

                // Skip methods without a public setter
                if (property.GetSetMethod() == null)
                {
                    continue;
                }

                // Skip methods without db type defined
                if (!property.IsDefined(typeof(DapperTypeAttribute), false))
                {
                    continue;
                }

                object value  = typeof(T).GetProperty(property.Name).GetValue(obj, null);
                DbType dbType = ((DapperTypeAttribute)property.GetCustomAttribute(typeof(DapperTypeAttribute))).DbType;
                if (property.IsDefined(typeof(DapperKeyAttribute), false))
                {
                    propertyContainer.AddKey(property.Name, value, dbType, ((DapperKeyAttribute)property.GetCustomAttribute(typeof(DapperKeyAttribute))).Identity);
                }
                else
                {
                    propertyContainer.AddValue(property.Name, value, dbType);
                }
            }
            return(propertyContainer);
        }