예제 #1
0
        /// <summary>
        /// Verifies the type of for entity.
        /// </summary>
        /// <param name="entityType">Type of the entity.</param>
        /// <param name="DTOType">Type of the DTO.</param>
        /// <param name="mappingType">Type of the mapping.</param>
        /// <returns></returns>
        //private static bool VerifyForEntityType(Type entityType, Type DTOType, out MappingType mappingType)
        //{
        //    var attributes = DTOType.GetCustomAttributes(typeof(EntityMappingAttribute), false);
        //    if (attributes.Count() == 1)
        //    {
        //        var mappingAttribute = (EntityMappingAttribute)attributes[0];
        //        mappingType = mappingAttribute.MappingType;
        //        return true;
        //    }

        //    throw new EntityConversionException("Only one EntityMappingAttribute can be applied on type '{0}' !", DTOType.ToString());
        //}

        private static bool VerifyForEntityType(Type entityType, Type DTOType, out MappingType mappingType)
        {
            var attributes = DTOType.GetCustomAttributes(typeof(EntityMappingAttribute), false);

            if (attributes.Count() == 1)
            {
                var mappingAttribute = (EntityMappingAttribute)attributes[0];
                mappingType = mappingAttribute.MappingType;
                return(true);
            }

            throw new EntityConversionException("Only one EntityMappingAttribute can be applied on type '{0}' !", DTOType.ToString());
        }
예제 #2
0
        /// <summary>
        /// Verifies the type of for viewModel.
        /// </summary>
        /// <param name="entityType">Type of the viewModel.</param>
        /// <param name="DTOType">Type of the DTO.</param>
        /// <param name="mappingType">Type of the mapping.</param>
        /// <returns></returns>
        private static bool VerifyForViewModelType(Type entityType, Type DTOType, out MappingType mappingType)
        {
            var attributes = DTOType.GetCustomAttributes(typeof(ViewModelMappingAttribute), false);

            if (attributes.Count() == 1)
            {
                var mappingAttribute = (ViewModelMappingAttribute)attributes[0];
                mappingType = mappingAttribute.MappingType;
                return(mappingAttribute.MappedViewModelTypeFullName.Equals(entityType.FullName));
            }

            throw new DTOConversionException("Only one ViewModelMappingAttribute can be applied on type '{0}' !", DTOType.ToString());
        }
예제 #3
0
        /// <summary>
        /// Gets the name of the entity property.
        /// </summary>
        /// <param name="property">The property.</param>
        /// <param name="mappingType">Type of the mapping.</param>
        /// <param name="entityFromDTO">if set to <c>true</c> [entity from DTO].</param>
        /// <returns></returns>
        private static string GetEntityPropertyName(PropertyInfo property, MappingType mappingType, bool entityFromDTO)
        {
            string entityPropertyName = string.Empty;
            var    attribute          =
                (EntityPropertyMappingAttribute)
                Attribute.GetCustomAttribute(property, typeof(EntityPropertyMappingAttribute));

            bool skipMapping = false;

            if (attribute != null)
            {
                if (entityFromDTO)
                {
                    skipMapping = !(attribute.MappingDirection == MappingDirectionType.EntityFromDTO || attribute.MappingDirection == MappingDirectionType.Both);
                }
                else
                {
                    skipMapping = !(attribute.MappingDirection == MappingDirectionType.DTOFromEntity || attribute.MappingDirection == MappingDirectionType.Both);
                }
            }

            switch (mappingType)
            {
            case MappingType.TotalExplicit:
                if (attribute == null)
                {
                    throw new EntityConversionException(
                              string.Format(
                                  Thread.CurrentThread.CurrentCulture,
                                  "Property '{0}' should have EntityPropertyMappingAttribute !"),
                              entityPropertyName);
                }

                entityPropertyName = skipMapping ? string.Empty : attribute.MappedEntityPropertyName;
                break;

            case MappingType.TotalImplicit:
                if (attribute != null && skipMapping)
                {
                    entityPropertyName = string.Empty;
                }
                else
                {
                    entityPropertyName = property.Name;
                }

                break;

            case MappingType.Hybrid:
                if (attribute == null)
                {
                    entityPropertyName = property.Name;
                }
                else if (skipMapping)
                {
                    entityPropertyName = string.Empty;
                }
                else
                {
                    entityPropertyName = attribute.MappedEntityPropertyName;
                }
                break;

            default:
                break;
            }

            return(entityPropertyName);
        }