예제 #1
0
        /// <summary>Loads a collection of entity objects into the collection.</summary>
        /// <param name="items">Collection of entity objects to be added to the <see cref="Microsoft.OData.Client.DataServiceCollection{T}" />.</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();
                }
            }
        }
예제 #2
0
        /// <summary>Initializes a new instance of the <see cref="Microsoft.OData.Client.DataServiceCollection{T}" /> class based on query execution, with the supplied change method delegates, and that uses the supplied <see cref="Microsoft.OData.Client.DataServiceContext" />.</summary>
        /// <param name="context">The <see cref="Microsoft.OData.Client.DataServiceContext" /> used to track items in the collection.</param>
        /// <param name="items">A <see cref="Microsoft.OData.Client.DataServiceQuery{TElement}" /> or LINQ query that returns an <see cref="System.Collections.Generic.IEnumerable{T}" /> collection of objects that are used to initialize the collection.</param>
        /// <param name="trackingMode">A <see cref="Microsoft.OData.Client.TrackingMode" /> value that indicated whether or not changes made to items in the collection are automatically tracked.</param>
        /// <param name="entitySetName">The entity set of the objects in the collection.</param>
        /// <param name="entityChangedCallback">A delegate that encapsulates a method that is called when an entity changes.</param>
        /// <param name="collectionChangedCallback">A delegate that encapsulates a method that is called when the collection of entities 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);
            }
        }