1 //---------------------------------------------------------------------
2 // <copyright file="SchemaTypeEmitter.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
7 // @backupOwner Microsoft
8 //---------------------------------------------------------------------
12 using System.Data.EntityModel.SchemaObjectModel;
13 using Som = System.Data.EntityModel.SchemaObjectModel;
14 using System.Data.Metadata.Edm;
15 using System.Reflection;
16 using System.Diagnostics;
17 using System.Collections.Generic;
18 using System.Data.Entity.Design;
19 using System.Data.Entity.Design.SsdlGenerator;
22 namespace System.Data.EntityModel.Emitters
27 internal abstract class SchemaTypeEmitter : MetadataItemEmitter
29 #region Public Methods
30 public abstract CodeTypeDeclarationCollection EmitApiClass();
33 #region Protected Methods
37 /// <param name="generator"></param>
38 /// <param name="schemaType"></param>
39 protected SchemaTypeEmitter(ClientApiGenerator generator, MetadataItem item)
40 : base(generator, item)
47 /// <param name="typeDecl"></param>
48 protected virtual void EmitTypeAttributes( CodeTypeDeclaration typeDecl )
50 Generator.AttributeEmitter.EmitTypeAttributes( this, typeDecl );
54 /// Emitter-specific validation: for SchemaTypeEmitter-derived classes, we
55 /// check the EdmItemCollection for other entities that have the same name
56 /// but differ in case
58 protected override void Validate()
60 Generator.VerifyLanguageCaseSensitiveCompatibilityForType(Item);
64 /// Add attributes to a type's CustomAttributes collection
66 /// <param name="itemName">The name of the type</param>
67 /// <param name="typeDecl">The type to annotate</param>
68 /// <param name="additionalAttributes">The additional attributes</param>
69 protected void EmitTypeAttributes(string itemName, CodeTypeDeclaration typeDecl,
70 List<CodeAttributeDeclaration> additionalAttributes)
72 if (additionalAttributes != null && additionalAttributes.Count > 0)
76 typeDecl.CustomAttributes.AddRange(additionalAttributes.ToArray());
78 catch (ArgumentNullException e)
80 Generator.AddError(Strings.InvalidAttributeSuppliedForType(itemName),
81 ModelBuilderErrorCode.InvalidAttributeSuppliedForType,
82 EdmSchemaErrorSeverity.Error,
87 EmitTypeAttributes(typeDecl);
91 /// Add interfaces to the type's list of BaseTypes
93 /// <param name="itemName">The name of the type</param>
94 /// <param name="typeDecl">The type whose list of base types needs to be extended</param>
95 /// <param name="additionalInterfaces">The interfaces to add to the list of base types</param>
96 protected void AddInterfaces(string itemName,
97 CodeTypeDeclaration typeDecl,
98 List<Type> additionalInterfaces)
100 if (additionalInterfaces != null)
104 foreach (Type interfaceType in additionalInterfaces)
106 typeDecl.BaseTypes.Add(new CodeTypeReference(interfaceType));
109 catch (ArgumentNullException e)
111 Generator.AddError(Strings.InvalidInterfaceSuppliedForType(itemName),
112 ModelBuilderErrorCode.InvalidInterfaceSuppliedForType,
113 EdmSchemaErrorSeverity.Error,
120 /// Add interfaces to the type's list of BaseTypes
122 /// <param name="itemName">The name of the type</param>
123 /// <param name="typeDecl">The type to which members need to be added</param>
124 /// <param name="additionalMembers">The members to add</param>
125 protected void AddMembers(string itemName, CodeTypeDeclaration typeDecl,
126 List<CodeTypeMember> additionalMembers)
128 if (additionalMembers != null && additionalMembers.Count > 0)
132 typeDecl.Members.AddRange(additionalMembers.ToArray());
134 catch (ArgumentNullException e)
136 Generator.AddError(Strings.InvalidMemberSuppliedForType(itemName),
137 ModelBuilderErrorCode.InvalidMemberSuppliedForType,
138 EdmSchemaErrorSeverity.Error,
146 #region Protected Properties
149 /// Gets the element that code is being emitted for.
151 internal new GlobalItem Item
155 return base.Item as GlobalItem;
159 internal void SetTypeVisibility(CodeTypeDeclaration typeDecl)
161 typeDecl.TypeAttributes &= ~System.Reflection.TypeAttributes.VisibilityMask;
162 typeDecl.TypeAttributes |= GetTypeAccessibilityValue(Item);