SetValue() 공개 메소드

Sets a value of the specified property.
Note that this method will define the property if it doesn't exist yet. If it does exist, it will overwrite its value.
public SetValue ( string propertyName, object value ) : void
propertyName string The name of the property to set.
value object The value to set the property to.
리턴 void
예제 #1
0
        /// <summary>
        /// Sets the value of the given property on the target object
        /// </summary>
        /// <param name="targetResource">target object which defines the property</param>
        /// <param name="propertyName">name of the property whose value needs to be updated</param>
        /// <param name="propertyValue">value of the property</param>
        /// <remarks>This method should pend a change which will set a resource property with name <paramref name="propertyName"/>
        /// to value specified by <paramref name="propertyValue"/> on the resource specified by the resource "handle" <paramref name="targetResource"/>.
        /// All changes made by this method should be creates as pending until SaveChanges is called which will commit them (or if it's not called and ClearChanges
        /// is called instead they should be discarded).</remarks>
        public void SetValue(object targetResource, string propertyName, object propertyValue)
        {
            DSPResource dspTargetResource = ValidateDSPResource(targetResource);

            // Add a pending change to modify the value of the property
            this.pendingChanges.Add(() =>
            {
                dspTargetResource.SetValue(propertyName, propertyValue);
            });
        }
예제 #2
0
        /// <summary>
        /// Resets the value of the given resource to its default value
        /// </summary>
        /// <param name="resource">resource whose value needs to be reset</param>
        /// <returns>same resource with its value reset</returns>
        /// <remarks>This method should reset resource properties to their default values.
        /// The resource is specfied by its resource "handle" by the <paramref name="resource"/>.
        /// The method can choose to modify the existing resource or create a new one and it should return a resource "handle"
        /// to the resource which has its properties with default values. If it chooses to return a new resource it must also
        /// replace that old resource with the new one in its resource set and all the references which may point to it.
        /// The returned resource must have the same identity as the one on the input. That means all its key properties must have the same value.
        /// All changes made by this method should be creates as pending until SaveChanges is called which will commit them (or if it's not called and ClearChanges
        /// is called instead they should be discarded).</remarks>
        public object ResetResource(object resource)
        {
            DSPResource dspResource = ValidateDSPResource(resource);

            this.pendingChanges.Add(() =>
            {
                foreach (var resourceProperty in dspResource.ResourceType.Properties)
                {
                    // We must only reset values of non-key properties, the key properties must be persited (to maintain the identity of the resource instance)
                    if ((resourceProperty.Kind & ResourcePropertyKind.Key) != ResourcePropertyKind.Key)
                    {
                        dspResource.SetValue(resourceProperty.Name, null);
                    }
                }
            });

            return(resource);
        }
예제 #3
0
 private static void AssignNullCollections(DSPResource resource, ResourceType resourceType)
 {
     foreach (var resourceProperty in resourceType.Properties)
     {
         var propertyValue = resource.GetValue(resourceProperty.Name);
         if (resourceProperty.Kind == ResourcePropertyKind.Collection)
         {
             if (propertyValue == null)
             {
                 resource.SetValue(resourceProperty.Name, new object[0]);
             }
         }
         else if (propertyValue is DSPResource)
         {
             AssignNullCollections(propertyValue as DSPResource, resourceProperty.ResourceType);
         }
     }
 }
