Exemplo n.º 1
0
        internal static object Execute(Render.FrontContext context, DataMethodCompiled CompiledMethod)
        {
            List <object> paras = ParameterBinder.Bind(CompiledMethod.Parameters, CompiledMethod.ParameterBindings, context.RenderContext.DataContext);

            CheckAndAssignDefaultValue(paras, CompiledMethod, context, default(Guid));
            return(Execute(CompiledMethod, paras.ToArray(), context));
        }
Exemplo n.º 2
0
 private static bool IsQueryByCategory(DataMethodCompiled CompiledMethod)
 {
     if (!string.IsNullOrEmpty(CompiledMethod.OriginalMethodName))
     {
         if (CompiledMethod.OriginalMethodName.ToLower().Contains("bycategory"))
         {
             return(true);
         }
     }
     return(false);
 }
Exemplo n.º 3
0
        private static object GetDefaultValue(DataMethodCompiled CompiledMethod, Render.FrontContext context, Guid CurrentMethodId, string paraname, Guid folderid)
        {
            object value = null;

            var defaultcontent = GetDefaultContentNew(folderid, context, CurrentMethodId);

            if (defaultcontent != null)
            {
                var key = GetBindingKey(paraname, CompiledMethod.ParameterBindings);
                value = Kooboo.Lib.Reflection.Dynamic.GetObjectMember(defaultcontent, key);
            }

            return(value);
        }
Exemplo n.º 4
0
        internal static void CheckAndAssignDefaultValue(List <object> values, DataMethodCompiled CompiledMethod, Render.FrontContext context, Guid CurrentMethodId)
        {
            if (!values.Where(o => o == null).Any())
            {
                return;
            }

            int count   = CompiledMethod.Parameters.Count();
            var keylist = CompiledMethod.Parameters.Keys.ToList();

            bool IsContentList = CompiledMethod.DeclareType == typeof(Kooboo.Sites.DataSources.ContentList);

            bool IsContentQueried = false;

            bool IsTextContentMethod = CompiledMethod.ReturnType == typeof(TextContentViewModel) || Kooboo.Lib.Reflection.TypeHelper.GetGenericType(CompiledMethod.ReturnType) == typeof(TextContentViewModel);

            TextContentViewModel samplecontent = null;


            bool IsByCategory = IsQueryByCategory(CompiledMethod);


            for (int i = 0; i < count; i++)
            {
                if (values[i] == null)
                {
                    var paraname = keylist[i];

                    if (IsContentList)
                    {
                        //int PageSize, int PageNumber, string SortField, Boolean IsAscending
                        if (paraname == "PageSize" || paraname == "PageNumber" || paraname == "SortField" || paraname == "IsAscending")
                        {
                            var x        = keylist[i];
                            var paratype = CompiledMethod.Parameters[x];
                            values[i] = GetDefaultValueForDataType(paratype);
                        }
                    }

                    if (IsByCategory)
                    {
                        if (paraname.ToLower() == "id")
                        {
                            values[i] = default(Guid);
                            continue;
                        }
                        else if (paraname.ToLower() == "userkey")
                        {
                            values[i] = string.Empty;
                            continue;
                        }
                    }

                    if (!IsContentQueried)
                    {
                        if (IsTextContentMethod)
                        {
                            var folderid = TryGetFolderGuid(CompiledMethod.ParameterBindings);
                            samplecontent = context.SiteDb.TextContent.GetDefaultContentFromFolder(folderid, context.RenderContext.Culture);
                        }
                        IsContentQueried = true;
                    }

                    if (samplecontent != null)
                    {
                        var key   = GetBindingKey(paraname, CompiledMethod.ParameterBindings);
                        var value = Kooboo.Lib.Reflection.Dynamic.GetObjectMember(samplecontent, key);

                        if (value != null)
                        {
                            values[i] = value;
                        }
                    }

                    if (values[i] == null)
                    {
                        var x        = keylist[i];
                        var paratype = CompiledMethod.Parameters[x];
                        values[i] = GetDefaultValueForDataType(paratype);
                    }
                }
            }
        }
Exemplo n.º 5
0
        internal static object Execute(DataMethodCompiled method, object[] Paras, Render.FrontContext Context)
        {
            // assign default values.

            object result;

            if (method.IsStatic)
            {
                if (method.IsVoid)
                {
                    method.StaticVoid(Paras);
                    return(null);
                }
                else
                {
                    result = method.StaticFunc(Paras);
                }
            }
            else
            {
                var instance = Activator.CreateInstance(method.DeclareType);
                if (instance is SiteDataSource)
                {
                    var datasource = instance as SiteDataSource;
                    datasource.Context = Context;
                    if (method.IsVoid)
                    {
                        method.Void(datasource, Paras);
                        return(null);
                    }
                    else
                    {
                        result = method.Func(datasource, Paras);
                    }
                }
                else
                {
                    if (method.IsVoid)
                    {
                        method.Void(instance, Paras);
                        return(null);
                    }
                    else
                    {
                        result = method.Func(instance, Paras);
                    }
                }
            }

            if (result == null)
            {
                return(null);
            }
            else
            {
                var resultAsTask = result as Task;
                if (resultAsTask != null)
                {
                    if (method.ReturnType == typeof(Task))
                    {
                        ThrowIfWrappedTaskInstance(resultAsTask.GetType(), "Not getted", method.DeclareType);
                        resultAsTask.Wait();
                        return(null);
                    }

                    var taskValueType = GetTaskInnerTypeOrNull(method.ReturnType);
                    if (taskValueType != null)
                    {
                        // for: public Task<T> Action()
                        // constructs: return (Task<object>)Convert<T>((Task<T>)result)
                        var genericMethodInfo = _convertOfTMethod.MakeGenericMethod(taskValueType);
                        var convertedResult   = genericMethodInfo.Invoke(null, new object[] { result });
                        return(convertedResult);
                    }

                    // This will be the case for:
                    // 1. Types which have derived from Task and Task<T>,
                    // 2. Action methods which use dynamic keyword but return a Task or Task<T>.
                    throw new InvalidOperationException(
                              String.Format("The method '{0}' on type '{1}' returned a Task instance even though it is not an asynchronous method.",
                                            "Not getted",
                                            method.DeclareType
                                            )
                              );
                }
                else
                {
                    return(result);
                }
            }
        }