Ejemplo n.º 1
0
        public override object Clone()
        {
            ComplexResourceInstance result = (ComplexResourceInstance)base.Clone();

            result.Properties = (ResourceInstanceProperties)this.Properties.Clone();
            return(result);
        }
Ejemplo n.º 2
0
        private static ResourceInstanceComplexProperty ConvertComplexPayloadObjectToComplexProperty(ResourceProperty rp, PayloadComplexProperty payloadComplexProperty)
        {
            List <ResourceInstanceProperty> properties = new List <ResourceInstanceProperty>();

            foreach (ResourceProperty childProperty in (rp.Type as ComplexType).Properties.OfType <ResourceProperty>())
            {
                if (childProperty.IsComplexType)
                {
                    PayloadComplexProperty fromPayload = payloadComplexProperty.PayloadProperties[childProperty.Name] as PayloadComplexProperty;
                    properties.Add(ConvertComplexPayloadObjectToComplexProperty(childProperty, fromPayload));
                }
                else
                {
                    PayloadSimpleProperty fromPayload = payloadComplexProperty.PayloadProperties[childProperty.Name] as PayloadSimpleProperty;
                    if (fromPayload != null)
                    {
                        ResourceInstanceProperty newProperty = null;
                        object val = CommonPayload.DeserializeStringToObject(fromPayload.Value, childProperty.Type.ClrType, false, fromPayload.ParentObject.Format);
                        newProperty = new ResourceInstanceSimpleProperty(childProperty.Name, new NodeValue(val, childProperty.Type));
                        properties.Add(newProperty);
                    }
                }
            }
            ComplexResourceInstance complexResourceInstance = new ComplexResourceInstance(rp.Type.Name, properties.ToArray());

            return(new ResourceInstanceComplexProperty(rp.Type.Name, rp.Name, complexResourceInstance));
        }
Ejemplo n.º 3
0
        private static List <ResourceInstanceProperty> CloneObjectToResourceInstanceProperties(IEnumerable <ResourceProperty> properties, object o)
        {
            List <ResourceInstanceProperty> instanceProperties = new List <ResourceInstanceProperty>();

            foreach (ResourceProperty resProperty in properties)
            {
                if (resProperty.IsNavigation)
                {
                    continue;
                }
                object propertyObject = o.GetType().InvokeMember(resProperty.Name, System.Reflection.BindingFlags.GetProperty, null, o, new object[] { });
                ResourceInstanceProperty resourceInstanceProperty = null;
                if (resProperty.Type is ComplexType)
                {
                    ComplexResourceInstance complexInstance = CloneObjectToComplexResourceInstance(resProperty.Type as ComplexType, propertyObject);
                    resourceInstanceProperty = new ResourceInstanceComplexProperty(resProperty.Type.Name, resProperty.Name, complexInstance);
                }
                else
                {
                    resourceInstanceProperty = new ResourceInstanceSimpleProperty(resProperty.Name, new NodeValue(propertyObject, resProperty.Type));
                }
                instanceProperties.Add(resourceInstanceProperty);
            }
            return(instanceProperties);
        }
Ejemplo n.º 4
0
        public ResourceInstanceProperty CreateRandomResourceInstanceProperty()
        {
            ResourceInstanceProperty instanceProperty;

            if (this.IsComplexType)
            {
                ComplexType ct = (ComplexType)this.Type;
                if (ct.Facets.AbstractType)
                {
                    ct = ct.DerivedTypes.Where(t => !t.Facets.AbstractType).Choose();
                }

                ComplexResourceInstance cri = ResourceInstanceUtil.CreateComplexResourceInstance(ct);
                return(instanceProperty = new ResourceInstanceComplexProperty(ct.Name, this.Name, cri));
            }
            else
            {
                return(this.CreateRandomResourceSimpleInstanceProperty());
            }
        }
