示例#1
0
        public object ExecuteUpdate(IEngineContext context, IController controller, IControllerContext controllerContext)
        {
            controllerContext.Action = "update";
            using (var transaction = new TransactionScope()) {
                var instance = AR.Find <T>(context.GetParameter(IdParameter));
                if (instance == null)
                {
                    context.Handle404();
                    return(null);
                }

                var pk = AR.Holder.GetModel(Type).Metadata.GetIdentifier(instance, EntityMode.Poco).ToString();

                try {
                    var excludeattr  = controller.GetType().GetAttr <UpdateExcludeAttribute>();
                    var includeattr  = controller.GetType().GetAttr <UpdateIncludeAttribute>();
                    var includeprops = includeattr == null ? null : includeattr.Properties;
                    var excludeprops = excludeattr == null ? null : excludeattr.Properties;

                    var form = BuildCompositeNode(context.Request.Form, Queryfilters.Where(qf => qf.ApplyOnSave), true);

                    OnPreBind(controller, instance);
                    Binder.AutoLoad = AutoLoadBehavior.NullIfInvalidKey;
                    Binder.BindObjectInstance(instance, TemplateObjectName, excludeprops, includeprops, form);
                    OnPostBind(controller, instance);

                    OnPreSave(controller, instance);
                    OnPreUpdate(controller, instance);
                    if (Validator.IsValid(instance))
                    {
                        AR.Save <T>(instance);
                        OnPostUpdate(controller, instance);
                        OnPostSave(controller, instance);

                        transaction.Flush();
                        transaction.VoteCommit();

                        pk = AR.Holder.GetClassMetadata(Type).GetIdentifier(instance, EntityMode.Poco).ToString();
                        controllerContext.PropertyBag[TemplateObjectName] = instance;
                        context.SuccessMessage(instance + " has been updated");
                    }
                    else
                    {
                        transaction.VoteRollBack();
                        context.ErrorMessages(Validator.GetErrorSummary(instance).ErrorMessages);
                    }
                } catch (Exception ex) {
                    transaction.VoteRollBack();
                    context.ErrorMessages(ex);
                } finally {
                    context.Flash[TemplateObjectName]         = instance;
                    controllerContext.PropertyBag["objectid"] = pk;
                    context.CurrentControllerContext.PropertyBag[TemplateObjectName] = instance;
                }
            }

            return(null);
        }
示例#2
0
        public object ExecuteCreate(IEngineContext context, IController controller, IControllerContext controllerContext)
        {
            T      instance = null;
            string pk       = null;
            var    form     = BuildCompositeNode(context.Request.Form, Queryfilters.Where(qf => qf.ApplyOnSave), true);

            using (var transaction = new TransactionScope()) {
                try {
                    Binder.AutoLoad = AutoLoadBehavior.OnlyNested;
                    instance        = (T)Binder.BindObject(typeof(T), TemplateObjectName, CreateExcludedProperties, CreateAllowedProperties, form);
                    OnPostBind(controller, instance);

                    OnPreSave(controller, instance);
                    OnPreCreate(controller, instance);
                    if (Validator.IsValid(instance))
                    {
                        AR.Save <T>(instance);

                        OnPostCreate(controller, instance);
                        OnPostSave(controller, instance);

                        transaction.Flush();
                        transaction.VoteCommit();

                        pk = AR.Holder.GetClassMetadata(Type).GetIdentifier(instance, EntityMode.Poco).ToString();
                        context.SuccessMessage(instance + " has been created");
                    }
                    else
                    {
                        transaction.VoteRollBack();
                        context.ErrorMessages(400, Validator.GetErrorSummary(instance).ErrorMessages);
                        context.Flash[TemplateObjectName + "_form"] = form;
                    }
                } catch (Exception ex) {
                    transaction.VoteRollBack();
                    context.ErrorMessages(ex);
                    context.Flash[TemplateObjectName + "_form"] = form;
                } finally {
                    controllerContext.PropertyBag[TemplateObjectName] = instance;
                    controllerContext.PropertyBag["objectid"]         = pk;
                }
            }

            return(null);
        }
示例#3
0
        public object ExecuteDelete(IEngineContext context, IController controller, IControllerContext controllerContext)
        {
            using (var transaction = new TransactionScope()) {
                var instance = AR.Find <T>(context.GetParameter("id"));
                try {
                    OnPreDelete(controller, instance);
                    AR.Delete <T>(instance);
                    OnPostDelete(controller, instance);

                    transaction.Flush();
                    transaction.VoteCommit();

                    context.SuccessMessage(instance + " has been deleted");
                } catch (Exception ex) {
                    transaction.VoteRollBack();
                    context.ErrorMessages(ex);
                } finally {
                    controllerContext.PropertyBag[TemplateObjectName] = instance;
                    context.Flash[TemplateObjectName] = instance;
                }
            }

            return(null);
        }