Пример #1
0
        public object ResetResource(object resource)
        {
            CustomUtils.CheckArgumentNotNull(resource, "resource");

            CustomEntityType entityType;

            if (!MetadataWorkspace.TryGetEntityType(resource.GetType(), out entityType))
            {
                throw new NotImplementedException(String.Format(
                                                      "ResetResource() is implemented only for entity types. " +
                                                      "The given object '{0}' is not a valid entity."
                                                      ));
            }

            foreach (var property in entityType.Propeties)
            {
                if (!property.IsReadOnly && !property.IsPrimaryKey && null == property.ForeignKeyConstraint)
                {
                    object defaultValue;
                    if (property.IsNullable)
                    {
                        defaultValue = null;
                    }
                    else
                    {
                        defaultValue = Activator.CreateInstance(property.ClrType);
                    }

                    property.SetValue(resource, defaultValue);
                }
            }

            return(resource);
        }
Пример #2
0
        public object ResolveResource(object targetResource)
        {
            CustomUtils.CheckArgumentNotNull(targetResource, "targetResource");
            CheckEntityObjectAttached(targetResource);

            return(targetResource);
        }
Пример #3
0
        public object CreateResource(string containerName, string fullTypeName)
        {
            CustomUtils.CheckArgumentNotNull(fullTypeName, "fullTypeName");

            Type   entityOrComplexType = Type.GetType(fullTypeName);
            object obj = Activator.CreateInstance(entityOrComplexType);

            if (IsComplexType(entityOrComplexType))
            {
                if (null != containerName)
                {
                    throw new ArgumentException(String.Format(
                                                    "Parameter 'containerName' must be null when CreateResource is called for " +
                                                    "complex type objects. Container name: '{0}'. Complex type: '{1}'.",
                                                    containerName, entityOrComplexType.Name
                                                    ));
                }
                return(obj);
            }
            else
            {
                CustomUtils.CheckArgumentNotNull(containerName, "containerName");
                AddObject(containerName, obj);  // will throw if the object is not a valid entity
                Debug.Assert(IsAttached(obj));
            }

            return(obj);
        }
Пример #4
0
        public void DeleteResource(object targetResource)
        {
            CustomUtils.CheckArgumentNotNull(targetResource, "targetResource");
            CheckEntityObjectAttached(targetResource);

            DeleteObject(targetResource);
        }
Пример #5
0
        public void Remove(object entityObject)
        {
            CustomUtils.CheckArgumentNotNull(entityObject, "entityObject");
            CustomEntityType entityType = CheckTypeCompatibility(entityObject);

            RemoveInternal(entityObject);
        }
Пример #6
0
        public void SetReference(object targetResource, string propertyName, object propertyValue)
        {
            CustomUtils.CheckArgumentNotNull(targetResource, "targetResource");
            CustomUtils.CheckArgumentNotNull(propertyName, "propertyName");

            GetEntityMember(targetResource, propertyName).SetValue(targetResource, propertyValue);
        }
Пример #7
0
        public void Update(object entityObject)
        {
            CustomUtils.CheckArgumentNotNull(entityObject, "entityObject");
            CustomEntityType entityType = CheckTypeCompatibility(entityObject);

            UpdateInternal(entityObject);
            RunPostUpdateStoreTriggers(entityObject, entityType);
        }
Пример #8
0
 public FileBasedCustomEntitySetContainer(
     CustomMetadataWorkspace metadataWorkspace,
     string backendFileName
     )
     : base(metadataWorkspace)
 {
     CustomUtils.CheckArgumentNotNull(backendFileName, "backendFileName");
     _backendFileName = backendFileName;
 }
Пример #9
0
        public IQueryable CreateQuery(string entitySetName)
        {
            CustomUtils.CheckArgumentNotNull(entitySetName, "entitySetName");

            CustomEntitySet entitySet = GetEntitySetByName(entitySetName);

            Debug.Assert(null != entitySet);
            Debug.Assert(entitySet is IEnumerable);

            return(((IEnumerable)entitySet).AsQueryable());
        }
Пример #10
0
        public CustomEntitySetContainer(
            CustomMetadataWorkspace metadataWorkspace
            )
        {
            CustomUtils.CheckArgumentNotNull(metadataWorkspace, "metadataWorkspace");
            _metadata = metadataWorkspace;

            foreach (CustomEntitySetType entitySetType in MetadataWorkspace.EntitySets)
            {
                CreateQuery(entitySetType.Name);
            }
        }
