Ejemplo n.º 1
0
        /// <summary>Loads the collection from another collection.</summary>
        /// <param name="items">Collection whose elements will be loaded into the DataServiceCollection.</param>
        /// <remarks>
        /// When tracking is enabled, the behavior of Load would be to attach all those entities that are not already tracked by the context
        /// associated with the collection. The operation will go deep into the input entities so that all related
        /// entities are attached to the context if not already present. All entities in <paramref name="items"/>
        /// will be tracked after Load is done.
        /// Load method checks for duplication. The collection will ignore any duplicated items been loaded.
        /// For large amount of items, consider DataServiceContext.LoadProperty instead.
        /// </remarks>
        public void Load(IEnumerable <T> items)
        {
            DataServiceCollection <T> .ValidateIteratorParameter(items);

            if (this.trackingOnLoad)
            {
                // This throws if no context can be obtained, no need to check here
                DataServiceContext context = DataServiceCollection <T> .GetContextFromItems(items);

                this.trackingOnLoad = false;

                this.StartTracking(context, items, this.entitySetName, this.entityChangedCallback, this.collectionChangedCallback);
            }
            else
            {
                this.StartLoading();
                try
                {
                    this.InternalLoadCollection(items);
                }
                finally
                {
                    this.FinishLoading();
                }
            }
        }
Ejemplo n.º 2
0
 public DataServiceCollection(DataServiceContext context, IEnumerable <T> items, TrackingMode trackingMode, string entitySetName, Func <EntityChangedParams, bool> entityChangedCallback, Func <EntityCollectionChangedParams, bool> collectionChangedCallback)
 {
     if (trackingMode == TrackingMode.AutoChangeTracking)
     {
         if (context == null)
         {
             if (items == null)
             {
                 this.trackingOnLoad            = true;
                 this.entitySetName             = entitySetName;
                 this.entityChangedCallback     = entityChangedCallback;
                 this.collectionChangedCallback = collectionChangedCallback;
             }
             else
             {
                 context = DataServiceCollection <T> .GetContextFromItems(items);
             }
         }
         if (!this.trackingOnLoad)
         {
             if (items != null)
             {
                 DataServiceCollection <T> .ValidateIteratorParameter(items);
             }
             this.StartTracking(context, items, entitySetName, entityChangedCallback, collectionChangedCallback);
         }
     }
     else if (items != null)
     {
         this.Load(items);
     }
 }
Ejemplo n.º 3
0
        internal bool LookupParent <T>(DataServiceCollection <T> collection, out object parentEntity, out string parentProperty)
        {
            string sourceEntitySet;
            string targetEntitySet;

            this.bindingGraph.GetEntityCollectionInfo(collection, out parentEntity, out parentProperty, out sourceEntitySet, out targetEntitySet);

            return(parentEntity != null);
        }
Ejemplo n.º 4
0
        internal static void VerifyObserverNotPresent <T>(object oec, string sourceProperty, Type sourceType)
        {
            DataServiceCollection <T> services = oec as DataServiceCollection <T>;

            if (services.Observer != null)
            {
                throw new InvalidOperationException(Strings.DataBinding_CollectionPropertySetterValueHasObserver(sourceProperty, sourceType));
            }
        }
Ejemplo n.º 5
0
        internal static void VerifyObserverNotPresent <T>(object oec, string sourceProperty, Type sourceType)
        {
            Debug.Assert(BindingEntityInfo.IsDataServiceCollection(oec.GetType()), "Must be an DataServiceCollection.");

            DataServiceCollection <T> typedCollection = oec as DataServiceCollection <T>;

            if (typedCollection.Observer != null)
            {
                throw new InvalidOperationException(Strings.DataBinding_CollectionPropertySetterValueHasObserver(sourceProperty, sourceType));
            }
        }
Ejemplo n.º 6
0
 internal void StartTracking <T>(DataServiceCollection <T> collection, string collectionEntitySet)
 {
     try
     {
         this.AttachBehavior = true;
         this.bindingGraph.AddDataServiceCollection(null, null, collection, collectionEntitySet);
     }
     finally
     {
         this.AttachBehavior = false;
     }
 }
