Exemplo n.º 1
0
        private static ODataValue GetComplexValue(InstanceAnnotation entryAnnotation, object annotation, IEdmModel model, ODataComplexTypeSerializer complexSerializer, ODataSerializerContext serializerContext)
        {
            Contract.Requires(entryAnnotation != null);
            Contract.Requires(annotation != null);
            Contract.Requires(model != null);
            Contract.Requires(complexSerializer != null);
            Contract.Requires(serializerContext != null);

            var complexType = (IEdmComplexType)model.FindDeclaredType(entryAnnotation.AnnotationTypeName);

            if (complexType == null)
            {
                return(null);
            }

            var typeRef = new EdmComplexTypeReference(complexType, entryAnnotation.IsNullable);

            if (!entryAnnotation.IsCollection)
            {
                return(complexSerializer.CreateODataComplexValue(annotation, typeRef, serializerContext));
            }

            var typeName = Invariant($"Collection({typeRef.FullName()})");
            var items    = GetComplexValues(annotation, complexSerializer, typeRef, serializerContext);

            return(new ODataCollectionValue()
            {
                Items = items, TypeName = typeName
            });
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="InstanceAnnotationConfiguration"/> class.
        /// </summary>
        /// <param name="typeConfiguration">The associated <see cref="IEdmTypeConfiguration">type configuration</see>.</param>
        /// <param name="name">The name of the annotation.</param>
        /// <param name="annotation">The <see cref="InstanceAnnotation">annotation</see> to configure.</param>
        protected InstanceAnnotationConfiguration(IEdmTypeConfiguration typeConfiguration, string name, InstanceAnnotation annotation)
        {
            Arg.NotNull(typeConfiguration, nameof(typeConfiguration));
            Arg.NotNullOrEmpty(name, nameof(name));
            Arg.NotNull(annotation, nameof(annotation));

            this.typeConfiguration = typeConfiguration;
            this.name       = name;
            this.annotation = annotation;
        }
Exemplo n.º 3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ComplexTypeInstanceAnnotationConfiguration{TStructuralType, TProperty}"/> class.
 /// </summary>
 /// <param name="configuration">The associated <see cref="StructuralTypeConfiguration{TStructuralType}">configuration</see>.</param>
 /// <param name="name">The name of the annotation.</param>
 /// <param name="annotationType">The associated annotation <see cref="ComplexTypeConfiguration{TComplexType}">complex type configuration</see>.</param>
 /// <param name="annotation">The <see cref="InstanceAnnotation">annotation</see> to configure.</param>
 public ComplexTypeInstanceAnnotationConfiguration(
     StructuralTypeConfiguration <TStructuralType> configuration,
     string name,
     ComplexTypeConfiguration <TProperty> annotationType,
     InstanceAnnotation annotation)
     : base(configuration, name, annotation)
 {
     Arg.NotNull(annotationType, nameof(annotationType));
     this.annotationType = annotationType;
 }
        private static IEdmTypeReference AddComplexTerm(IEdmModel model, InstanceAnnotation annotation)
        {
            Contract.Requires(model != null);
            Contract.Requires(annotation != null);
            Contract.Ensures(Contract.Result <IEdmTypeReference>() != null);

            var complexType    = (IEdmComplexType)model.FindDeclaredType(annotation.AnnotationTypeName);
            var complexTypeRef = new EdmComplexTypeReference(complexType, annotation.IsNullable);

            return(complexTypeRef);
        }
        private static IEdmTypeReference AddPrimitiveTerm(IEdmModel model, InstanceAnnotation annotation)
        {
            Contract.Requires(model != null);
            Contract.Requires(annotation != null);
            Contract.Ensures(Contract.Result <IEdmTypeReference>() != null);

            var primitiveType    = (IEdmPrimitiveType)model.FindType(annotation.AnnotationTypeName);
            var primitiveTypeRef = new EdmPrimitiveTypeReference(primitiveType, annotation.IsNullable);

            return(primitiveTypeRef);
        }
Exemplo n.º 6
0
 /// <summary>
 /// Initializes a new instance of the <see cref="EntitySetComplexTypeInstanceAnnotationConfiguration{TStructuralType}"/> class.
 /// </summary>
 /// <param name="entitySetName">The name of the associated entity set.</param>
 /// <param name="typeConfiguration">The associated <see cref="IEdmTypeConfiguration">type configuration</see>.</param>
 /// <param name="name">The name of the annotation.</param>
 /// <param name="annotationType">The associated annotation <see cref="ComplexTypeConfiguration{TComplexType}">complex type configuration</see>.</param>
 /// <param name="annotation">The <see cref="InstanceAnnotation">annotation</see> to configure.</param>
 public EntitySetComplexTypeInstanceAnnotationConfiguration(
     string entitySetName,
     IEdmTypeConfiguration typeConfiguration,
     string name,
     ComplexTypeConfiguration <TStructuralType> annotationType,
     InstanceAnnotation annotation)
     : base(entitySetName, typeConfiguration, name, annotation)
 {
     Arg.NotNull(annotationType, nameof(annotationType));
     this.annotationType = annotationType;
 }
Exemplo n.º 7
0
        public static ComplexTypeInstanceAnnotationConfiguration <TStructuralType, TProperty> HasComplexAnnotations <TStructuralType, TProperty>(
            this StructuralTypeConfiguration <TStructuralType> configuration,
            Expression <Func <TStructuralType, IEnumerable <TProperty> > > propertyExpression)
            where TStructuralType : class
            where TProperty : class
        {
            Arg.NotNull(configuration, nameof(configuration));
            Arg.NotNull(propertyExpression, nameof(propertyExpression));
            Contract.Ensures(Contract.Result <ComplexTypeInstanceAnnotationConfiguration <TStructuralType, TProperty> >() != null);

            string name;
            var    key            = propertyExpression.GetInstanceAnnotationKey(out name);
            var    builder        = configuration.GetModelBuilder();
            var    configurations = builder.GetAnnotationConfigurations();
            ComplexTypeInstanceAnnotationConfiguration <TStructuralType, TProperty> annotationConfig;

            // if the property has already been configured, return the existing configuration
            if (configurations.TryGet(key, out annotationConfig))
            {
                return(annotationConfig);
            }

            var annotationType = typeof(TProperty);

            // ensure a nested collection hasn't been specified
            if (annotationType.IsEnumerable())
            {
                throw new InvalidOperationException(string.Format(CurrentCulture, InvalidComplexTypeCollection, annotationType));
            }

            // always ignore the annotation property from the entity model
            propertyExpression.IgnoredBy(configuration);

            // build an annotation for the entity property
            var accessor             = propertyExpression.ToLazyContravariantFunc();
            var qualifiedName        = Invariant($"{configuration.Namespace}.{name}");
            var annotationTypeConfig = builder.ComplexType <TProperty>();
            var annotation           = new InstanceAnnotation(accessor, qualifiedName, annotationTypeConfig.ToEdmTypeConfiguration())
            {
                IsComplex    = true,
                IsCollection = true,
                IsNullable   = true
            };

            annotationConfig = new ComplexTypeInstanceAnnotationConfiguration <TStructuralType, TProperty>(configuration, name, annotationTypeConfig, annotation);
            configurations.Add(key, annotationConfig);

            return(annotationConfig);
        }
Exemplo n.º 8
0
        public static EntitySetComplexTypeInstanceAnnotationConfiguration <TStructuralType> HasComplexAnnotations <TEntity, TStructuralType>(
            this EntitySetConfiguration <TEntity> configuration,
            string name,
            IEnumerable <TStructuralType> complexValues)
            where TEntity : class
            where TStructuralType : class
        {
            Arg.NotNull(configuration, nameof(configuration));
            Arg.NotNullOrEmpty(name, nameof(name));
            Contract.Ensures(Contract.Result <EntitySetComplexTypeInstanceAnnotationConfiguration <TStructuralType> >() != null);

            var entitySetName  = configuration.GetEntitySetName();
            var key            = Invariant($"EntitySet_{entitySetName}_{name}");
            var builder        = configuration.EntityType.GetModelBuilder();
            var configurations = builder.GetAnnotationConfigurations();
            EntitySetComplexTypeInstanceAnnotationConfiguration <TStructuralType> annotationConfig;

            // if the property has already been configured, return the existing configuration
            if (configurations.TryGet(key, out annotationConfig))
            {
                return(annotationConfig);
            }

            var annotationType = typeof(TStructuralType);

            // ensure this isn't a collection; must use HasAnnotations instead
            if (annotationType.IsEnumerable())
            {
                throw new InvalidOperationException(string.Format(CurrentCulture, InvalidComplexType, annotationType));
            }

            // build an annotation for the entity property
            var entityType           = configuration.EntityType;
            var qualifiedName        = Invariant($"{entityType.Namespace}.{name}");
            var annotationTypeConfig = builder.ComplexType <TStructuralType>();
            var annotation           = new InstanceAnnotation(o => complexValues, qualifiedName, annotationTypeConfig.ToEdmTypeConfiguration())
            {
                IsComplex    = true,
                IsCollection = true,
                IsNullable   = true
            };

            annotationConfig = new EntitySetComplexTypeInstanceAnnotationConfiguration <TStructuralType>(entitySetName, entityType.ToEdmTypeConfiguration(), name, annotationTypeConfig, annotation);
            configurations.Add(key, annotationConfig);

            return(annotationConfig);
        }
Exemplo n.º 9
0
        private static ODataValue GetPrimitiveValue(InstanceAnnotation entryAnnotation, object annotation, IEdmModel model)
        {
            if (!entryAnnotation.IsCollection)
            {
                return(new ODataPrimitiveValue(annotation));
            }

            // note: primitives are not wrapped as a collection of ODataPrimitiveValue
            var type     = model.FindType(entryAnnotation.AnnotationTypeName);
            var typeName = Invariant($"Collection({type.FullName()})");
            var items    = (System.Collections.IEnumerable)annotation;

            return(new ODataCollectionValue()
            {
                Items = items, TypeName = typeName
            });
        }
Exemplo n.º 10
0
        private static InstanceAnnotationConfiguration <TStructuralType> HasPrimitiveAnnotations <TStructuralType, TProperty>(
            this StructuralTypeConfiguration <TStructuralType> configuration,
            Expression <Func <TStructuralType, IEnumerable <TProperty> > > propertyExpression)
            where TStructuralType : class
        {
            Arg.NotNull(configuration, nameof(configuration));
            Arg.NotNull(propertyExpression, nameof(propertyExpression));
            Contract.Ensures(Contract.Result <InstanceAnnotationConfiguration <TStructuralType> >() != null);

            string name;
            var    key            = propertyExpression.GetInstanceAnnotationKey(out name);
            var    builder        = configuration.GetModelBuilder();
            var    configurations = builder.GetAnnotationConfigurations();
            InstanceAnnotationConfiguration <TStructuralType> annotationConfig;

            // if the property has already been configured, return the existing configuration
            if (configurations.TryGet(key, out annotationConfig))
            {
                return(annotationConfig);
            }

            // always ignore the annotation property from the entity model
            propertyExpression.IgnoredBy(configuration);

            // build an annotation for the entity property
            var accessor             = propertyExpression.ToLazyContravariantFunc();
            var qualifiedName        = Invariant($"{configuration.Namespace}.{name}");
            var annotationType       = typeof(TProperty);
            var annotationTypeConfig = builder.GetTypeConfigurationOrNull(annotationType);
            var annotation           = new InstanceAnnotation(accessor, qualifiedName, annotationTypeConfig)
            {
                IsCollection = true,
                IsNullable   = annotationType.IsNullable()
            };

            annotationConfig = new InstanceAnnotationConfiguration <TStructuralType>(configuration, name, annotation);
            configurations.Add(key, annotationConfig);

            return(annotationConfig);
        }
Exemplo n.º 11
0
        private static EntitySetInstanceAnnotationConfiguration HasPrimitiveAnnotations <TEntity, TValue>(
            this EntitySetConfiguration <TEntity> configuration,
            string name,
            IEnumerable <TValue> values)
            where TEntity : class
        {
            Arg.NotNull(configuration, nameof(configuration));
            Arg.NotNullOrEmpty(name, nameof(name));
            Contract.Ensures(Contract.Result <EntitySetInstanceAnnotationConfiguration>() != null);

            var entitySetName  = configuration.GetEntitySetName();
            var key            = Invariant($"EntitySet_{entitySetName}_{name}");
            var builder        = configuration.EntityType.GetModelBuilder();
            var configurations = builder.GetAnnotationConfigurations();
            EntitySetInstanceAnnotationConfiguration annotationConfig;

            // if the property has already been configured, return the existing configuration
            if (configurations.TryGet(key, out annotationConfig))
            {
                return(annotationConfig);
            }

            var entityType           = configuration.EntityType;
            var qualifiedName        = Invariant($"{entityType.Namespace}.{name}");
            var annotationType       = typeof(TValue);
            var annotationTypeConfig = builder.GetTypeConfigurationOrNull(annotationType);
            var annotation           = new InstanceAnnotation(o => values, qualifiedName, annotationTypeConfig)
            {
                IsCollection = true,
                IsNullable   = annotationType.IsNullable()
            };

            annotationConfig = new EntitySetInstanceAnnotationConfiguration(entitySetName, entityType.ToEdmTypeConfiguration(), name, annotation);
            configurations.Add(key, annotationConfig);

            return(annotationConfig);
        }
 protected InstanceAnnotationConfigurationContract(IEdmTypeConfiguration typeConfiguration, string name, InstanceAnnotation annotation)
     : base(typeConfiguration, name, annotation)
 {
 }
Exemplo n.º 13
0
 /// <summary>
 /// Initializes a new instance of the <see cref="EntitySetInstanceAnnotationConfiguration"/> class.
 /// </summary>
 /// <param name="entitySetName">The name of the associated entity set.</param>
 /// <param name="typeConfiguration">The associated <see cref="IEdmTypeConfiguration">type configuration</see>.</param>
 /// <param name="name">The name of the annotation.</param>
 /// <param name="annotation">The <see cref="InstanceAnnotation">annotation</see> to configure.</param>
 public EntitySetInstanceAnnotationConfiguration(string entitySetName, IEdmTypeConfiguration typeConfiguration, string name, InstanceAnnotation annotation)
     : base(typeConfiguration, name, annotation)
 {
     Arg.NotNullOrEmpty(entitySetName, nameof(entitySetName));
     this.entitySetName = entitySetName;
 }
Exemplo n.º 14
0
        internal static void AddTerm(this IEdmModel model, IAnnotationConfiguration configuration, InstanceAnnotation annotation, string appliesTo)
        {
            Contract.Requires(model != null);
            Contract.Requires(configuration != null);
            Contract.Requires(annotation != null);
            Contract.Requires(!string.IsNullOrEmpty(appliesTo));

            // short-circuit if the term has already been added
            if (model.FindValueTerm(annotation.QualifiedName) != null)
            {
                return;
            }

            var typeRef  = annotation.IsComplex ? AddComplexTerm(model, annotation) : AddPrimitiveTerm(model, annotation);
            var termType = annotation.IsCollection ? new EdmCollectionTypeReference(new EdmCollectionType(typeRef)) : typeRef;

            AddTerm(model, configuration, termType, appliesTo);
        }
Exemplo n.º 15
0
 /// <summary>
 /// Instantiates a new instance of the <see cref="InstanceAnnotationConfiguration{TStructuralType}"/> class.
 /// </summary>
 /// <param name="configuration">The associated <see cref="StructuralTypeConfiguration{TStructuralType}">configuration</see>.</param>
 /// <param name="name">The name of the annotation.</param>
 /// <param name="annotation">The <see cref="InstanceAnnotation">annotation</see> to configure.</param>
 public InstanceAnnotationConfiguration(StructuralTypeConfiguration <TStructuralType> configuration, string name, InstanceAnnotation annotation)
     : base(configuration?.ToEdmTypeConfiguration(), name, annotation)
 {
     Arg.NotNull(configuration, nameof(configuration));
     this.configuration = configuration;
 }