예제 #4
0
        public static DSPResource CreateDSPResource(BsonDocument document, MongoMetadata mongoMetadata, string resourceName, string ownerPrefix = null)
        {
            var resourceType = mongoMetadata.ResolveResourceType(resourceName, ownerPrefix);
            if (resourceType == null)
                throw new ArgumentException(string.Format("Unable to resolve resource type {0}", resourceName), "resourceName");
            var resource = new DSPResource(resourceType);

            foreach (var element in document.Elements)
            {
                var resourceProperty = mongoMetadata.ResolveResourceProperty(resourceType, element);
                if (resourceProperty == null)
                    continue;

                object propertyValue = ConvertBsonValue(element.Value, resourceType, resourceProperty, resourceProperty.Name, mongoMetadata);
                resource.SetValue(resourceProperty.Name, propertyValue);
            }
            AssignNullCollections(resource, resourceType);

            return resource;
        }
 private DSPResource CreateDSPResource(ResourceType resourceType, BsonDocument bsonDocument)
 {
     var resource = new DSPResource(resourceType);
     foreach (var element in bsonDocument.Elements)
     {
         if (element.Name == "_id")
         {
             resource.SetValue("ID", element.Value.ToString());
             this.resourceMap.Add(element.Value.AsObjectId, resource);
         }
         else if (element.Value.GetType() == typeof(BsonDocument))
         {
             this.resourceReferences.Add(new Tuple<ObjectId, string, ObjectId>(
                 bsonDocument["_id"].AsObjectId, element.Name, element.Value.AsBsonDocument["_id"].AsObjectId));
         }
         else if (element.Value.GetType() == typeof(BsonArray))
         {
             var bsonArray = element.Value.AsBsonArray;
             if (bsonArray != null && bsonArray.Count > 0)
             {
                 var tuple = new Tuple<ObjectId, string, List<ObjectId>>(
                     bsonDocument["_id"].AsObjectId, element.Name, new List<ObjectId>());
                 resourceSetReferences.Add(tuple);
                 foreach (var item in bsonArray)
                 {
                     tuple.Item3.Add(item.AsBsonDocument["_id"].AsObjectId);
                 }
             }
         }
         else if (element.Value.RawValue != null)
         {
             resource.SetValue(element.Name, element.Value.RawValue);
         }
     }
     return resource;
 }
예제 #6
0
        public static DSPResource CreateDSPResource(BsonDocument document, MongoMetadata mongoMetadata, string resourceName, string ownerPrefix = null)
        {
            var resourceType = mongoMetadata.ResolveResourceType(resourceName, ownerPrefix);
            if (resourceType == null)
                throw new ArgumentException(string.Format("Unable to resolve resource type {0}", resourceName), "resourceName");
            var resource = new DSPResource(resourceType);

            foreach (var element in document.Elements)
            {
                var resourceProperty = mongoMetadata.ResolveResourceProperty(resourceType, element);
                if (resourceProperty == null)
                    continue;

                string propertyName = null;
                object propertyValue = null;

                if (MongoMetadata.IsObjectId(element))
                {
                    propertyName = MongoMetadata.MappedObjectIdName;
                    propertyValue = element.Value.RawValue.ToString();
                }
                else if (element.Value.GetType() == typeof(BsonDocument))
                {
                    propertyName = element.Name;
                    propertyValue = CreateDSPResource(element.Value.AsBsonDocument, mongoMetadata, element.Name,
                        MongoMetadata.GetComplexTypePrefix(resourceType.Name));
                }
                else if (element.Value.GetType() == typeof(BsonArray))
                {
                    var bsonArray = element.Value.AsBsonArray;
                    if (bsonArray != null && bsonArray.Count > 0)
                    {
                        propertyName = element.Name;
                        int nonNullItemCount = 0;
                        for (int index = 0; index < bsonArray.Count; index++)
                        {
                            if (bsonArray[index] != BsonNull.Value)
                                ++nonNullItemCount;
                        }
                        var valueArray = new DSPResource[nonNullItemCount];
                        int valueIndex = 0;
                        for (int index = 0; index < bsonArray.Count; index++)
                        {
                            if (bsonArray[index] != BsonNull.Value)
                            {
                                valueArray[valueIndex++] = CreateDSPResource(bsonArray[index].AsBsonDocument, mongoMetadata, element.Name,
                                    MongoMetadata.GetCollectionTypePrefix(resourceType.Name));
                            }
                        }
                        propertyValue = valueArray;
                    }
                }
                else
                {
                    propertyName = element.Name;
                    if (element.Value.RawValue != null)
                    {
                        switch (element.Value.BsonType)
                        {
                            case BsonType.DateTime:
                                propertyValue = UnixEpoch + TimeSpan.FromMilliseconds(element.Value.AsBsonDateTime.MillisecondsSinceEpoch);
                                break;
                            default:
                                propertyValue = element.Value.RawValue;
                                break;
                        }
                    }
                    else
                    {
                        switch (element.Value.BsonType)
                        {
                            case BsonType.Binary:
                                propertyValue = element.Value.AsBsonBinaryData.Bytes;
                                break;
                            default:
                                propertyValue = element.Value.RawValue;
                                break;
                        }
                    }
                }

                if (propertyValue != null && element.Value.GetType() != typeof(BsonArray))
                {
                    propertyValue = Convert.ChangeType(propertyValue, resourceProperty.ResourceType.InstanceType);
                }
                resource.SetValue(propertyName, propertyValue);
            }

            return resource;
        }