public void Init()
        {
            this.entityType = new ResourceType(typeof(object), ResourceTypeKind.EntityType, null, "Fake.NS", "Type", false)
            {
                CanReflectOnInstanceType = false
            };
            this.entityType.AddProperty(new ResourceProperty("Id", ResourcePropertyKind.Key | ResourcePropertyKind.Primitive, ResourceType.GetPrimitiveResourceType(typeof(int)))
            {
                CanReflectOnInstanceTypeProperty = false
            });
            this.entityType.SetReadOnly();

            var resourceSet = new ResourceSet("Set", this.entityType);

            resourceSet.SetReadOnly();
            this.resourceSetWrapper = ResourceSetWrapper.CreateForTests(resourceSet);

            this.action = new ServiceAction("Fake", ResourceType.GetPrimitiveResourceType(typeof(int)), OperationParameterBindingKind.Sometimes, new[] { new ServiceActionParameter("p1", this.entityType) }, null);
            this.action.SetReadOnly();

            this.provider = new TestDataServiceProvider();

            this.actionProvider = DataServiceActionProviderWrapper.Create(this.provider, ODataProtocolVersion.V4, () => this.operationContext);
            var host = new DataServiceHostSimulator();

            this.operationContext = new DataServiceOperationContext(host);
            this.operationContext.InitializeAndCacheHeaders(new DataServiceSimulator());
        }
        public OrchardDataServiceMetadataProvider(OrchardDataSource source)
        {
            OrchardDataSource = source;

            foreach (var definition in OrchardDataSource.ContentManager.GetContentTypeDefinitions())
            {
                var name = definition.Name;
                var r    = new ResourceType(typeof(ContentItem), ResourceTypeKind.EntityType, null, null, name, false);

                r.AddProperty(new ResourceProperty("Id", ResourcePropertyKind.Primitive | ResourcePropertyKind.Key, ResourceType.GetPrimitiveResourceType(typeof(int))));
                r.AddProperty(new ResourceProperty("Version", ResourcePropertyKind.Primitive, ResourceType.GetPrimitiveResourceType(typeof(int))));

                foreach (var part in definition.Parts)
                {
                    var propertyResourceType = GetComplexResourceType(part);
                    var resourceProperty     = new ResourceProperty(part.PartDefinition.Name, ResourcePropertyKind.ComplexType, propertyResourceType);
                    resourceProperty.CanReflectOnInstanceTypeProperty = false;
                    r.AddProperty(resourceProperty);
                }

                r.SetReadOnly();
                resourceTypes.Add(name, r);
                var s = new ResourceSet(name, r);
                s.CustomState = definition;
                s.SetReadOnly();
                resourceSets.Add(name, s);
            }
        }
Esempio n. 3
0
        private ResourceSet CreateResourceSet(IEntity entity, ResourceType resourceType)
        {
            ResourceSet resourceSet = new ResourceSet(entity.Name, resourceType);

            resourceSet.SetReadOnly();
            return(resourceSet);
        }
        public OpenReflectionDataContext()
        {
            ResourceType ct = null;

            if (OpenReflectionDataContext <T> .ComplexTypeName != null)
            {
                ct = new ResourceType(
                    typeof(T),
                    ResourceTypeKind.ComplexType,
                    null,
                    OpenReflectionDataContext <T> .NamespaceNameValue,
                    ComplexTypeName,
                    false);
                AddResourceTypeProperties(ct, ct);
                ct.CanReflectOnInstanceType = false;

                this.rts.Add(ct);
            }

            ResourceType rt = new ResourceType(
                typeof(T),
                ResourceTypeKind.EntityType,
                null,
                OpenReflectionDataContext <T> .NamespaceNameValue,
                ResourceTypeNames[0],
                false);

            AddResourceTypeProperties(rt, ct);
            rt.IsOpenType = true;
            rt.CanReflectOnInstanceType = false;

            this.rts.Add(rt);

            rs = new ResourceSet("Values", rt);

            foreach (string rtName in ResourceTypeNames.Skip(1))
            {
                ResourceType derivedRT = new ResourceType(
                    typeof(T),
                    ResourceTypeKind.EntityType,
                    rt,
                    OpenReflectionDataContext <T> .NamespaceNameValue,
                    rtName,
                    false);
                this.rts.Add(derivedRT);
            }

            this.rts.Act(x => x.SetReadOnly());
            rs.SetReadOnly();

            this.currentToken   = 0;
            this.tokens         = new Dictionary <int, T>();
            this.pendingChanges = new List <KeyValuePair <object, EntityState> >();
        }
Esempio n. 5
0
        public void Initialize()
        {
            this.host = new DataServiceHost2Simulator();

            var context = new DataServiceOperationContext(this.host);

            this.dataServiceSimulator = new DataServiceSimulator {
                OperationContext = context
            };

            var providerSimulator = new DataServiceProviderSimulator();

            DataServiceStaticConfiguration staticConfiguration = new DataServiceStaticConfiguration(this.dataServiceSimulator.Instance.GetType(), providerSimulator);
            IDataServiceProviderBehavior   providerBehavior    = DataServiceProviderBehavior.CustomDataServiceProviderBehavior;

            var resourceType = new ResourceType(typeof(object), ResourceTypeKind.EntityType, null, "SelectTestNamespace", "Fake", false)
            {
                CanReflectOnInstanceType = false, IsOpenType = true
            };

            resourceType.AddProperty(new ResourceProperty("Id", ResourcePropertyKind.Key | ResourcePropertyKind.Primitive, ResourceType.GetPrimitiveResourceType(typeof(int)))
            {
                CanReflectOnInstanceTypeProperty = false
            });
            var resourceSet = new ResourceSet("FakeSet", resourceType);

            resourceSet.SetReadOnly();

            providerSimulator.AddResourceSet(resourceSet);

            var configuration = new DataServiceConfiguration(providerSimulator);

            configuration.SetEntitySetAccessRule("*", EntitySetRights.All);

            var provider = new DataServiceProviderWrapper(
                new DataServiceCacheItem(
                    configuration,
                    staticConfiguration),
                providerSimulator,
                providerSimulator,
                this.dataServiceSimulator,
                false);

            this.dataServiceSimulator.ProcessingPipeline = new DataServiceProcessingPipeline();
            this.dataServiceSimulator.Provider           = provider;
            provider.ProviderBehavior = providerBehavior;
            this.dataServiceSimulator.Configuration = new DataServiceConfiguration(providerSimulator);
            this.dataServiceSimulator.Configuration.DataServiceBehavior.MaxProtocolVersion = ODataProtocolVersion.V4;

            this.responseMessageSimulator = new ODataResponseMessageSimulator();
        }
        public void MetadataProviderModelShouldGoDirectlyToProviderWhenLookingUpAnEntitySetFromThePath()
        {
            var metadataProvider = new DataServiceProviderSimulator();
            var resourceSet      = new ResourceSet("Some.Really.Var1.Name.<>+-.Something", CreateResourceTypeWithKeyProperties("Id"));

            resourceSet.SetReadOnly();
            metadataProvider.AddResourceSet(resourceSet);
            var model = CreateMetadataProviderEdmModel(metadataProvider);

            var result = model.FindDeclaredEntitySet(resourceSet.Name);

            result.Should().NotBeNull();
            result.Name.Should().Be(resourceSet.Name);
        }
        private void PopulateTestMetadata()
        {
            ResourceType entityType = new ResourceType(typeof(TestEntityType), ResourceTypeKind.EntityType, null, "AstoriaUnitTests.Tests.Server", "TestEntityType", false);

            entityType.CanReflectOnInstanceType = true;
            entityType.AddProperty(new ResourceProperty("ID", ResourcePropertyKind.Key | ResourcePropertyKind.Primitive, ResourceType.GetPrimitiveResourceType(typeof(Int32))));
            entityType.AddProperty(new ResourceProperty("Name", ResourcePropertyKind.Primitive, ResourceType.GetPrimitiveResourceType(typeof(String))));
            entityType.AddProperty(new ResourceProperty("Spatial", ResourcePropertyKind.Primitive, ResourceType.GetPrimitiveResourceType(typeof(Geography))));
            entityType.AddProperty(new ResourceProperty("Point", ResourcePropertyKind.Primitive, ResourceType.GetPrimitiveResourceType(typeof(GeographyPoint))));
            entityType.SetReadOnly();

            serviceFactory.AddResourceType(entityType);

            ResourceSet entitySet = new ResourceSet("Entities", entityType);

            entitySet.SetReadOnly();
            serviceFactory.AddResourceSet(entitySet);
        }
