Esempio n. 1
0
        static public DataServiceState Save(DataServiceContext context, Dictionary <string, object> collections)
        {
            List <Type> knownTypes = GetKnownTypes(context);

            Dictionary <EntityDescriptor, Guid> entityDescriptorToId;
            ContextState contextState    = context.SaveState(out entityDescriptorToId);
            string       contextAsString = SerializeContextToString(contextState, knownTypes);

            var collectionsState = new Dictionary <string, CollectionState>();

            if (collections != null)
            {
                foreach (KeyValuePair <string, object> kvp in collections)
                {
                    IDataServiceCollection collection      = (IDataServiceCollection)kvp.Value;
                    CollectionState        collectionState = collection.SaveState(context, entityDescriptorToId);
                    collectionsState.Add(kvp.Key, collectionState);
                }
            }

            DataServiceState state = new DataServiceState()
            {
                CollectionsState = collectionsState,
                ContextAsString  = contextAsString,
                ContextTypeName  = context.GetType().AssemblyQualifiedName,
                KnownTypeNames   = knownTypes.Select(t => t.AssemblyQualifiedName).ToList()
            };

            return(state);
        }
        CollectionState IDataServiceCollection.SaveState(
            DataServiceContext context, Dictionary <EntityDescriptor, Guid> entityDescriptorToId)
        {
            if (this.observer == null)
            {
                throw new InvalidOperationException(Strings.DataServiceCollection_OperationForTrackedOnly);
            }

            if (this.observer.Context != context)
            {
                throw new InvalidOperationException(Strings.DataServiceState_CollectionNotInContext);
            }

            var state = new CollectionState();

            state.EntitySetName  = this.entitySetName;
            state.EntityTypeName = typeof(T).AssemblyQualifiedName;
            state.RootCollection = this.rootCollection;

            state.DescriptorIds = new List <Guid>(this.Count);
            foreach (object entity in this)
            {
                EntityDescriptor entityDescriptor = context.GetEntityDescriptor(entity);
                Guid             id = entityDescriptorToId[entityDescriptor];
                state.DescriptorIds.Add(id);
            }

            return(state);
        }
        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);
        }
Esempio n. 4
0
        Dictionary <string, object> RestoreCollections(DataServiceContext context, Dictionary <Guid, EntityDescriptor> idToEntityDescriptor)
        {
            var collections = new Dictionary <string, object>(this.CollectionsState.Count);

            foreach (KeyValuePair <string, CollectionState> kvp in this.CollectionsState)
            {
                CollectionState collectionState = kvp.Value;

                Type       entityType     = Type.GetType(collectionState.EntityTypeName);
                Type       collectionType = typeof(DataServiceCollection <>).MakeGenericType(new Type[] { entityType });
                MethodInfo restoreMethod  = collectionType.GetMethod(
                    "RestoreState", BindingFlags.NonPublic | BindingFlags.Static);
                Debug.Assert(restoreMethod != null);

                object collection = restoreMethod.Invoke(null, new object[] { collectionState, context, idToEntityDescriptor });
                collections.Add(kvp.Key, collection);
            }

            return(collections);
        }