FoghornModel.tt 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  1. <#@ template language="C#" debug="false" hostspecific="true"#>
  2. <#@ include file="EF.Utility.CS.ttinclude"#><#@
  3. output extension=".cs"#><#
  4. const string inputFile = @"FoghornModel.edmx";
  5. var textTransform = DynamicTextTransformation.Create(this);
  6. var code = new CodeGenerationTools(this);
  7. var ef = new MetadataTools(this);
  8. var typeMapper = new TypeMapper(code, ef, textTransform.Errors);
  9. var fileManager = EntityFrameworkTemplateFileManager.Create(this);
  10. var itemCollection = new EdmMetadataLoader(textTransform.Host, textTransform.Errors).CreateEdmItemCollection(inputFile);
  11. var codeStringGenerator = new CodeStringGenerator(code, typeMapper, ef);
  12. if (!typeMapper.VerifyCaseInsensitiveTypeUniqueness(typeMapper.GetAllGlobalItems(itemCollection), inputFile))
  13. {
  14. return string.Empty;
  15. }
  16. WriteHeader(codeStringGenerator, fileManager);
  17. foreach (var entity in typeMapper.GetItemsToGenerate<EntityType>(itemCollection))
  18. {
  19. fileManager.StartNewFile(entity.Name + ".cs");
  20. BeginNamespace(code);
  21. #>
  22. <#=codeStringGenerator.UsingDirectives(inHeader: false)#>
  23. <#=codeStringGenerator.EntityClassOpening(entity)#>
  24. {
  25. <#
  26. var propertiesWithDefaultValues = typeMapper.GetPropertiesWithDefaultValues(entity);
  27. var collectionNavigationProperties = typeMapper.GetCollectionNavigationProperties(entity);
  28. var complexProperties = typeMapper.GetComplexProperties(entity);
  29. if (propertiesWithDefaultValues.Any() || collectionNavigationProperties.Any() || complexProperties.Any())
  30. {
  31. #>
  32. public <#=code.Escape(entity)#>()
  33. {
  34. <#
  35. foreach (var edmProperty in propertiesWithDefaultValues)
  36. {
  37. #>
  38. this.<#=code.Escape(edmProperty)#> = <#=typeMapper.CreateLiteral(edmProperty.DefaultValue)#>;
  39. <#
  40. }
  41. foreach (var navigationProperty in collectionNavigationProperties)
  42. {
  43. #>
  44. this.<#=code.Escape(navigationProperty)#> = new HashSet<<#=typeMapper.GetTypeName(navigationProperty.ToEndMember.GetEntityType())#>>();
  45. <#
  46. }
  47. foreach (var complexProperty in complexProperties)
  48. {
  49. #>
  50. this.<#=code.Escape(complexProperty)#> = new <#=typeMapper.GetTypeName(complexProperty.TypeUsage)#>();
  51. <#
  52. }
  53. #>
  54. }
  55. <#
  56. }
  57. var simpleProperties = typeMapper.GetSimpleProperties(entity);
  58. if (simpleProperties.Any())
  59. {
  60. foreach (var edmProperty in simpleProperties)
  61. {
  62. #>
  63. <#=codeStringGenerator.Property(edmProperty)#>
  64. <#
  65. }
  66. }
  67. if (complexProperties.Any())
  68. {
  69. #>
  70. <#
  71. foreach(var complexProperty in complexProperties)
  72. {
  73. #>
  74. <#=codeStringGenerator.Property(complexProperty)#>
  75. <#
  76. }
  77. }
  78. var navigationProperties = typeMapper.GetNavigationProperties(entity);
  79. if (navigationProperties.Any())
  80. {
  81. #>
  82. <#
  83. foreach (var navigationProperty in navigationProperties)
  84. {
  85. #>
  86. <#=codeStringGenerator.NavigationProperty(navigationProperty)#>
  87. <#
  88. }
  89. }
  90. #>
  91. }
  92. <#
  93. EndNamespace(code);
  94. }
  95. foreach (var complex in typeMapper.GetItemsToGenerate<ComplexType>(itemCollection))
  96. {
  97. fileManager.StartNewFile(complex.Name + ".cs");
  98. BeginNamespace(code);
  99. #>
  100. <#=codeStringGenerator.UsingDirectives(inHeader: false, includeCollections: false)#>
  101. <#=Accessibility.ForType(complex)#> partial class <#=code.Escape(complex)#>
  102. {
  103. <#
  104. var complexProperties = typeMapper.GetComplexProperties(complex);
  105. var propertiesWithDefaultValues = typeMapper.GetPropertiesWithDefaultValues(complex);
  106. if (propertiesWithDefaultValues.Any() || complexProperties.Any())
  107. {
  108. #>
  109. public <#=code.Escape(complex)#>()
  110. {
  111. <#
  112. foreach (var edmProperty in propertiesWithDefaultValues)
  113. {
  114. #>
  115. this.<#=code.Escape(edmProperty)#> = <#=typeMapper.CreateLiteral(edmProperty.DefaultValue)#>;
  116. <#
  117. }
  118. foreach (var complexProperty in complexProperties)
  119. {
  120. #>
  121. this.<#=code.Escape(complexProperty)#> = new <#=typeMapper.GetTypeName(complexProperty.TypeUsage)#>();
  122. <#
  123. }
  124. #>
  125. }
  126. <#
  127. }
  128. var simpleProperties = typeMapper.GetSimpleProperties(complex);
  129. if (simpleProperties.Any())
  130. {
  131. foreach(var edmProperty in simpleProperties)
  132. {
  133. #>
  134. <#=codeStringGenerator.Property(edmProperty)#>
  135. <#
  136. }
  137. }
  138. if (complexProperties.Any())
  139. {
  140. #>
  141. <#
  142. foreach(var edmProperty in complexProperties)
  143. {
  144. #>
  145. <#=codeStringGenerator.Property(edmProperty)#>
  146. <#
  147. }
  148. }
  149. #>
  150. }
  151. <#
  152. EndNamespace(code);
  153. }
  154. foreach (var enumType in typeMapper.GetEnumItemsToGenerate(itemCollection))
  155. {
  156. fileManager.StartNewFile(enumType.Name + ".cs");
  157. BeginNamespace(code);
  158. #>
  159. <#=codeStringGenerator.UsingDirectives(inHeader: false, includeCollections: false)#>
  160. <#
  161. if (typeMapper.EnumIsFlags(enumType))
  162. {
  163. #>
  164. [Flags]
  165. <#
  166. }
  167. #>
  168. <#=codeStringGenerator.EnumOpening(enumType)#>
  169. {
  170. <#
  171. var foundOne = false;
  172. foreach (MetadataItem member in typeMapper.GetEnumMembers(enumType))
  173. {
  174. foundOne = true;
  175. #>
  176. <#=code.Escape(typeMapper.GetEnumMemberName(member))#> = <#=typeMapper.GetEnumMemberValue(member)#>,
  177. <#
  178. }
  179. if (foundOne)
  180. {
  181. this.GenerationEnvironment.Remove(this.GenerationEnvironment.Length - 3, 1);
  182. }
  183. #>
  184. }
  185. <#
  186. EndNamespace(code);
  187. }
  188. fileManager.Process();
  189. #>
  190. <#+
  191. public void WriteHeader(CodeStringGenerator codeStringGenerator, EntityFrameworkTemplateFileManager fileManager)
  192. {
  193. fileManager.StartHeader();
  194. #>
  195. //------------------------------------------------------------------------------
  196. // <auto-generated>
  197. // <#=GetResourceString("Template_GeneratedCodeCommentLine1")#>
  198. //
  199. // <#=GetResourceString("Template_GeneratedCodeCommentLine2")#>
  200. // <#=GetResourceString("Template_GeneratedCodeCommentLine3")#>
  201. // </auto-generated>
  202. //------------------------------------------------------------------------------
  203. <#=codeStringGenerator.UsingDirectives(inHeader: true)#>
  204. <#+
  205. fileManager.EndBlock();
  206. }
  207. public void BeginNamespace(CodeGenerationTools code)
  208. {
  209. var codeNamespace = code.VsNamespaceSuggestion();
  210. if (!String.IsNullOrEmpty(codeNamespace))
  211. {
  212. #>
  213. namespace <#=code.EscapeNamespace(codeNamespace)#>
  214. {
  215. <#+
  216. PushIndent(" ");
  217. }
  218. }
  219. public void EndNamespace(CodeGenerationTools code)
  220. {
  221. if (!String.IsNullOrEmpty(code.VsNamespaceSuggestion()))
  222. {
  223. PopIndent();
  224. #>
  225. }
  226. <#+
  227. }
  228. }
  229. public const string TemplateId = "CSharp_DbContext_Types_EF5";
  230. public class CodeStringGenerator
  231. {
  232. private readonly CodeGenerationTools _code;
  233. private readonly TypeMapper _typeMapper;
  234. private readonly MetadataTools _ef;
  235. public CodeStringGenerator(CodeGenerationTools code, TypeMapper typeMapper, MetadataTools ef)
  236. {
  237. ArgumentNotNull(code, "code");
  238. ArgumentNotNull(typeMapper, "typeMapper");
  239. ArgumentNotNull(ef, "ef");
  240. _code = code;
  241. _typeMapper = typeMapper;
  242. _ef = ef;
  243. }
  244. public string Property(EdmProperty edmProperty)
  245. {
  246. return string.Format(
  247. CultureInfo.InvariantCulture,
  248. "{0} {1} {2} {{ {3}get; {4}set; }}",
  249. Accessibility.ForProperty(edmProperty),
  250. _typeMapper.GetTypeName(edmProperty.TypeUsage),
  251. _code.Escape(edmProperty),
  252. _code.SpaceAfter(Accessibility.ForGetter(edmProperty)),
  253. _code.SpaceAfter(Accessibility.ForSetter(edmProperty)));
  254. }
  255. public string NavigationProperty(NavigationProperty navigationProperty)
  256. {
  257. var endType = _typeMapper.GetTypeName(navigationProperty.ToEndMember.GetEntityType());
  258. return string.Format(
  259. CultureInfo.InvariantCulture,
  260. "{0} {1} {2} {{ {3}get; {4}set; }}",
  261. AccessibilityAndVirtual(Accessibility.ForProperty(navigationProperty)),
  262. navigationProperty.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many ? ("ICollection<" + endType + ">") : endType,
  263. _code.Escape(navigationProperty),
  264. _code.SpaceAfter(Accessibility.ForGetter(navigationProperty)),
  265. _code.SpaceAfter(Accessibility.ForSetter(navigationProperty)));
  266. }
  267. public string AccessibilityAndVirtual(string accessibility)
  268. {
  269. return accessibility + (accessibility != "private" ? " virtual" : "");
  270. }
  271. public string EntityClassOpening(EntityType entity)
  272. {
  273. return string.Format(
  274. CultureInfo.InvariantCulture,
  275. "{0} {1}partial class {2}{3}",
  276. Accessibility.ForType(entity),
  277. _code.SpaceAfter(_code.AbstractOption(entity)),
  278. _code.Escape(entity),
  279. _code.StringBefore(" : ", _typeMapper.GetTypeName(entity.BaseType)));
  280. }
  281. public string EnumOpening(SimpleType enumType)
  282. {
  283. return string.Format(
  284. CultureInfo.InvariantCulture,
  285. "{0} enum {1} : {2}",
  286. Accessibility.ForType(enumType),
  287. _code.Escape(enumType),
  288. _code.Escape(_typeMapper.UnderlyingClrType(enumType)));
  289. }
  290. public void WriteFunctionParameters(EdmFunction edmFunction, Action<string, string, string, string> writeParameter)
  291. {
  292. var parameters = FunctionImportParameter.Create(edmFunction.Parameters, _code, _ef);
  293. foreach (var parameter in parameters.Where(p => p.NeedsLocalVariable))
  294. {
  295. var isNotNull = parameter.IsNullableOfT ? parameter.FunctionParameterName + ".HasValue" : parameter.FunctionParameterName + " != null";
  296. var notNullInit = "new ObjectParameter(\"" + parameter.EsqlParameterName + "\", " + parameter.FunctionParameterName + ")";
  297. var nullInit = "new ObjectParameter(\"" + parameter.EsqlParameterName + "\", typeof(" + parameter.RawClrTypeName + "))";
  298. writeParameter(parameter.LocalVariableName, isNotNull, notNullInit, nullInit);
  299. }
  300. }
  301. public string ComposableFunctionMethod(EdmFunction edmFunction, string modelNamespace)
  302. {
  303. var parameters = _typeMapper.GetParameters(edmFunction);
  304. return string.Format(
  305. CultureInfo.InvariantCulture,
  306. "{0} IQueryable<{1}> {2}({3})",
  307. AccessibilityAndVirtual(Accessibility.ForMethod(edmFunction)),
  308. _typeMapper.GetTypeName(_typeMapper.GetReturnType(edmFunction), modelNamespace),
  309. _code.Escape(edmFunction),
  310. string.Join(", ", parameters.Select(p => p.FunctionParameterType + " " + p.FunctionParameterName).ToArray()));
  311. }
  312. public string ComposableCreateQuery(EdmFunction edmFunction, string modelNamespace)
  313. {
  314. var parameters = _typeMapper.GetParameters(edmFunction);
  315. return string.Format(
  316. CultureInfo.InvariantCulture,
  317. "return ((IObjectContextAdapter)this).ObjectContext.CreateQuery<{0}>(\"[{1}].[{2}]({3})\"{4});",
  318. _typeMapper.GetTypeName(_typeMapper.GetReturnType(edmFunction), modelNamespace),
  319. edmFunction.NamespaceName,
  320. edmFunction.Name,
  321. string.Join(", ", parameters.Select(p => "@" + p.EsqlParameterName).ToArray()),
  322. _code.StringBefore(", ", string.Join(", ", parameters.Select(p => p.ExecuteParameterName).ToArray())));
  323. }
  324. public string FunctionMethod(EdmFunction edmFunction, string modelNamespace, bool includeMergeOption)
  325. {
  326. var parameters = _typeMapper.GetParameters(edmFunction);
  327. var returnType = _typeMapper.GetReturnType(edmFunction);
  328. var paramList = String.Join(", ", parameters.Select(p => p.FunctionParameterType + " " + p.FunctionParameterName).ToArray());
  329. if (includeMergeOption)
  330. {
  331. paramList = _code.StringAfter(paramList, ", ") + "MergeOption mergeOption";
  332. }
  333. return string.Format(
  334. CultureInfo.InvariantCulture,
  335. "{0} {1} {2}({3})",
  336. AccessibilityAndVirtual(Accessibility.ForMethod(edmFunction)),
  337. returnType == null ? "int" : "ObjectResult<" + _typeMapper.GetTypeName(returnType, modelNamespace) + ">",
  338. _code.Escape(edmFunction),
  339. paramList);
  340. }
  341. public string ExecuteFunction(EdmFunction edmFunction, string modelNamespace, bool includeMergeOption)
  342. {
  343. var parameters = _typeMapper.GetParameters(edmFunction);
  344. var returnType = _typeMapper.GetReturnType(edmFunction);
  345. var callParams = _code.StringBefore(", ", String.Join(", ", parameters.Select(p => p.ExecuteParameterName).ToArray()));
  346. if (includeMergeOption)
  347. {
  348. callParams = ", mergeOption" + callParams;
  349. }
  350. return string.Format(
  351. CultureInfo.InvariantCulture,
  352. "return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction{0}(\"{1}\"{2});",
  353. returnType == null ? "" : "<" + _typeMapper.GetTypeName(returnType, modelNamespace) + ">",
  354. edmFunction.Name,
  355. callParams);
  356. }
  357. public string DbSet(EntitySet entitySet)
  358. {
  359. return string.Format(
  360. CultureInfo.InvariantCulture,
  361. "{0} DbSet<{1}> {2} {{ get; set; }}",
  362. Accessibility.ForReadOnlyProperty(entitySet),
  363. _typeMapper.GetTypeName(entitySet.ElementType),
  364. _code.Escape(entitySet));
  365. }
  366. public string UsingDirectives(bool inHeader, bool includeCollections = true)
  367. {
  368. return inHeader == string.IsNullOrEmpty(_code.VsNamespaceSuggestion())
  369. ? string.Format(
  370. CultureInfo.InvariantCulture,
  371. "{0}using System;{1}" +
  372. "{2}",
  373. inHeader ? Environment.NewLine : "",
  374. includeCollections ? (Environment.NewLine + "using System.Collections.Generic;") : "",
  375. inHeader ? "" : Environment.NewLine)
  376. : "";
  377. }
  378. }
  379. public class TypeMapper
  380. {
  381. private const string ExternalTypeNameAttributeName = @"http://schemas.microsoft.com/ado/2006/04/codegeneration:ExternalTypeName";
  382. private readonly System.Collections.IList _errors;
  383. private readonly CodeGenerationTools _code;
  384. private readonly MetadataTools _ef;
  385. public TypeMapper(CodeGenerationTools code, MetadataTools ef, System.Collections.IList errors)
  386. {
  387. ArgumentNotNull(code, "code");
  388. ArgumentNotNull(ef, "ef");
  389. ArgumentNotNull(errors, "errors");
  390. _code = code;
  391. _ef = ef;
  392. _errors = errors;
  393. }
  394. public string GetTypeName(TypeUsage typeUsage)
  395. {
  396. return typeUsage == null ? null : GetTypeName(typeUsage.EdmType, _ef.IsNullable(typeUsage), modelNamespace: null);
  397. }
  398. public string GetTypeName(EdmType edmType)
  399. {
  400. return GetTypeName(edmType, isNullable: null, modelNamespace: null);
  401. }
  402. public string GetTypeName(TypeUsage typeUsage, string modelNamespace)
  403. {
  404. return typeUsage == null ? null : GetTypeName(typeUsage.EdmType, _ef.IsNullable(typeUsage), modelNamespace);
  405. }
  406. public string GetTypeName(EdmType edmType, string modelNamespace)
  407. {
  408. return GetTypeName(edmType, isNullable: null, modelNamespace: modelNamespace);
  409. }
  410. public string GetTypeName(EdmType edmType, bool? isNullable, string modelNamespace)
  411. {
  412. if (edmType == null)
  413. {
  414. return null;
  415. }
  416. var collectionType = edmType as CollectionType;
  417. if (collectionType != null)
  418. {
  419. return String.Format(CultureInfo.InvariantCulture, "ICollection<{0}>", GetTypeName(collectionType.TypeUsage, modelNamespace));
  420. }
  421. var typeName = _code.Escape(edmType.MetadataProperties
  422. .Where(p => p.Name == ExternalTypeNameAttributeName)
  423. .Select(p => (string)p.Value)
  424. .FirstOrDefault())
  425. ?? (modelNamespace != null && edmType.NamespaceName != modelNamespace ?
  426. _code.CreateFullName(_code.EscapeNamespace(edmType.NamespaceName), _code.Escape(edmType)) :
  427. _code.Escape(edmType));
  428. if (edmType is StructuralType)
  429. {
  430. return typeName;
  431. }
  432. if (edmType is SimpleType)
  433. {
  434. var clrType = UnderlyingClrType(edmType);
  435. if (!IsEnumType(edmType))
  436. {
  437. typeName = _code.Escape(clrType);
  438. }
  439. return clrType.IsValueType && isNullable == true ?
  440. String.Format(CultureInfo.InvariantCulture, "Nullable<{0}>", typeName) :
  441. typeName;
  442. }
  443. throw new ArgumentException("edmType");
  444. }
  445. public Type UnderlyingClrType(EdmType edmType)
  446. {
  447. ArgumentNotNull(edmType, "edmType");
  448. var primitiveType = edmType as PrimitiveType;
  449. if (primitiveType != null)
  450. {
  451. return primitiveType.ClrEquivalentType;
  452. }
  453. if (IsEnumType(edmType))
  454. {
  455. return GetEnumUnderlyingType(edmType).ClrEquivalentType;
  456. }
  457. return typeof(object);
  458. }
  459. public object GetEnumMemberValue(MetadataItem enumMember)
  460. {
  461. ArgumentNotNull(enumMember, "enumMember");
  462. var valueProperty = enumMember.GetType().GetProperty("Value");
  463. return valueProperty == null ? null : valueProperty.GetValue(enumMember, null);
  464. }
  465. public string GetEnumMemberName(MetadataItem enumMember)
  466. {
  467. ArgumentNotNull(enumMember, "enumMember");
  468. var nameProperty = enumMember.GetType().GetProperty("Name");
  469. return nameProperty == null ? null : (string)nameProperty.GetValue(enumMember, null);
  470. }
  471. public System.Collections.IEnumerable GetEnumMembers(EdmType enumType)
  472. {
  473. ArgumentNotNull(enumType, "enumType");
  474. var membersProperty = enumType.GetType().GetProperty("Members");
  475. return membersProperty != null
  476. ? (System.Collections.IEnumerable)membersProperty.GetValue(enumType, null)
  477. : Enumerable.Empty<MetadataItem>();
  478. }
  479. public bool EnumIsFlags(EdmType enumType)
  480. {
  481. ArgumentNotNull(enumType, "enumType");
  482. var isFlagsProperty = enumType.GetType().GetProperty("IsFlags");
  483. return isFlagsProperty != null && (bool)isFlagsProperty.GetValue(enumType, null);
  484. }
  485. public bool IsEnumType(GlobalItem edmType)
  486. {
  487. ArgumentNotNull(edmType, "edmType");
  488. return edmType.GetType().Name == "EnumType";
  489. }
  490. public PrimitiveType GetEnumUnderlyingType(EdmType enumType)
  491. {
  492. ArgumentNotNull(enumType, "enumType");
  493. return (PrimitiveType)enumType.GetType().GetProperty("UnderlyingType").GetValue(enumType, null);
  494. }
  495. public string CreateLiteral(object value)
  496. {
  497. if (value == null || value.GetType() != typeof(TimeSpan))
  498. {
  499. return _code.CreateLiteral(value);
  500. }
  501. return string.Format(CultureInfo.InvariantCulture, "new TimeSpan({0})", ((TimeSpan)value).Ticks);
  502. }
  503. public bool VerifyCaseInsensitiveTypeUniqueness(IEnumerable<string> types, string sourceFile)
  504. {
  505. ArgumentNotNull(types, "types");
  506. ArgumentNotNull(sourceFile, "sourceFile");
  507. var hash = new HashSet<string>(StringComparer.InvariantCultureIgnoreCase);
  508. if (types.Any(item => !hash.Add(item)))
  509. {
  510. _errors.Add(
  511. new CompilerError(sourceFile, -1, -1, "6023",
  512. String.Format(CultureInfo.CurrentCulture, GetResourceString("Template_CaseInsensitiveTypeConflict"))));
  513. return false;
  514. }
  515. return true;
  516. }
  517. public IEnumerable<SimpleType> GetEnumItemsToGenerate(IEnumerable<GlobalItem> itemCollection)
  518. {
  519. return GetItemsToGenerate<SimpleType>(itemCollection)
  520. .Where(e => IsEnumType(e));
  521. }
  522. public IEnumerable<T> GetItemsToGenerate<T>(IEnumerable<GlobalItem> itemCollection) where T: EdmType
  523. {
  524. return itemCollection
  525. .OfType<T>()
  526. .Where(i => !i.MetadataProperties.Any(p => p.Name == ExternalTypeNameAttributeName))
  527. .OrderBy(i => i.Name);
  528. }
  529. public IEnumerable<string> GetAllGlobalItems(IEnumerable<GlobalItem> itemCollection)
  530. {
  531. return itemCollection
  532. .Where(i => i is EntityType || i is ComplexType || i is EntityContainer || IsEnumType(i))
  533. .Select(g => GetGlobalItemName(g));
  534. }
  535. public string GetGlobalItemName(GlobalItem item)
  536. {
  537. if (item is EdmType)
  538. {
  539. return ((EdmType)item).Name;
  540. }
  541. else
  542. {
  543. return ((EntityContainer)item).Name;
  544. }
  545. }
  546. public IEnumerable<EdmProperty> GetSimpleProperties(EntityType type)
  547. {
  548. return type.Properties.Where(p => p.TypeUsage.EdmType is SimpleType && p.DeclaringType == type);
  549. }
  550. public IEnumerable<EdmProperty> GetSimpleProperties(ComplexType type)
  551. {
  552. return type.Properties.Where(p => p.TypeUsage.EdmType is SimpleType && p.DeclaringType == type);
  553. }
  554. public IEnumerable<EdmProperty> GetComplexProperties(EntityType type)
  555. {
  556. return type.Properties.Where(p => p.TypeUsage.EdmType is ComplexType && p.DeclaringType == type);
  557. }
  558. public IEnumerable<EdmProperty> GetComplexProperties(ComplexType type)
  559. {
  560. return type.Properties.Where(p => p.TypeUsage.EdmType is ComplexType && p.DeclaringType == type);
  561. }
  562. public IEnumerable<EdmProperty> GetPropertiesWithDefaultValues(EntityType type)
  563. {
  564. return type.Properties.Where(p => p.TypeUsage.EdmType is SimpleType && p.DeclaringType == type && p.DefaultValue != null);
  565. }
  566. public IEnumerable<EdmProperty> GetPropertiesWithDefaultValues(ComplexType type)
  567. {
  568. return type.Properties.Where(p => p.TypeUsage.EdmType is SimpleType && p.DeclaringType == type && p.DefaultValue != null);
  569. }
  570. public IEnumerable<NavigationProperty> GetNavigationProperties(EntityType type)
  571. {
  572. return type.NavigationProperties.Where(np => np.DeclaringType == type);
  573. }
  574. public IEnumerable<NavigationProperty> GetCollectionNavigationProperties(EntityType type)
  575. {
  576. return type.NavigationProperties.Where(np => np.DeclaringType == type && np.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many);
  577. }
  578. public FunctionParameter GetReturnParameter(EdmFunction edmFunction)
  579. {
  580. ArgumentNotNull(edmFunction, "edmFunction");
  581. var returnParamsProperty = edmFunction.GetType().GetProperty("ReturnParameters");
  582. return returnParamsProperty == null
  583. ? edmFunction.ReturnParameter
  584. : ((IEnumerable<FunctionParameter>)returnParamsProperty.GetValue(edmFunction, null)).FirstOrDefault();
  585. }
  586. public bool IsComposable(EdmFunction edmFunction)
  587. {
  588. ArgumentNotNull(edmFunction, "edmFunction");
  589. var isComposableProperty = edmFunction.GetType().GetProperty("IsComposableAttribute");
  590. return isComposableProperty != null && (bool)isComposableProperty.GetValue(edmFunction, null);
  591. }
  592. public IEnumerable<FunctionImportParameter> GetParameters(EdmFunction edmFunction)
  593. {
  594. return FunctionImportParameter.Create(edmFunction.Parameters, _code, _ef);
  595. }
  596. public TypeUsage GetReturnType(EdmFunction edmFunction)
  597. {
  598. var returnParam = GetReturnParameter(edmFunction);
  599. return returnParam == null ? null : _ef.GetElementType(returnParam.TypeUsage);
  600. }
  601. public bool GenerateMergeOptionFunction(EdmFunction edmFunction, bool includeMergeOption)
  602. {
  603. var returnType = GetReturnType(edmFunction);
  604. return !includeMergeOption && returnType != null && returnType.EdmType.BuiltInTypeKind == BuiltInTypeKind.EntityType;
  605. }
  606. }
  607. public class EdmMetadataLoader
  608. {
  609. private readonly IDynamicHost _host;
  610. private readonly System.Collections.IList _errors;
  611. public EdmMetadataLoader(IDynamicHost host, System.Collections.IList errors)
  612. {
  613. ArgumentNotNull(host, "host");
  614. ArgumentNotNull(errors, "errors");
  615. _host = host;
  616. _errors = errors;
  617. }
  618. public IEnumerable<GlobalItem> CreateEdmItemCollection(string sourcePath)
  619. {
  620. ArgumentNotNull(sourcePath, "sourcePath");
  621. if (!ValidateInputPath(sourcePath))
  622. {
  623. return new EdmItemCollection();
  624. }
  625. var schemaElement = LoadRootElement(_host.ResolvePath(sourcePath));
  626. if (schemaElement != null)
  627. {
  628. using (var reader = schemaElement.CreateReader())
  629. {
  630. IList<EdmSchemaError> errors;
  631. var itemCollection = MetadataItemCollectionFactory.CreateEdmItemCollection(new[] { reader }, out errors);
  632. ProcessErrors(errors, sourcePath);
  633. return itemCollection;
  634. }
  635. }
  636. return new EdmItemCollection();
  637. }
  638. public string GetModelNamespace(string sourcePath)
  639. {
  640. ArgumentNotNull(sourcePath, "sourcePath");
  641. if (!ValidateInputPath(sourcePath))
  642. {
  643. return string.Empty;
  644. }
  645. var model = LoadRootElement(_host.ResolvePath(sourcePath));
  646. if (model == null)
  647. {
  648. return string.Empty;
  649. }
  650. var attribute = model.Attribute("Namespace");
  651. return attribute != null ? attribute.Value : "";
  652. }
  653. private bool ValidateInputPath(string sourcePath)
  654. {
  655. if (sourcePath == "$" + "edmxInputFile" + "$")
  656. {
  657. _errors.Add(
  658. new CompilerError(_host.TemplateFile ?? sourcePath, 0, 0, string.Empty,
  659. GetResourceString("Template_ReplaceVsItemTemplateToken")));
  660. return false;
  661. }
  662. return true;
  663. }
  664. public XElement LoadRootElement(string sourcePath)
  665. {
  666. ArgumentNotNull(sourcePath, "sourcePath");
  667. var root = XElement.Load(sourcePath, LoadOptions.SetBaseUri | LoadOptions.SetLineInfo);
  668. return root.Elements()
  669. .Where(e => e.Name.LocalName == "Runtime")
  670. .Elements()
  671. .Where(e => e.Name.LocalName == "ConceptualModels")
  672. .Elements()
  673. .Where(e => e.Name.LocalName == "Schema")
  674. .FirstOrDefault()
  675. ?? root;
  676. }
  677. private void ProcessErrors(IEnumerable<EdmSchemaError> errors, string sourceFilePath)
  678. {
  679. foreach (var error in errors)
  680. {
  681. _errors.Add(
  682. new CompilerError(
  683. error.SchemaLocation ?? sourceFilePath,
  684. error.Line,
  685. error.Column,
  686. error.ErrorCode.ToString(CultureInfo.InvariantCulture),
  687. error.Message)
  688. {
  689. IsWarning = error.Severity == EdmSchemaErrorSeverity.Warning
  690. });
  691. }
  692. }
  693. public bool IsLazyLoadingEnabled(EntityContainer container)
  694. {
  695. string lazyLoadingAttributeValue;
  696. var lazyLoadingAttributeName = MetadataConstants.EDM_ANNOTATION_09_02 + ":LazyLoadingEnabled";
  697. bool isLazyLoading;
  698. return !MetadataTools.TryGetStringMetadataPropertySetting(container, lazyLoadingAttributeName, out lazyLoadingAttributeValue)
  699. || !bool.TryParse(lazyLoadingAttributeValue, out isLazyLoading)
  700. || isLazyLoading;
  701. }
  702. }
  703. public static void ArgumentNotNull<T>(T arg, string name) where T : class
  704. {
  705. if (arg == null)
  706. {
  707. throw new ArgumentNullException(name);
  708. }
  709. }
  710. private static readonly Lazy<System.Resources.ResourceManager> ResourceManager =
  711. new Lazy<System.Resources.ResourceManager>(
  712. () => new System.Resources.ResourceManager("System.Data.Entity.Design", typeof(MetadataItemCollectionFactory).Assembly), isThreadSafe: true);
  713. public static string GetResourceString(string resourceName)
  714. {
  715. ArgumentNotNull(resourceName, "resourceName");
  716. return ResourceManager.Value.GetString(resourceName, null);
  717. }
  718. #>