Esempio n. 8
0
        public ExpandAndSelectParsingTests()
        {
            this.requestDescription = new RequestDescription(Microsoft.OData.Service.RequestTargetKind.Resource, RequestTargetSource.EntitySet, new Uri("http://fake.org/"));
            var resourceType = new ResourceType(typeof(object), ResourceTypeKind.EntityType, null, "Fake", "Type", false)
            {
                CanReflectOnInstanceType = false, IsOpenType = true
            };

            resourceType.AddProperty(new ResourceProperty("Id", ResourcePropertyKind.Key | ResourcePropertyKind.Primitive, ResourceType.GetPrimitiveResourceType(typeof(int)))
            {
                CanReflectOnInstanceTypeProperty = false
            });
            var resourceSet = new ResourceSet("FakeSet", resourceType);

            resourceSet.SetReadOnly();

            this.requestDescription.LastSegmentInfo.TargetResourceType = resourceType;
            this.requestDescription.LastSegmentInfo.TargetResourceSet  = ResourceSetWrapper.CreateForTests(resourceSet);
        }
        public void Init()
        {
            this.baseType = new ResourceType(typeof(object), ResourceTypeKind.EntityType, null, "Fake.NS", "BaseType", false)
            {
                CanReflectOnInstanceType = false
            };
            this.baseType.AddProperty(new ResourceProperty("Id", ResourcePropertyKind.Key | ResourcePropertyKind.Primitive, ResourceType.GetPrimitiveResourceType(typeof(int)))
            {
                CanReflectOnInstanceTypeProperty = false
            });
            this.baseType.SetReadOnly();

            this.entityType = new ResourceType(typeof(object), ResourceTypeKind.EntityType, this.baseType, "Fake.NS", "Type", false)
            {
                CanReflectOnInstanceType = false
            };
            this.entityType.SetReadOnly();

            this.derivedType = new ResourceType(typeof(object), ResourceTypeKind.EntityType, this.entityType, "Fake.NS", "DerivedType", false)
            {
                CanReflectOnInstanceType = false
            };
            this.derivedType.SetReadOnly();

            var resourceSet = new ResourceSet("Set", this.entityType);

            resourceSet.SetReadOnly();
            this.resourceSetWrapper = ResourceSetWrapper.CreateForTests(resourceSet);

            this.action = new ServiceAction("Fake", ResourceType.GetPrimitiveResourceType(typeof(int)), OperationParameterBindingKind.Sometimes, new[] { new ServiceActionParameter("p1", this.entityType) }, null);
            this.action.SetReadOnly();
            this.actionWrapper = new OperationWrapper(action);

            this.derivedAction = new ServiceAction("Fake", ResourceType.GetPrimitiveResourceType(typeof(int)), OperationParameterBindingKind.Sometimes, new[] { new ServiceActionParameter("p1", this.derivedType) }, null);
            this.derivedAction.SetReadOnly();
            this.derivedActionWrapper = new OperationWrapper(derivedAction);

            this.testSubject = new SelectedOperationsCache();
        }
Esempio n. 10
0
        private MessageWriterBuilder ForNormalRequest()
        {
            var requestDescription = new RequestDescription(RequestTargetKind.Resource, RequestTargetSource.EntitySet, new Uri("http://temp.org/"));

            var resourceType = new ResourceType(typeof(object), ResourceTypeKind.EntityType, null, "Fake", "Type", false)
            {
                CanReflectOnInstanceType = false, IsOpenType = true
            };

            resourceType.AddProperty(new ResourceProperty("Id", ResourcePropertyKind.Key | ResourcePropertyKind.Primitive, ResourceType.GetPrimitiveResourceType(typeof(int)))
            {
                CanReflectOnInstanceTypeProperty = false
            });
            var resourceSet = new ResourceSet("FakeSet", resourceType);

            resourceSet.SetReadOnly();

            requestDescription.LastSegmentInfo.TargetResourceType = resourceType;
            requestDescription.LastSegmentInfo.TargetResourceSet  = ResourceSetWrapper.CreateForTests(resourceSet);

            return(this.ForNormalRequest(requestDescription));
        }
Esempio n. 11
0
        public void DataServiceProviderWrapperShouldFailOnMultipleActionsWithSameNameAndBindingType()
        {
            var entityType = new ResourceType(typeof(object), ResourceTypeKind.EntityType, null, "Fake.NS", "Type", false)
            {
                CanReflectOnInstanceType = false
            };

            entityType.AddProperty(new ResourceProperty("Id", ResourcePropertyKind.Key | ResourcePropertyKind.Primitive, ResourceType.GetPrimitiveResourceType(typeof(int)))
            {
                CanReflectOnInstanceTypeProperty = false
            });
            entityType.SetReadOnly();

            var resourceSet = new ResourceSet("MyEntitySet", entityType);

            resourceSet.SetReadOnly();

            ResourceType stringType       = ResourceType.GetPrimitiveResourceType(typeof(string));
            var          duplicateAction1 = new ServiceAction("Duplicate", stringType, OperationParameterBindingKind.Always, new[] { new ServiceActionParameter("p1", entityType), new ServiceActionParameter("p2", stringType) }, null);

            duplicateAction1.SetReadOnly();
            var duplicateAction2 = new ServiceAction("Duplicate", ResourceType.GetPrimitiveResourceType(typeof(int)), OperationParameterBindingKind.Sometimes, new[] { new ServiceActionParameter("p1", entityType) }, null);

            duplicateAction2.SetReadOnly();

            var actionProvider = new TestActionProvider
            {
                GetServiceActionsCallback = ctx => new[] { duplicateAction1, duplicateAction2 }
            };

            var providerWrapper = CreateProviderWrapper(actionProvider, p => p.AddResourceSet(resourceSet));

            Action getVisibleOperations = () => providerWrapper.GetVisibleOperations().ToList();

            getVisibleOperations.ShouldThrow <DataServiceException>()
            .WithMessage(ErrorStrings.DataServiceActionProviderWrapper_DuplicateAction("Duplicate"))
            .And.StatusCode.Should().Be(500);
        }
