Exemple #1
0
        /// <summary>
        /// 参数输入
        /// </summary>
        /// <param name="service"></param>
        /// <param name="jsonInput"></param>
        /// <param name="properties"></param>
        private static void SetInputProperties(IService service, string jsonInput, System.Reflection.PropertyInfo[] properties)
        {
            var jInput = JObject.Parse(jsonInput);

            foreach (var property in properties)
            {
                if (ServiceHelper.IsInput(property))
                {
                    JToken jToken = null;
                    if (jInput.TryGetValue(property.Name, out jToken))
                    {
                        //如果是一般属性,则直接赋值。
                        var jValue = jToken as JValue;
                        if (jValue != null)
                        {
                            var value = TypeHelper.CoerceValue(property.PropertyType, jValue.Value);
                            property.SetValue(service, value, null);
                        }
                        else
                        {
                            //如果不是一般的属性,则表示这是一个引用属性,
                            //目前,引用属性只支持实体及实体列表。
                            var jEntityList   = jToken as JObject;
                            var modelProperty = jEntityList.Property("_model");
                            if (modelProperty == null)
                            {
                                throw new NotSupportedException("目前,服务输入中的引用属性只支持实体及实体列表。");
                            }
                            var list = EntityJsonConverter.JsonToEntityList(jEntityList);

                            //如果有这个属性,表明这只是一个用实体列表包装起来的单个实体。
                            var isEntity = jEntityList.Property(Consts.isEntityProperty) != null;
                            if (isEntity)
                            {
                                if (list.Count > 0)
                                {
                                    var entity = list[0];
                                    property.SetValue(service, entity, null);
                                }
                            }
                            else
                            {
                                property.SetValue(service, list, null);
                            }
                        }
                    }
                }
            }
        }
Exemple #2
0
        private JsonModel SaveEntityList(HttpRequest request, EntityMeta meta)
        {
            var clientResult = new ClientResult();

            var entityListJson = request.Form["entityList"];

            if (!string.IsNullOrWhiteSpace(entityListJson))
            {
                var repo = RF.Find(meta.EntityType);

                JObject jEntityList = JObject.Parse(entityListJson);

                var list = EntityJsonConverter.JsonToEntityList(jEntityList, repo);

                if (RafyEnvironment.IsDebuggingEnabled)
                {
                    SaveList(repo, list);

                    clientResult.Success = true;
                }
                else
                {
                    try
                    {
                        SaveList(repo, list);

                        clientResult.Success = true;
                    }
                    catch (Exception ex)
                    {
                        clientResult.Message = ex.Message
                                               .Replace("\"", string.Empty)
                                               .Replace("'", string.Empty)
                                               .Replace(Environment.NewLine, string.Empty);
                    }
                }
            }

            return(clientResult);
        }