Ejemplo n.º 1
0
        private static IEdmModel CreateModelForFullMetadata(bool sameLinksForIdAndEdit, bool sameLinksForEditAndRead)
        {
            ODataModelBuilder builder = ODataModelBuilderMocks.GetModelBuilderMock <ODataModelBuilder>();

            EntitySetConfiguration <MainEntity> mainSet = builder.EntitySet <MainEntity>("MainEntity");

            Func <EntityInstanceContext <MainEntity>, Uri> idLinkFactory = (e) =>
                                                                           CreateAbsoluteUri("/MainEntity/id/" + e.GetPropertyValue("Id").ToString());

            mainSet.HasIdLink(idLinkFactory, followsConventions: true);

            if (!sameLinksForIdAndEdit)
            {
                Func <EntityInstanceContext <MainEntity>, Uri> editLinkFactory =
                    (e) => CreateAbsoluteUri("/MainEntity/edit/" + e.GetPropertyValue("Id").ToString());
                mainSet.HasEditLink(editLinkFactory, followsConventions: false);
            }

            if (!sameLinksForEditAndRead)
            {
                Func <EntityInstanceContext <MainEntity>, Uri> readLinkFactory =
                    (e) => CreateAbsoluteUri("/MainEntity/read/" + e.GetPropertyValue("Id").ToString());
                mainSet.HasReadLink(readLinkFactory, followsConventions: false);
            }

            EntityTypeConfiguration <MainEntity> main = mainSet.EntityType;

            main.HasKey <int>((e) => e.Id);
            main.Property <short>((e) => e.Int16);
            NavigationPropertyConfiguration mainToRelated = mainSet.EntityType.HasRequired((e) => e.Related);

            main.Action("DoAlways").ReturnsCollectionFromEntitySet <MainEntity>("MainEntity").HasActionLink((c) =>
                                                                                                            CreateAbsoluteUri("/MainEntity/DoAlways/" + c.GetPropertyValue("Id")),
                                                                                                            followsConventions: false);
            main.Action("DoSometimes").ReturnsCollectionFromEntitySet <MainEntity>(
                "MainEntity").HasActionLink((c) =>
                                            CreateAbsoluteUri("/MainEntity/DoSometimes/" + c.GetPropertyValue("Id")),
                                            followsConventions: false);

            main.Function("IsAlways").ReturnsCollectionFromEntitySet <MainEntity>("MainEntity").HasFunctionLink(c =>
                                                                                                                CreateAbsoluteUri(String.Format(
                                                                                                                                      "/MainEntity({0})/Default.IsAlways()", c.GetPropertyValue("Id"))),
                                                                                                                followsConventions: false);

            // action and function bound to collection
            main.Collection.Action("DoAllAction")
            .HasFeedActionLink(c => CreateAbsoluteUri("/MainEntity/Default.DoAllAction"), followsConventions: false);

            main.Collection.Function("DoAllFunction").Returns <int>()
            .HasFeedFunctionLink(
                c => CreateAbsoluteUri("/MainEntity/Default.DoAllFunction()"),
                followsConventions: false);

            mainSet.HasNavigationPropertyLink(mainToRelated, (c, p) => new Uri("/MainEntity/RelatedEntity/" +
                                                                               c.GetPropertyValue("Id"), UriKind.Relative), followsConventions: true);

            EntitySetConfiguration <RelatedEntity> related = builder.EntitySet <RelatedEntity>("RelatedEntity");

            return(builder.GetEdmModel());
        }
Ejemplo n.º 2
0
        public static EntitySetConfiguration <TEntityType> HasEditLinkForKey <TEntityType, TIdType>(
            this EntitySetConfiguration <TEntityType> entitySetConfiguration,
            bool followsConventions, Expression <Func <TEntityType, TIdType> > property)
            where TEntityType : class
        {
            if (property == null)
            {
                throw new ArgumentNullException("property");
            }

            var propertyResumedInfo = property.GetPropertyResumedInfo();

            entitySetConfiguration.HasEditLink(
                ctx =>
            {
                object value;
                ctx.EdmObject.TryGetPropertyValue(propertyResumedInfo.Item1, out value);
                var path =
                    propertyResumedInfo.Item2
                            ? "'" + value + "'"
                            : value.ToString();

                var oDataLink =
                    ctx.Url.CreateODataLink(
                        new EntitySetPathSegment(typeof(TEntityType).Name),
                        new KeyValuePathSegment(path));
                return(new Uri(oDataLink));
            }, followsConventions);

            return(entitySetConfiguration);
        }
