Exemplo n.º 1
0
        public static DetailsViewModel DtoMapper <T>(T entity)
        {
            Type             dtoType          = typeof(T);
            DetailsViewModel detailsViewModel = new DetailsViewModel();
            var properties = dtoType.GetProperties();

            foreach (var property in properties)
            {
                if (property.HasAttribute <DetailsOrderAttribute>())
                {
                    var attribute = property.GetAttribute <DetailsOrderAttribute>();
                    int order     = attribute.Order;
                    DetailsPropertyViewModel detailsProperty = new DetailsPropertyViewModel();
                    detailsProperty.Name  = string.IsNullOrEmpty(attribute.Title) ? StringFunctions.SplitWordsByCapitalLetters(property.Name) : attribute.Title;
                    detailsProperty.Value = property.GetValue(entity);
                    detailsProperty.Type  = TableCellType.Text;
                    if (property.GetCustomAttributes(typeof(TableCellAttribute), false).Length > 0)
                    {
                        detailsProperty.Type = ((TableCellAttribute)property.GetCustomAttributes(typeof(TableCellAttribute), false).FirstOrDefault()).Type;
                    }

                    detailsViewModel.AddProperty(detailsProperty, order);
                }
            }

            return(detailsViewModel);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Map entities to <see cref="DetailsViewModel"/> by using the decorated properties of the view model entity implementation by <seealso cref="DetailsFieldAttribute"/>.
        /// </summary>
        /// <typeparam name="T">ViewModel entity implementation.</typeparam>
        /// <param name="entity"></param>
        /// <returns></returns>
        public static DetailsViewModel Map <T>(T entity)
        {
            Type             dtoType          = typeof(T);
            DetailsViewModel detailsViewModel = new DetailsViewModel();
            var properties = dtoType.GetProperties();

            foreach (var property in properties)
            {
                if (property.HasAttribute <DetailsFieldAttribute>())
                {
                    var attribute = property.GetAttribute <DetailsFieldAttribute>();
                    int order     = attribute.Order;
                    DetailsPropertyViewModel detailsProperty = new DetailsPropertyViewModel();
                    detailsProperty.Name  = string.IsNullOrEmpty(attribute.Title) ? StringFunctions.SplitWordsByCapitalLetters(property.Name) : attribute.Title;
                    detailsProperty.Value = property.GetValue(entity);
                    detailsProperty.DetailsFieldElement = (IDetailsFieldElement)Activator.CreateInstance(attribute.UIElementType);
                    detailsProperty.Order = order;
                    detailsViewModel.AddProperty(detailsProperty);
                }
            }

            return(detailsViewModel);
        }