コード例 #1
0
        public EntityUniqueConstraint(PropertyProfile uniqueProperty)
        {
            if (uniqueProperty == null)
            {
                throw new ArgumentNullException(nameof(uniqueProperty));
            }

            UniqueProperties = new List <PropertyProfile>()
            {
                uniqueProperty
            };
        }
 /// <summary>
 /// Will the property value getter execute dynamic invoke or not.
 /// </summary>
 /// <returns>Returns True if it will use dynamic invoke, otherwise returns False.</returns>
 public static bool IsDynamicallyInvoked(this PropertyProfile profile)
 {
     if (profile.PropertyExpression.Body is UnaryExpression)
     {
         return(true);
     }
     switch (profile.DbColumnType)
     {
     case NpgsqlDbType.Bigint:
     case NpgsqlDbType.Integer:
     case NpgsqlDbType.Real:
     case NpgsqlDbType.Double:
     case NpgsqlDbType.Numeric:
     case NpgsqlDbType.Boolean:
     case NpgsqlDbType.Smallint:
         return(false);
     }
     return(true);
 }
        /// <summary> Calculates property values of item </summary>
        /// <param name="profile">information about which property it is needed to get value</param>
        /// <param name="item">item with values</param>
        /// <typeparam name="TEntity">Type of entity</typeparam>
        /// <returns>
        /// Returns string representation of the value.
        /// For nullable value returns "null" string.
        /// For those cases where it is impossible to calculate property value without boxing, returns null
        /// </returns>
        public static string GetPropertyValueAsString <TEntity>(this PropertyProfile profile, TEntity item)
            where TEntity : class
        {
            if (profile == null)
            {
                throw new ArgumentNullException(nameof(profile));
            }

            var propertyName = "";

            if (profile.PropertyExpression.Body is MemberExpression memberExpression)
            {
                propertyName = memberExpression.Member.Name;
            }
            else if (profile.PropertyExpression.Body is ParameterExpression parameterExpression)
            {
                propertyName = parameterExpression.Name;
            }
            else if (profile.PropertyExpression.Body is UnaryExpression unaryExpression &&
                     unaryExpression.Operand is MemberExpression operand)
            {
                propertyName = operand.Member.Name;
            }
        private void MapProperties()
        {
            var annotationProperties = EntityType.GetProperties();

            foreach (var propertyInfo in annotationProperties)
            {
                var          propertyAttributes = propertyInfo.GetCustomAttributes(true);
                var          isNotMapped        = false;
                var          isKey = false;
                var          isPartOfUniqueConstraint = false;
                var          isAutoGenerated          = false;
                var          hasUpdateAfterInsert     = false;
                var          hasUpdateAfterUpdate     = false;
                var          columnName   = "";
                NpgsqlDbType?columnDbType = null;

                foreach (var attr in propertyAttributes)
                {
                    if (attr is ColumnAttribute columnAttribute)
                    {
                        columnName = columnAttribute.Name;
                    }
                    else if (attr is KeyAttribute keyAttribute)
                    {
                        isKey = true;
                        var typeId = keyAttribute.TypeId;
                    }
                    else if (attr is DataTypeAttribute dataTypeAttribute)
                    {
                        columnDbType = dataTypeAttribute.DataType.ToNpgsql();
                    }
                    else if (attr is NotMappedAttribute notMappedAttribute)
                    {
                        isNotMapped = true;
                    }
                    else if (attr is AutoGeneratedAttribute)
                    {
                        isAutoGenerated = true;
                    }
                    else if (attr is PartOfUniqueConstraintAttribute)
                    {
                        isPartOfUniqueConstraint = true;
                    }
                    else if (attr is UpdateAfterInsertAttribute)
                    {
                        hasUpdateAfterInsert = true;
                    }
                    else if (attr is UpdateAfterUpdateAttribute)
                    {
                        hasUpdateAfterUpdate = true;
                    }
                }

                if (isNotMapped)
                {
                    continue;
                }

                if (string.IsNullOrWhiteSpace(columnName))
                {
                    columnName = propertyInfo.Name.ToSnakeCase();
                }

                if (!columnDbType.HasValue)
                {
                    columnDbType = propertyInfo.PropertyType.ToNpgsql();
                }

                var parameter = Expression.Parameter(EntityType, "entity");
                var property  = Expression.Property(parameter, propertyInfo);
                var funcType  = typeof(Func <,>).MakeGenericType(EntityType, propertyInfo.PropertyType);
                var lambda    = Expression.Lambda(funcType, property, parameter);

                var propertyProfile = new PropertyProfile(columnName, lambda);
                propertyProfile.HasColumnType(columnDbType.Value, propertyInfo.PropertyType);
                if (isPartOfUniqueConstraint)
                {
                    propertyProfile.ThatIsPartOfUniqueConstraint();
                }

                this.Properties.Add(propertyInfo.Name, propertyProfile);

                if (isKey)
                {
                    propertyProfile.ThatIsPrivateKey();
                }
                if (isAutoGenerated)
                {
                    propertyProfile.ThatIsAutoGenerated();
                }
                if (hasUpdateAfterInsert)
                {
                    propertyProfile.MustBeUpdatedAfterInsert();
                }
                if (hasUpdateAfterUpdate)
                {
                    propertyProfile.MustBeUpdatedAfterUpdate();
                }
            }
        }