Ejemplo n.º 3
0
        public static EntitySetConfiguration <TEntityType> HasEditLinkForKey <TEntityType>(
            this EntitySetConfiguration <TEntityType> entitySetConfiguration,
            bool followsConventions,
            Expression <Func <TEntityType, object> > property01, Expression <Func <TEntityType, object> > property02,
            params Expression <Func <TEntityType, object> >[] properties)
            where TEntityType : class
        {
            if (property01 == null)
            {
                throw new ArgumentNullException("property01");
            }
            if (property02 == null)
            {
                throw new ArgumentNullException("property02");
            }

            var propertyList =
                new[]
            {
                property01.GetPropertyResumedInfo(),
                property02.GetPropertyResumedInfo()
            };

            if (properties != null && properties.Length > 0)
            {
                propertyList = propertyList.Concat(
                    properties.Select(p => p.GetPropertyResumedInfo())).ToArray();
            }

            entitySetConfiguration.HasEditLink(
                ctx =>
            {
                object value;
                ctx.EdmObject.TryGetPropertyValue(propertyList[0].Item1, out value);
                var sb = new StringBuilder(
                    propertyList[0].Item2
                            ? propertyList[0].Item1 + "='" + value + "'"
                            : propertyList[0].Item1 + "=" + value);
                for (var i = 1; i < propertyList.Length; i++)
                {
                    ctx.EdmObject.TryGetPropertyValue(propertyList[i].Item1, out value);
                    sb.Append(
                        propertyList[i].Item2
                                ? "," + propertyList[i].Item1 + "='" + value + "'"
                                : "," + propertyList[i].Item1 + "=" + value);
                }

                var oDataLink =
                    ctx.Url.CreateODataLink(
                        new EntitySetPathSegment(typeof(TEntityType).Name),
                        new KeyValuePathSegment(sb.ToString()));
                return(new Uri(oDataLink));
            }, followsConventions);

            return(entitySetConfiguration);
        }
        public void Apply(EntitySetConfiguration configuration, ODataModelBuilder model)
        {
            if (configuration == null)
            {
                throw Error.ArgumentNull("configuration");
            }

            // Configure the self link for the feed
            if (configuration.GetFeedSelfLink() == null)
            {
                configuration.HasFeedSelfLink(entitySetContext =>
                {
                    string routeName = ODataRouteNames.Default;
                    string selfLink = entitySetContext.UrlHelper.Link(
                        routeName,
                        new
                        {
                            controller = configuration.Name
                        });
                    if (selfLink == null)
                    {
                        throw Error.InvalidOperation(SRResources.DefaultRouteMissingOrIncorrect, routeName);
                    }
                    return new Uri(selfLink);
                });
            }

            // We only need to configure the EditLink by convention, ReadLink and IdLink both delegate to EditLink
            if (configuration.GetEditLink() == null)
            {
                bool derivedTypesDefineNavigationProperty = model.DerivedTypes(configuration.EntityType).Any(e => e.NavigationProperties.Any());

                // generate links with cast if any of the derived types define a navigation property
                if (derivedTypesDefineNavigationProperty)
                {
                    configuration.HasEditLink((entityContext) => GenerateSelfLink(configuration, entityContext, includeCast: true));
                }
                else
                {
                    configuration.HasEditLink((entityContext) => GenerateSelfLink(configuration, entityContext, includeCast: false));
                }
            }
        }
        public void BuildEditLink_WhenEditLinkIsNotSameAsIdLink(bool followsConventions, ODataMetadataLevel metadataLevel, bool linkEmitted)
        {
            // Arrange
            _entitySet.HasEditLink(new SelfLinkBuilder <Uri>((context) => new Uri("http://editlink/"), followsConventions));
            NavigationSourceLinkBuilderAnnotation linkBuilder = new NavigationSourceLinkBuilderAnnotation(_entitySet);

            // Act
            Uri generatedEditLink = linkBuilder.BuildEditLink(new ResourceContext(), metadataLevel, new Uri("http://selflink"));

            // Assert
            if (linkEmitted)
            {
                Assert.Equal("http://editlink/", generatedEditLink.AbsoluteUri);
            }
            else
            {
                Assert.Null(generatedEditLink);
            }
        }
