Represents the set of information to serialize to the client for a load request for a specific ModelType.
Serializes as: { static : { prop1 : "", prop2 : "", ... propN : "" }, instance1 : ["v1", "v2", "...", "vN"], instance2 : ["v1", "v2", "...", "vN"], ..., instanceN : ["v1", "v2", "...", "vN"] }
Inheritance: IJsonSerializable
Beispiel #1
0
        public ModelTypeInfo GetModelTypeInfo(ModelType type)
        {
            // Create the set of type instance information if not initialized
            if (Instances == null)
                Instances = new Dictionary<string, ModelTypeInfo>();

            // Get or initialize the model type instance information store
            ModelTypeInfo typeInfo;
            if (!Instances.TryGetValue(type.Name, out typeInfo))
                Instances[type.Name] = typeInfo = new ModelTypeInfo();

            // Return the requested value
            return typeInfo;
        }
Beispiel #2
0
        public ModelTypeInfo GetModelTypeInfo(ModelType type)
        {
            // Create the set of type instance information if not initialized
            if (Instances == null)
            {
                Instances = new Dictionary <string, ModelTypeInfo>();
            }

            // Get or initialize the model type instance information store
            ModelTypeInfo typeInfo;

            if (!Instances.TryGetValue(type.Name, out typeInfo))
            {
                Instances[type.Name] = typeInfo = new ModelTypeInfo();
            }

            // Return the requested value
            return(typeInfo);
        }
Beispiel #3
0
            /// <summary>
            /// Processes static property paths in order to determine the information to serialize.
            /// </summary>
            /// <param name="path"></param>
            internal static void PrepareStaticPath(string path, ServiceResponse response)
            {
                string type     = null;
                string property = null;

                try
                {
                    if (path.IndexOf('.') < 0)
                    {
                        throw new ArgumentException("'" + path + "' is not a valid static property path.");
                    }

                    // Split the static property reference
                    int propertyIndex = path.LastIndexOf('.');
                    type     = path.Substring(0, propertyIndex);
                    property = path.Substring(propertyIndex + 1);

                    // Get the model type
                    ModelType modelType = ModelContext.Current.GetModelType(type);
                    if (modelType == null)
                    {
                        throw new ArgumentException("'" + type + "' is not a valid model type for the static property path of '" + path + "'.");
                    }

                    // Get the model property
                    ModelProperty modelProperty = modelType.Properties[property];
                    if (modelProperty == null || !modelProperty.IsStatic)
                    {
                        throw new ArgumentException("'" + property + "' is not a valid property for the static property path of '" + path + "'.");
                    }

                    // Add the property to the set of static properties to serialize
                    response.GetModelTypeInfo(modelType).StaticProperties.Add(modelProperty);

                    // Register instances for static reference properties to be serialized
                    ModelReferenceProperty reference = modelProperty as ModelReferenceProperty;
                    if (reference != null)
                    {
                        // Get the cached set of instances to be serialized for the property type
                        ModelTypeInfo propertyTypeInfo = response.GetModelTypeInfo(reference.PropertyType);

                        // Static lists
                        if (reference.IsList)
                        {
                            foreach (ModelInstance instance in modelType.GetList(reference))
                            {
                                ModelTypeInfo typeInfo = instance.Type == reference.PropertyType ? propertyTypeInfo : response.GetModelTypeInfo(instance.Type);
                                if (!typeInfo.Instances.ContainsKey(instance.Id))
                                {
                                    typeInfo.Instances.Add(instance.Id, new ModelInstanceInfo(instance));
                                }
                            }
                        }

                        // Static references
                        else
                        {
                            ModelInstance instance = modelType.GetReference(reference);

                            if (instance != null)
                            {
                                ModelTypeInfo typeInfo = instance.Type == reference.PropertyType ? propertyTypeInfo : response.GetModelTypeInfo(instance.Type);
                                if (!typeInfo.Instances.ContainsKey(instance.Id))
                                {
                                    typeInfo.Instances.Add(instance.Id, new ModelInstanceInfo(instance));
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw new ApplicationException(string.Format("Error preparing static path '{0}'{1}{2}: [{3}]",
                                                                 path,
                                                                 type == null ? string.Empty : (" for type '" + type + "'"),
                                                                 property == null ? string.Empty : (" and property '" + property + "'"),
                                                                 ex.Message),
                                                   ex);
                }
            }
Beispiel #4
0
        /// <summary>
        /// Recursively builds up a list of instances to serialize.
        /// </summary>
        /// <param name="instance"></param>
        /// <param name="instances"></param>
        /// <param name="paths"></param>
        /// <param name="path"></param>
        static void ProcessInstance(ModelInstance instance, ModelStepList steps, bool includeInResponse, bool inScope, bool forLoad, ServiceResponse response)
        {
            // Avoid processing cached instances not included in the response
            if (instance.IsCached && !includeInResponse)
            {
                return;
            }

            ModelInstanceInfo instanceInfo = null;

            // Track the instance if the query represents a load request
            if (includeInResponse)
            {
                // Fetch or initialize the dictionary of instances for the type of the current instance
                ModelTypeInfo typeInfo = response.GetModelTypeInfo(instance.Type);

                // Add the current instance to the dictionary if it is not already there
                if (!typeInfo.Instances.TryGetValue(instance.Id, out instanceInfo))
                {
                    typeInfo.Instances[instance.Id] = instanceInfo = new ModelInstanceInfo(instance);
                }

                // Track in scope instances to limit conditions
                if (inScope && !instance.IsCached)
                {
                    response.inScopeInstances.Add(instance);
                }
            }

            // Exit immediately if there are no child steps to process
            if (steps == null)
            {
                return;
            }

            // Process query steps for the current instance
            foreach (var step in steps)
            {
                // Recursively process child instances
                foreach (var childInstance in step.GetInstances(instance))
                {
                    ProcessInstance(childInstance, step.NextSteps, includeInResponse, inScope, forLoad, response);
                }

                // Mark value lists to be included during serialization
                if (step.Property.IsList && includeInResponse)
                {
                    instanceInfo.IncludeList(step.Property);
                }
            }

            // Run all property get rules on the instance
            if (inScope)
            {
                if (forLoad)
                {
                    instance.RunPendingPropertyGetRules(p => p is ModelValueProperty || steps.Any(s => s.Property == p));
                }
                else
                {
                    instance.RunPendingPropertyGetRules(p => p is ModelValueProperty);
                }
            }
        }