Esempio n. 12
0
        public void GetTargetSetTestsSetBindingTypeShouldPerformCorrectValidation()
        {
            ResourceType actorEntityType = new ResourceType(typeof(object), ResourceTypeKind.EntityType, null, "foo", "Actor", false)
            {
                CanReflectOnInstanceType = false
            };
            ResourceProperty idProperty = new ResourceProperty("ID", ResourcePropertyKind.Primitive | ResourcePropertyKind.Key, ResourceType.GetPrimitiveResourceType(typeof(int)))
            {
                CanReflectOnInstanceTypeProperty = false
            };

            actorEntityType.AddProperty(idProperty);

            ResourceType addressComplexType = new ResourceType(typeof(object), ResourceTypeKind.ComplexType, null, "foo", "Address", false)
            {
                CanReflectOnInstanceType = false
            };

            addressComplexType.AddProperty(new ResourceProperty("StreetAddress", ResourcePropertyKind.Primitive, ResourceType.GetPrimitiveResourceType(typeof(string)))
            {
                CanReflectOnInstanceTypeProperty = false
            });
            addressComplexType.AddProperty(new ResourceProperty("ZipCode", ResourcePropertyKind.Primitive, ResourceType.GetPrimitiveResourceType(typeof(int)))
            {
                CanReflectOnInstanceTypeProperty = false
            });

            actorEntityType.AddProperty(new ResourceProperty("PrimaryAddress", ResourcePropertyKind.ComplexType, addressComplexType)
            {
                CanReflectOnInstanceTypeProperty = false
            });
            actorEntityType.AddProperty(new ResourceProperty("OtherAddresses", ResourcePropertyKind.Collection, ResourceType.GetCollectionResourceType(addressComplexType))
            {
                CanReflectOnInstanceTypeProperty = false
            });

            ResourceType movieEntityType = new ResourceType(typeof(object), ResourceTypeKind.EntityType, null, "foo", "Movie", false)
            {
                CanReflectOnInstanceType = false
            };

            movieEntityType.AddProperty(idProperty);

            ResourceProperty moviesNavProp = new ResourceProperty("Movies", ResourcePropertyKind.ResourceSetReference, movieEntityType)
            {
                CanReflectOnInstanceTypeProperty = false
            };

            actorEntityType.AddProperty(moviesNavProp);
            ResourceProperty actorsNavProp = new ResourceProperty("Actors", ResourcePropertyKind.ResourceSetReference, actorEntityType)
            {
                CanReflectOnInstanceTypeProperty = false
            };

            movieEntityType.AddProperty(actorsNavProp);

            ResourceType derivedActorEntityType = new ResourceType(typeof(object), ResourceTypeKind.EntityType, actorEntityType, "foo", "DerivedActor", false)
            {
                CanReflectOnInstanceType = false
            };
            ResourceType derivedMovieEntityType = new ResourceType(typeof(object), ResourceTypeKind.EntityType, movieEntityType, "foo", "DerivedMovie", false)
            {
                CanReflectOnInstanceType = false
            };

            actorEntityType.SetReadOnly();
            derivedActorEntityType.SetReadOnly();
            movieEntityType.SetReadOnly();
            derivedMovieEntityType.SetReadOnly();
            addressComplexType.SetReadOnly();
            DataServiceProviderSimulator providerSimulator = new DataServiceProviderSimulator();

            providerSimulator.AddResourceType(actorEntityType);
            providerSimulator.AddResourceType(derivedActorEntityType);
            providerSimulator.AddResourceType(movieEntityType);
            providerSimulator.AddResourceType(derivedMovieEntityType);
            providerSimulator.AddResourceType(addressComplexType);

            ResourceSet actorSet = new ResourceSet("Actors", actorEntityType);
            ResourceSet movieSet = new ResourceSet("Movies", movieEntityType);

            actorSet.SetReadOnly();
            movieSet.SetReadOnly();
            providerSimulator.AddResourceSet(actorSet);
            providerSimulator.AddResourceSet(movieSet);

            providerSimulator.AddResourceAssociationSet(new ResourceAssociationSet("Actors_Movies", new ResourceAssociationSetEnd(actorSet, actorEntityType, moviesNavProp), new ResourceAssociationSetEnd(movieSet, movieEntityType, actorsNavProp)));

            DataServiceConfiguration config = new DataServiceConfiguration(providerSimulator);

            config.SetEntitySetAccessRule("*", EntitySetRights.All);
            config.DataServiceBehavior.MaxProtocolVersion = ODataProtocolVersion.V4;

            var dataService = new DataServiceSimulator()
            {
                OperationContext = new DataServiceOperationContext(new DataServiceHostSimulator())
            };

            dataService.ProcessingPipeline = new DataServiceProcessingPipeline();
            DataServiceStaticConfiguration staticConfiguration = new DataServiceStaticConfiguration(dataService.Instance.GetType(), providerSimulator);
            IDataServiceProviderBehavior   providerBehavior    = DataServiceProviderBehavior.CustomDataServiceProviderBehavior;

            DataServiceProviderWrapper provider = new DataServiceProviderWrapper(
                new DataServiceCacheItem(
                    config,
                    staticConfiguration),
                providerSimulator,
                providerSimulator,
                dataService,
                false);

            dataService.Provider      = provider;
            provider.ProviderBehavior = providerBehavior;

            var testCases = new[]
            {
                new
                {
                    AppendParameterName = true,
                    Path         = "",
                    BindingSet   = ResourceSetWrapper.CreateResourceSetWrapper(actorSet, provider, set => set),
                    TargetSet    = ResourceSetWrapper.CreateResourceSetWrapper(actorSet, provider, set => set),
                    ErrorMessage = default(string)
                },
                new
                {
                    AppendParameterName = true,
                    Path         = "/Movies",
                    BindingSet   = ResourceSetWrapper.CreateResourceSetWrapper(actorSet, provider, set => set),
                    TargetSet    = ResourceSetWrapper.CreateResourceSetWrapper(movieSet, provider, set => set),
                    ErrorMessage = default(string)
                },
                new
                {
                    AppendParameterName = true,
                    Path         = "/Movies/Actors/Movies",
                    BindingSet   = ResourceSetWrapper.CreateResourceSetWrapper(actorSet, provider, set => set),
                    TargetSet    = ResourceSetWrapper.CreateResourceSetWrapper(movieSet, provider, set => set),
                    ErrorMessage = default(string)
                },
                new
                {
                    AppendParameterName = true,
                    Path         = "/foo.DerivedActor/Movies/foo.DerivedMovie/Actors/foo.DerivedActor/Movies",
                    BindingSet   = ResourceSetWrapper.CreateResourceSetWrapper(actorSet, provider, set => set),
                    TargetSet    = ResourceSetWrapper.CreateResourceSetWrapper(movieSet, provider, set => set),
                    ErrorMessage = default(string)
                },
                new
                {
                    AppendParameterName = true,
                    Path         = "/",
                    BindingSet   = ResourceSetWrapper.CreateResourceSetWrapper(actorSet, provider, set => set),
                    TargetSet    = default(ResourceSetWrapper),
                    ErrorMessage = "The path expression '{0}/' is not a valid expression because it contains an empty segment or it ends with '/'."
                },
                new
                {
                    AppendParameterName = true,
                    Path         = "/Movies/",
                    BindingSet   = ResourceSetWrapper.CreateResourceSetWrapper(actorSet, provider, set => set),
                    TargetSet    = default(ResourceSetWrapper),
                    ErrorMessage = "The path expression '{0}/Movies/' is not a valid expression because it contains an empty segment or it ends with '/'."
                },
                new
                {
                    AppendParameterName = true,
                    Path         = "/Movies//Actors",
                    BindingSet   = ResourceSetWrapper.CreateResourceSetWrapper(actorSet, provider, set => set),
                    TargetSet    = ResourceSetWrapper.CreateResourceSetWrapper(movieSet, provider, set => set),
                    ErrorMessage = "The path expression '{0}/Movies//Actors' is not a valid expression because it contains an empty segment or it ends with '/'."
                },
                new
                {
                    AppendParameterName = true,
                    Path         = "/foo.DerivedActor",
                    BindingSet   = ResourceSetWrapper.CreateResourceSetWrapper(actorSet, provider, set => set),
                    TargetSet    = default(ResourceSetWrapper),
                    ErrorMessage = "The path expression '{0}/foo.DerivedActor' is not a valid expression because it ends with the type identifier 'foo.DerivedActor'. A valid path expression must not end in a type identifier."
                },
                new
                {
                    AppendParameterName = true,
                    Path         = "/Movies/foo.DerivedMovie",
                    BindingSet   = ResourceSetWrapper.CreateResourceSetWrapper(actorSet, provider, set => set),
                    TargetSet    = default(ResourceSetWrapper),
                    ErrorMessage = "The path expression '{0}/Movies/foo.DerivedMovie' is not a valid expression because it ends with the type identifier 'foo.DerivedMovie'. A valid path expression must not end in a type identifier."
                },
                new
                {
                    AppendParameterName = true,
                    Path         = "/Foo",
                    BindingSet   = ResourceSetWrapper.CreateResourceSetWrapper(actorSet, provider, set => set),
                    TargetSet    = default(ResourceSetWrapper),
                    ErrorMessage = "The path expression '{0}/Foo' is not a valid expression because the segment 'Foo' is not a type identifier or a property on the resource type 'foo.Actor'."
                },
                new
                {
                    AppendParameterName = true,
                    Path         = "/ID",
                    BindingSet   = ResourceSetWrapper.CreateResourceSetWrapper(actorSet, provider, set => set),
                    TargetSet    = default(ResourceSetWrapper),
                    ErrorMessage = "The path expression '{0}/ID' is not a valid expression because the segment 'ID' is a property of type 'Edm.Int32'. A valid path expression must only contain properties of entity type."
                },
                new
                {
                    AppendParameterName = true,
                    Path         = "/OtherAddresses",
                    BindingSet   = ResourceSetWrapper.CreateResourceSetWrapper(actorSet, provider, set => set),
                    TargetSet    = default(ResourceSetWrapper),
                    ErrorMessage = "The path expression '{0}/OtherAddresses' is not a valid expression because the segment 'OtherAddresses' is a property of type 'Collection(foo.Address)'. A valid path expression must only contain properties of entity type."
                },
                new
                {
                    AppendParameterName = true,
                    Path         = "/Movies/Actors/PrimaryAddress",
                    BindingSet   = ResourceSetWrapper.CreateResourceSetWrapper(actorSet, provider, set => set),
                    TargetSet    = default(ResourceSetWrapper),
                    ErrorMessage = "The path expression '{0}/Movies/Actors/PrimaryAddress' is not a valid expression because the segment 'PrimaryAddress' is a property of type 'foo.Address'. A valid path expression must only contain properties of entity type."
                },
                new
                {
                    AppendParameterName = false,
                    Path         = "foo",
                    BindingSet   = ResourceSetWrapper.CreateResourceSetWrapper(actorSet, provider, set => set),
                    TargetSet    = default(ResourceSetWrapper),
                    ErrorMessage = "The path expression 'foo' is not a valid path expression. A valid path expression must start with the binding parameter name '{0}'."
                },
                new
                {
                    AppendParameterName = false,
                    Path         = "abc/pqr",
                    BindingSet   = ResourceSetWrapper.CreateResourceSetWrapper(actorSet, provider, set => set),
                    TargetSet    = default(ResourceSetWrapper),
                    ErrorMessage = "The path expression 'abc/pqr' is not a valid path expression. A valid path expression must start with the binding parameter name '{0}'."
                },
                new
                {
                    AppendParameterName = false,
                    Path         = "actorParameter",
                    BindingSet   = ResourceSetWrapper.CreateResourceSetWrapper(actorSet, provider, set => set),
                    TargetSet    = default(ResourceSetWrapper),
                    ErrorMessage = "The path expression 'actorParameter' is not a valid path expression. A valid path expression must start with the binding parameter name '{0}'."
                },
                new
                {
                    AppendParameterName = false,
                    Path         = "actorsParameter",
                    BindingSet   = ResourceSetWrapper.CreateResourceSetWrapper(actorSet, provider, set => set),
                    TargetSet    = default(ResourceSetWrapper),
                    ErrorMessage = "The path expression 'actorsParameter' is not a valid path expression. A valid path expression must start with the binding parameter name '{0}'."
                },
            };

            ServiceActionParameter actorParameter  = new ServiceActionParameter("actor", actorEntityType);
            ServiceActionParameter actorsParameter = new ServiceActionParameter("actors", ResourceType.GetEntityCollectionResourceType(actorEntityType));
            var parameters = new ServiceActionParameter[] { actorParameter, actorsParameter };

            foreach (var parameter in parameters)
            {
                foreach (var testCase in testCases)
                {
                    string pathString = testCase.AppendParameterName ? parameter.Name + testCase.Path : testCase.Path;
                    var    path       = new ResourceSetPathExpression(pathString);
                    Assert.AreEqual(pathString, path.PathExpression);
                    string expectedErrorMessage = testCase.ErrorMessage == null ? null : string.Format(testCase.ErrorMessage, parameter.Name);
                    try
                    {
                        path.SetBindingParameter(parameter);
                        path.InitializePathSegments(provider);
                        var targetSet = path.GetTargetSet(provider, testCase.BindingSet);
                        Assert.IsNull(expectedErrorMessage, "Expecting exception but received none.");
                        Assert.AreEqual(targetSet.Name, testCase.TargetSet.Name);
                    }
                    catch (InvalidOperationException e)
                    {
                        Assert.AreEqual(expectedErrorMessage, e.Message);
                    }
                }
            }
        }
