PageRenderTime 73ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
C# | 272 lines | 201 code | 31 blank | 40 comment | 17 complexity | e5c7988fa3216085946ab128e9e9d353 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.LINQ
  48. {
  49. class LINQGeneratedTemplate : EFCodeFirstBaseTemplate
  50. {
  51. private StringBuilder sb = new StringBuilder();
  52. private Table _currentTable;
  53. public LINQGeneratedTemplate(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}Query.Generated.cs", _currentTable.PascalName); }
  62. }
  63. public string ParentItemName
  64. {
  65. get { return string.Format("{0}Query.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() + ".LINQ");
  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. sb.AppendLine(" /// <summary>");
  114. sb.AppendLine(" /// This is a helper object for running LINQ queries on the " + _currentTable.PascalName + " collection.");
  115. sb.AppendLine(" /// </summary>");
  116. sb.AppendLine(" [Serializable()]");
  117. sb.AppendLine(" [Table(Name = \"" + _currentTable.DatabaseName + "\")]");
  118. sb.AppendLine(" public partial class " + _currentTable.PascalName + "Query : Widgetsphere.EFCore.DataAccess.IBusinessObjectLINQQuery");
  119. sb.AppendLine(" {");
  120. sb.AppendLine(" #region Properties");
  121. IEnumerable<Table> allTables = _currentTable.GetTableHierarchy();
  122. List<Column> columnList = _currentTable.GetColumnsFullHierarchy().Where(x => x.Generated).ToList();
  123. foreach (Column c in columnList.OrderBy(x => x.Name))
  124. {
  125. string description = c.Description.Trim();
  126. if (!string.IsNullOrEmpty(description)) description += "\r\n ///";
  127. description += "(Maps to the '" + _currentTable.DatabaseName + "." + c.DatabaseName + "' database field)";
  128. sb.AppendLine(" /// <summary>");
  129. sb.AppendLine(" /// " + description);
  130. sb.AppendLine(" /// </summary>");
  131. sb.Append(" [Column(");
  132. sb.Append("Name = \"" + c.DatabaseName + "\", ");
  133. sb.Append("DbType = \"" + c.DatabaseTypeRaw + "\", ");
  134. sb.Append("CanBeNull = " + c.AllowNull.ToString().ToLower() + ", ");
  135. sb.Append("IsPrimaryKey = " + _currentTable.PrimaryKeyColumns.Contains(c).ToString().ToLower());
  136. sb.AppendLine(")]");
  137. sb.AppendLine(" public virtual " + c.GetCodeType(true) + " " + c.PascalName + " { get; set; }");
  138. }
  139. if (_currentTable.AllowCreateAudit)
  140. {
  141. sb.AppendLine(" /// <summary>");
  142. sb.AppendLine(" /// The date of creation");
  143. sb.AppendLine(" /// </summary>");
  144. sb.AppendLine(" [Column(Name = \"" + _model.Database.CreatedDateColumnName + "\", DbType = \"DateTime\", CanBeNull = true)]");
  145. sb.AppendLine(" public virtual DateTime? " + _model.Database.CreatedDatePascalName + " { get; set; }");
  146. sb.AppendLine(" /// <summary>");
  147. sb.AppendLine(" /// The name of the creating entity");
  148. sb.AppendLine(" /// </summary>");
  149. sb.AppendLine(" [Column(Name = \"" + _model.Database.CreatedByColumnName + "\", DbType = \"VarChar(100)\", CanBeNull = true)]");
  150. sb.AppendLine(" public virtual string " + _model.Database.CreatedByPascalName + " { get; set; }");
  151. }
  152. if (_currentTable.AllowModifiedAudit)
  153. {
  154. sb.AppendLine(" /// <summary>");
  155. sb.AppendLine(" /// The date of last modification");
  156. sb.AppendLine(" /// </summary>");
  157. sb.AppendLine(" [Column(Name = \"" + _model.Database.ModifiedDateColumnName + "\", DbType = \"DateTime\", CanBeNull = true)]");
  158. sb.AppendLine(" public virtual DateTime? " + _model.Database.ModifiedDatePascalName + " { get; set; }");
  159. sb.AppendLine(" /// <summary>");
  160. sb.AppendLine(" /// The name of the last modifing entity");
  161. sb.AppendLine(" /// </summary>");
  162. sb.AppendLine(" [Column(Name = \"" + _model.Database.ModifiedByColumnName + "\", DbType = \"VarChar(100)\", CanBeNull = true)]");
  163. sb.AppendLine(" public virtual string " + _model.Database.ModifiedByPascalName + " { get; set; }");
  164. }
  165. if (_currentTable.AllowTimestamp)
  166. {
  167. sb.AppendLine(" /// <summary>");
  168. sb.AppendLine(" /// This is an internal field and is not to be used.");
  169. sb.AppendLine(" /// </summary>");
  170. sb.AppendLine(" [Column(Name = \"" + _model.Database.TimestampColumnName + "\", DbType = \"Binary\", CanBeNull = false)]");
  171. sb.AppendLine(" public virtual byte[] " + _model.Database.TimestampPascalName + " { get; set; }");
  172. }
  173. //Add child relationships
  174. foreach (Relation relation in _model.Database.Relations.FindByParentTable(_currentTable, true).Where(x => x.IsGenerated))
  175. {
  176. //Relation relation = (Relation)reference.Object;
  177. Table parentTable = (Table)relation.ParentTableRef.Object;
  178. Table childTable = (Table)relation.ChildTableRef.Object;
  179. //Column pkColumn = (Column)relation.ColumnRelationships[0].ChildColumnRef.Object;
  180. string thisKey = "";
  181. string otherKey = "";
  182. foreach (ColumnRelationship columnRelationship in relation.ColumnRelationships)
  183. {
  184. thisKey += ((Column)columnRelationship.ParentColumnRef.Object).PascalName + ",";
  185. otherKey += ((Column)columnRelationship.ChildColumnRef.Object).PascalName + ",";
  186. }
  187. if (!string.IsNullOrEmpty(thisKey)) thisKey = thisKey.Substring(0, thisKey.Length - 1);
  188. if (!string.IsNullOrEmpty(otherKey)) otherKey = otherKey.Substring(0, otherKey.Length - 1);
  189. if (childTable.Generated && (!allTables.Contains(childTable)))
  190. {
  191. sb.AppendLine(" /// <summary>");
  192. sb.AppendLine(" /// This is a mapping of the relationship with the " + childTable.PascalName + " entity." + (relation.PascalRoleName == "" ? "" : " (Role: '" + relation.RoleName + "')"));
  193. sb.AppendLine(" /// </summary>");
  194. sb.AppendLine(" [Association(ThisKey = \"" + thisKey + "\", OtherKey = \"" + otherKey + "\")]");
  195. if (relation.IsOneToOne)
  196. sb.AppendLine(" public " + this.GetLocalNamespace() + ".LINQ." + childTable.PascalName + "Query " + relation.PascalRoleName + childTable.PascalName + " { get; private set; }");
  197. else
  198. sb.AppendLine(" public " + this.GetLocalNamespace() + ".LINQ." + childTable.PascalName + "Query " + relation.PascalRoleName + childTable.PascalName + "List { get; private set; }");
  199. }
  200. }
  201. //Add parent relationships
  202. foreach (Relation relation in _model.Database.Relations.FindByChildTable(_currentTable, true).Where(x => x.IsGenerated))
  203. {
  204. Table parentTable = (Table)relation.ParentTableRef.Object;
  205. Table childTable = (Table)relation.ChildTableRef.Object;
  206. //Do not process self-referencing relationships
  207. if (parentTable != _currentTable)
  208. {
  209. string thisKey = "";
  210. string otherKey = "";
  211. foreach (ColumnRelationship columnRelationship in relation.ColumnRelationships)
  212. {
  213. thisKey += ((Column)columnRelationship.ChildColumnRef.Object).PascalName + ",";
  214. otherKey += ((Column)columnRelationship.ParentColumnRef.Object).PascalName + ",";
  215. }
  216. if (!string.IsNullOrEmpty(thisKey)) thisKey = thisKey.Substring(0, thisKey.Length - 1);
  217. if (!string.IsNullOrEmpty(otherKey)) otherKey = otherKey.Substring(0, otherKey.Length - 1);
  218. if (parentTable.Generated && (!allTables.Contains(parentTable)))
  219. {
  220. sb.AppendLine(" /// <summary>");
  221. sb.AppendLine(" /// This is a mapping of the relationship with the " + parentTable.PascalName + " entity." + (relation.PascalRoleName == "" ? "" : " (Role: '" + relation.RoleName + "')"));
  222. sb.AppendLine(" /// </summary>");
  223. sb.AppendLine(" [Association(ThisKey = \"" + thisKey + "\", OtherKey = \"" + otherKey + "\")]");
  224. sb.AppendLine(" public " + this.GetLocalNamespace() + ".LINQ." + parentTable.PascalName + "Query " + relation.PascalRoleName + parentTable.PascalName + " { get; private set; }");
  225. }
  226. }
  227. }
  228. sb.AppendLine();
  229. sb.AppendLine(" #endregion");
  230. sb.AppendLine();
  231. sb.AppendLine(" }");
  232. sb.AppendLine();
  233. }
  234. catch (Exception ex)
  235. {
  236. throw;
  237. }
  238. }
  239. #endregion
  240. }
  241. }