PageRenderTime 56ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/Source/nHydrate.Generator.EFDAL/Generators/LINQ/BusinessObjectLINQGeneratedTemplate.cs

#
C# | 277 lines | 202 code | 35 blank | 40 comment | 17 complexity | 68338d55849908d124d3c0db02289aaa MD5 | raw file
Possible License(s): JSON, CC-BY-SA-3.0
  1. #region Copyright (c) 2006-2012 nHydrate.org, All Rights Reserved
  2. //--------------------------------------------------------------------- *
  3. // NHYDRATE.ORG *
  4. // Copyright (c) 2006-2012 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 THE NHYDRATE GROUP *
  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 THIS PRODUCT *
  23. // *
  24. //THE REGISTERED DEVELOPER ACKNOWLEDGES THAT THIS SOURCE CODE *
  25. //CONTAINS VALUABLE AND PROPRIETARY TRADE SECRETS OF NHYDRATE, *
  26. //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.Text;
  41. using nHydrate.Generator.Models;
  42. namespace nHydrate.Generator.EFDAL.Generators.LINQ
  43. {
  44. class LINQGeneratedTemplate : EFDALBaseTemplate
  45. {
  46. private readonly StringBuilder sb = new StringBuilder();
  47. public LINQGeneratedTemplate(ModelRoot model)
  48. {
  49. _model = model;
  50. }
  51. #region BaseClassTemplate overrides
  52. public override string FileName
  53. {
  54. get { return _model.ProjectName + "EntitiesQueries.Generated.cs"; }
  55. }
  56. public string ParentItemName
  57. {
  58. get { return _model.ProjectName + "EntitiesQueries.cs"; }
  59. }
  60. public override string FileContent
  61. {
  62. get
  63. {
  64. GenerateContent();
  65. return sb.ToString();
  66. }
  67. }
  68. #endregion
  69. #region GenerateContent
  70. private void GenerateContent()
  71. {
  72. try
  73. {
  74. nHydrate.Generator.GenerationHelper.AppendCopyrightInCode(sb, _model);
  75. this.AppendUsingStatements();
  76. sb.AppendLine("namespace " + this.GetLocalNamespace());
  77. sb.AppendLine("{");
  78. foreach (var table in _model.Database.Tables.Where(x => x.Generated).OrderBy(x => x.Name))
  79. {
  80. this.AppendTableClass(table);
  81. }
  82. sb.AppendLine("}");
  83. }
  84. catch (Exception ex)
  85. {
  86. throw;
  87. }
  88. }
  89. #endregion
  90. #region namespace / objects
  91. public void AppendUsingStatements()
  92. {
  93. sb.AppendLine("using System;");
  94. sb.AppendLine("using System.Data;");
  95. sb.AppendLine("using System.Linq;");
  96. sb.AppendLine("using System.Data.Linq;");
  97. sb.AppendLine("using System.Linq.Expressions;");
  98. sb.AppendLine("using System.Data.Linq.Mapping;");
  99. sb.AppendLine("using System.Collections;");
  100. sb.AppendLine("using System.Collections.Generic;");
  101. sb.AppendLine("using " + this.GetLocalNamespace() + ";");
  102. sb.AppendLine("using nHydrate.EFCore.DataAccess;");
  103. sb.AppendLine();
  104. }
  105. private void AppendTableClass(Table table)
  106. {
  107. try
  108. {
  109. sb.AppendLine(" #region " + table.PascalName + "Query");
  110. sb.AppendLine();
  111. sb.AppendLine(" /// <summary>");
  112. sb.AppendLine(" /// This is a helper object for running LINQ queries on the " + table.PascalName + " collection.");
  113. sb.AppendLine(" /// </summary>");
  114. sb.AppendLine(" [Serializable()]");
  115. sb.AppendLine(" [Table(Name = \"" + table.DatabaseName + "\")]");
  116. sb.AppendLine(" [System.CodeDom.Compiler.GeneratedCode(\"nHydrateModelGenerator\", \"" + _model.ModelToolVersion + "\")]");
  117. sb.AppendLine(" public partial class " + table.PascalName + "Query : nHydrate.EFCore.DataAccess.IBusinessObjectLINQQuery");
  118. sb.AppendLine(" {");
  119. sb.AppendLine(" #region Properties");
  120. var allTables = table.GetTableHierarchy();
  121. var columnList = table.GetColumnsFullHierarchy().Where(x => x.Generated).ToList();
  122. foreach (var c in columnList.OrderBy(x => x.Name))
  123. {
  124. var description = c.Description.Trim();
  125. if (!string.IsNullOrEmpty(description)) description += "\r\n ///";
  126. description += "(Maps to the '" + table.DatabaseName + "." + c.DatabaseName + "' database field)";
  127. sb.AppendLine(" /// <summary>");
  128. sb.AppendLine(" /// " + description);
  129. sb.AppendLine(" /// </summary>");
  130. sb.Append(" [Column(");
  131. sb.Append("Name = \"" + c.DatabaseName + "\", ");
  132. sb.Append("DbType = \"" + c.DatabaseTypeRaw + "\", ");
  133. sb.Append("CanBeNull = " + c.AllowNull.ToString().ToLower() + ", ");
  134. sb.Append("IsPrimaryKey = " + table.PrimaryKeyColumns.Contains(c).ToString().ToLower());
  135. sb.AppendLine(")]");
  136. sb.AppendLine(" public virtual " + c.GetCodeType(true) + " " + c.PascalName + " { get; set; }");
  137. }
  138. if (table.AllowCreateAudit)
  139. {
  140. sb.AppendLine(" /// <summary>");
  141. sb.AppendLine(" /// The date of creation");
  142. sb.AppendLine(" /// </summary>");
  143. sb.AppendLine(" [Column(Name = \"" + _model.Database.CreatedDateColumnName + "\", DbType = \"DateTime\", CanBeNull = true)]");
  144. sb.AppendLine(" public virtual DateTime? " + _model.Database.CreatedDatePascalName + " { get; set; }");
  145. sb.AppendLine(" /// <summary>");
  146. sb.AppendLine(" /// The name of the creating entity");
  147. sb.AppendLine(" /// </summary>");
  148. sb.AppendLine(" [Column(Name = \"" + _model.Database.CreatedByColumnName + "\", DbType = \"VarChar(100)\", CanBeNull = true)]");
  149. sb.AppendLine(" public virtual string " + _model.Database.CreatedByPascalName + " { get; set; }");
  150. }
  151. if (table.AllowModifiedAudit)
  152. {
  153. sb.AppendLine(" /// <summary>");
  154. sb.AppendLine(" /// The date of last modification");
  155. sb.AppendLine(" /// </summary>");
  156. sb.AppendLine(" [Column(Name = \"" + _model.Database.ModifiedDateColumnName + "\", DbType = \"DateTime\", CanBeNull = true)]");
  157. sb.AppendLine(" public virtual DateTime? " + _model.Database.ModifiedDatePascalName + " { get; set; }");
  158. sb.AppendLine(" /// <summary>");
  159. sb.AppendLine(" /// The name of the last modifing entity");
  160. sb.AppendLine(" /// </summary>");
  161. sb.AppendLine(" [Column(Name = \"" + _model.Database.ModifiedByColumnName + "\", DbType = \"VarChar(100)\", CanBeNull = true)]");
  162. sb.AppendLine(" public virtual string " + _model.Database.ModifiedByPascalName + " { get; set; }");
  163. }
  164. if (table.AllowTimestamp)
  165. {
  166. sb.AppendLine(" /// <summary>");
  167. sb.AppendLine(" /// This is an internal field and is not to be used.");
  168. sb.AppendLine(" /// </summary>");
  169. sb.AppendLine(" [Column(Name = \"" + _model.Database.TimestampColumnName + "\", DbType = \"Binary\", CanBeNull = false)]");
  170. sb.AppendLine(" public virtual byte[] " + _model.Database.TimestampPascalName + " { get; set; }");
  171. }
  172. //Add child relationships
  173. foreach (var relation in _model.Database.Relations.FindByParentTable(table, true).Where(x => x.IsGenerated))
  174. {
  175. //Relation relation = (Relation)reference.Object;
  176. var parentTable = (Table)relation.ParentTableRef.Object;
  177. var childTable = (Table)relation.ChildTableRef.Object;
  178. //Column pkColumn = (Column)relation.ColumnRelationships[0].ChildColumnRef.Object;
  179. var thisKey = string.Empty;
  180. var otherKey = string.Empty;
  181. foreach (ColumnRelationship columnRelationship in relation.ColumnRelationships)
  182. {
  183. thisKey += ((Column)columnRelationship.ParentColumnRef.Object).PascalName + ",";
  184. otherKey += ((Column)columnRelationship.ChildColumnRef.Object).PascalName + ",";
  185. }
  186. if (!string.IsNullOrEmpty(thisKey)) thisKey = thisKey.Substring(0, thisKey.Length - 1);
  187. if (!string.IsNullOrEmpty(otherKey)) otherKey = otherKey.Substring(0, otherKey.Length - 1);
  188. if (childTable.Generated && (!allTables.Contains(childTable)))
  189. {
  190. sb.AppendLine(" /// <summary>");
  191. sb.AppendLine(" /// This is a mapping of the relationship with the " + childTable.PascalName + " entity." + (relation.PascalRoleName == "" ? "" : " (Role: '" + relation.RoleName + "')"));
  192. sb.AppendLine(" /// </summary>");
  193. sb.AppendLine(" [Association(ThisKey = \"" + thisKey + "\", OtherKey = \"" + otherKey + "\")]");
  194. if (relation.IsOneToOne)
  195. sb.AppendLine(" public " + this.GetLocalNamespace() + "." + childTable.PascalName + "Query " + relation.PascalRoleName + childTable.PascalName + " { get; private set; }");
  196. else
  197. sb.AppendLine(" public " + this.GetLocalNamespace() + "." + childTable.PascalName + "Query " + relation.PascalRoleName + childTable.PascalName + "List { get; private set; }");
  198. }
  199. }
  200. //Add parent relationships
  201. foreach (var relation in _model.Database.Relations.FindByChildTable(table, true).Where(x => x.IsGenerated))
  202. {
  203. var parentTable = (Table)relation.ParentTableRef.Object;
  204. var childTable = (Table)relation.ChildTableRef.Object;
  205. //Do not process self-referencing relationships
  206. if (parentTable != table)
  207. {
  208. var thisKey = string.Empty;
  209. var otherKey = string.Empty;
  210. foreach (ColumnRelationship columnRelationship in relation.ColumnRelationships)
  211. {
  212. thisKey += ((Column)columnRelationship.ChildColumnRef.Object).PascalName + ",";
  213. otherKey += ((Column)columnRelationship.ParentColumnRef.Object).PascalName + ",";
  214. }
  215. if (!string.IsNullOrEmpty(thisKey)) thisKey = thisKey.Substring(0, thisKey.Length - 1);
  216. if (!string.IsNullOrEmpty(otherKey)) otherKey = otherKey.Substring(0, otherKey.Length - 1);
  217. if (parentTable.Generated && (!allTables.Contains(parentTable)))
  218. {
  219. sb.AppendLine(" /// <summary>");
  220. sb.AppendLine(" /// This is a mapping of the relationship with the " + parentTable.PascalName + " entity." + (relation.PascalRoleName == "" ? "" : " (Role: '" + relation.RoleName + "')"));
  221. sb.AppendLine(" /// </summary>");
  222. sb.AppendLine(" [Association(ThisKey = \"" + thisKey + "\", OtherKey = \"" + otherKey + "\")]");
  223. sb.AppendLine(" public " + this.GetLocalNamespace() + "." + parentTable.PascalName + "Query " + relation.PascalRoleName + parentTable.PascalName + " { get; private set; }");
  224. }
  225. }
  226. }
  227. sb.AppendLine();
  228. sb.AppendLine(" #endregion");
  229. sb.AppendLine();
  230. sb.AppendLine(" }");
  231. sb.AppendLine();
  232. sb.AppendLine(" #endregion");
  233. sb.AppendLine();
  234. }
  235. catch (Exception ex)
  236. {
  237. throw;
  238. }
  239. }
  240. #endregion
  241. }
  242. }