Esempio n. 1
0
        /// <summary>
        /// Creates new instance of Workspace type
        /// </summary>
        internal Workspace(Guid snapshotId, TimeSpan timeout, INodeProvider <Guid, object, EdgeData> nodeProvider, IWorkspaceFacade commitTarget, ProxyCreatorService proxyCreatorService, TypesService typesService, IsolationLevel isolationLevel, IProxyMap immutableProxyMap)
        {
            this.workspaceId = Guid.NewGuid();
            this.thread      = Thread.CurrentThread;

            if (!typeof(TDataType).IsInterface)
            {
                throw new ArgumentException("Interface type expected: " + typeof(TDataType).AssemblyQualifiedName);
            }

            this.snapshotId          = snapshotId;
            this.nodeProvider        = nodeProvider;
            this.proxyCreatorService = proxyCreatorService;
            this.typesService        = typesService;
            this.isolationLevel      = isolationLevel;
            this.workspaceFacade     = commitTarget;
            this.immutableProxyMap   = immutableProxyMap;

            workspaceFacade.OpenWorkspace(workspaceId, snapshotId, isolationLevel, timeout);

            if (isolationLevel == IsolationLevel.ReadOnly)
            {
                // Rely directly on parent provider if read only
                this.objectInstancesService     = new ObjectInstancesService(nodeProvider, typesService);
                this.immutableInstancesService  = new ObjectInstancesService(nodeProvider, typesService);
                this.collectionInstancesService = new CollectionInstancesService(nodeProvider, typesService);
                this.dictionaryInstancesService = new DictionaryInstancesService(nodeProvider, typesService);
            }
            else
            {
                // Construct isolated provider for local changes
                var isolatedStorage = new DirectNodeProviderUnsafe <Guid, object, EdgeData>(new MemoryStorageUnsafe <Guid, object>(), false);
                isolatedProvider                = new IsolatedNodeProvider(nodeProvider, isolatedStorage, thread);
                this.objectInstancesService     = new ObjectInstancesService(isolatedProvider, typesService);
                this.immutableInstancesService  = new ObjectInstancesService(nodeProvider, typesService);
                this.collectionInstancesService = new CollectionInstancesService(isolatedProvider, typesService);
                this.dictionaryInstancesService = new DictionaryInstancesService(isolatedProvider, typesService);
            }

            this.runtimeProxyFacade = new RuntimeProxyFacade(typesService, objectInstancesService, immutableInstancesService, collectionInstancesService, new CollectionInstancesService(nodeProvider, typesService), dictionaryInstancesService, new DictionaryInstancesService(nodeProvider, typesService), mutableProxyMap, immutableProxyMap, proxyCreatorService);

            // Initialize root data proxy
            var rootObjectId = commitTarget.GetRootObjectId(snapshotId);

            rootProxy = proxyCreatorService.NewObject <TDataType>(runtimeProxyFacade, rootObjectId, isolationLevel == IsolationLevel.ReadOnly);

            if (isolationLevel == IsolationLevel.ReadOnly)
            {
                immutableProxyMap.AddProxy(rootObjectId, rootProxy);
            }
            else
            {
                mutableProxyMap.AddProxy(rootObjectId, rootProxy);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Creates object from given revision ID returning the last commited state.
        /// This method is not usable on new uncommited objects, because they dont have commited state.
        /// Returned object is read only.
        /// </summary>
        /// <typeparam name="T">Type of object instance expected</typeparam>
        /// <param name="revisionId">Revision ID to create</param>
        /// <returns>Entity object instance</returns>
        public T SpawnImmutable <T>(Guid revisionId)
        {
            if (isolatedProvider.GetNodeState(revisionId) == NodeState.Created)
            {
                throw new ArgumentException("Operation not allowed for uncommited instance");
            }

            object proxy = null;

            if (!immutableProxyMap.TryGetProxy(revisionId, out proxy))
            {
                proxy = proxyCreatorService.NewObject <T>(runtimeProxyFacade, revisionId, true);
                immutableProxyMap.AddProxy(revisionId, proxy);
            }

            return((T)proxy);
        }
Esempio n. 3
0
        private object GetProxyInstance(bool isReadOnly, Guid referenceId, Guid typeId)
        {
            if (referenceId.Equals(Constants.NullReferenceNodeId))
            {
                return(null);
            }
            else
            {
                object proxy = null;

                IProxyMap map = mutableProxyMap;
                var       collectionService = collectionInstancesService;
                var       objectService     = objectInstancesService;

                if (isReadOnly)
                {
                    map = immutableProxyMap;
                    collectionService = immutableCollectionInstancesService;
                    objectService     = immutableInstancesService;
                }

                if (!map.TryGetProxy(referenceId, out proxy))
                {
                    if (typeId.Equals(Guid.Empty))
                    {
                        if (collectionService.IsCollectionInstance(referenceId)) // TODO (nsabo) Merge this information into proxy static data
                        {
                            typeId = collectionService.GetInstanceTypeId(referenceId);
                        }
                        else
                        {
                            // Get type ID of referenced instance
                            typeId = objectService.GetInstanceTypeId(referenceId);
                        }
                    }
                    // Create proxy object
                    proxy = proxyCreatorService.NewObject(this, typeId, referenceId, isReadOnly);
                    // Add to proxy map
                    map.AddProxy(referenceId, proxy);
                }

                return(proxy);
            }
        }