示例#1
0
        public static TNode CreateAndAddNode <TNodeType, TNode>(this IContainerNode <TNodeType> containerNode, Func <TNode> nodeFactoryMethod)
            where TNode : Node <TNodeType>
        {
            Contract.Requires(containerNode != null);
            Contract.Requires(nodeFactoryMethod != null);

            // Create and add new node to the container node.
            var newNode = nodeFactoryMethod();

            containerNode.Add(newNode);
            return(newNode);
        }
        private void InitializeResource(IContainerNode <DomNodeType> domContainerNode, object clrResource)
        {
            Contract.Requires(clrResource != null);

            if (clrResource == null)
            {
                return;
            }

            // Map the incoming CLR resource to the DOM resource node.
            var resourceType = this.ResourceType;

            var domReadWriteResource = DomReadWriteResource.Create();

            resourceType.MapClrTypeToDomResource(domReadWriteResource);
            resourceType.MapClrIdToDomResource(domReadWriteResource, clrResource);

            var domResource    = (IDomResource)domReadWriteResource;
            var domResourceKey = domResource.CreateDomResourceKey();

            // Do not add the DOM read-write resource node if it already has been added to the DOM document.
            if (this.DocumentBuilderContext.ContainsDomReadWriteResource(domResourceKey))
            {
                return;
            }

            // Add the DOM read/write resource nodes to the DOM document.
            this.DocumentBuilderContext.AddDomReadWriteResource(domResourceKey, domReadWriteResource);
            domContainerNode.Add(domReadWriteResource);

            // Finish mapping the DOM read/write resource attributes nodes to the DOM document.
            var queryParameters    = this.DocumentBuilderContext.QueryParameters;
            var apiType            = resourceType.ResourceIdentityInfo.ApiType;
            var useSparseFieldsets = this.DocumentBuilderContext.SparseFieldsetsEnabled && queryParameters.ContainsField(apiType);

            if (!useSparseFieldsets)
            {
                resourceType.MapClrAttributesToDomResource(domReadWriteResource, clrResource);
            }
            else
            {
                resourceType.MapClrAttributesToDomResource(domReadWriteResource, clrResource, (x, y) => queryParameters.ContainsField(x, y));
            }

            this.BuildingResource     = true;
            this.DomReadWriteResource = domReadWriteResource;
            this.ClrResource          = clrResource;
        }
示例#3
0
        // PRIVATE METHODS //////////////////////////////////////////////////
        #region Methods
        private void InitializeResource(IContainerNode <DomNodeType> domContainerNode, TResource clrResource)
        {
            // Add the DOM read/write resource node to the DOM document.
            var domReadWriteResource = DomReadWriteResource.Create();

            domContainerNode.Add(domReadWriteResource);

            // Map the incoming CLR resource to the DOM resource node.
            var serviceModel    = this.ServiceModel;
            var clrResourceType = typeof(TResource);
            var resourceType    = serviceModel.GetResourceType(clrResourceType);

            resourceType.MapClrTypeToDomResource(domReadWriteResource);
            resourceType.MapClrIdToDomResource(domReadWriteResource, clrResource);
            resourceType.MapClrAttributesToDomResource(domReadWriteResource, clrResource);

            this.ResourceType         = resourceType;
            this.DomReadWriteResource = domReadWriteResource;
            this.ClrResource          = clrResource;
        }
