PageRenderTime 55ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/Plugins/GizmoBeach.Components/ServiceLayer/StandardWithBaseClassesServiceLayerObjects.cs

http://github.com/kahanu/CondorXE
C# | 301 lines | 269 code | 32 blank | 0 comment | 0 complexity | c7d671c156b41d20b963317202bbd122 MD5 | raw file
  1. using System;
  2. using System.Linq;
  3. using Condor.Core;
  4. using Condor.Core.Interfaces;
  5. using MyMeta;
  6. namespace GizmoBeach.Components.ServiceLayer
  7. {
  8. public class StandardWithBaseClassesServiceLayerObjects : RenderBase, IServiceObjects
  9. {
  10. #region ctors
  11. private readonly RequestContext _context;
  12. public StandardWithBaseClassesServiceLayerObjects(RequestContext context)
  13. : base(context.Zeus.Output)
  14. {
  15. this._context = context;
  16. }
  17. #endregion
  18. #region IRenderObject Members
  19. public void Render()
  20. {
  21. _context.Dialog.InitDialog(_context.Database.Tables.Count);
  22. _context.FileList.Add("");
  23. _context.FileList.Add("Generated Standard Service Layer interfaces: ");
  24. RenderServiceCRUDInterface();
  25. foreach (string tableName in _script.Tables)
  26. {
  27. _context.Dialog.Display("Adding I" + tableName + "Service Interface");
  28. ITable table = _context.Database.Tables[tableName];
  29. RenderInterface(table);
  30. }
  31. _context.Dialog.InitDialog(_context.Database.Tables.Count);
  32. _context.FileList.Add("");
  33. _context.FileList.Add("Generated Standard Service Layer Base classes: ");
  34. foreach (string tableName in _script.Tables)
  35. {
  36. _context.Dialog.Display("Adding " + tableName + "ServiceBase class");
  37. ITable table = _context.Database.Tables[tableName];
  38. RenderServiceBaseClass(table);
  39. }
  40. _context.Dialog.InitDialog(_context.Database.Tables.Count);
  41. _context.FileList.Add("");
  42. _context.FileList.Add("Generated Standard Service Layer classes: ");
  43. foreach (string tableName in _script.Tables)
  44. {
  45. _context.Dialog.Display("Adding " + tableName + "Service class");
  46. ITable table = _context.Database.Tables[tableName];
  47. RenderConcreteClass(table);
  48. }
  49. }
  50. #endregion
  51. #region Private Methods
  52. private void RenderConcreteClass(ITable table)
  53. {
  54. _hdrUtil.WriteClassHeader(_output);
  55. try
  56. {
  57. GenTable genTable = new GenTable(table, _context);
  58. string sortColumn = genTable.GetFirstStringColumnName();
  59. _output.autoTabLn("using System;");
  60. _output.autoTabLn("using System.Collections.Generic;");
  61. _output.autoTabLn("using System.Linq;");
  62. _output.autoTabLn("using System.Text;");
  63. _output.autoTabLn("");
  64. _output.autoTabLn("using " + _script.Settings.BusinessObjects.BusinessObjectsNamespace + ";");
  65. _output.autoTabLn("using " + _script.Settings.ServiceLayer.ServiceNamespace + ".Interfaces;");
  66. _output.autoTabLn("using " + _script.Settings.DataOptions.DataObjectsNamespace + ";");
  67. _output.autoTabLn("using " + _script.Settings.DataOptions.DataObjectsNamespace + ".Interfaces;");
  68. _output.autoTabLn("");
  69. _output.autoTabLn("namespace " + _script.Settings.ServiceLayer.ServiceNamespace);
  70. _output.autoTabLn("{");
  71. _output.autoTabLn(" public class " + StringFormatter.CleanUpClassName(table.Name) + "Service : " + StringFormatter.CleanUpClassName(table.Name) + "ServiceBase, I" + StringFormatter.CleanUpClassName(table.Name) + "Service");
  72. _output.autoTabLn(" {");
  73. _output.autoTabLn("");
  74. _output.tabLevel++;
  75. _output.autoTabLn("#region Dependency Injection");
  76. _output.tabLevel++;
  77. _output.autoTabLn("");
  78. _output.tabLevel--;
  79. _output.autoTabLn("public " + StringFormatter.CleanUpClassName(table.Name) + "Service(I" + StringFormatter.CleanUpClassName(table.Name) + _script.Settings.DataOptions.ClassSuffix.Name + " " + StringFormatter.CleanUpClassName(table.Name) + _script.Settings.DataOptions.ClassSuffix.Name + "):base(" + StringFormatter.CleanUpClassName(table.Name) + _script.Settings.DataOptions.ClassSuffix.Name + ")");
  80. _output.autoTabLn("{");
  81. _output.autoTabLn("}");
  82. _output.autoTabLn("");
  83. _output.autoTabLn("#endregion");
  84. _output.tabLevel--;
  85. _output.autoTabLn("");
  86. _output.tabLevel++;
  87. _output.autoTabLn("#region I" + StringFormatter.CleanUpClassName(table.Name) + "Service Members");
  88. _output.tabLevel--;
  89. _output.autoTabLn("");
  90. _output.tabLevel++;
  91. _output.autoTabLn("public List<" + _context.Utility.BuildModelClassWithNameSpace(StringFormatter.CleanUpClassName(table.Name)) + "> GetAll(string sortExpression, int startRowIndex, int maximumRows)");
  92. _output.autoTabLn("{");
  93. _output.tabLevel++;
  94. _output.autoTabLn("if (string.IsNullOrEmpty(sortExpression))");
  95. _output.tabLevel++;
  96. _output.autoTabLn("sortExpression = \"" + sortColumn + "\";");
  97. _output.tabLevel--;
  98. _output.tabLevel--;
  99. _output.autoTabLn("");
  100. _output.tabLevel++;
  101. _output.autoTabLn("return " + StringFormatter.CamelCasing(StringFormatter.CleanUpClassName(table.Name)) + _script.Settings.DataOptions.ClassSuffix.Name + ".GetAll(sortExpression, startRowIndex, maximumRows);");
  102. _output.tabLevel--;
  103. _output.autoTabLn("}");
  104. _output.tabLevel--;
  105. _output.autoTabLn("");
  106. _output.tabLevel++;
  107. _output.autoTabLn("public int GetCount()");
  108. _output.autoTabLn("{");
  109. _output.tabLevel++;
  110. _output.autoTabLn("return " + StringFormatter.CamelCasing(StringFormatter.CleanUpClassName(table.Name)) + _script.Settings.DataOptions.ClassSuffix.Name + ".GetAll().Count;");
  111. _output.tabLevel--;
  112. _output.autoTabLn("}");
  113. _output.autoTabLn("");
  114. _output.autoTabLn("public " + _context.Utility.BuildModelClassWithNameSpace(StringFormatter.CleanUpClassName(table.Name)) + " GetById(" + _context.Utility.RenderConcreteMethodParameters(table) + ")");
  115. _output.autoTabLn("{");
  116. _output.tabLevel++;
  117. _output.autoTabLn("return " + StringFormatter.CamelCasing(StringFormatter.CleanUpClassName(table.Name)) + _script.Settings.DataOptions.ClassSuffix.Name + ".GetById(" + _context.Utility.RenderCallingMethodParameters(table) + ");");
  118. _output.tabLevel--;
  119. _output.autoTabLn("}");
  120. _output.tabLevel--;
  121. _output.autoTabLn("");
  122. _output.tabLevel++;
  123. _output.autoTabLn("#endregion");
  124. _output.tabLevel--;
  125. _output.autoTabLn(" }");
  126. _output.autoTabLn("}");
  127. _context.FileList.Add(" " + StringFormatter.CleanUpClassName(table.Name) + "Service.cs");
  128. SaveOutput(CreateFullPath(_script.Settings.ServiceLayer.ServiceNamespace, StringFormatter.CleanUpClassName(table.Name) + "Service.cs"), SaveActions.DontOverwrite);
  129. }
  130. catch (Exception ex)
  131. {
  132. throw new Exception("Error rendering ServiceLayer Concrete class - " + ex.Message);
  133. }
  134. }
  135. private void RenderServiceBaseClass(ITable table)
  136. {
  137. _hdrUtil.WriteClassHeader(_output);
  138. try
  139. {
  140. _output.autoTabLn("using System.Collections.Generic;");
  141. _output.autoTabLn("using System.Linq;");
  142. _output.autoTabLn("using " + _script.Settings.BusinessObjects.BusinessObjectsNamespace + ";");
  143. _output.autoTabLn("using " + _script.Settings.ServiceLayer.ServiceNamespace + ".Interfaces;");
  144. _output.autoTabLn("using " + _script.Settings.DataOptions.DataObjectsNamespace + ";");
  145. _output.autoTabLn("using " + _script.Settings.DataOptions.DataObjectsNamespace + ".Interfaces;");
  146. _output.autoTabLn("");
  147. _output.autoTabLn("namespace " + _script.Settings.ServiceLayer.ServiceNamespace);
  148. _output.autoTabLn("{");
  149. _output.autoTabLn(" public class " + StringFormatter.CleanUpClassName(table.Name) + "ServiceBase : ICRUDService<" + _context.Utility.BuildModelClassWithNameSpace(StringFormatter.CleanUpClassName(table.Name)) + ">");
  150. _output.autoTabLn(" {");
  151. _output.tabLevel++;
  152. _output.autoTabLn("#region Dependency Injection");
  153. _output.autoTabLn("");
  154. _output.autoTabLn("protected I" + StringFormatter.CleanUpClassName(table.Name) + _script.Settings.DataOptions.ClassSuffix.Name + " " + StringFormatter.CamelCasing(StringFormatter.CleanUpClassName(table.Name)) + _script.Settings.DataOptions.ClassSuffix.Name + ";");
  155. _output.tabLevel--;
  156. _output.autoTabLn("");
  157. _output.tabLevel++;
  158. _output.autoTabLn("public " + StringFormatter.CleanUpClassName(table.Name) + "ServiceBase(I" + StringFormatter.CleanUpClassName(table.Name) + _script.Settings.DataOptions.ClassSuffix.Name + " " + StringFormatter.CleanUpClassName(table.Name) + _script.Settings.DataOptions.ClassSuffix.Name + ")");
  159. _output.autoTabLn("{");
  160. _output.tabLevel++;
  161. _output.autoTabLn("this." + StringFormatter.CamelCasing(StringFormatter.CleanUpClassName(table.Name)) + _script.Settings.DataOptions.ClassSuffix.Name + " = " + StringFormatter.CleanUpClassName(table.Name) + _script.Settings.DataOptions.ClassSuffix.Name + ";");
  162. _output.tabLevel--;
  163. _output.autoTabLn("}");
  164. _output.autoTabLn("");
  165. _output.autoTabLn("#endregion");
  166. _output.tabLevel--;
  167. _output.autoTabLn("");
  168. _output.tabLevel++;
  169. _output.autoTabLn("public List<" + _context.Utility.BuildModelClassWithNameSpace(StringFormatter.CleanUpClassName(table.Name)) + "> GetAll()");
  170. _output.autoTabLn("{");
  171. _output.tabLevel++;
  172. _output.autoTabLn("return " + StringFormatter.CamelCasing(StringFormatter.CleanUpClassName(table.Name)) + _script.Settings.DataOptions.ClassSuffix.Name + ".GetAll().ToList();");
  173. _output.tabLevel--;
  174. _output.autoTabLn("}");
  175. _output.tabLevel--;
  176. _output.autoTabLn("");
  177. _output.tabLevel++;
  178. _output.autoTabLn("public void Insert(" + _context.Utility.BuildModelClassWithNameSpace(StringFormatter.CleanUpClassName(table.Name)) + " model)");
  179. _output.autoTabLn("{");
  180. _output.tabLevel++;
  181. _output.autoTabLn("" + StringFormatter.CamelCasing(StringFormatter.CleanUpClassName(table.Name)) + _script.Settings.DataOptions.ClassSuffix.Name + ".Insert(model);");
  182. _output.tabLevel--;
  183. _output.autoTabLn("}");
  184. _output.tabLevel--;
  185. _output.autoTabLn("");
  186. _output.tabLevel++;
  187. _output.autoTabLn("public void Update(" + _context.Utility.BuildModelClassWithNameSpace(StringFormatter.CleanUpClassName(table.Name)) + " model)");
  188. _output.autoTabLn("{");
  189. _output.tabLevel++;
  190. _output.autoTabLn("" + StringFormatter.CamelCasing(StringFormatter.CleanUpClassName(table.Name)) + _script.Settings.DataOptions.ClassSuffix.Name + ".Update(model);");
  191. _output.tabLevel--;
  192. _output.autoTabLn("}");
  193. _output.tabLevel--;
  194. _output.autoTabLn("");
  195. _output.tabLevel++;
  196. _output.autoTabLn("public void Delete(" + _context.Utility.BuildModelClassWithNameSpace(StringFormatter.CleanUpClassName(table.Name)) + " model)");
  197. _output.autoTabLn("{");
  198. _output.tabLevel++;
  199. _output.autoTabLn("" + StringFormatter.CamelCasing(StringFormatter.CleanUpClassName(table.Name)) + _script.Settings.DataOptions.ClassSuffix.Name + ".Delete(model);");
  200. _output.tabLevel--;
  201. _output.autoTabLn("}");
  202. _output.tabLevel--;
  203. _output.autoTabLn(" }");
  204. _output.autoTabLn("}");
  205. _context.FileList.Add(" " + StringFormatter.CleanUpClassName(table.Name) + "ServiceBase.cs");
  206. SaveOutput(CreateFullPath(_script.Settings.ServiceLayer.ServiceNamespace + "\\Generated", StringFormatter.CleanUpClassName(table.Name) + "ServiceBase.cs"), SaveActions.Overwrite);
  207. }
  208. catch (Exception ex)
  209. {
  210. throw new Exception("Error rendering ServiceBase class - " + ex.Message);
  211. }
  212. }
  213. private void RenderInterface(ITable table)
  214. {
  215. _hdrUtil.WriteClassHeader(_output);
  216. try
  217. {
  218. _output.autoTabLn("using System;");
  219. _output.autoTabLn("using System.Collections.Generic;");
  220. _output.autoTabLn("using System.Linq;");
  221. _output.autoTabLn("");
  222. _output.autoTabLn("");
  223. _output.autoTabLn("namespace " + _script.Settings.ServiceLayer.ServiceNamespace + ".Interfaces");
  224. _output.autoTabLn("{");
  225. _output.autoTabLn(" public interface I" + StringFormatter.CleanUpClassName(table.Name) + "Service : ICRUDService<" + _context.Utility.BuildModelClassWithNameSpace(StringFormatter.CleanUpClassName(table.Name)) + ">");
  226. _output.autoTabLn(" {");
  227. _output.tabLevel++;
  228. _output.tabLevel++;
  229. _output.autoTabLn(_context.Utility.BuildModelClassWithNameSpace(StringFormatter.CleanUpClassName(table.Name)) + " GetById(" + _context.Utility.RenderConcreteMethodParameters(table) + ");");
  230. _output.autoTabLn("List<" + _context.Utility.BuildModelClassWithNameSpace(StringFormatter.CleanUpClassName(table.Name)) + "> GetAll(string sortExpression, int startRowIndex, int maximumRows);");
  231. _output.autoTabLn("int GetCount();");
  232. _output.tabLevel--;
  233. _output.tabLevel--;
  234. _output.autoTabLn(" }");
  235. _output.autoTabLn("}");
  236. _context.FileList.Add(" I" + StringFormatter.CleanUpClassName(table.Name) + "Service.cs");
  237. SaveOutput(CreateFullPath(_script.Settings.ServiceLayer.ServiceNamespace + "\\Interfaces", "I" + StringFormatter.CleanUpClassName(table.Name) + "Service.cs"), SaveActions.DontOverwrite);
  238. }
  239. catch (Exception ex)
  240. {
  241. throw new Exception("Error rendering RenderServiceLayerInterface - " + ex.Message);
  242. }
  243. }
  244. private void RenderServiceCRUDInterface()
  245. {
  246. _hdrUtil.WriteClassHeader(_output);
  247. _output.autoTabLn("using System;");
  248. _output.autoTabLn("using System.Collections.Generic;");
  249. _output.autoTabLn("using System.Linq;");
  250. _output.autoTabLn("using System.Text;");
  251. _output.autoTabLn("");
  252. _output.autoTabLn("using " + _script.Settings.BusinessObjects.BusinessObjectsNamespace + ";");
  253. _output.autoTabLn("");
  254. _output.autoTabLn("namespace " + _script.Settings.ServiceLayer.ServiceNamespace);
  255. _output.autoTabLn("{");
  256. _output.autoTabLn(" public interface ICRUDService<T>");
  257. _output.autoTabLn(" {");
  258. _output.autoTabLn(" List<T> GetAll();");
  259. _output.autoTabLn(" void Insert(T model);");
  260. _output.autoTabLn(" void Update(T model);");
  261. _output.autoTabLn(" void Delete(T model);");
  262. _output.autoTabLn(" }");
  263. _output.autoTabLn("}");
  264. _context.FileList.Add(" ICRUDService.cs");
  265. SaveOutput(CreateFullPath(_script.Settings.ServiceLayer.ServiceNamespace + "\\Interfaces", "ICRUDService.cs"), SaveActions.Overwrite);
  266. }
  267. #endregion
  268. }
  269. }