Ejemplo n.º 6
0
        public void HasEditLink_RoundTrips()
        {
            // Arrange
            SelfLinkBuilder <Uri> editLinkBuilder = new SelfLinkBuilder <Uri>((ctxt) => null, followsConventions: true);

            // Act
            _entityset.HasEditLink(editLinkBuilder);

            // Assert
            Assert.Equal(editLinkBuilder, _entityset.GetEditLink());
        }
        public void Apply(EntitySetConfiguration configuration, ODataModelBuilder model)
        {
            if (configuration == null)
            {
                throw Error.ArgumentNull("configuration");
            }

            // Configure the self link for the feed
            if (configuration.GetFeedSelfLink() == null)
            {
                configuration.HasFeedSelfLink(entitySetContext =>
                {
                    string selfLink = entitySetContext.UrlHelper.ODataLink(entitySetContext.PathHandler, new EntitySetPathSegment(entitySetContext.EntitySet));

                    if (selfLink == null)
                    {
                        return null;
                    }
                    return new Uri(selfLink);
                });
            }

            // We only need to configure the EditLink by convention, ReadLink and IdLink both delegate to EditLink
            if (configuration.GetEditLink() == null)
            {
                bool derivedTypesDefineNavigationProperty = model.DerivedTypes(configuration.EntityType).Any(e => e.NavigationProperties.Any());

                // generate links with cast if any of the derived types define a navigation property
                if (derivedTypesDefineNavigationProperty)
                {
                    configuration.HasEditLink((entityContext) => GenerateSelfLink(configuration, entityContext, includeCast: true));
                }
                else
                {
                    configuration.HasEditLink((entityContext) => GenerateSelfLink(configuration, entityContext, includeCast: false));
                }
            }
        }
        public void Apply(EntitySetConfiguration configuration, ODataModelBuilder model)
        {
            if (configuration == null)
            {
                throw Error.ArgumentNull("configuration");
            }

            // Configure the self link for the feed
            if (configuration.GetFeedSelfLink() == null)
            {
                configuration.HasFeedSelfLink(entitySetContext =>
                {
                    string selfLink = entitySetContext.UrlHelper.ODataLink(entitySetContext.PathHandler, new EntitySetPathSegment(entitySetContext.EntitySet));

                    if (selfLink == null)
                    {
                        return(null);
                    }
                    return(new Uri(selfLink));
                });
            }

            // We only need to configure the EditLink by convention, ReadLink and IdLink both delegate to EditLink
            if (configuration.GetEditLink() == null)
            {
                bool derivedTypesDefineNavigationProperty = model.DerivedTypes(configuration.EntityType).Any(e => e.NavigationProperties.Any());

                // generate links with cast if any of the derived types define a navigation property
                if (derivedTypesDefineNavigationProperty)
                {
                    configuration.HasEditLink((entityContext) => GenerateSelfLink(configuration, entityContext, includeCast: true));
                }
                else
                {
                    configuration.HasEditLink((entityContext) => GenerateSelfLink(configuration, entityContext, includeCast: false));
                }
            }
        }
Ejemplo n.º 9
0
        public static IEdmModel GetExplicitModel(WebRouteConfiguration configuration)
        {
            ODataModelBuilder builder = new ODataModelBuilder();
            var employee = builder.EntityType <Employee>();

            employee.HasKey(c => c.ID);
            employee.Property(c => c.Name);
            employee.CollectionProperty <Skill>(c => c.SkillSet);
            employee.EnumProperty <Gender>(c => c.Gender);
            employee.EnumProperty <AccessLevel>(c => c.AccessLevel);
            employee.ComplexProperty <FavoriteSports>(c => c.FavoriteSports);
            employee.HasInstanceAnnotations(c => c.InstanceAnnotations);

            var skill = builder.EnumType <Skill>();

            skill.Member(Skill.CSharp);
            skill.Member(Skill.Sql);
            skill.Member(Skill.Web);

            var gender = builder.EnumType <Gender>();

            gender.Member(Gender.Female);
            gender.Member(Gender.Male);

            var accessLevel = builder.EnumType <AccessLevel>();

            accessLevel.Member(AccessLevel.Execute);
            accessLevel.Member(AccessLevel.Read);
            accessLevel.Member(AccessLevel.Write);

            var favoriteSports = builder.ComplexType <FavoriteSports>();

            favoriteSports.EnumProperty <Sport>(f => f.LikeMost);
            favoriteSports.CollectionProperty <Sport>(f => f.Like);

            var sport = builder.EnumType <Sport>();

            sport.Member(Sport.Basketball);
            sport.Member(Sport.Pingpong);

            AddBoundActionsAndFunctions(employee);
            AddUnboundActionsAndFunctions(builder);

            EntitySetConfiguration <Employee> employees = builder.EntitySet <Employee>("Employees");

            employees.HasEditLink(link, true);
            employees.HasIdLink(link, true);
            builder.Namespace = "NS";
            return(builder.GetEdmModel());
        }