Esempio n. 13
0
			void LoadMetadata()
			{
				var n = 0;
				var list =
				(
					from p in typeof(T).GetProperties()
					let t   = p.PropertyType
					where typeof(ITable<>).IsSameOrParentOf(t)
					let tt  = t.GetGenericArguments()[0]
					let tbl = new SqlTable(_mappingSchema, tt)
					where tbl.Fields.Any(f => f.IsPrimaryKey)
					let m   = _mappingSchema.GetEntityDescriptor(tt)
					select new
					{
						p.Name,
						ID     = n++,
						Type   = tt,
						Table  = tbl,
						Mapper = m
					}
				).ToList();

				var baseTypes = new Dictionary<Type,Type>();

				foreach (var item in list)
					foreach (var m in item.Mapper.InheritanceMapping)
						if (!baseTypes.ContainsKey(m.Type))
							baseTypes.Add(m.Type, item.Type);

				list.Sort((x,y) =>
				{
					if (baseTypes.TryGetValue(x.Type, out var baseType))
						if (y.Type == baseType)
							return 1;

					if (baseTypes.TryGetValue(y.Type, out baseType))
						if (x.Type == baseType)
							return -1;

					return x.ID - y.ID;
				});

				foreach (var item in list)
				{
					baseTypes.TryGetValue(item.Type, out var baseType);

					var type = GetTypeInfo(item.Type, baseType, item.Table, item.Mapper);
					var set  = new ResourceSet(item.Name, type.Type);

					set.SetReadOnly();

					Sets.Add(set.Name, set);
				}

				foreach (var item in list)
				{
					foreach (var m in item.Mapper.InheritanceMapping)
					{
						if (!TypeDic.ContainsKey(m.Type))
						{
							GetTypeInfo(
								m.Type,
								item.Type,
								new SqlTable(_mappingSchema, item.Type),
								_mappingSchema.GetEntityDescriptor(item.Type));
						}
					}
				}
			}
