Example #1
0
        /// <summary>
        /// 从远程获取对象实例。
        /// </summary>
        /// <param name="parameter">获取前检索用的参数信息。</param>
        /// <param name="targetType">需要获取的最终类型。</param>
        /// <returns>符合条件的实例对象。</returns>
        public object GetObject(RemoteDataParameter parameter, Type targetType)
        {
            var uri = parameter.BuildUri();

            var method = "GetByOData";

            if (parameter.Executor == "Count" || parameter.Executor == "LongCount")
            {
                method = "CountByOData";
            }
            else if (parameter.Executor == "Any")
            {
                method = "AnyByOData";
            }

            var absoluteUrl = string.Format("{0}#{1}", Client.BaseAddress, method);

            if (parameter.Executor == "First" || parameter.Executor == "FirstOrDefault")
            {
                targetType = typeof(IQueryable <>).MakeGenericType(targetType);
            }

            var result = Client.GetAsync(method).FetchValue(targetType, "GET", absoluteUrl);

            if (parameter.Executor == "First" || parameter.Executor == "FirstOrDefault")
            {
                return((result as IEnumerable).OfType <object>().FirstOrDefault());
            }
            return(result);
        }
Example #2
0
        private string ReplenishURL(IMethodCallMessage methodCall)
        {
            var data       = new List <string>();
            var parameters = methodCall.MethodBase.GetParameters();

            for (int i = 0; i < methodCall.ArgCount; i++)
            {
                var argName   = methodCall.GetInArgName(i);
                var arg       = methodCall.GetInArg(i);
                var parameter = parameters.First(p => p.Name == argName);

                var binding = parameter.GetCustomAttribute <ParameterBindingAttribute>();
                if (binding == null)
                {
                    continue;
                }

                if (binding.BindingType == "Uri")
                {
                    data.Add(string.Format("{0}={1}", argName, Uri.EscapeUriString(ReferenceEquals(arg, null) ? "null" : arg.ToString())));
                }
                else if (binding.BindingType == "ODataParameter" && !ReferenceEquals(arg, null))
                {
                    var dataParameter = new RemoteDataParameter();

                    var processor = RemoteObjectContext.DefaultObjectResolver.GetInstance <IExpressionProcessor>();
                    processor.Writer        = RemoteObjectContext.DefaultObjectResolver.GetInstance <IExpressionWriter>();
                    processor.DataParameter = dataParameter;

                    processor.Build((arg as LambdaExpression).Body as MethodCallExpression);

                    data.Add(dataParameter.BuildUri());
                }
            }

            return(string.Join("&", data));
        }