protected override void OnLoadComplete(EventArgs e) { if (!IsPostBack && FieldMappings.Count() > 0) { LoadFromMappings(true); } base.OnLoadComplete(e); }
protected virtual void Update() { this.Validate(); if (this.IsValid) { bool shouldNavigate = (Entity.ID == 0); if (FieldMappings.Count() > 0) { LoadFromMappings(false); } EasyContext.Entry <ENTITY>(Entity).State = (Entity.ID == 0) ? EntityState.Added : EntityState.Modified; EasyContext.SaveChanges(); if (shouldNavigate) { Response.Redirect(Request.Url.ToString().Split('?').First() + "?id=" + Entity.ID); } } }
protected void LoadFromMappings(bool LoadFields) { /* * The code in this method walks the expression tree and * maps either the value in the entity into the defined * control property or pulls the value from the controls * property into the entity. * * This allows us to define the mappings between the entity * and the controls so that we can create a generic means of * loading and extracting data from the form. * */ if (FieldMappings.Count() == 0) { throw new Exception("No FieldMappings Defined!"); } foreach (var mapf in FieldMappings) { var map = mapf.Body as BinaryExpression; if (map != null && map.NodeType == ExpressionType.Equal) { Expression left = null; Expression right = null; #region We can deal with ToString() and also can handle ToString() extension methods if (map.Left.NodeType == ExpressionType.Call && ((MethodCallExpression)map.Left).Method.Name == "ToString") { left = (!((MethodCallExpression)map.Left).Method.IsStatic) ? ((MethodCallExpression)map.Left).Object : ((MethodCallExpression)map.Left).Arguments[0]; } else { left = map.Left; } if (map.Right.NodeType == ExpressionType.Call && ((MethodCallExpression)map.Right).Method.Name == "ToString") { right = (!((MethodCallExpression)map.Right).Method.IsStatic) ? ((MethodCallExpression)map.Right).Object : ((MethodCallExpression)map.Right).Arguments[0]; } else { right = map.Right; } #endregion if (left != null && left.NodeType == ExpressionType.MemberAccess && ((MemberExpression)left).Member.DeclaringType.IsSubclassOf(typeof(EntityBase))) { if (right.NodeType == ExpressionType.MemberAccess) { if (LoadFields) { RecursiveMemberAccess(((MemberExpression)right), GetValue(map.Left, right.Type, Converters[mapf][1])); //Field <--- Entity } else { RecursiveMemberAccess(((MemberExpression)left), GetValue(map.Right, left.Type, Converters[mapf][0])); //Field ---> Entity } } else { throw new Exception("Feild Mappings must map to a property on a derivative of the System.Web.UI.Control class!"); } } else if (right != null && right.NodeType == ExpressionType.MemberAccess && ((MemberExpression)right).Member.DeclaringType.IsSubclassOf(typeof(EntityBase))) { if (left.NodeType == ExpressionType.MemberAccess) { if (LoadFields) { RecursiveMemberAccess(((MemberExpression)left), GetValue(map.Right, left.Type, Converters[mapf][1])); //Field <--- Entity } else { RecursiveMemberAccess(((MemberExpression)right), GetValue(map.Left, right.Type, Converters[mapf][0])); //Field ---> Entity } } else { throw new Exception("Feild Mappings must map to a property on a derivative of the System.Web.UI.Control class!"); } } else { throw new Exception("Feild Mappings must map to a property on a derivative of the EntityBase class!"); } } else { throw new Exception("Only == is allowed in feild mappings!"); } } }