Пример #11
0
        //
        // WARNING these serialization methods are quick hack for Astoria Test Framework to let it
        // read in-memory data on the server side from the test client
        //

        public virtual void WriteDataObjectsUnsafe(Stream stream)
        {
            CustomUtils.CheckArgumentNotNull(stream, "stream");

            if (stream.CanSeek)
            {
                stream.Seek(0, SeekOrigin.Begin);
            }

            var entitySetsToSerialize = GetAllEntitySets().ToDictionary(es => es.EntitySetType.Name);

            GetDataContractSerializer().WriteObject(stream, entitySetsToSerialize);
        }
Пример #12
0
        public object GetValue(object targetResource, string propertyName)
        {
            CustomUtils.CheckArgumentNotNull(targetResource, "targetResource");
            CustomUtils.CheckArgumentNotNull(propertyName, "propertyName");

            var property = GetProperty(targetResource.GetType(), propertyName);

            if (null == property.GetGetMethod() || !property.CanRead)
            {
                // we don't generate properties without getter; but just in case...
                throw new InvalidOperationException(String.Format(
                                                        "Property '{0}' of resource type '{1}' is not accessible.",
                                                        propertyName,
                                                        targetResource.GetType().Name
                                                        ));
            }
            return(property.GetValue(targetResource, null));
        }
Пример #13
0
        public object GetResource(IQueryable query, string fullTypeName)
        {
            CustomUtils.CheckArgumentNotNull(query, "query");

            object resource = FindResourceByQuery(query);

            if (
                null != resource &&
                null != fullTypeName &&
                resource.GetType().FullName != fullTypeName)
            {
                throw new ArgumentException(String.Format(
                                                "Invalid URI specified. ExpectedType: '{0}'. ActualType: '{1}'.",
                                                fullTypeName, resource.GetType().FullName
                                                ));
            }

            return(resource);
        }
Пример #14
0
        public void RemoveReferenceFromCollection(object targetResource, string propertyName, object resourceToBeRemoved)
        {
            CustomUtils.CheckArgumentNotNull(targetResource, "targetResource");
            CustomUtils.CheckArgumentNotNull(propertyName, "propertyName");

            object objCollection = GetObjectCollection(targetResource, propertyName);

            if (objCollection is CustomObjectCollectionBase)
            {
                // the entity object was already added to the context
                // and the underlying CLR collection was wrapped into our internal
                // tracking collection
                ((CustomObjectCollectionBase)objCollection).Remove(resourceToBeRemoved);
            }
            else
            {
                MethodInfo addMethod = objCollection.GetType().GetMethod("Remove");
                Debug.Assert(null != addMethod);
                addMethod.Invoke(targetResource, new object[] { resourceToBeRemoved });
            }
        }
Пример #15
0
        public IQueryable <EntityObjectType> CreateQueryOfT <EntityObjectType>(string entitySetName)
        {
            CustomUtils.CheckArgumentNotNull(entitySetName, "entitySetName");

            CustomEntitySetType entitySetType = _metadata.GetEntitySet(entitySetName);
            CustomEntityType    entityType    = _metadata.GetEntityType(typeof(EntityObjectType));

            if (!entitySetType.BaseElementType.IsAssignableFrom(entityType))
            {
                throw new InvalidOperationException(String.Format(
                                                        "The base element type '{0}' of the entity set '{1}' is not compatible with " +
                                                        "the queried entity type '{2}'.",
                                                        entitySetType.BaseElementType, entitySetName, entityType
                                                        ));
            }

            CustomEntitySet entitySet = GetEntitySetByName(entitySetName);

            Debug.Assert(null != entitySet);
            Debug.Assert(typeof(IEnumerable <EntityObjectType>).IsAssignableFrom(entitySet.GetType()));

            return(((IEnumerable <EntityObjectType>)entitySet).AsQueryable());
        }
