Example #1
0
        private void TestApplyItemsToCollection(
            TestCustomer customer,
            IEnumerable orders,
            MergeOption option,
            TestEntityTracker entityTracker,
            bool isContinuation)
        {
            var customerDescriptor = new EntityDescriptor(this.clientEdmModel)
            {
                Entity = customer
            };

            var materializerEntry = MaterializerEntry.CreateEntryForLoadProperty(customerDescriptor, ODataFormat.Json, true);

            materializerEntry.ActualType = this.clientEdmModel.GetClientTypeAnnotation(clientEdmModel.GetOrCreateEdmType(typeof(TestCustomer)));

            var adapter = new EntityTrackingAdapter(
                entityTracker,
                option,
                clientEdmModel,
                new DataServiceContext());

            EntryValueMaterializationPolicy evmp = new EntryValueMaterializationPolicy(
                materializerContext,
                adapter,
                null,
                new Dictionary <IEnumerable, DataServiceQueryContinuation>());

            evmp.ApplyItemsToCollection(
                materializerEntry,
                ordersProperty,
                orders,
                null,
                null,
                isContinuation);

            if (entityTracker.GetEntityDescriptorFunc != null)
            {
                adapter.MaterializationLog.ApplyToContext();
            }
        }
        /// <summary>
        /// Implementation of Read/>.
        /// </summary>
        /// <returns>
        /// Return value of Read/>
        /// </returns>
        protected override bool ReadImplementation()
        {
            // Eagerly materialize the entire collection of objects into the items cache in LoadProperty scenario.
            if (this.iteration == 0)
            {
                while (base.ReadImplementation())
                {
                    this.items.Add(this.currentValue);
                }

                ClientPropertyAnnotation property         = this.responseInfo.Property;
                EntityDescriptor         entityDescriptor = this.responseInfo.EntityDescriptor;
                object entity = entityDescriptor.Entity;

                MaterializerEntry entry = MaterializerEntry.CreateEntryForLoadProperty(
                    entityDescriptor,
                    this.Format,
                    this.responseInfo.MergeOption != MergeOption.NoTracking);

                entry.ActualType = this.responseInfo.Model.GetClientTypeAnnotation(this.responseInfo.Model.GetOrCreateEdmType(entity.GetType()));

                if (property.IsEntityCollection)
                {
                    this.EntryValueMaterializationPolicy.ApplyItemsToCollection(
                        entry,
                        property,
                        this.items,
                        this.CurrentFeed != null ? this.CurrentFeed.NextPageLink : null,
                        this.MaterializeEntryPlan,
                        this.responseInfo.IsContinuation);
                }
                else
                {
                    Debug.Assert(this.items.Count <= 1, "Expecting 0 or 1 element.");
                    object target = this.items.Count > 0 ? this.items[0] : null;

                    Debug.Assert(property.EdmProperty.Type.TypeKind() == EdmTypeKind.Entity, "Must be entity typed property if not an entity collection property.");
                    this.EntityTrackingAdapter.MaterializationLog.SetLink(entry, property.PropertyName, target);

                    // Singleton entity property
                    property.SetValue(entity, target, property.PropertyName, false);
                }

                // Apply the materialization log.
                this.ApplyLogToContext();

                // Clear the log after applying it.
                this.ClearLog();
            }

            // Read object from the already loaded items cache.
            if (this.items.Count > this.iteration)
            {
                this.currentValue = this.items[this.iteration];
                this.iteration++;
                return(true);
            }
            else
            {
                this.currentValue = null;
                return(false);
            }
        }