Esempio n. 14
0
        public DefaultContainer_Metadata()
        {
            // ComplexTypes declared here

            Microsoft.Test.OData.Framework.TestProviders.Common.LazyResourceType contactdetails_ComplexType = new Microsoft.Test.OData.Framework.TestProviders.Common.LazyResourceType(typeof(Microsoft.Test.OData.Framework.TestProviders.Dictionary.ResourceInstance), ResourceTypeKind.ComplexType, null, "Microsoft.Test.OData.Services.OpenTypesService", "ContactDetails", false);
            contactdetails_ComplexType.IsOpenType = false;
            contactdetails_ComplexType.CanReflectOnInstanceType = false;
            this.ResourceTypes.Add(contactdetails_ComplexType);

            // ComplexTypes properties declared

            ResourceProperty contactdetails_FirstContacted = new ResourceProperty("FirstContacted", ResourcePropertyKind.Primitive, ResourceType.GetPrimitiveResourceType(typeof(byte[])));

            contactdetails_FirstContacted.CanReflectOnInstanceTypeProperty = false;
            contactdetails_ComplexType.AddLazyProperty(contactdetails_FirstContacted);
            ResourceProperty contactdetails_LastContacted = new ResourceProperty("LastContacted", ResourcePropertyKind.Primitive, ResourceType.GetPrimitiveResourceType(typeof(System.DateTimeOffset)));

            contactdetails_LastContacted.CanReflectOnInstanceTypeProperty = false;
            contactdetails_ComplexType.AddLazyProperty(contactdetails_LastContacted);
            ResourceProperty contactdetails_Contacted = new ResourceProperty("Contacted", ResourcePropertyKind.Primitive, ResourceType.GetPrimitiveResourceType(typeof(System.DateTimeOffset)));

            contactdetails_Contacted.CanReflectOnInstanceTypeProperty = false;
            contactdetails_ComplexType.AddLazyProperty(contactdetails_Contacted);
            ResourceProperty contactdetails_GUID = new ResourceProperty("GUID", ResourcePropertyKind.Primitive, ResourceType.GetPrimitiveResourceType(typeof(System.Guid)));

            contactdetails_GUID.CanReflectOnInstanceTypeProperty = false;
            contactdetails_ComplexType.AddLazyProperty(contactdetails_GUID);
            ResourceProperty contactdetails_PreferedContactTime = new ResourceProperty("PreferedContactTime", ResourcePropertyKind.Primitive, ResourceType.GetPrimitiveResourceType(typeof(System.TimeSpan)));

            contactdetails_PreferedContactTime.CanReflectOnInstanceTypeProperty = false;
            contactdetails_ComplexType.AddLazyProperty(contactdetails_PreferedContactTime);
            ResourceProperty contactdetails_Byte = new ResourceProperty("Byte", ResourcePropertyKind.Primitive, ResourceType.GetPrimitiveResourceType(typeof(byte)));

            contactdetails_Byte.CanReflectOnInstanceTypeProperty = false;
            contactdetails_ComplexType.AddLazyProperty(contactdetails_Byte);
            ResourceProperty contactdetails_SignedByte = new ResourceProperty("SignedByte", ResourcePropertyKind.Primitive, ResourceType.GetPrimitiveResourceType(typeof(sbyte)));

            contactdetails_SignedByte.CanReflectOnInstanceTypeProperty = false;
            contactdetails_ComplexType.AddLazyProperty(contactdetails_SignedByte);
            ResourceProperty contactdetails_Double = new ResourceProperty("Double", ResourcePropertyKind.Primitive, ResourceType.GetPrimitiveResourceType(typeof(double)));

            contactdetails_Double.CanReflectOnInstanceTypeProperty = false;
            contactdetails_ComplexType.AddLazyProperty(contactdetails_Double);
            ResourceProperty contactdetails_Single = new ResourceProperty("Single", ResourcePropertyKind.Primitive, ResourceType.GetPrimitiveResourceType(typeof(float)));

            contactdetails_Single.CanReflectOnInstanceTypeProperty = false;
            contactdetails_ComplexType.AddLazyProperty(contactdetails_Single);
            ResourceProperty contactdetails_Short = new ResourceProperty("Short", ResourcePropertyKind.Primitive, ResourceType.GetPrimitiveResourceType(typeof(short)));

            contactdetails_Short.CanReflectOnInstanceTypeProperty = false;
            contactdetails_ComplexType.AddLazyProperty(contactdetails_Short);
            ResourceProperty contactdetails_Int = new ResourceProperty("Int", ResourcePropertyKind.Primitive, ResourceType.GetPrimitiveResourceType(typeof(int)));

            contactdetails_Int.CanReflectOnInstanceTypeProperty = false;
            contactdetails_ComplexType.AddLazyProperty(contactdetails_Int);
            ResourceProperty contactdetails_Long = new ResourceProperty("Long", ResourcePropertyKind.Primitive, ResourceType.GetPrimitiveResourceType(typeof(long)));

            contactdetails_Long.CanReflectOnInstanceTypeProperty = false;
            contactdetails_ComplexType.AddLazyProperty(contactdetails_Long);

            // EntityTypes declared here

            Microsoft.Test.OData.Framework.TestProviders.Common.LazyResourceType row_EntityType = new Microsoft.Test.OData.Framework.TestProviders.Common.LazyResourceType(typeof(Microsoft.Test.OData.Framework.TestProviders.Dictionary.ResourceInstance), ResourceTypeKind.EntityType, null, "Microsoft.Test.OData.Services.OpenTypesService", "Row", false);
            row_EntityType.IsOpenType = true;
            row_EntityType.CanReflectOnInstanceType = false;
            this.ResourceTypes.Add(row_EntityType);

            Microsoft.Test.OData.Framework.TestProviders.Common.LazyResourceType indexedrow_EntityType = new Microsoft.Test.OData.Framework.TestProviders.Common.LazyResourceType(typeof(Microsoft.Test.OData.Framework.TestProviders.Dictionary.ResourceInstance), ResourceTypeKind.EntityType, row_EntityType, "Microsoft.Test.OData.Services.OpenTypesService", "IndexedRow", false);
            indexedrow_EntityType.IsOpenType = true;
            indexedrow_EntityType.CanReflectOnInstanceType = false;
            this.ResourceTypes.Add(indexedrow_EntityType);

            Microsoft.Test.OData.Framework.TestProviders.Common.LazyResourceType rowindex_EntityType = new Microsoft.Test.OData.Framework.TestProviders.Common.LazyResourceType(typeof(Microsoft.Test.OData.Framework.TestProviders.Dictionary.ResourceInstance), ResourceTypeKind.EntityType, null, "Microsoft.Test.OData.Services.OpenTypesService", "RowIndex", false);
            rowindex_EntityType.IsOpenType = true;
            rowindex_EntityType.CanReflectOnInstanceType = false;
            this.ResourceTypes.Add(rowindex_EntityType);

            // EntityType primitive and complexType properties declared here

            ResourceProperty row_Id = new ResourceProperty("Id", (ResourcePropertyKind.Primitive | ResourcePropertyKind.Key), ResourceType.GetPrimitiveResourceType(typeof(System.Guid)));

            row_Id.CanReflectOnInstanceTypeProperty = false;
            row_EntityType.AddLazyProperty(row_Id);


            ResourceProperty rowindex_Id = new ResourceProperty("Id", (ResourcePropertyKind.Primitive | ResourcePropertyKind.Key), ResourceType.GetPrimitiveResourceType(typeof(int)));

            rowindex_Id.CanReflectOnInstanceTypeProperty = false;
            rowindex_EntityType.AddLazyProperty(rowindex_Id);

            // EntityTypes navigation properties declared here



            ResourceProperty rowindex_Rows = new ResourceProperty("Rows", ResourcePropertyKind.ResourceSetReference, indexedrow_EntityType);

            rowindex_Rows.CanReflectOnInstanceTypeProperty = false;
            rowindex_EntityType.AddLazyProperty(rowindex_Rows);
            // Add EntitySet Declarations

            var row_EntitySet = new ResourceSet("Row", row_EntityType);

            this.ResourceSets.Add(row_EntitySet);

            var rowindex_EntitySet = new ResourceSet("RowIndex", rowindex_EntityType);

            this.ResourceSets.Add(rowindex_EntitySet);

            // Add AssociationSet Declarations

            Microsoft.OData.Service.Providers.ResourceAssociationSet index_rows_AssociationSet = new ResourceAssociationSet("Index_Rows", new ResourceAssociationSetEnd(rowindex_EntitySet, rowindex_EntityType, rowindex_Rows), new ResourceAssociationSetEnd(row_EntitySet, indexedrow_EntityType, null));
            this.ResourceAssociationSets.Add(index_rows_AssociationSet);
            // ServiceOperations declared here

            row_EntitySet.SetReadOnly();
            rowindex_EntitySet.SetReadOnly();

            row_EntityType.SetReadOnly();
            indexedrow_EntityType.SetReadOnly();
            rowindex_EntityType.SetReadOnly();

            contactdetails_ComplexType.SetReadOnly();
        }