Ejemplo n.º 10
0
        public static IEdmModel GetExplicitModel()
        {
            ODataModelBuilder builder = new ODataModelBuilder();
            EntityTypeConfiguration <Window> windowType = builder.EntityType <Window>();

            windowType.HasKey(a => a.Id);
            windowType.Property(a => a.Name).IsRequired();
            windowType.ComplexProperty(w => w.CurrentShape).IsOptional();
            windowType.CollectionProperty(w => w.OptionalShapes);
            windowType.CollectionProperty(w => w.PolygonalShapes);
            windowType.HasOptional <Window>(w => w.Parent);

            ComplexTypeConfiguration <Shape> shapeType = builder.ComplexType <Shape>();

            shapeType.Property(s => s.HasBorder);
            shapeType.Abstract();

            ComplexTypeConfiguration <Circle> circleType = builder.ComplexType <Circle>();

            circleType.ComplexProperty(c => c.Center);
            circleType.Property(c => c.Radius);
            circleType.DerivesFrom <Shape>();

            ComplexTypeConfiguration <Polygon> polygonType = builder.ComplexType <Polygon>();

            polygonType.CollectionProperty(p => p.Vertexes);
            polygonType.DerivesFrom <Shape>();

            ComplexTypeConfiguration <Rectangle> rectangleType = builder.ComplexType <Rectangle>();

            rectangleType.ComplexProperty(r => r.TopLeft);
            rectangleType.Property(r => r.Width);
            rectangleType.Property(r => r.Height);
            rectangleType.DerivesFrom <Polygon>();

            ComplexTypeConfiguration <Point> pointType = builder.ComplexType <Point>();

            pointType.Property(p => p.X);
            pointType.Property(p => p.Y);

            EntitySetConfiguration <Window> windows = builder.EntitySet <Window>("Windows");

            windows.HasEditLink(link, true);
            windows.HasIdLink(link, true);
            windows.HasOptionalBinding(c => c.Parent, "Windows");

            builder.Namespace = typeof(Window).Namespace;

            return(builder.GetEdmModel());
        }
        private static IEdmModel CreateModelForFullMetadata(bool sameLinksForIdAndEdit, bool sameLinksForEditAndRead)
        {
            ODataModelBuilder builder = new ODataModelBuilder();

            EntitySetConfiguration <MainEntity> mainSet = builder.EntitySet <MainEntity>("MainEntity");

            Func <EntityInstanceContext <MainEntity>, string> idLinkFactory = (e) =>
                                                                              CreateAbsoluteLink("/MainEntity/id/" + e.EntityInstance.Id.ToString());

            mainSet.HasIdLink(idLinkFactory, followsConventions: true);

            Func <EntityInstanceContext <MainEntity>, string> editLinkFactory;

            if (!sameLinksForIdAndEdit)
            {
                editLinkFactory = (e) => CreateAbsoluteLink("/MainEntity/edit/" + e.EntityInstance.Id.ToString());
                mainSet.HasEditLink(editLinkFactory, followsConventions: false);
            }

            Func <EntityInstanceContext <MainEntity>, string> readLinkFactory;

            if (!sameLinksForEditAndRead)
            {
                readLinkFactory = (e) => CreateAbsoluteLink("/MainEntity/read/" + e.EntityInstance.Id.ToString());
                mainSet.HasReadLink(readLinkFactory, followsConventions: false);
            }

            EntityTypeConfiguration <MainEntity> main = mainSet.EntityType;

            main.HasKey <int>((e) => e.Id);
            main.Property <short>((e) => e.Int16);
            NavigationPropertyConfiguration mainToRelated = mainSet.EntityType.HasRequired((e) => e.Related);

            main.Action("DoAlways").ReturnsCollectionFromEntitySet <MainEntity>("MainEntity").HasActionLink((c) =>
                                                                                                            CreateAbsoluteUri("/MainEntity/DoAlways/" + ((MainEntity)(c.EntityInstance)).Id),
                                                                                                            followsConventions: true);
            main.TransientAction("DoSometimes").ReturnsCollectionFromEntitySet <MainEntity>(
                "MainEntity").HasActionLink((c) =>
                                            CreateAbsoluteUri("/MainEntity/DoSometimes/" + ((MainEntity)(c.EntityInstance)).Id),
                                            followsConventions: false);

            mainSet.HasNavigationPropertyLink(mainToRelated, (c, p) => new Uri("/MainEntity/RelatedEntity/" +
                                                                               c.EntityInstance.Id, UriKind.Relative), followsConventions: true);

            EntitySetConfiguration <RelatedEntity> related = builder.EntitySet <RelatedEntity>("RelatedEntity");

            return(builder.GetEdmModel());
        }