示例#1
0
        public static T MapToEntity <T>(IUModel model, IUEConfig <T> uConfig) where T : class, new()
        {
            var entity = new T();

            SetPrimaryKeyPropertyValue <T>(entity, model.Id);

            var nameProperty = uConfig.NamePropertyInfo;

            nameProperty.SetValue(entity, model.Name, null);

            var properties = model.Tabs.SelectMany(x => x.Properties);

            foreach (var p in properties)
            {
                var modelProperty = typeof(T).GetProperty(p.Alias);

                var propertyType = GetPropertyTypeFromView(p.View);

                if (propertyType == UEPropertyType.DropDown)
                {
                    string key = p.DropdownOptions?.SingleOrDefault(x => x.Value == p.Value.ToString()).Key;

                    if (modelProperty.PropertyType == typeof(int))
                    {
                        int.TryParse(key, out var intSaveValue);
                        modelProperty.SetValue(entity, intSaveValue, null);
                        continue;
                    }

                    if (modelProperty.PropertyType == typeof(long))
                    {
                        long.TryParse(key, out var longSaveValue);
                        modelProperty.SetValue(entity, longSaveValue, null);
                        continue;
                    }

                    if (modelProperty.PropertyType == typeof(string))
                    {
                        modelProperty.SetValue(entity, key, null);
                        continue;
                    }
                }

                if (propertyType == UEPropertyType.CheckBox)
                {
                    string primitiveBooleanValue = p.Value.ToString();
                    bool   booleanValue          = primitiveBooleanValue == "1" ? true : false;
                    modelProperty.SetValue(entity, booleanValue, null);

                    continue;
                }

                modelProperty.SetValue(entity, p.Value, null);
            }

            return(entity);
        }
示例#2
0
        public static IUModel MapToUModel <T>(IUEConfig <T> uConfig, T originalEntity)
        {
            var tableAttribute = typeof(T).GetCustomAttributes(typeof(TableNameAttribute), true).SingleOrDefault() as TableNameAttribute;

            var primaryKeyAttribute     = typeof(T).GetCustomAttributes(typeof(PrimaryKeyAttribute), true).SingleOrDefault() as PrimaryKeyAttribute; //TODO: NULL CHECKS
            var primaryKeyProperty      = originalEntity.GetType().GetProperty(primaryKeyAttribute.Value);
            var primaryKeyPropertyValue = primaryKeyProperty.GetValue(originalEntity, null);

            var m = new UModel();

            m.Id    = primaryKeyPropertyValue;
            m.Name  = uConfig.NameExpression.Compile().Invoke(originalEntity);
            m.Alias = tableAttribute.Value;

            //TODO: Parent, Path?

            m.Tabs = new List <IUTabModel>();

            int tabsCount = 0;

            foreach (var tab in uConfig.Tabs)
            {
                tabsCount++;

                var t = new UTabModel();

                t.Id         = tabsCount;
                t.Alias      = tab.TabName.Replace(" ", "_");
                t.Label      = tab.TabName;
                t.Properties = new List <IUPropertyModel>();

                int propertiesCount = 0;
                foreach (var property in tab.Properties)
                {
                    propertiesCount++;

                    var p = new UPropertyModel();

                    p.Id    = propertiesCount;
                    p.Alias = property.PropertyInfo.Name;

                    p.Config = ResolveConfig(property);

                    p.Editor      = ResolveEditor(property);
                    p.View        = ResolveView(property);
                    p.DataTypeKey = null; //TODO: Investigate?

                    p.Label       = property.Label;
                    p.IsHideLabel = false;
                    p.Description = ResolveDescription(property, originalEntity);
                    p.Value       = ResolveValue(property, originalEntity);
                    p.Validation  = new Dictionary <string, object> {
                        { "mandatory", property.IsRequired }, { "pattern", property.ValidationRegexPattern }
                    };
                    p.IsReadOnly  = property.ReadOnly;
                    p.IsSensitive = false;
                    p.Culture     = null;

                    t.Properties.Add(p);
                }

                m.Tabs.Add(t);
            }

            return(m);
        }