示例#4
0
        public static TNode GetOrAddNode <TNodeType, TNode>(this IContainerNode <TNodeType> containerNode, TNodeType nodeType, Func <TNode> nodeFactoryMethod, IEqualityComparer <TNodeType> nodeTypeEqualityComparer)
            where TNode : Node <TNodeType>
        {
            Contract.Requires(containerNode != null);
            Contract.Requires(nodeFactoryMethod != null);
            Contract.Requires(nodeTypeEqualityComparer != null);

            // Try and get an existing node from the container node.
            var existingNode = containerNode.GetNode <TNodeType, TNode>(nodeType, nodeTypeEqualityComparer);

            if (existingNode != null)
            {
                return(existingNode);
            }

            // Add new node to the container node.
            var newNode = nodeFactoryMethod();

            containerNode.Add(newNode);
            return(newNode);
        }
        private void InitializeResource(IContainerNode <DomNodeType> domContainerNode, TResource clrResource)
        {
            this.BuildingResource = false;
            if (clrResource == null)
            {
                return;
            }

            // Map the incoming CLR resource to the DOM resource node.
            var serviceModel    = this.ServiceModel;
            var clrResourceType = typeof(TResource);
            var resourceType    = serviceModel.GetResourceType(clrResourceType);

            var domReadWriteResource = DomReadWriteResource.Create();

            resourceType.MapClrTypeToDomResource(domReadWriteResource);
            resourceType.MapClrIdToDomResource(domReadWriteResource, clrResource);

            var domResource    = (IDomResource)domReadWriteResource;
            var domResourceKey = domResource.CreateDomResourceKey();

            // Do not add the DOM read-write resource node if it already has been added to the DOM document.
            if (this.DocumentBuilderContext.ContainsDomReadWriteResource(domResourceKey))
            {
                return;
            }

            // Add the DOM read/write resource nodes to the DOM document.
            this.DocumentBuilderContext.AddDomReadWriteResource(domResourceKey, domReadWriteResource);
            domContainerNode.Add(domReadWriteResource);

            // Finish mapping the DOM read/write resource attributes nodes to the DOM document.
            resourceType.MapClrAttributesToDomResource(domReadWriteResource, clrResource);

            this.BuildingResource     = true;
            this.DomReadWriteResource = domReadWriteResource;
            this.ClrResource          = clrResource;
        }
示例#6
0
        private void InitializeResourceCollection(IContainerNode <DomNodeType> domContainerNode, IEnumerable <TResource> clrResourceCollection)
        {
            // Map the incoming CLR resources to the DOM equilvalent nodes.
            var serviceModel    = this.ServiceModel;
            var clrResourceType = typeof(TResource);
            var resourceType    = serviceModel.GetResourceType(clrResourceType);

            var domResourceTupleCollection1 = clrResourceCollection
                                              .EmptyIfNull()
                                              .Where(clrResource => clrResource != null)
                                              .Select(clrResource =>
            {
                var domReadWriteResource = DomReadWriteResource.Create();
                resourceType.MapClrTypeToDomResource(domReadWriteResource);
                resourceType.MapClrIdToDomResource(domReadWriteResource, clrResource);

                var domResource    = (IDomResource)domReadWriteResource;
                var domResourceKey = domResource.CreateDomResourceKey();

                var domResourceTuple = new Tuple <ResourceIdentifier, DomReadWriteResource, TResource>(domResourceKey, domReadWriteResource, clrResource);
                return(domResourceTuple);
            })
                                              .ToList();

            // Add the DOM read/write resource nodes to the DOM document.
            var domResourceTupleCollection2 = new List <Tuple <ResourceIdentifier, DomReadWriteResource, TResource> >();

            foreach (var domResourceTuple in domResourceTupleCollection1)
            {
                // Add the DOM read/write resource nodes to the DOM document.
                var domResourceKey       = domResourceTuple.Item1;
                var domReadWriteResource = domResourceTuple.Item2;

                // Do not add the DOM read-write resource node if it already has been added to the DOM document.
                if (this.DocumentBuilderContext.ContainsDomReadWriteResource(domResourceKey))
                {
                    continue;
                }

                this.DocumentBuilderContext.AddDomReadWriteResource(domResourceKey, domReadWriteResource);
                domContainerNode.Add(domReadWriteResource);

                // Finish mapping the DOM read/write resource attributes nodes to the DOM document.
                var clrResource = domResourceTuple.Item3;

                resourceType.MapClrAttributesToDomResource(domReadWriteResource, clrResource);

                // Keep track of the actual DOM read-write resource nodes added to the DOM document.
                domResourceTupleCollection2.Add(domResourceTuple);
            }

            var domReadWriteResourceCollection = domResourceTupleCollection2
                                                 .Select(domResourceTuple => domResourceTuple.Item2)
                                                 .ToList();

            this.DomReadWriteResourceCollection = domReadWriteResourceCollection;

            var clrResourceCollectionInternal = domResourceTupleCollection2
                                                .Select(domResourceTuple => domResourceTuple.Item3)
                                                .ToList();

            this.ClrResourceCollection = clrResourceCollectionInternal;
        }
