/// <summary> /// Generates the [DataServiceKey] attribute on a given client type /// </summary> /// <param name="entityType">The complex type's metadata</param> /// <param name="entityTypeClass">The complex types declaration</param> protected virtual void GenerateKeyAttribute(EntityType entityType, CodeTypeDeclaration entityTypeClass) { if (entityType.BaseType == null) { var keyAttribute = entityTypeClass.AddCustomAttribute(Code.TypeRef("Key")); keyAttribute.Arguments.AddRange(entityType.AllKeyProperties.Select(p => new CodeAttributeArgument(Code.Primitive(p.Name))).ToArray()); } }
private void GenerateNamedStreamAttribute(CodeTypeDeclaration entityTypeClass, MemberProperty streamProperty) { var epmAttribute = entityTypeClass.AddCustomAttribute(Code.TypeRef("NamedStream")); epmAttribute.Arguments.Add(new CodeAttributeArgument(Code.Primitive(streamProperty.Name))); }
/// <summary> /// Builds a code representation of an <see cref="EnumType"/>. /// </summary> /// <param name="type">The <see cref="EnumType"/> from which to generate code.</param> /// <returns>A <see cref="CodeTypeDeclaration"/> which represents the <see cref="EnumType"/>.</returns> protected virtual CodeTypeDeclaration BuildType(EnumType type) { var codeEnum = new CodeTypeDeclaration(type.Name); codeEnum.IsEnum = true; if (type.UnderlyingType != null) { codeEnum.BaseTypes.Add(type.UnderlyingType); } if (type.IsFlags == true) { codeEnum.AddCustomAttribute(typeof(FlagsAttribute)); } ApplyTypeAccessModifier(codeEnum, type.Annotations.OfType<TypeAccessModifierAnnotation>().SingleOrDefault()); if (type.Annotations.Any(a => a is SerializableAnnotation)) { codeEnum.AddCustomAttribute(typeof(SerializableAttribute)); } this.AddEnumMembers(codeEnum, type); return codeEnum; }
/// <summary> /// Generates and adds stream related attributes and elements to the entity type /// </summary> /// <param name="entityType">The entity type's metadata</param> /// <param name="entityTypeClass">The entity types declaration</param> protected virtual void GenerateHasStreamEntityTypeCodeElements(EntityType entityType, CodeTypeDeclaration entityTypeClass) { ExceptionUtilities.Assert(entityType.HasStream(), "This method should not be called for entity types without stream."); entityTypeClass.AddCustomAttribute(Code.TypeRef("HasStream")); }
/// <summary> /// Builds a code representation of an <see cref="ComplexType"/>. /// </summary> /// <param name="type">The <see cref="ComplexType"/> from which to generate code.</param> /// <returns>A <see cref="CodeTypeDeclaration"/> which represents the <see cref="ComplexType"/>.</returns> protected virtual CodeTypeDeclaration BuildType(ComplexType type) { var codeClass = new CodeTypeDeclaration(type.Name); codeClass.AddConstructor(); ApplyTypeAccessModifier(codeClass, type.Annotations.OfType<TypeAccessModifierAnnotation>().SingleOrDefault()); if (type.Annotations.Any(a => a is SerializableAnnotation)) { codeClass.AddCustomAttribute(typeof(SerializableAttribute)); } if (type.Annotations.Any(a => a is CodeAttributeAnnotation)) { this.AddCodeAttributeAnnotationAsCustomAttribute(codeClass, type.Annotations.OfType<CodeAttributeAnnotation>()); } this.AddProperties(codeClass, type); return codeClass; }
/// <summary> /// Builds a code representation of an <see cref="EntityType"/>. /// </summary> /// <param name="type">The <see cref="EntityType"/> from which to generate code.</param> /// <returns>A <see cref="CodeTypeDeclaration"/> which represents the <see cref="EntityType"/>.</returns> protected virtual CodeTypeDeclaration BuildType(EntityType type) { var codeClass = new CodeTypeDeclaration(type.Name); var defaultConstructor = codeClass.AddConstructor(); ApplyTypeAccessModifier(codeClass, type.Annotations.OfType<TypeAccessModifierAnnotation>().SingleOrDefault()); if (type.Annotations.Any(a => a is SerializableAnnotation)) { codeClass.AddCustomAttribute(typeof(SerializableAttribute)); } if (type.Annotations.Any(a => a is CodeAttributeAnnotation)) { this.AddCodeAttributeAnnotationAsCustomAttribute(codeClass, type.Annotations.OfType<CodeAttributeAnnotation>()); } var genericTypeAnnotation = type.Annotations.OfType<GenericTypeAnnotation>().FirstOrDefault(); if (genericTypeAnnotation != null) { foreach (var typeParameter in genericTypeAnnotation.TypeParameters) { codeClass.TypeParameters.Add(new CodeTypeParameter(typeParameter)); } } if (type.BaseType != null) { var baseType = new CodeTypeReference(type.BaseType.FullName); var baseGenericTypeAnnotation = type.BaseType.Annotations.OfType<GenericTypeAnnotation>().SingleOrDefault(); var genericArgumentsAnnotation = type.Annotations.OfType<GenericArgumentsAnnotation>().SingleOrDefault(); if (genericArgumentsAnnotation != null) { foreach (var typeParameter in baseGenericTypeAnnotation.TypeParameters) { var typeRef = Code.TypeRef(typeParameter); var argument = genericArgumentsAnnotation.GenericArguments.SingleOrDefault(g => g.TypeParameterName == typeParameter); if (argument != null) { typeRef = codeTypeReferenceResolver.Resolve(argument.DataType); } else { if (genericTypeAnnotation == null || !genericTypeAnnotation.TypeParameters.Contains(typeParameter)) { throw new TaupoArgumentException( string.Format( CultureInfo.InvariantCulture, "Entity type {0} cannot derive from entity type {1} because it does not specify a {2} or {3} to fill in {1}'s generic parameter {4}.", type.Name, type.BaseType.Name, typeof(GenericTypeAnnotation).Name, typeof(GenericArgumentsAnnotation).Name, typeParameter)); } } baseType.TypeArguments.Add(typeRef); } } else { if (baseGenericTypeAnnotation != null) { throw new TaupoArgumentException( string.Format( CultureInfo.InvariantCulture, "Entity type {0} cannot derive from entity type {1} because it does not specify a {2} or {3} to fill in {1}'s generic parameter {4}.", type.Name, type.BaseType.Name, typeof(GenericTypeAnnotation).Name, typeof(GenericArgumentsAnnotation).Name, baseGenericTypeAnnotation.TypeParameters.First())); } } codeClass.InheritsFrom(baseType); } if (type.IsAbstract) { codeClass.SetAbstract(); } this.AddProperties(codeClass, type); this.AddNavigationProperties(codeClass, type, defaultConstructor); return codeClass; }
/// <summary> /// Generates and adds stream related attributes and elements to the entity type /// </summary> /// <param name="entityType">The entity type's metadata</param> /// <param name="entityTypeClass">The entity types declaration</param> protected override void GenerateHasStreamEntityTypeCodeElements(EntityType entityType, CodeTypeDeclaration entityTypeClass) { ExceptionUtilities.Assert(entityType.HasStream(), "This method should not be called for entity types without stream."); ClientMediaEntryAnnotation clientMediaEntryAnnotation = entityType.Annotations.OfType<ClientMediaEntryAnnotation>().FirstOrDefault(); if (clientMediaEntryAnnotation != null) { // generate MediaEntry and MimeTypeProperty properties and attributes for V1 style stream support var attributeArguments1 = new CodeAttributeArgument[] { new CodeAttributeArgument(Code.Primitive(clientMediaEntryAnnotation.MediaEntryName)), }; var attributeArguments2 = new CodeAttributeArgument[] { new CodeAttributeArgument(Code.Primitive(clientMediaEntryAnnotation.MediaEntryName)), new CodeAttributeArgument(Code.Primitive(clientMediaEntryAnnotation.MimeTypePropertyName)) }; entityTypeClass.AddCustomAttribute(Code.TypeRef("MediaEntry"), attributeArguments1); entityTypeClass.AddCustomAttribute(Code.TypeRef("MimeTypeProperty"), attributeArguments2); entityTypeClass.AddAutoImplementedProperty(Code.TypeRef<byte[]>(), clientMediaEntryAnnotation.MediaEntryName); entityTypeClass.AddAutoImplementedProperty(Code.TypeRef<string>(), clientMediaEntryAnnotation.MimeTypePropertyName); } else { // No ClientMediaEntryAnnotation is found, generate HasStream atttribute for V2 and up stream support entityTypeClass.AddCustomAttribute(Code.TypeRef("HasStream")); } }
/// <summary> /// Adds the [EntitySet('setName')] attribute on the client type /// </summary> /// <param name="entityTypeClass">The class declaration for the client type</param> /// <param name="entitySetAnnotation">The DataServiceEntitySetAnnotation which contains the name of the set</param> private void GenerateEntitySetAttribute(CodeTypeDeclaration entityTypeClass, DataServiceEntitySetAnnotation entitySetAnnotation) { var entitySetAttribute = entityTypeClass.AddCustomAttribute(Code.TypeRef("EntitySet")); entitySetAttribute.Arguments.Add(new CodeAttributeArgument(Code.Primitive(entitySetAnnotation.EntitySetName))); }
/// <summary> /// Adds the [DataServiceEntity] attribute on the client type /// </summary> /// <param name="entityTypeClass">The class declaration for the client type</param> private void GenerateDataServiceEntityAttribute(CodeTypeDeclaration entityTypeClass) { entityTypeClass.AddCustomAttribute(Code.TypeRef("DataServiceEntity")); }