Пример #16
0
        protected internal virtual void ReloadDataObjectsUnsafe(Stream stream)
        {
            CheckNotReadOnly();

            CustomUtils.CheckArgumentNotNull(stream, "stream");
            Debug.Assert(null != _metadata);

            if (stream.CanSeek)
            {
                stream.Seek(0, SeekOrigin.Begin);
            }

            DataContractSerializer dataContractSerializer = GetDataContractSerializer();
            var deserializedEntitySets = (IDictionary <string, CustomEntitySet>)dataContractSerializer.ReadObject(stream);

            _entitySetMap.Clear();
            foreach (var entitySetEntry in deserializedEntitySets)
            {
                var entitySetType = MetadataWorkspace.EntitySets
                                    .Where(et => et.Name == entitySetEntry.Key)
                                    .Select(et => et)
                                    .SingleOrDefault();
                Debug.Assert(null != entitySetType);

                // the metadata information for entity sets was not serialized, so we need
                // to fix it up immediately after the entity set is deserialized
                var entitySet = entitySetEntry.Value;
                entitySet.MetadataWorkspace = MetadataWorkspace;
                entitySet.EntitySetType     = entitySetType;
                entitySet.ParentContainer   = this;

                _entitySetMap.Add(entitySetType.Name, entitySet);
            }

            _defaultDataCreated = true;
        }
Пример #17
0
        public void SetValue(object targetResource, string propertyName, object propertyValue)
        {
            CustomUtils.CheckArgumentNotNull(targetResource, "targetResource");
            CustomUtils.CheckArgumentNotNull(propertyName, "propertyName");

            var property = GetProperty(targetResource.GetType(), propertyName);

            if (IsComplexType(property.PropertyType))
            {
                // we treat complex types as value types, an inherit part of an entity type,
                // they cannot be null, have no separate metadata, always exist and
                // by design complex type properties have no setters
                //
                // we can relax these constraints later, but now change detection
                // logic in custom object context depends on it

                if (null == propertyValue)
                {
                    throw new ArgumentNullException(String.Format(
                                                        "Value for complex type property '{0}' cannot be null.",
                                                        propertyName
                                                        ));
                }

                object currentComplexValue = property.GetValue(targetResource, null);
                CopyComplexTypeObject(currentComplexValue, propertyValue);
                return;
            }

            CustomMemberType entityMember;

            if (TryGetEntityMember(targetResource, propertyName, out entityMember))
            {
                if (entityMember is CustomNavigationPropertyType)
                {
                    throw new InvalidOperationException(String.Format(
                                                            "Given property '{0}' of entity object '{1}' is a navigation " +
                                                            "property of type '{2}'. SetValue() is not supposed for " +
                                                            "changing navigation properties. Use SetReference() or " +
                                                            "AddReferenceToCollection() methods.",
                                                            propertyName, targetResource, entityMember.ClrType
                                                            ));
                }

                Debug.Assert(entityMember is CustomPropertyType);

                CustomPropertyType entityProperty = (CustomPropertyType)entityMember;

                if (null != entityProperty.ForeignKeyConstraint)
                {
                    // attempt to set a foreign key property is incorrect in the world of our custom
                    // data model because there is no setter for such properties at all and the getter
                    // simply returns value of a primary key property from the primary end entity using
                    // corresponding navigation property
                    // get
                    // {
                    //    if(null == _primaryEnd)
                    //    {
                    //       return default(int);
                    //    }
                    //    return _primaryEnd.Id;
                    // }
                    //
                    // silently return from SetValue without error reporting for compatibility
                    // with existing tests
                    return;
                }

                if (entityProperty.IsPrimaryKey)
                {
                    // all our primary keys are store generated and have internal setter, but
                    // test framework for Astoria suppose to be able to set primary keys and
                    // then use same values to query the object; we allow it here to be
                    // compatible with the existing tests; for objects with initialized primary keys
                    // the custom object context will actually queue an update operation, but not insert
                    entityProperty.SetValue(targetResource, propertyValue);
                    return;
                }
            }
            else
            {
                Debug.Assert(IsComplexType(targetResource.GetType()));
            }

            if (null == property.GetSetMethod() || !property.CanWrite)
            {
                throw new InvalidOperationException(String.Format(
                                                        "Property '{0}' of resource type '{1}' is not writable.",
                                                        propertyName,
                                                        targetResource.GetType().Name
                                                        ));
            }
            property.SetValue(targetResource, propertyValue, null);
        }