1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
|
//---------------------------------------------------------------------
// <copyright file="AttributeEmitter.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//
// @owner [....]
// @backupOwner [....]
//---------------------------------------------------------------------
using System.CodeDom;
using System.Diagnostics;
using System.Data.SqlTypes;
using System.Data.Metadata.Edm;
using System.Collections.Generic;
using System.Data.Entity.Design;
using System.Data.Entity.Design.Common;
using System.Data.EntityModel.SchemaObjectModel;
using System.Data.Entity.Design.SsdlGenerator;
using System.Globalization;
namespace System.Data.EntityModel.Emitters
{
/// <summary>
/// Summary description for AttributeEmitter.
/// </summary>
internal sealed class AttributeEmitter
{
TypeReference _typeReference;
internal TypeReference TypeReference
{
get { return _typeReference; }
}
static readonly string AdoAttributeDataClassesNamespace = "System.Data.Objects.DataClasses";
internal AttributeEmitter(TypeReference typeReference)
{
_typeReference = typeReference;
}
/// <summary>
/// The method to be called to create the type level attributes for the ItemTypeEmitter
/// </summary>
/// <param name="emitter">The strongly typed emitter</param>
/// <param name="typeDecl">The type declaration to add the attribues to.</param>
public void EmitTypeAttributes(EntityTypeEmitter emitter, CodeTypeDeclaration typeDecl)
{
Debug.Assert(emitter != null, "emitter should not be null");
Debug.Assert(typeDecl != null, "typeDecl should not be null");
EmitSchemaTypeAttribute(FQAdoFrameworkDataClassesName("EdmEntityTypeAttribute"), emitter, typeDecl);
CodeAttributeDeclaration attribute2 = EmitSimpleAttribute("System.Runtime.Serialization.DataContractAttribute");
AttributeEmitter.AddNamedAttributeArguments(attribute2,
"IsReference", true);
typeDecl.CustomAttributes.Add(attribute2);
CodeAttributeDeclaration attribute3 = EmitSimpleAttribute("System.Serializable");
typeDecl.CustomAttributes.Add(attribute3);
EmitKnownTypeAttributes(emitter.Item, emitter.Generator, typeDecl);
}
private void EmitKnownTypeAttributes(EdmType baseType, ClientApiGenerator generator, CodeTypeDeclaration typeDecl)
{
foreach (EdmType edmType in generator.GetDirectSubTypes(baseType))
{
Debug.Assert(edmType.BaseType == baseType, "The types must be directly derived from basetype");
CodeTypeReference subTypeRef;
if (generator.Language == LanguageOption.GenerateCSharpCode)
{
bool useGlobalPrefix = true;
subTypeRef = generator.GetFullyQualifiedTypeReference(edmType, useGlobalPrefix);
}
else
{
Debug.Assert(generator.Language == LanguageOption.GenerateVBCode, "Did you add a new language?");
subTypeRef = generator.GetLeastPossibleQualifiedTypeReference(edmType);
}
CodeAttributeDeclaration attribute = EmitSimpleAttribute("System.Runtime.Serialization.KnownTypeAttribute", new CodeTypeOfExpression(subTypeRef));
typeDecl.CustomAttributes.Add(attribute);
}
}
/// <summary>
/// The method to be called to create the type level attributes for the StructuredTypeEmitter
/// </summary>
/// <param name="emitter">The strongly typed emitter</param>
/// <param name="typeDecl">The type declaration to add the attribues to.</param>
public void EmitTypeAttributes(StructuredTypeEmitter emitter, CodeTypeDeclaration typeDecl)
{
Debug.Assert(emitter != null, "emitter should not be null");
Debug.Assert(typeDecl != null, "typeDecl should not be null");
// nothing to do here yet
}
/// <summary>
/// The method to be called to create the type level attributes for the SchemaTypeEmitter
/// </summary>
/// <param name="emitter">The strongly typed emitter</param>
/// <param name="typeDecl">The type declaration to add the attribues to.</param>
public void EmitTypeAttributes(SchemaTypeEmitter emitter, CodeTypeDeclaration typeDecl)
{
Debug.Assert(emitter != null, "emitter should not be null");
Debug.Assert(typeDecl != null, "typeDecl should not be null");
}
/// <summary>
/// Common way to fill out EdmTypeAttribute derived attributes
/// </summary>
/// <param name="attributeName">Unqualified name of the attribute</param>
/// <param name="emitter">The strongly typed emitter</param>
/// <param name="typeDecl">The type declaration to add the attribues to.</param>
public void EmitSchemaTypeAttribute(string attributeName, SchemaTypeEmitter emitter, CodeTypeDeclaration typeDecl)
{
// call the shared static version
EdmType type = emitter.Item as EdmType;
Debug.Assert(type != null, "type is not an EdmType");
EmitSchemaTypeAttribute(attributeName, type, typeDecl as CodeTypeMember);
}
/// <summary>
/// Shared code for adding a EdmTypeAttribute derived attribute including parameters to a type or property
/// </summary>
/// <param name="attributeName">Unqualified name of the attribute</param>
/// <param name="type">The type or property type of the code that is having the attribute attached.</param>
/// <param name="member">The type declaration to add the attribues to.</param>
public void EmitSchemaTypeAttribute(string attributeName, EdmType type, CodeTypeMember member)
{
Debug.Assert(attributeName != null, "attributeName should not be null");
Debug.Assert(type != null, "type should not be null");
Debug.Assert(member != null, "typeDecl should not be null");
// [mappingattribute(SchemaName="namespace",TypeName="classname")
CodeAttributeDeclaration attribute = EmitSimpleAttribute(attributeName);
AttributeEmitter.AddNamedAttributeArguments(attribute,
"NamespaceName", type.NamespaceName,
"Name", type.Name);
member.CustomAttributes.Add(attribute);
}
/// <summary>
/// Emit the attributes for the new navigation property
/// </summary>
/// <param name="generator">The ClientApiGenerator instance</param>
/// <param name="targetRelationshipEnd">The relationship end that is being targeted</param>
/// <param name="propertyDecl">The property declaration to attach the attribute to.</param>
/// <param name="additionalAttributes">Additional attributes</param>
public void EmitNavigationPropertyAttributes(ClientApiGenerator generator,
RelationshipEndMember targetRelationshipEnd,
CodeMemberProperty propertyDecl,
List<CodeAttributeDeclaration> additionalAttributes)
{
CodeAttributeDeclaration attribute = EmitSimpleAttribute(FQAdoFrameworkDataClassesName("EdmRelationshipNavigationPropertyAttribute"),
targetRelationshipEnd.DeclaringType.NamespaceName,
targetRelationshipEnd.DeclaringType.Name,
targetRelationshipEnd.Name);
propertyDecl.CustomAttributes.Add(attribute);
EmitGeneratedCodeAttribute(propertyDecl);
if (additionalAttributes != null && additionalAttributes.Count > 0)
{
try
{
propertyDecl.CustomAttributes.AddRange(additionalAttributes.ToArray());
}
catch (ArgumentNullException e)
{
generator.AddError(Strings.InvalidAttributeSuppliedForProperty(propertyDecl.Name),
ModelBuilderErrorCode.InvalidAttributeSuppliedForProperty,
EdmSchemaErrorSeverity.Error,
e);
}
}
}
//
//
// Emit
// [global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")]
//
// this allows FxCop to skip analysis of these methods and types, it should not be applied to partial types, only the
// generated members of partial types
//
CodeAttributeDeclaration _GeneratedCodeAttribute;
internal void EmitGeneratedCodeAttribute(CodeTypeMember member)
{
if(_GeneratedCodeAttribute == null)
{
_GeneratedCodeAttribute = EmitSimpleAttribute("System.CodeDom.Compiler.GeneratedCode",
"System.Data.Entity.Design.EntityClassGenerator",
typeof(EntityClassGenerator).Assembly.GetName().Version.ToString());
}
member.CustomAttributes.Add(_GeneratedCodeAttribute);
}
/// <summary>
/// The method to be called to create the property level attributes for the PropertyEmitter
/// </summary>
/// <param name="emitter">The strongly typed emitter</param>
/// <param name="propertyDecl">The type declaration to add the attribues to.</param>
/// <param name="additionalAttributes">Additional attributes to emit</param>
public void EmitPropertyAttributes(PropertyEmitter emitter,
CodeMemberProperty propertyDecl,
List<CodeAttributeDeclaration> additionalAttributes)
{
if (MetadataUtil.IsPrimitiveType(emitter.Item.TypeUsage.EdmType) || MetadataUtil.IsEnumerationType(emitter.Item.TypeUsage.EdmType))
{
CodeAttributeDeclaration scalarPropertyAttribute = EmitSimpleAttribute(FQAdoFrameworkDataClassesName("EdmScalarPropertyAttribute"));
if (emitter.IsKeyProperty)
{
Debug.Assert(emitter.Item.Nullable == false, "An EntityKeyProperty cannot be nullable.");
AttributeEmitter.AddNamedAttributeArguments(scalarPropertyAttribute, "EntityKeyProperty", true);
}
if (!emitter.Item.Nullable)
{
AttributeEmitter.AddNamedAttributeArguments(scalarPropertyAttribute, "IsNullable", false);
}
propertyDecl.CustomAttributes.Add(scalarPropertyAttribute);
}
else //Complex property
{
Debug.Assert(MetadataUtil.IsComplexType(emitter.Item.TypeUsage.EdmType) ||
(MetadataUtil.IsCollectionType(emitter.Item.TypeUsage.EdmType)),
"not a complex type or a collection type");
CodeAttributeDeclaration attribute = EmitSimpleAttribute(FQAdoFrameworkDataClassesName("EdmComplexPropertyAttribute"));
propertyDecl.CustomAttributes.Add(attribute);
// Have CodeDOM serialization set the properties on the ComplexObject, not the ComplexObject instance.
attribute = EmitSimpleAttribute("System.ComponentModel.DesignerSerializationVisibility");
AttributeEmitter.AddAttributeArguments(attribute,
new object[] { new CodePropertyReferenceExpression(
new CodeTypeReferenceExpression(TypeReference.ForType(
typeof(System.ComponentModel.DesignerSerializationVisibility))),"Content") });
propertyDecl.CustomAttributes.Add(attribute);
if (!MetadataUtil.IsCollectionType(emitter.Item.TypeUsage.EdmType))
{
// Non-collection complex properties also need additional serialization attributes to force them to be explicitly serialized if they are null
// If this is omitted, null complex properties do not get explicitly set to null during deserialization, which causes
// them to be lazily constructed once the property is accessed after the entity is deserialized. If the property is
// actually null during serialiation, that means the user has explicitly set it, so we need to maintain that during serialization.
// This doesn't apply to collection types because they aren't lazily constructed and don't need this extra information.
attribute = EmitSimpleAttribute("System.Xml.Serialization.XmlElement");
AttributeEmitter.AddNamedAttributeArguments(attribute, "IsNullable", true);
propertyDecl.CustomAttributes.Add(attribute);
attribute = EmitSimpleAttribute("System.Xml.Serialization.SoapElement");
AttributeEmitter.AddNamedAttributeArguments(attribute, "IsNullable", true);
propertyDecl.CustomAttributes.Add(attribute);
}
}
// serialization attribute
AddDataMemberAttribute(propertyDecl);
if (additionalAttributes != null && additionalAttributes.Count > 0)
{
try
{
propertyDecl.CustomAttributes.AddRange(additionalAttributes.ToArray());
}
catch (ArgumentNullException e)
{
emitter.Generator.AddError(Strings.InvalidAttributeSuppliedForProperty(emitter.Item.Name),
ModelBuilderErrorCode.InvalidAttributeSuppliedForProperty,
EdmSchemaErrorSeverity.Error,
e);
}
}
EmitGeneratedCodeAttribute(propertyDecl);
}
/// <summary>
/// The method to be called to create the type level attributes for the NestedTypeEmitter
/// </summary>
/// <param name="emitter">The strongly typed emitter</param>
/// <param name="typeDecl">The type declaration to add the attribues to.</param>
public void EmitTypeAttributes(ComplexTypeEmitter emitter, CodeTypeDeclaration typeDecl)
{
Debug.Assert(emitter != null, "emitter should not be null");
Debug.Assert(typeDecl != null, "typeDecl should not be null");
EmitSchemaTypeAttribute(FQAdoFrameworkDataClassesName("EdmComplexTypeAttribute"),
emitter, typeDecl);
CodeAttributeDeclaration attribute = EmitSimpleAttribute("System.Runtime.Serialization.DataContractAttribute");
AttributeEmitter.AddNamedAttributeArguments(attribute,
"IsReference", true);
typeDecl.CustomAttributes.Add(attribute);
CodeAttributeDeclaration attribute2 = EmitSimpleAttribute("System.Serializable");
typeDecl.CustomAttributes.Add(attribute2);
EmitKnownTypeAttributes(emitter.Item, emitter.Generator, typeDecl);
}
#region Static Methods
/// <summary>
/// Returns the name qualified with the Ado.Net EDM DataClasses Attribute namespace
/// </summary>
/// <param name="unqualifiedName"></param>
/// <returns></returns>
public static string FQAdoFrameworkDataClassesName(string unqualifiedName)
{
return AdoAttributeDataClassesNamespace + "." + unqualifiedName;
}
/// <summary>
///
/// </summary>
/// <param name="attributeType"></param>
/// <param name="arguments"></param>
/// <returns></returns>
public CodeAttributeDeclaration EmitSimpleAttribute(string attributeType, params object[] arguments)
{
CodeAttributeDeclaration attribute = new CodeAttributeDeclaration(TypeReference.FromString(attributeType, true));
AddAttributeArguments(attribute, arguments);
return attribute;
}
/// <summary>
///
/// </summary>
/// <param name="attribute"></param>
/// <param name="arguments"></param>
public static void AddAttributeArguments(CodeAttributeDeclaration attribute, object[] arguments)
{
foreach (object argument in arguments)
{
CodeExpression expression = argument as CodeExpression;
if (expression == null)
expression = new CodePrimitiveExpression(argument);
attribute.Arguments.Add(new CodeAttributeArgument(expression));
}
}
/// <summary>
///
/// </summary>
/// <param name="attribute"></param>
/// <param name="arguments"></param>
public static void AddNamedAttributeArguments(CodeAttributeDeclaration attribute, params object[] arguments)
{
for (int i = 1; i < arguments.Length; i += 2)
{
CodeExpression expression = arguments[i] as CodeExpression;
if (expression == null)
expression = new CodePrimitiveExpression(arguments[i]);
attribute.Arguments.Add(new CodeAttributeArgument(arguments[i - 1].ToString(), expression));
}
}
/// <summary>
/// Adds an XmlIgnore attribute to the given property declaration. This is
/// used to explicitly skip certain properties during XML serialization.
/// </summary>
/// <param name="propertyDecl">the property to mark with XmlIgnore</param>
public void AddIgnoreAttributes(CodeMemberProperty propertyDecl)
{
CodeAttributeDeclaration xmlIgnoreAttribute = EmitSimpleAttribute(typeof(System.Xml.Serialization.XmlIgnoreAttribute).FullName);
CodeAttributeDeclaration soapIgnoreAttribute = EmitSimpleAttribute(typeof(System.Xml.Serialization.SoapIgnoreAttribute).FullName);
propertyDecl.CustomAttributes.Add(xmlIgnoreAttribute);
propertyDecl.CustomAttributes.Add(soapIgnoreAttribute);
}
/// <summary>
/// Adds an Browsable(false) attribute to the given property declaration.
/// This is used to explicitly avoid display property in the PropertyGrid.
/// </summary>
/// <param name="propertyDecl">the property to mark with XmlIgnore</param>
public void AddBrowsableAttribute(CodeMemberProperty propertyDecl)
{
CodeAttributeDeclaration browsableAttribute = EmitSimpleAttribute(typeof(System.ComponentModel.BrowsableAttribute).FullName, false);
propertyDecl.CustomAttributes.Add(browsableAttribute);
}
public void AddDataMemberAttribute(CodeMemberProperty propertyDecl)
{
CodeAttributeDeclaration browsableAttribute = EmitSimpleAttribute("System.Runtime.Serialization.DataMemberAttribute");
propertyDecl.CustomAttributes.Add(browsableAttribute);
}
#endregion
}
}
|