Ejemplo n.º 7
0
        internal static DataServiceCollection <T> RestoreState(
            CollectionState state, DataServiceContext context, Dictionary <Guid, EntityDescriptor> idToEntityDescriptor)
        {
            var items = new List <T>(state.DescriptorIds.Count);

            foreach (Guid id in state.DescriptorIds)
            {
                EntityDescriptor entityDescriptor = idToEntityDescriptor[id];
                items.Add((T)entityDescriptor.Entity);
            }

            DataServiceCollection <T> collection = new DataServiceCollection <T>(
                context, items, TrackingMode.AutoChangeTracking, state.EntitySetName, null, null);

            collection.rootCollection = state.RootCollection;

            return(collection);
        }
Ejemplo n.º 8
0
        /// <summary>Creates a new DataServiceCollection.</summary>
        /// <param name="context"><see cref="DataServiceContext"/> associated with the new collection.</param>
        /// <param name="items">Enumeration of items to initialize the new DataServiceCollection with.</param>
        /// <param name="trackingMode">The tracking mode for the new collection.</param>
        /// <param name="entitySetName">The name of the entity set the elements in the collection belong to.</param>
        /// <param name="entityChangedCallback">Delegate that gets called when an entity changes.</param>
        /// <param name="collectionChangedCallback">Delegate that gets called when an entity collection changes.</param>
        public DataServiceCollection(
            DataServiceContext context,
            IEnumerable <T> items,
            TrackingMode trackingMode,
            string entitySetName,
            Func <EntityChangedParams, bool> entityChangedCallback,
            Func <EntityCollectionChangedParams, bool> collectionChangedCallback)
        {
            if (trackingMode == TrackingMode.AutoChangeTracking)
            {
                if (context == null)
                {
                    if (items == null)
                    {
                        // Enable tracking on first Load/LoadAsync call, when we can obtain a context
                        this.trackingOnLoad = true;

                        // Save off these for when we enable tracking later
                        this.entitySetName             = entitySetName;
                        this.entityChangedCallback     = entityChangedCallback;
                        this.collectionChangedCallback = collectionChangedCallback;
                    }
                    else
                    {
                        // This throws if no context can be obtained, no need to check here
                        context = DataServiceCollection <T> .GetContextFromItems(items);
                    }
                }

                if (!this.trackingOnLoad)
                {
                    if (items != null)
                    {
                        DataServiceCollection <T> .ValidateIteratorParameter(items);
                    }

                    this.StartTracking(context, items, entitySetName, entityChangedCallback, collectionChangedCallback);
                }
            }
            else if (items != null)
            {
                this.Load(items);
            }
        }
Ejemplo n.º 9
0
        internal void StartTracking <T>(DataServiceCollection <T> collection, string collectionEntitySet)
        {
            Debug.Assert(collection != null, "Only constructed collections are tracked.");

            if (!BindingEntityInfo.IsEntityType(typeof(T)))
            {
                throw new ArgumentException(Strings.DataBinding_DataServiceCollectionArgumentMustHaveEntityType(typeof(T)));
            }

            try
            {
                this.AttachBehavior = true;

                this.bindingGraph.AddCollection(null, null, collection, collectionEntitySet);
            }
            finally
            {
                this.AttachBehavior = false;
            }
        }
        /// <summary>Start tracking the specified DataServiceCollection.</summary>
        /// <typeparam name="T">An entity type.</typeparam>
        /// <param name="collection">An DataServiceCollection.</param>
        /// <param name="collectionEntitySet">The entity set of the elements in <paramref name="collection"/>.</param>
        internal void StartTracking <T>(DataServiceCollection <T> collection, string collectionEntitySet)
        {
            Debug.Assert(collection != null, "Only constructed collections are tracked.");

            // Verify that T corresponds to an entity type.
            if (!BindingEntityInfo.IsEntityType(typeof(T)))
            {
                throw new ArgumentException(Strings.DataBinding_DataServiceCollectionArgumentMustHaveEntityType(typeof(T)));
            }

            try
            {
                this.AttachBehavior = true;

                // Recursively traverse the entire object graph under the root collection.
                this.bindingGraph.AddCollection(null, null, collection, collectionEntitySet);
            }
            finally
            {
                this.AttachBehavior = false;
            }
        }
Ejemplo n.º 11
0
        private void SetObserver <T>(ICollection collection)
        {
            DataServiceCollection <T> oec = collection as DataServiceCollection <T>;

            oec.Observer = this.observer;
        }