示例#7
0
        private void InitializeResourceCollection(IContainerNode <DomNodeType> domContainerNode, Type clrResourceType, IEnumerable <object> clrResourceCollection)
        {
            Contract.Requires(clrResourceType != null);

            // ReSharper disable PossibleMultipleEnumeration
            if (clrResourceCollection == null || clrResourceCollection.Any() == false)
            {
                this.ClrResourceCollection = new List <object>();
                return;
            }

            // Map the incoming CLR resources to the DOM equivalent nodes.
            var serviceModel = this.ServiceModel;
            var resourceType = serviceModel.GetResourceType(clrResourceType);

            var domResourceTupleCollection1 = clrResourceCollection
                                              .Where(clrResource => clrResource != null)
                                              .Select(clrResource =>
            {
                var domReadWriteResource = DomReadWriteResource.Create();
                resourceType.MapClrTypeToDomResource(domReadWriteResource);
                resourceType.MapClrIdToDomResource(domReadWriteResource, clrResource);

                var domResource    = (IDomResource)domReadWriteResource;
                var domResourceKey = domResource.CreateDomResourceKey();

                var domResourceTuple = new Tuple <ResourceIdentifier, DomReadWriteResource, object>(domResourceKey, domReadWriteResource, clrResource);
                return(domResourceTuple);
            })
                                              .ToList();
            // ReSharper restore PossibleMultipleEnumeration

            // Add the DOM read/write resource nodes to the DOM document.
            var queryParameters    = this.DocumentBuilderContext.QueryParameters;
            var apiType            = resourceType.ResourceIdentityInfo.ApiType;
            var useSparseFieldsets = this.DocumentBuilderContext.SparseFieldsetsEnabled && queryParameters.ContainsField(apiType);

            var domResourceTupleCollection2 = new List <Tuple <ResourceIdentifier, DomReadWriteResource, object> >();

            foreach (var domResourceTuple in domResourceTupleCollection1)
            {
                // Add the DOM read/write resource nodes to the DOM document.
                var domResourceKey       = domResourceTuple.Item1;
                var domReadWriteResource = domResourceTuple.Item2;

                // Do not add the DOM read-write resource node if it already has been added to the DOM document.
                if (this.DocumentBuilderContext.ContainsDomReadWriteResource(domResourceKey))
                {
                    continue;
                }

                this.DocumentBuilderContext.AddDomReadWriteResource(domResourceKey, domReadWriteResource);
                domContainerNode.Add(domReadWriteResource);

                // Finish mapping the DOM read/write resource attributes nodes to the DOM document.
                var clrResource = domResourceTuple.Item3;

                if (!useSparseFieldsets)
                {
                    resourceType.MapClrAttributesToDomResource(domReadWriteResource, clrResource);
                }
                else
                {
                    resourceType.MapClrAttributesToDomResource(domReadWriteResource, clrResource, (x, y) => queryParameters.ContainsField(x, y));
                }

                // Keep track of the actual DOM read-write resource nodes added to the DOM document.
                domResourceTupleCollection2.Add(domResourceTuple);
            }

            var domReadWriteResourceCollection = domResourceTupleCollection2
                                                 .Select(domResourceTuple => domResourceTuple.Item2)
                                                 .ToList();

            this.DomReadWriteResourceCollection = domReadWriteResourceCollection;

            var clrResourceCollectionInternal = domResourceTupleCollection2
                                                .Select(domResourceTuple => domResourceTuple.Item3)
                                                .ToList();

            this.ClrResourceCollection = clrResourceCollectionInternal;

            this.BuildingResourceCollection = true;
        }