/// <summary> /// Creates new instance of type T /// </summary> /// <typeparam name="T">Interface type of instance to create</typeparam> /// <returns>Created object instance</returns> public T New <T>() { CheckThread(); Guid typeId = typesService.GetTypeIdCached(typeof(T)); if (typeId.Equals(Guid.Empty)) { throw new ArgumentException("Type not registered:" + typeof(T).AssemblyQualifiedName); } Guid instanceId = Guid.Empty; if (typesService.IsDictionaryType(typeId)) { instanceId = dictionaryInstancesService.NewInstance(typeId); } else if (typesService.IsCollectionType(typeId)) { instanceId = collectionInstancesService.NewInstance(typeId); } else { instanceId = objectInstancesService.NewInstance(typeId); } T proxy = proxyCreatorService.NewObject <T>(runtimeProxyFacade, instanceId, false); mutableProxyMap.AddProxy(instanceId, proxy); return(proxy); }
/// <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); } }
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); } }