Esempio n. 15
0
        private void InitWithQueryOptions(string selectQueryOption, string expandQueryOption)
        {
            string queryOption = string.Empty;

            if (selectQueryOption != null && expandQueryOption != null)
            {
                queryOption = "?$select=" + selectQueryOption + "&$expand=" + expandQueryOption;
            }
            else if (selectQueryOption != null)
            {
                queryOption = "?$select=" + selectQueryOption;
            }
            else
            {
                queryOption = "?$expand=" + expandQueryOption;
            }

            this.host = new DataServiceHostSimulator
            {
                AbsoluteRequestUri = new Uri("http://fake.org/FakeSet" + queryOption),
                AbsoluteServiceUri = new Uri("http://fake.org/"),
                RequestHttpMethod  = "GET",
                RequestVersion     = "2.0",
            };

            if (selectQueryOption != null)
            {
                this.host.SetQueryStringItem("$select", selectQueryOption);
            }

            if (expandQueryOption != null)
            {
                this.host.SetQueryStringItem("$expand", expandQueryOption);
            }

            DataServiceProviderSimulator provider = new DataServiceProviderSimulator
            {
                ContainerName      = "SelectTestContainer",
                ContainerNamespace = "SelectTestNamespace"
            };

            var resourceType = new ResourceType(typeof(object), ResourceTypeKind.EntityType, null, "SelectTestNamespace", "Fake", false)
            {
                CanReflectOnInstanceType = false, IsOpenType = true
            };

            resourceType.AddProperty(new ResourceProperty("Id", ResourcePropertyKind.Key | ResourcePropertyKind.Primitive, ResourceType.GetPrimitiveResourceType(typeof(int)))
            {
                CanReflectOnInstanceTypeProperty = false
            });
            var resourceSet = new ResourceSet("FakeSet", resourceType);

            resourceSet.SetReadOnly();

            provider.AddResourceSet(resourceSet);

            DataServiceConfiguration configuration = new DataServiceConfiguration(provider);

            configuration.SetEntitySetAccessRule("*", EntitySetRights.All);
            this.service = new DataServiceSimulator
            {
                OperationContext = new DataServiceOperationContext(this.host),
                Configuration    = configuration,
            };

            DataServiceStaticConfiguration staticConfiguration = new DataServiceStaticConfiguration(this.service.Instance.GetType(), provider);
            IDataServiceProviderBehavior   providerBehavior    = DataServiceProviderBehavior.CustomDataServiceProviderBehavior;

            this.service.Provider = new DataServiceProviderWrapper(
                new DataServiceCacheItem(
                    configuration,
                    staticConfiguration),
                provider,
                provider,
                this.service,
                false);

            this.service.ProcessingPipeline        = new DataServiceProcessingPipeline();
            this.service.Provider.ProviderBehavior = providerBehavior;
            this.service.ActionProvider            = DataServiceActionProviderWrapper.Create(this.service);
            this.service.OperationContext.InitializeAndCacheHeaders(service);
        }
Esempio n. 16
0
        private static AnswerFileDataSource DefineAnswerFileDataSource(string dataSourceId, string templateLocator, string templateName)
        {
            Debug.Assert(s_readerWriterLock.IsWriteLockHeld);

            try
            {
                Template         template         = Template.Locate(templateLocator);
                TemplateManifest templateManifest = template.GetManifest(ManifestParseFlags.ParseDataSources);

                DataSource dataSource = templateManifest.DataSources.Single(ds => string.CompareOrdinal(ds.Id, dataSourceId) == 0);

                string dataSourcePath = Path.Combine(template.Location.GetTemplateDirectory(), dataSource.Name);
                AnswerFileDataSource answerFileDataSource = new AnswerFileDataSource(dataSourceId, dataSource.Name, dataSourcePath);

                // Create the metadata for the answer file data source.
                ResourceType resourceType = s_metadata.AddEntityType(answerFileDataSource.ResourceId);

                // All resources need a key property.
                s_metadata.AddKeyProperty(resourceType, "ID", typeof(int));
                answerFileDataSource.PropertyNameToSourceNameMap["ID"] = "ID";

                Type propType;
                foreach (var dataSourceField in dataSource.Fields)
                {
                    switch (dataSourceField.FieldType)
                    {
                    case DataSourceFieldType.Text:
                        propType = typeof(string);
                        break;

                    case DataSourceFieldType.Number:
                        propType = typeof(double?);
                        break;

                    case DataSourceFieldType.Date:
                        propType = typeof(DateTime?);
                        break;

                    case DataSourceFieldType.TrueFalse:
                        propType = typeof(bool?);
                        break;

                    default:
                        propType = null;
                        break;
                    }

                    if (propType != null)
                    {
                        string sourceFieldName = dataSourceField.SourceName;
                        string propertyName    = "Prop" + MakeValidIdent(sourceFieldName);
                        s_metadata.AddPrimitiveProperty(resourceType, propertyName, propType);
                        answerFileDataSource.PropertyNameToSourceNameMap[propertyName] = sourceFieldName;
                    }
                }
                resourceType.SetReadOnly();

                // Create a resource set for the resource type.
                ResourceSet resourceSet = s_metadata.AddResourceSet(answerFileDataSource.ResourceId, resourceType);
                resourceSet.SetReadOnly();

                return(answerFileDataSource);
            }
            catch (Exception e)
            {
                throw new DataServiceException(string.Format("Failed to read the metadata for the data source Id '{0}' from the template manifest file for the " +
                                                             "template '{1}'.", dataSourceId, templateName), e);
            }
        }
Esempio n. 17
0
 public void AddResourceSet(ResourceSet set)
 {
     set.SetReadOnly();
     resourceSets.Add(set.Name, set);
 }
