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); }
public UModelMapperTests() { this._testClass = new TestClassGuid { Id = new Guid("87175372-8573-41ca-96c6-437aa6d9c63f"), Name = "Valentine", Summary = "This is first test", Boolean = true, Dropdown = 1 }; this._testClassIdsLong = new TestClassLong { Id = 1, Name = "Valentine", Summary = "This is first test", Boolean = true, Dropdown = 1 }; this._uModel = new UModel { Id = "87175372-8573-41ca-96c6-437aa6d9c63f", Name = "Valentine", Alias = "tblTestCalss", Tabs = new List <IUTabModel> { new UTabModel { Id = 1, Alias = "Profile", Label = "Profile", Properties = new List <IUPropertyModel> { new UPropertyModel { Id = 1, Alias = "Name", Value = "Valentine", View = "textbox" }, new UPropertyModel { Id = 2, Alias = "Summary", Value = "This is first test", View = "textarea" }, new UPropertyModel { Id = 3, Alias = "Boolean", Value = "1", View = "boolean" }, new UPropertyModel { Id = 4, Alias = "Dropdown", Value = "A", View = "dropdownFlexible", DropdownOptions = new Dictionary <string, string> { { "1", "A" }, { "2", "B" }, { "3", "C" } } } } } } }; this._uModelIdAsLong = new UModel { Id = 1, Name = "Valentine", Alias = "tblTestCalss", Tabs = new List <IUTabModel> { new UTabModel { Id = 1, Alias = "Profile", Label = "Profile", Properties = new List <IUPropertyModel> { new UPropertyModel { Id = 1, Alias = "Name", Value = "Valentine", View = "textbox" }, new UPropertyModel { Id = 2, Alias = "Summary", Value = "This is first test", View = "textarea" }, new UPropertyModel { Id = 3, Alias = "Boolean", Value = "1", View = "boolean" }, new UPropertyModel { Id = 4, Alias = "Dropdown", Value = "A", View = "dropdownFlexible", DropdownOptions = new Dictionary <string, string> { { "1", "A" }, { "2", "B" }, { "3", "C" } } } } } } }; }