EMISOnline.Entities.tt 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. <#@ template language="C#" debug="false" hostspecific="true"#>
  2. <#@ include file="EF.Utility.CS.ttinclude"#><#@
  3. output extension=".cs"#><#
  4. CodeGenerationTools code = new CodeGenerationTools(this);
  5. MetadataLoader loader = new MetadataLoader(this);
  6. CodeRegion region = new CodeRegion(this, 1);
  7. MetadataTools ef = new MetadataTools(this);
  8. string inputFile = @"EMISOnlineContext.edmx";
  9. EdmItemCollection ItemCollection = loader.CreateEdmItemCollection(inputFile);
  10. string namespaceName = code.VsNamespaceSuggestion();
  11. EntityFrameworkTemplateFileManager fileManager = EntityFrameworkTemplateFileManager.Create(this);
  12. WriteHeader(fileManager);
  13. string summary=string.Empty;
  14. foreach (EntityType entity in ItemCollection.GetItems<EntityType>().OrderBy(e => e.Name))
  15. {
  16. fileManager.StartNewFile(entity.Name + ".cs");
  17. BeginNamespace(namespaceName, code);
  18. if(entity.Documentation !=null && entity.Documentation.Summary!=null)
  19. summary=entity.Documentation.Summary;
  20. else
  21. summary=entity.Name;
  22. #>
  23. #pragma warning disable 1573
  24. using System;
  25. using System.Collections.Generic;
  26. /// <summary>
  27. /// <#=summary#>
  28. /// </summary>
  29. <#=Accessibility.ForType(entity)#> <#=code.SpaceAfter(code.AbstractOption(entity))#>partial class <#=code.Escape(entity)#><#=code.StringBefore(" : ", code.Escape(entity.BaseType))#>
  30. {
  31. <#
  32. var propertiesWithDefaultValues = entity.Properties.Where(p => p.TypeUsage.EdmType is PrimitiveType && p.DeclaringType == entity && p.DefaultValue != null);
  33. var collectionNavigationProperties = entity.NavigationProperties.Where(np => np.DeclaringType == entity && np.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many);
  34. var complexProperties = entity.Properties.Where(p => p.TypeUsage.EdmType is ComplexType && p.DeclaringType == entity);
  35. if (propertiesWithDefaultValues.Any() || collectionNavigationProperties.Any() || complexProperties.Any())
  36. {
  37. #>
  38. public <#=code.Escape(entity)#>()
  39. {
  40. <#
  41. foreach (var edmProperty in propertiesWithDefaultValues)
  42. {
  43. #>
  44. this.<#=code.Escape(edmProperty)#> = <#=code.CreateLiteral(edmProperty.DefaultValue)#>;
  45. <#
  46. }
  47. foreach (var navigationProperty in collectionNavigationProperties)
  48. {
  49. #>
  50. this.<#=code.Escape(navigationProperty)#> = new HashSet<<#=code.Escape(navigationProperty.ToEndMember.GetEntityType())#>>();
  51. <#
  52. }
  53. foreach (var complexProperty in complexProperties)
  54. {
  55. #>
  56. this.<#=code.Escape(complexProperty)#> = new <#=code.Escape(complexProperty.TypeUsage)#>();
  57. <#
  58. }
  59. #>
  60. }
  61. <#
  62. }
  63. var primitiveProperties = entity.Properties.Where(p => p.TypeUsage.EdmType is PrimitiveType && p.DeclaringType == entity);
  64. if (primitiveProperties.Any())
  65. {
  66. foreach (var edmProperty in primitiveProperties)
  67. {
  68. WriteProperty(code, edmProperty);
  69. }
  70. }
  71. if (complexProperties.Any())
  72. {
  73. #>
  74. <#
  75. foreach(var complexProperty in complexProperties)
  76. {
  77. WriteProperty(code, complexProperty);
  78. }
  79. }
  80. var navigationProperties = entity.NavigationProperties.Where(np => np.DeclaringType == entity);
  81. if (navigationProperties.Any())
  82. {
  83. #>
  84. <#
  85. foreach (var navigationProperty in navigationProperties)
  86. {
  87. WriteNavigationProperty(code, navigationProperty);
  88. }
  89. }
  90. #>
  91. }
  92. <#
  93. EndNamespace(namespaceName);
  94. }
  95. foreach (var complex in ItemCollection.GetItems<ComplexType>().OrderBy(e => e.Name))
  96. {
  97. fileManager.StartNewFile(complex.Name + ".cs");
  98. BeginNamespace(namespaceName, code);
  99. #>
  100. using System;
  101. <#=Accessibility.ForType(complex)#> partial class <#=code.Escape(complex)#>
  102. {
  103. <#
  104. var complexProperties = complex.Properties.Where(p => p.TypeUsage.EdmType is ComplexType && p.DeclaringType == complex);
  105. var propertiesWithDefaultValues = complex.Properties.Where(p => p.TypeUsage.EdmType is PrimitiveType && p.DeclaringType == complex && p.DefaultValue != null);
  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)#> = <#=code.CreateLiteral(edmProperty.DefaultValue)#>;
  116. <#
  117. }
  118. foreach (var complexProperty in complexProperties)
  119. {
  120. #>
  121. this.<#=code.Escape(complexProperty)#> = new <#=code.Escape(complexProperty.TypeUsage)#>();
  122. <#
  123. }
  124. #>
  125. }
  126. <#
  127. }
  128. var primitiveProperties = complex.Properties.Where(p => p.TypeUsage.EdmType is PrimitiveType && p.DeclaringType == complex);
  129. if (primitiveProperties.Any())
  130. {
  131. foreach(var edmProperty in primitiveProperties)
  132. {
  133. WriteProperty(code, edmProperty);
  134. }
  135. }
  136. if (complexProperties.Any())
  137. {
  138. #>
  139. <#
  140. foreach(var edmProperty in complexProperties)
  141. {
  142. WriteProperty(code, edmProperty);
  143. }
  144. }
  145. #>
  146. }
  147. <#
  148. EndNamespace(namespaceName);
  149. }
  150. if (!VerifyTypesAreCaseInsensitiveUnique(ItemCollection))
  151. {
  152. return "";
  153. }
  154. fileManager.Process();
  155. #>
  156. <#+
  157. void WriteHeader(EntityFrameworkTemplateFileManager fileManager)
  158. {
  159. fileManager.StartHeader();
  160. #>
  161. //------------------------------------------------------------------------------
  162. // <auto-generated>
  163. // This code was generated from a template.
  164. //
  165. // Manual changes to this file may cause unexpected behavior in your application.
  166. // Manual changes to this file will be overwritten if the code is regenerated.
  167. // </auto-generated>
  168. //------------------------------------------------------------------------------
  169. <#+
  170. fileManager.EndBlock();
  171. }
  172. void BeginNamespace(string namespaceName, CodeGenerationTools code)
  173. {
  174. CodeRegion region = new CodeRegion(this);
  175. if (!String.IsNullOrEmpty(namespaceName))
  176. {
  177. #>
  178. namespace <#=code.EscapeNamespace(namespaceName)#>
  179. {
  180. <#+
  181. PushIndent(CodeRegion.GetIndent(1));
  182. }
  183. }
  184. void EndNamespace(string namespaceName)
  185. {
  186. if (!String.IsNullOrEmpty(namespaceName))
  187. {
  188. PopIndent();
  189. #>
  190. }
  191. <#+
  192. }
  193. }
  194. void WriteProperty(CodeGenerationTools code, EdmProperty edmProperty)
  195. {
  196. if (edmProperty.Documentation != null && edmProperty.Documentation.Summary != null)
  197. {
  198. WriteProperty(Accessibility.ForProperty(edmProperty),
  199. code.Escape(edmProperty.Documentation.Summary),
  200. code.Escape(edmProperty.TypeUsage),
  201. code.Escape(edmProperty),
  202. code.SpaceAfter(Accessibility.ForGetter(edmProperty)),
  203. code.SpaceAfter(Accessibility.ForSetter(edmProperty)));
  204. }
  205. else
  206. {
  207. WriteProperty(Accessibility.ForProperty(edmProperty),
  208. code.Escape(edmProperty.Name),
  209. code.Escape(edmProperty.TypeUsage),
  210. code.Escape(edmProperty),
  211. code.SpaceAfter(Accessibility.ForGetter(edmProperty)),
  212. code.SpaceAfter(Accessibility.ForSetter(edmProperty)));
  213. }
  214. }
  215. void WriteNavigationProperty(CodeGenerationTools code, NavigationProperty navigationProperty)
  216. {
  217. var endType = code.Escape(navigationProperty.ToEndMember.GetEntityType());
  218. if (navigationProperty.Documentation != null && navigationProperty.Documentation.Summary != null)
  219. {
  220. WriteProperty(PropertyVirtualModifier(Accessibility.ForProperty(navigationProperty)),
  221. code.Escape(navigationProperty.Documentation.Summary),
  222. navigationProperty.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many ? ("HashSet<" + endType + ">") : endType,
  223. code.Escape(navigationProperty),
  224. code.SpaceAfter(Accessibility.ForGetter(navigationProperty)),
  225. code.SpaceAfter(Accessibility.ForSetter(navigationProperty)));
  226. }
  227. else
  228. {
  229. WriteProperty(PropertyVirtualModifier(Accessibility.ForProperty(navigationProperty)),
  230. code.Escape(navigationProperty.Name),
  231. navigationProperty.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many ? ("HashSet<" + endType + ">") : endType,
  232. code.Escape(navigationProperty),
  233. code.SpaceAfter(Accessibility.ForGetter(navigationProperty)),
  234. code.SpaceAfter(Accessibility.ForSetter(navigationProperty)));
  235. }
  236. }
  237. void WriteProperty(string accessibility, string summary, string type, string name, string getterAccessibility, string setterAccessibility)
  238. {
  239. #>
  240. /// <summary>
  241. /// <#=summary#>
  242. /// </summary>
  243. <#=accessibility#> <#=type#> <#=name#> { <#=getterAccessibility#>get; <#=setterAccessibility#>set; }
  244. <#+
  245. }
  246. string PropertyVirtualModifier(string accessibility)
  247. {
  248. return accessibility + (accessibility != "private" ? " virtual" : "");
  249. }
  250. bool VerifyTypesAreCaseInsensitiveUnique(EdmItemCollection itemCollection)
  251. {
  252. var alreadySeen = new Dictionary<string, bool>(StringComparer.OrdinalIgnoreCase);
  253. foreach(var type in itemCollection.GetItems<StructuralType>())
  254. {
  255. if (!(type is EntityType || type is ComplexType))
  256. {
  257. continue;
  258. }
  259. if (alreadySeen.ContainsKey(type.FullName))
  260. {
  261. Error(String.Format(CultureInfo.CurrentCulture, "This template does not support types that differ only by case, the types {0} are not supported", type.FullName));
  262. return false;
  263. }
  264. else
  265. {
  266. alreadySeen.Add(type.FullName, true);
  267. }
  268. }
  269. return true;
  270. }
  271. #>