PageRenderTime 56ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/Source/nHydrate.Generator.EFCodeFirst/Generators/IncludeTreeLINQ/IncludeTreeLINQGeneratedTemplate.cs

#
C# | 238 lines | 175 code | 24 blank | 39 comment | 17 complexity | 3ee19bc318d6821adba1888e6162db72 MD5 | raw file
Possible License(s): JSON, CC-BY-SA-3.0
  1. #region Copyright (c) 2006-2011 Widgetsphere LLC, All Rights Reserved
  2. //--------------------------------------------------------------------- *
  3. // Widgetsphere LLC *
  4. // Copyright (c) 2006-2011 All Rights reserved *
  5. // *
  6. // *
  7. //This file and its contents are protected by United States and *
  8. //International copyright laws. Unauthorized reproduction and/or *
  9. //distribution of all or any portion of the code contained herein *
  10. //is strictly prohibited and will result in severe civil and criminal *
  11. //penalties. Any violations of this copyright will be prosecuted *
  12. //to the fullest extent possible under law. *
  13. // *
  14. //THE SOURCE CODE CONTAINED HEREIN AND IN RELATED FILES IS PROVIDED *
  15. //TO THE REGISTERED DEVELOPER FOR THE PURPOSES OF EDUCATION AND *
  16. //TROUBLESHOOTING. UNDER NO CIRCUMSTANCES MAY ANY PORTION OF THE SOURCE *
  17. //CODE BE DISTRIBUTED, DISCLOSED OR OTHERWISE MADE AVAILABLE TO ANY *
  18. //THIRD PARTY WITHOUT THE EXPRESS WRITTEN CONSENT OF WIDGETSPHERE LLC *
  19. // *
  20. //UNDER NO CIRCUMSTANCES MAY THE SOURCE CODE BE USED IN WHOLE OR IN *
  21. //PART, AS THE BASIS FOR CREATING A PRODUCT THAT PROVIDES THE SAME, OR *
  22. //SUBSTANTIALLY THE SAME, FUNCTIONALITY AS ANY WIDGETSPHERE PRODUCT. *
  23. // *
  24. //THE REGISTERED DEVELOPER ACKNOWLEDGES THAT THIS SOURCE CODE *
  25. //CONTAINS VALUABLE AND PROPRIETARY TRADE SECRETS OF WIDGETSPHERE, *
  26. //INC. THE REGISTERED DEVELOPER AGREES TO EXPEND EVERY EFFORT TO *
  27. //INSURE ITS CONFIDENTIALITY. *
  28. // *
  29. //THE END USER LICENSE AGREEMENT (EULA) ACCOMPANYING THE PRODUCT *
  30. //PERMITS THE REGISTERED DEVELOPER TO REDISTRIBUTE THE PRODUCT IN *
  31. //EXECUTABLE FORM ONLY IN SUPPORT OF APPLICATIONS WRITTEN USING *
  32. //THE PRODUCT. IT DOES NOT PROVIDE ANY RIGHTS REGARDING THE *
  33. //SOURCE CODE CONTAINED HEREIN. *
  34. // *
  35. //THIS COPYRIGHT NOTICE MAY NOT BE REMOVED FROM THIS FILE. *
  36. //--------------------------------------------------------------------- *
  37. #endregion
  38. using System;
  39. using System.Linq;
  40. using System.Collections.Generic;
  41. using System.Text;
  42. using Widgetsphere.Generator.Models;
  43. using Widgetsphere.Generator.Common.Util;
  44. using System.Collections;
  45. using System.Collections.ObjectModel;
  46. using Widgetsphere.Generator.ProjectItemGenerators;
  47. namespace Widgetsphere.Generator.EFCodeFirst.Generators.IncludeTreeLINQ
  48. {
  49. class IncludeTreeLINQGeneratedTemplate : EFCodeFirstBaseTemplate
  50. {
  51. private StringBuilder sb = new StringBuilder();
  52. private Table _currentTable;
  53. public IncludeTreeLINQGeneratedTemplate(ModelRoot model, Table currentTable)
  54. {
  55. _model = model;
  56. _currentTable = currentTable;
  57. }
  58. #region BaseClassTemplate overrides
  59. public override string FileName
  60. {
  61. get { return string.Format("{0}Include.Generated.cs", _currentTable.PascalName); }
  62. }
  63. public string ParentItemName
  64. {
  65. get { return string.Format("{0}Include.cs", _currentTable.PascalName); }
  66. }
  67. public override string FileContent
  68. {
  69. get
  70. {
  71. GenerateContent();
  72. return sb.ToString();
  73. }
  74. }
  75. #endregion
  76. #region GenerateContent
  77. private void GenerateContent()
  78. {
  79. try
  80. {
  81. ValidationHelper.AppendCopyrightInCode(sb, _model);
  82. this.AppendUsingStatements();
  83. sb.AppendLine("namespace " + this.GetLocalNamespace() + ".ContextIncludeTree");
  84. sb.AppendLine("{");
  85. this.AppendClass();
  86. sb.AppendLine("}");
  87. }
  88. catch (Exception ex)
  89. {
  90. throw;
  91. }
  92. }
  93. #endregion
  94. #region namespace / objects
  95. public void AppendUsingStatements()
  96. {
  97. sb.AppendLine("using System;");
  98. sb.AppendLine("using System.Data;");
  99. sb.AppendLine("using System.Linq;");
  100. sb.AppendLine("using System.Data.Linq;");
  101. sb.AppendLine("using System.Linq.Expressions;");
  102. sb.AppendLine("using System.Data.Linq.Mapping;");
  103. sb.AppendLine("using System.Collections;");
  104. sb.AppendLine("using System.Collections.Generic;");
  105. sb.AppendLine("using " + this.GetLocalNamespace() + ";");
  106. sb.AppendLine("using Widgetsphere.EFCore.DataAccess;");
  107. sb.AppendLine();
  108. }
  109. private void AppendClass()
  110. {
  111. try
  112. {
  113. IEnumerable<Table> allTables = _currentTable.GetTableHierarchy();
  114. sb.AppendLine(" /// <summary>");
  115. sb.AppendLine(" /// This is a helper object for creating LINQ definitions for context includes on the " + _currentTable.PascalName + " collection.");
  116. sb.AppendLine(" /// </summary>");
  117. sb.AppendLine(" [Serializable()]");
  118. sb.AppendLine(" [Table(Name = \"" + _currentTable.DatabaseName + "\")]");
  119. sb.AppendLine(" public partial class " + _currentTable.PascalName + "Include : Widgetsphere.EFCore.DataAccess.IContextInclude");
  120. sb.AppendLine(" {");
  121. //Add child relationships
  122. foreach (Relation relation in _model.Database.Relations.FindByParentTable(_currentTable, true).Where(x => x.IsGenerated))
  123. {
  124. Table parentTable = (Table)relation.ParentTableRef.Object;
  125. Table childTable = (Table)relation.ChildTableRef.Object;
  126. if (childTable.AssociativeTable)
  127. {
  128. Table middleTable = childTable;
  129. var relationlist = middleTable.GetRelationsWhereChild();
  130. if (relationlist.First() == relation)
  131. childTable = (Table)relationlist.Last().ParentTableRef.Object;
  132. else
  133. childTable = (Table)relationlist.First().ParentTableRef.Object;
  134. if (childTable.Generated &&
  135. parentTable.Generated &&
  136. !childTable.IsInheritedFrom(parentTable) &&
  137. (!allTables.Contains(childTable)))
  138. {
  139. sb.AppendLine(" /// <summary>");
  140. sb.AppendLine(" /// This is a mapping of the relationship with the " + childTable.PascalName + " entity. This is a N:M relation with two relationships though an intermediary table. (" + parentTable.PascalName + " -> " + middleTable.PascalName + " -> " + childTable.PascalName + ")");
  141. sb.AppendLine(" /// </summary>");
  142. //sb.AppendLine(" [Association(ThisKey = \"" + thisKey + "\", OtherKey = \"" + otherKey + "\")]");
  143. if (relation.IsOneToOne)
  144. sb.AppendLine(" public " + this.GetLocalNamespace() + ".ContextIncludeTree." + childTable.PascalName + "Include " + relation.PascalRoleName + childTable.PascalName + " { get; private set; }");
  145. else
  146. sb.AppendLine(" public " + this.GetLocalNamespace() + ".ContextIncludeTree." + childTable.PascalName + "Include " + relation.PascalRoleName + childTable.PascalName + "List { get; private set; }");
  147. }
  148. }
  149. else
  150. {
  151. string thisKey = "";
  152. string otherKey = "";
  153. foreach (ColumnRelationship columnRelationship in relation.ColumnRelationships)
  154. {
  155. thisKey += ((Column)columnRelationship.ParentColumnRef.Object).PascalName + ",";
  156. otherKey += ((Column)columnRelationship.ChildColumnRef.Object).PascalName + ",";
  157. }
  158. if (!string.IsNullOrEmpty(thisKey)) thisKey = thisKey.Substring(0, thisKey.Length - 1);
  159. if (!string.IsNullOrEmpty(otherKey)) otherKey = otherKey.Substring(0, otherKey.Length - 1);
  160. if (childTable.Generated &&
  161. parentTable.Generated &&
  162. !childTable.IsInheritedFrom(parentTable) &&
  163. (!allTables.Contains(childTable)))
  164. {
  165. sb.AppendLine(" /// <summary>");
  166. sb.AppendLine(" /// This is a mapping of the relationship with the " + childTable.PascalName + " entity." + (relation.PascalRoleName == "" ? "" : " (Role: '" + relation.RoleName + "')"));
  167. sb.AppendLine(" /// </summary>");
  168. sb.AppendLine(" [Association(ThisKey = \"" + thisKey + "\", OtherKey = \"" + otherKey + "\")]");
  169. if (relation.IsOneToOne)
  170. sb.AppendLine(" public " + this.GetLocalNamespace() + ".ContextIncludeTree." + childTable.PascalName + "Include " + relation.PascalRoleName + childTable.PascalName + " { get; private set; }");
  171. else
  172. sb.AppendLine(" public " + this.GetLocalNamespace() + ".ContextIncludeTree." + childTable.PascalName + "Include " + relation.PascalRoleName + childTable.PascalName + "List { get; private set; }");
  173. }
  174. }
  175. }
  176. //Add parent relationships
  177. foreach (Relation relation in _model.Database.Relations.FindByChildTable(_currentTable, true).Where(x => x.IsGenerated))
  178. {
  179. Table parentTable = (Table)relation.ParentTableRef.Object;
  180. Table childTable = (Table)relation.ChildTableRef.Object;
  181. //Do not process self-referencing relationships
  182. if (parentTable != _currentTable)
  183. {
  184. string thisKey = "";
  185. string otherKey = "";
  186. foreach (ColumnRelationship columnRelationship in relation.ColumnRelationships)
  187. {
  188. thisKey += ((Column)columnRelationship.ChildColumnRef.Object).PascalName + ",";
  189. otherKey += ((Column)columnRelationship.ParentColumnRef.Object).PascalName + ",";
  190. }
  191. if (!string.IsNullOrEmpty(thisKey)) thisKey = thisKey.Substring(0, thisKey.Length - 1);
  192. if (!string.IsNullOrEmpty(otherKey)) otherKey = otherKey.Substring(0, otherKey.Length - 1);
  193. if ((parentTable.Generated) && (!allTables.Contains(parentTable)))
  194. {
  195. sb.AppendLine(" /// <summary>");
  196. sb.AppendLine(" /// This is a mapping of the relationship with the " + parentTable.PascalName + " entity." + (relation.PascalRoleName == "" ? "" : " (Role: '" + relation.RoleName + "')"));
  197. sb.AppendLine(" /// </summary>");
  198. sb.AppendLine(" [Association(ThisKey = \"" + thisKey + "\", OtherKey = \"" + otherKey + "\")]");
  199. sb.AppendLine(" public " + this.GetLocalNamespace() + ".ContextIncludeTree." + parentTable.PascalName + "Include " + relation.PascalRoleName + parentTable.PascalName + " { get; private set; }");
  200. }
  201. }
  202. }
  203. sb.AppendLine();
  204. sb.AppendLine(" }");
  205. sb.AppendLine();
  206. }
  207. catch (Exception ex)
  208. {
  209. throw;
  210. }
  211. }
  212. #endregion
  213. }
  214. }