Ejemplo n.º 5
0
        public static ComplexResourceInstance CreateComplexResourceInstance(ComplexType type)
        {
            List <ResourceInstanceProperty> instanceProperties = new List <ResourceInstanceProperty>();

            foreach (ResourceProperty childProperty in type.Properties)
            {
                ResourceInstanceProperty resourceInstanceProperty = null;
                if (childProperty.IsComplexType)
                {
                    resourceInstanceProperty = new ResourceInstanceComplexProperty(childProperty.Type.Name, childProperty.Name, CreateComplexResourceInstance((ComplexType)childProperty.Type));
                }
                else
                {
                    NodeValue nodeValue = Resource.CreateValue(childProperty);
                    resourceInstanceProperty = new ResourceInstanceSimpleProperty(childProperty.Name, nodeValue);
                }
                instanceProperties.Add(resourceInstanceProperty);
            }
            ComplexResourceInstance complexInstance = new ComplexResourceInstance(type.Name, instanceProperties.ToArray());

            return(complexInstance);
        }
Ejemplo n.º 6
0
 public ResourceInstanceComplexProperty(string typeName, string propertyName, ComplexResourceInstance complexResourceInstance)
     : base(propertyName)
 {
     _complexResourceInstance = complexResourceInstance;
     _typeName = typeName;
 }
Ejemplo n.º 7
0
        /// <summary>
        /// Writes the CreateResource and SetValue calls to create the given instance, recursing where necessary
        /// </summary>
        /// <param name="variableName">Name of variable to code-gen</param>
        /// <param name="instance">Entity/complex type to generate code for</param>
        /// <returns>generated lines of code</returns>
        private IEnumerable <string> WriteObject(string variableName, ComplexResourceInstance instance)
        {
            // determine the set and type for the CreateResource call
            //
            string typeName = workspace.ContextNamespace + "." + instance.TypeName;

            string setName;

            if (instance is KeyedResourceInstance)
            {
                setName = '"' + (instance as KeyedResourceInstance).ResourceSetName + '"';
            }
            else
            {
                setName = "null";
            }

            // return the CreateResource line
            //
            yield return("object " + variableName + " = updatable.CreateResource(" + setName + ", \"" + typeName + "\");");

            // if its an entity, and not a complex type, then generate the key properties as well
            //
            if (instance is KeyedResourceInstance)
            {
                foreach (ResourceInstanceSimpleProperty property in (instance as KeyedResourceInstance).KeyProperties.OfType <ResourceInstanceSimpleProperty>())
                {
                    yield return("updatable.SetValue(" + variableName + ", \"" + property.Name + "\", " + ConvertToCode((property as ResourceInstanceSimpleProperty).PropertyValue) + ");");
                }
            }

            // generate the SetValue calls for each property
            foreach (ResourceInstanceProperty property in instance.Properties)
            {
                // based on the type of property, generate the appropriate code
                //
                if (property is ResourceInstanceNavProperty)
                {
                    // not supported right now, this is non-trivial
                    throw new NotImplementedException();
                }
                else if (property is ResourceInstanceComplexProperty)
                {
                    // generate an instance of this property's value for passing to SetValue
                    //
                    string subVariableName = variableName + "_" + property.Name;

                    // recurse
                    foreach (string line in WriteObject(subVariableName, (property as ResourceInstanceComplexProperty).ComplexResourceInstance))
                    {
                        yield return(line);
                    }

                    // return the SetValue call
                    //
                    yield return("updatable.SetValue(" + variableName + ", \"" + property.Name + "\", " + subVariableName + ");");
                }
                else if (property is ResourceInstanceSimpleProperty)
                {
                    // return the SetValue call
                    //
                    yield return("updatable.SetValue(" + variableName + ", \"" + property.Name + "\", " + ConvertToCode((property as ResourceInstanceSimpleProperty).PropertyValue) + ");");
                }
                else
                {
                    // should never hit this
                    throw new NotSupportedException();
                }
            }
        }