Esempio n. 18
0
        private static DataServiceProviderWrapper CreateProvider(out DataServiceConfiguration config, out DataServiceOperationContext operationContext)
        {
            var baseUri = new Uri("http://localhost");
            var host    = new DataServiceHostSimulator()
            {
                AbsoluteServiceUri = baseUri,
                AbsoluteRequestUri = new Uri(baseUri.AbsoluteUri + "/$metadata", UriKind.Absolute),
                RequestHttpMethod  = "GET",
                RequestAccept      = "application/xml+atom",
                RequestVersion     = "4.0",
                RequestMaxVersion  = "4.0",
            };

            operationContext = new DataServiceOperationContext(host);
            var dataService = new DataServiceSimulator()
            {
                OperationContext = operationContext
            };

            operationContext.InitializeAndCacheHeaders(dataService);

            DataServiceProviderSimulator providerSimulator = new DataServiceProviderSimulator();

            providerSimulator.ContainerNamespace = "MyModel";
            providerSimulator.ContainerName      = "CustomersContainer";

            ResourceType customerEntityType = new ResourceType(
                typeof(object), ResourceTypeKind.EntityType, null, "MyModel", "Customer", false)
            {
                CanReflectOnInstanceType = false
            };

            ResourcePropertyKind idPropertyKind = ResourcePropertyKind.Primitive | ResourcePropertyKind.Key;
            ResourceProperty     idProperty     = new ResourceProperty(
                "Id", idPropertyKind, ResourceType.GetPrimitiveResourceType(typeof(int)))
            {
                CanReflectOnInstanceTypeProperty = false
            };

            customerEntityType.AddProperty(idProperty);

            ResourcePropertyKind firstNamePropertyKind = ResourcePropertyKind.Primitive | ResourcePropertyKind.Key;
            ResourceProperty     firstNameProperty     = new ResourceProperty(
                "FirstName", firstNamePropertyKind, ResourceType.GetPrimitiveResourceType(typeof(string)))
            {
                CanReflectOnInstanceTypeProperty = false
            };

            customerEntityType.AddProperty(firstNameProperty);

            customerEntityType.SetReadOnly();
            providerSimulator.AddResourceType(customerEntityType);

            ResourceSet customerSet = new ResourceSet("Customers", customerEntityType);

            customerSet.SetReadOnly();
            providerSimulator.AddResourceSet(customerSet);

            config = new DataServiceConfiguration(providerSimulator);
            config.SetEntitySetAccessRule("*", EntitySetRights.All);
            config.DataServiceBehavior.MaxProtocolVersion = ODataProtocolVersion.V4;

            IDataServiceProviderBehavior   providerBehavior = DataServiceProviderBehavior.CustomDataServiceProviderBehavior;
            DataServiceStaticConfiguration staticConfig     = new DataServiceStaticConfiguration(dataService.Instance.GetType(), providerSimulator);

            DataServiceProviderWrapper provider = new DataServiceProviderWrapper(
                new DataServiceCacheItem(config, staticConfig), providerSimulator, providerSimulator, dataService, false);

            dataService.ProcessingPipeline = new DataServiceProcessingPipeline();
            dataService.Provider           = provider;
            provider.ProviderBehavior      = providerBehavior;
            dataService.ActionProvider     = DataServiceActionProviderWrapper.Create(dataService);
#if DEBUG
            dataService.ProcessingPipeline.SkipDebugAssert = true;
#endif
            operationContext.RequestMessage.InitializeRequestVersionHeaders(VersionUtil.ToVersion(config.DataServiceBehavior.MaxProtocolVersion));
            return(provider);
        }
