コード例 #1
0
ファイル: BaseAction.cs プロジェクト: shosca/Dry.Common
        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
ファイル: BaseAction.cs プロジェクト: shosca/Dry.Common
        public object ExecuteView(IEngineContext context, IController controller, IControllerContext controllerContext)
        {
            OnPreView(controller);

            try {
                var instance = AR.Find <T>(context.GetParameter(IdParameter));
                if (instance == null)
                {
                    context.Handle404();
                    return(null);
                }

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

                var loader = DetachedCriteria.For <T>()
                             .Add(Restrictions.Eq(Projections.Id(), id));

                foreach (var b in AR.Holder.GetModel(typeof(T)).BelongsTos)
                {
                    loader.CreateCriteria(b.Key, JoinType.LeftOuterJoin);
                }

                loader.SetResultTransformer(Transformers.DistinctRootEntity);
                if (!DontDoManyFetch)
                {
                    foreach (var hasmany in AR.Holder.GetModel(typeof(T)).HasManys)
                    {
                        DetachedCriteria.For <T>()
                        .Add(Restrictions.Eq(Projections.Id(), id))
                        .CreateCriteria(hasmany.Value.Type.Role.Replace(typeof(T).FullName + ".", ""), JoinType.LeftOuterJoin)
                        .Future <T>();
                    }
                    foreach (var hasmany in AR.Holder.GetModel(typeof(T)).HasAndBelongsToManys)
                    {
                        DetachedCriteria.For <T>()
                        .Add(Restrictions.Eq(Projections.Id(), id))
                        .CreateCriteria(hasmany.Value.Type.Role.Replace(typeof(T).FullName + ".", ""), JoinType.LeftOuterJoin)
                        .Future <T>();
                    }
                }

                controllerContext.PropertyBag[TemplateObjectName] = instance;

                OnPostView(controller, instance);
            }
            catch (Exception e) {
                context.ErrorMessages(e);
            }
            return(null);
        }
コード例 #3
0
ファイル: ListAction.cs プロジェクト: shosca/Dry.Common
        public override object Execute(IEngineContext context, IController controller, IControllerContext controllerContext)
        {
            base.Execute(context, controller, controllerContext);
            var method = context.GetParameter("_method") ?? context.Request.HttpMethod;

            switch (method.ToUpper())
            {
            case "GET":
                return(ExecuteList(context, controller, controllerContext));

            case "POST":
                controllerContext.Action = "create";
                return(ExecuteCreate(context, controller, controllerContext));

            default:
                throw new MonoRailException("Unsupported method.");
            }
        }
コード例 #4
0
ファイル: BaseAction.cs プロジェクト: shosca/Dry.Common
        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);
        }
コード例 #5
0
ファイル: ViewFilter.cs プロジェクト: shosca/Dry.Common
        public bool Perform(ExecuteWhen exec, IEngineContext context, IController controller, IControllerContext controllerContext)
        {
            if (controllerContext.SelectedViewName == null)
            {
                return(true);
            }

            if (context.IsAjax())
            {
                controllerContext.LayoutNames = null;
            }

            var p        = Path.DirectorySeparatorChar.ToString(CultureInfo.InvariantCulture);
            var template = new TemplateLocation();

            if (context.Request.AcceptHeader != null && context.Request.AcceptHeader.Contains("application/json"))
            {
                template.Area                 = controllerContext.AreaName;
                template.Controller           = controllerContext.Name;
                template.Action               = "json";
                controllerContext.LayoutNames = null;
            }
            else
            {
                var view = context.GetParameter("view");

                template.Area       = controllerContext.AreaName;
                template.Controller = controllerContext.Name;
                template.Action     = string.IsNullOrEmpty(view) ? controllerContext.Action : view;

                if (view == "json")
                {
                    controllerContext.LayoutNames = null;
                }
                else
                {
                    var layout = context.GetParameter("layout");
                    if (context.Services.ViewEngineManager.HasTemplate(p + "layouts" + p + layout))
                    {
                        controllerContext.LayoutNames = new[] { layout };
                    }
                }
            }

            var t = template.ToString();

            if (context.Services.ViewEngineManager.HasTemplate(t))
            {
                controllerContext.SelectedViewName = t;
            }
            else
            {
                template.Area       = null;
                template.Controller = "common";
                t = template.ToString();
                if (context.Services.ViewEngineManager.HasTemplate(t))
                {
                    controllerContext.SelectedViewName = t;
                }
            }
            return(true);
        }