Esempio n. 1
0
        private static object ExecuteDataMethod(Render.FrontContext context, Guid MethodId)
        {
            var CompiledMethod = CompileMethodCache.GetCompiledMethod(context.SiteDb, MethodId);

            if (CompiledMethod == null)
            {
                return(null);
            }

            var ParameterBindings = CompiledMethod.ParameterBindings;

            List <object> paras = ParameterBinder.Bind(CompiledMethod.Parameters, ParameterBindings, context.RenderContext.DataContext);

            CheckAndAssignDefaultValue(paras, CompiledMethod, context, MethodId);

            return(Execute(CompiledMethod, paras.ToArray(), context));
        }
Esempio n. 2
0
        /// <summary>
        /// For example, Id ==> ReturnType(News).Id;
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        private static object ByMethodReturnType(string key, string value, RenderContext context)
        {
            string bindingkey = "{" + key + "}";

            string expression = DataSources.ParameterBinder.GetBindingKey(value);

            var frontcontext = context.GetItem <FrontContext>();

            object result = null;

            var viewmethods = frontcontext.ViewDataMethods;

            foreach (var item in viewmethods)
            {
                var  method   = CompileMethodCache.GetCompiledMethod(context.WebSite.SiteDb(), item.MethodId);
                bool hasmatch = false;
                foreach (var methodparaitem in method.ParameterBindings)
                {
                    if (methodparaitem.Value.Binding == bindingkey)
                    {
                        hasmatch = true;
                        break;
                    }
                }

                if (!hasmatch)
                {
                    continue;
                }

                var returntype = method.ReturnType;

                string newkey;
                int    dotindex = expression.IndexOf(".");

                if (dotindex > 0)
                {
                    newkey = returntype.Name + expression.Substring(dotindex);
                }
                else
                {
                    newkey = returntype.Name + "." + expression;
                }
                result = context.DataContext.GetValue(newkey);
                if (result != null)
                {
                    return(result);
                }

                dotindex = key.IndexOf(".");

                if (dotindex > 0)
                {
                    newkey = returntype.Name + key.Substring(dotindex);
                }
                else
                {
                    newkey = returntype.Name + "." + key;
                }
                result = context.DataContext.GetValue(newkey);

                if (result != null)
                {
                    return(result);
                }
            }
            return(result);
        }
Esempio n. 3
0
        public static object ExecuteViewDataMethod(Render.FrontContext context, ViewDataMethod ViewMethod, DataContext parentDataContext = null)
        {
            var CompiledMethod = CompileMethodCache.GetCompiledMethod(context.SiteDb, ViewMethod.MethodId);

            if (CompiledMethod == null)
            {
                return(null);
            }

            var ParameterBindings = CompiledMethod.ParameterBindings;

            DataContext dataContext = parentDataContext == null ? context.RenderContext.DataContext : parentDataContext;

            if (CompiledMethod.IsKScript)
            {
                var dictparas = ParameterBinder.BindKScript(CompiledMethod.Parameters, ParameterBindings, dataContext);

                return(Kooboo.Sites.Scripting.Manager.ExecuteDataSource(context.RenderContext, CompiledMethod.CodeId, dictparas));
            }

            List <object> paras = ParameterBinder.Bind(CompiledMethod.Parameters, ParameterBindings, dataContext);

            CheckAndAssignDefaultValue(paras, CompiledMethod, context, ViewMethod.MethodId);


            var result = Execute(CompiledMethod, paras.ToArray(), context);

            if (result == null)
            {
                return(null);
            }
            if (ViewMethod.HasChildren)
            {
                var type = result.GetType();

                if (Kooboo.Lib.Reflection.TypeHelper.IsGenericCollection(type))
                {
                    List <DataMethodResult> results = new List <DataMethodResult>();
                    var itemcollection = ((IEnumerable)result).Cast <object>().ToList();
                    foreach (var item in itemcollection)
                    {
                        var itemresult = ExecuteSubViewDataMethod(context, item, ViewMethod.Children);

                        results.Add(itemresult);
                    }
                    return(results);
                }
                else if (result is PagedResult)
                {
                    var pagedresult = result as PagedResult;

                    List <DataMethodResult> results = new List <DataMethodResult>();
                    var itemcollection = ((IEnumerable)pagedresult.DataList).Cast <object>().ToList();
                    foreach (var item in itemcollection)
                    {
                        var itemresult = ExecuteSubViewDataMethod(context, item, ViewMethod.Children);

                        results.Add(itemresult);
                    }

                    pagedresult.DataList = results.ToList <object>();

                    return(pagedresult);
                }
                else
                {
                    return(ExecuteSubViewDataMethod(context, result, ViewMethod.Children));
                }
            }
            else
            {
                return(result);
            }
        }