Esempio n. 19
0
        public NodeToExpressionTranslatorTests()
        {
            this.functionExpressionBinder = new FunctionExpressionBinder(t => { throw new Exception(); });

            this.customerResourceType = new ResourceType(typeof(Customer), ResourceTypeKind.EntityType, null, "Fake", "Customer", false)
            {
                IsOpenType = true
            };
            var derivedCustomerResourceType = new ResourceType(typeof(DerivedCustomer), ResourceTypeKind.EntityType, this.customerResourceType, "Fake", "DerivedCustomer", false);

            this.weaklyBackedDerivedType = new ResourceType(typeof(object), ResourceTypeKind.EntityType, derivedCustomerResourceType, "Fake", "WeaklyBackedCustomer", false)
            {
                CanReflectOnInstanceType = false
            };
            var nameResourceProperty = new ResourceProperty("Name", ResourcePropertyKind.Key | ResourcePropertyKind.Primitive, ResourceType.GetPrimitiveResourceType(typeof(string)));

            this.customerResourceType.AddProperty(nameResourceProperty);
            var addressResourceType     = new ResourceType(typeof(Address), ResourceTypeKind.ComplexType, null, "Fake", "Address", false);
            var addressResourceProperty = new ResourceProperty("Address", ResourcePropertyKind.ComplexType, addressResourceType);

            this.customerResourceType.AddProperty(addressResourceProperty);

            var namesResourceProperty = new ResourceProperty("Names", ResourcePropertyKind.Collection, ResourceType.GetCollectionResourceType(ResourceType.GetPrimitiveResourceType(typeof(string))));

            this.customerResourceType.AddProperty(namesResourceProperty);
            var addressesResourceProperty = new ResourceProperty("Addresses", ResourcePropertyKind.Collection, ResourceType.GetCollectionResourceType(addressResourceType));

            this.customerResourceType.AddProperty(addressesResourceProperty);

            var bestFriendResourceProperty = new ResourceProperty("BestFriend", ResourcePropertyKind.ResourceReference, this.customerResourceType);

            this.customerResourceType.AddProperty(bestFriendResourceProperty);
            var otherFriendsResourceProperty = new ResourceProperty("OtherFriends", ResourcePropertyKind.ResourceSetReference, this.customerResourceType);

            this.customerResourceType.AddProperty(otherFriendsResourceProperty);

            this.weaklyBackedResourceProperty = new ResourceProperty("WeaklyBacked", ResourcePropertyKind.Primitive, ResourceType.GetPrimitiveResourceType(typeof(string)))
            {
                CanReflectOnInstanceTypeProperty = false
            };

            var guid1ResourceProperty         = new ResourceProperty("Guid1", ResourcePropertyKind.Primitive, ResourceType.GetPrimitiveResourceType(typeof(Guid)));
            var guid2ResourceProperty         = new ResourceProperty("Guid2", ResourcePropertyKind.Primitive, ResourceType.GetPrimitiveResourceType(typeof(Guid)));
            var nullableGuid1ResourceProperty = new ResourceProperty("NullableGuid1", ResourcePropertyKind.Primitive, ResourceType.GetPrimitiveResourceType(typeof(Guid?)));
            var nullableGuid2ResourceProperty = new ResourceProperty("NullableGuid2", ResourcePropertyKind.Primitive, ResourceType.GetPrimitiveResourceType(typeof(Guid?)));

            this.customerResourceType.AddProperty(guid1ResourceProperty);
            this.customerResourceType.AddProperty(guid2ResourceProperty);
            this.customerResourceType.AddProperty(nullableGuid1ResourceProperty);
            this.customerResourceType.AddProperty(nullableGuid2ResourceProperty);

            var resourceSet = new ResourceSet("Customers", this.customerResourceType);

            resourceSet.SetReadOnly();
            var resourceSetWrapper = ResourceSetWrapper.CreateForTests(resourceSet, EntitySetRights.All);

            this.model = new EdmModel();

            this.customerEdmType = new MetadataProviderEdmEntityType("Fake", this.customerResourceType, null, false, true, false, t => {});
            this.model.AddElement(this.customerEdmType);

            this.derivedCustomerEdmType = new MetadataProviderEdmEntityType("Fake", derivedCustomerResourceType, this.customerEdmType, false, false, false, t => { });
            this.model.AddElement(this.derivedCustomerEdmType);

            this.weaklyBackedCustomerEdmType = new MetadataProviderEdmEntityType("Fake", weaklyBackedDerivedType, this.derivedCustomerEdmType, false, false, false, t => { });
            this.model.AddElement(this.weaklyBackedCustomerEdmType);

            this.nameProperty = new MetadataProviderEdmStructuralProperty(this.customerEdmType, nameResourceProperty, EdmCoreModel.Instance.GetString(true), null, EdmConcurrencyMode.None);
            this.customerEdmType.AddProperty(this.nameProperty);

            var addressEdmType = new MetadataProviderEdmComplexType("Fake", addressResourceType, null, false, false, t => {});

            this.model.AddElement(addressEdmType);

            this.addressProperty = new MetadataProviderEdmStructuralProperty(this.customerEdmType, addressResourceProperty, new EdmComplexTypeReference(addressEdmType, true), null, EdmConcurrencyMode.None);
            this.customerEdmType.AddProperty(this.addressProperty);

            this.namesProperty = new MetadataProviderEdmStructuralProperty(this.customerEdmType, namesResourceProperty, new EdmCollectionTypeReference(new EdmCollectionType(EdmCoreModel.Instance.GetString(false))), null, EdmConcurrencyMode.None);
            this.customerEdmType.AddProperty(this.namesProperty);

            this.addressesProperty = new MetadataProviderEdmStructuralProperty(this.customerEdmType, addressesResourceProperty, new EdmCollectionTypeReference(new EdmCollectionType(new EdmComplexTypeReference(addressEdmType, false))), null, EdmConcurrencyMode.None);
            this.customerEdmType.AddProperty(this.addressesProperty);

            this.bestFriendNavigation = new MetadataProviderEdmNavigationProperty(this.customerEdmType, bestFriendResourceProperty, new EdmEntityTypeReference(this.customerEdmType, true));
            this.customerEdmType.AddProperty(this.bestFriendNavigation);

            this.otherFriendsNavigation = new MetadataProviderEdmNavigationProperty(this.customerEdmType, otherFriendsResourceProperty, new EdmCollectionTypeReference(new EdmCollectionType(new EdmEntityTypeReference(this.customerEdmType, true))));
            this.customerEdmType.AddProperty(this.otherFriendsNavigation);

            this.weaklyBackedProperty = new MetadataProviderEdmStructuralProperty(this.customerEdmType, this.weaklyBackedResourceProperty, EdmCoreModel.Instance.GetString(true), null, EdmConcurrencyMode.None);
            this.customerEdmType.AddProperty(this.weaklyBackedProperty);

            var guid1EdmProperty = new MetadataProviderEdmStructuralProperty(this.customerEdmType, guid1ResourceProperty, EdmCoreModel.Instance.GetGuid(false), null, EdmConcurrencyMode.None);

            this.customerEdmType.AddProperty(guid1EdmProperty);
            var guid2EdmProperty = new MetadataProviderEdmStructuralProperty(this.customerEdmType, guid2ResourceProperty, EdmCoreModel.Instance.GetGuid(false), null, EdmConcurrencyMode.None);

            this.customerEdmType.AddProperty(guid2EdmProperty);
            var nullableGuid1EdmProperty = new MetadataProviderEdmStructuralProperty(this.customerEdmType, nullableGuid1ResourceProperty, EdmCoreModel.Instance.GetGuid(true), null, EdmConcurrencyMode.None);

            this.customerEdmType.AddProperty(nullableGuid1EdmProperty);
            var nullableGuid2EdmProperty = new MetadataProviderEdmStructuralProperty(this.customerEdmType, nullableGuid2ResourceProperty, EdmCoreModel.Instance.GetGuid(true), null, EdmConcurrencyMode.None);

            this.customerEdmType.AddProperty(nullableGuid2EdmProperty);

            this.entitySet = new EdmEntitySetWithResourceSet(new EdmEntityContainer("Fake", "Container"), resourceSetWrapper, this.customerEdmType);
            ((EdmEntitySet)this.entitySet).AddNavigationTarget(this.bestFriendNavigation, this.entitySet);
            ((EdmEntitySet)this.entitySet).AddNavigationTarget(this.otherFriendsNavigation, this.entitySet);

            this.model.SetAnnotationValue(this.customerEdmType, this.customerResourceType);
            this.model.SetAnnotationValue(this.derivedCustomerEdmType, derivedCustomerResourceType);
            this.model.SetAnnotationValue(this.weaklyBackedCustomerEdmType, this.weaklyBackedDerivedType);
            this.model.SetAnnotationValue(this.nameProperty, nameResourceProperty);
            this.model.SetAnnotationValue(addressEdmType, addressResourceType);
            this.model.SetAnnotationValue(this.addressProperty, addressResourceProperty);
            this.model.SetAnnotationValue(this.namesProperty, namesResourceProperty);
            this.model.SetAnnotationValue(this.addressesProperty, addressesResourceProperty);
            this.model.SetAnnotationValue(this.bestFriendNavigation, bestFriendResourceProperty);
            this.model.SetAnnotationValue(this.otherFriendsNavigation, otherFriendsResourceProperty);
            this.model.SetAnnotationValue(this.weaklyBackedProperty, this.weaklyBackedResourceProperty);
            this.model.SetAnnotationValue(this.entitySet, resourceSetWrapper);
            this.model.SetAnnotationValue(guid1EdmProperty, guid1ResourceProperty);
            this.model.SetAnnotationValue(guid2EdmProperty, guid2ResourceProperty);
            this.model.SetAnnotationValue(nullableGuid1EdmProperty, nullableGuid1ResourceProperty);
            this.model.SetAnnotationValue(nullableGuid2EdmProperty, nullableGuid2ResourceProperty);

            this.testSubject = this.CreateTestSubject();
        }
Esempio n. 20
0
            void LoadMetadata()
            {
                var n    = 0;
                var list =
                    (
                        from p in typeof(T).GetProperties()
                        let t = p.PropertyType
                                where t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Table <>)
                                let tt = t.GetGenericArguments()[0]
                                         let tbl = new SqlTable(_mappingSchema, tt)
                                                   where tbl.Fields.Values.Any(f => f.IsPrimaryKey)
                                                   let m = _mappingSchema.GetObjectMapper(tt)
                                                           select new
                {
                    p.Name,
                    ID = n++,
                    Type = tt,
                    Table = tbl,
                    Mapper = m
                }
                    ).ToList();

                var baseTypes = new Dictionary <Type, Type>();

                foreach (var item in list)
                {
                    foreach (var m in item.Mapper.InheritanceMapping)
                    {
                        if (!baseTypes.ContainsKey(m.Type))
                        {
                            baseTypes.Add(m.Type, item.Type);
                        }
                    }
                }

                list.Sort((x, y) =>
                {
                    Type baseType;

                    if (baseTypes.TryGetValue(x.Type, out baseType))
                    {
                        if (y.Type == baseType)
                        {
                            return(1);
                        }
                    }

                    if (baseTypes.TryGetValue(y.Type, out baseType))
                    {
                        if (x.Type == baseType)
                        {
                            return(-1);
                        }
                    }

                    return(x.ID - y.ID);
                });

                foreach (var item in list)
                {
                    Type baseType;
                    baseTypes.TryGetValue(item.Type, out baseType);

                    var type = GetTypeInfo(item.Type, baseType, item.Table, item.Mapper);
                    var set  = new ResourceSet(item.Name, type.Type);

                    set.SetReadOnly();

                    Sets.Add(set.Name, set);
                }

                foreach (var item in list)
                {
                    foreach (var m in item.Mapper.InheritanceMapping)
                    {
                        if (!TypeDic.ContainsKey(m.Type))
                        {
                            GetTypeInfo(
                                m.Type,
                                item.Type,
                                new SqlTable(_mappingSchema, item.Type),
                                _mappingSchema.GetObjectMapper(item.Type));
                        }
                    }
                }
            }