示例#1
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());
        }
示例#2
0
        /// <summary>
        /// Gets the name of the viewModel 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> [viewModel from DTO].</param>
        /// <returns></returns>
        private static string GetEntityPropertyName(PropertyInfo property, MappingType mappingType, bool entityFromDTO)
        {
            string entityPropertyName = string.Empty;
            var    attribute          =
                (ViewModelPropertyMappingAttribute)
                Attribute.GetCustomAttribute(property, typeof(ViewModelPropertyMappingAttribute));

            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 DTOConversionException(
                              string.Format(
                                  Thread.CurrentThread.CurrentCulture,
                                  "Property '{0}' should have ViewModelPropertyMappingAttribute !"),
                              entityPropertyName);
                }

                entityPropertyName = skipMapping ? string.Empty : attribute.MappedViewModelPropertyName;
                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.MappedViewModelPropertyName;
                }
                break;

            default:
                break;
            }

            return(entityPropertyName);
        }