PageRenderTime 6ms CodeModel.GetById 2ms app.highlight 19ms RepoModel.GetById 1ms app.codeStats 0ms

/Plugins/GizmoBeach.Components/UI/KingsMvc5ServiceLibraryCodeTemplatesForDbContext.cs

http://github.com/kahanu/CondorXE
C# | 409 lines | 379 code | 22 blank | 8 comment | 2 complexity | 01b8f421255d81ebdc2e973070bd435e MD5 | raw file
  1using System;
  2using System.Linq;
  3using Condor.Core;
  4using Condor.Core.Interfaces;
  5
  6namespace GizmoBeach.Components.UI
  7{
  8    /// <summary>
  9    /// This class will render the code templates for the MVC 5 project.  This is necessary because
 10    /// MVC 5 uses all new T4 templates to generate their controllers, views and WebApi classes.
 11    /// </summary>
 12    public class KingsMvc5ServiceLibraryCodeTemplatesForDbContext : RenderBase, ICodeTemplateBuilder
 13    {
 14
 15        #region ctors
 16        private readonly RequestContext _context;
 17
 18        private readonly UICriteria _criteria;
 19
 20        /// <summary>
 21        /// This class will render the code templates for the MVC 5 project.  This is necessary because
 22        /// MVC 5 uses all new T4 templates to generate their controllers, views and WebApi classes.
 23        /// </summary>
 24        public KingsMvc5ServiceLibraryCodeTemplatesForDbContext(RequestContext context)
 25            :this(context, null)
 26        {
 27            
 28        }
 29
 30        public KingsMvc5ServiceLibraryCodeTemplatesForDbContext(RequestContext context, UICriteria criteria)
 31            : base(context.Zeus.Output)
 32        {
 33            this._context = context;
 34            this._criteria = criteria;
 35        }
 36
 37        #endregion
 38
 39        #region Implemented Methods
 40
 41        public void RenderAspNetViewTemplates()
 42        {
 43            
 44        }
 45
 46        public void RenderControllerTemplate()
 47        {
 48            _output.autoTabLn("<#@ template language=\"C#\" HostSpecific=\"True\" #>");
 49            _output.autoTabLn("<#@ output extension=\"cs\" #>");
 50            _output.autoTabLn("<#@ import namespace=\"System\" #>");
 51            _output.autoTabLn("<#@ parameter type=\"System.String\" name=\"ControllerName\" #>");
 52            _output.autoTabLn("<#@ parameter type=\"System.String\" name=\"ControllerRootName\" #>");
 53            _output.autoTabLn("<#@ parameter type=\"System.String\" name=\"Namespace\" #>");
 54            _output.autoTabLn("<#@ parameter type=\"System.String\" name=\"AreaName\" #>");
 55            _output.autoTabLn("<#");
 56            _output.autoTabLn("");
 57            _output.autoTabLn("string camelCaseServiceName = CamelCaseString(ControllerRootName);");
 58            _output.autoTabLn("string camelCaseServiceNameWithPrefix = CamelCaseString(ControllerRootName, \"_\");");
 59            _output.autoTabLn("#>");
 60            _output.autoTabLn("using System;");
 61            _output.autoTabLn("using System.Collections.Generic;");
 62            _output.autoTabLn("using System.Linq;");
 63            _output.autoTabLn("using System.Web;");
 64            _output.autoTabLn("using System.Web.Mvc;");
 65            _output.autoTabLn("");
 66            _output.autoTabLn("using " + _script.Settings.UI.UINamespace + ".ViewData;");
 67            _output.autoTabLn("using " + _script.Settings.UI.UINamespace + ".Controllers;");
 68            _output.autoTabLn("using " + _script.Settings.DataOptions.DataObjectsNamespace + ";");
 69            _output.autoTabLn("using " + _script.Settings.ServiceLayer.ServiceNamespace + ".Interfaces;");
 70            _output.autoTabLn("using " + _script.Settings.BusinessObjects.BusinessObjectsNamespace + ";");
 71            _output.autoTabLn("using Kendo.Mvc.Extensions;");
 72            _output.autoTabLn("using Kendo.Mvc.UI;");
 73            _output.autoTabLn("");
 74            _output.autoTabLn("namespace <#= Namespace #>");
 75            _output.autoTabLn("{");
 76            _output.tabLevel++;
 77            _output.autoTabLn("[Authorize]");
 78            _output.autoTabLn("public class <#= ControllerName #> : BaseController");
 79            _output.autoTabLn("{");
 80            _output.autoTabLn("");
 81            _output.tabLevel++;
 82            _output.autoTabLn("#region ctors");
 83            _output.autoTabLn("");
 84            _output.autoTabLn("private readonly I<#= ControllerRootName #>Service <#= camelCaseServiceNameWithPrefix #>Service;");
 85            _output.autoTabLn("private readonly IUnitOfWork _unitOfWork;");
 86            _output.autoTabLn("");
 87            _output.autoTabLn("public <#= ControllerName #>(I<#= ControllerRootName #>Service <#= camelCaseServiceName #>Service, IUnitOfWork unitOfWork)");
 88            _output.autoTabLn("{");
 89            _output.tabLevel++;
 90            _output.autoTabLn("this._unitOfWork = unitOfWork;");
 91            _output.autoTabLn("this.<#= camelCaseServiceNameWithPrefix #>Service = <#= camelCaseServiceName #>Service;");
 92            _output.tabLevel--;
 93            _output.autoTabLn("}");
 94            _output.autoTabLn("");
 95            _output.autoTabLn("#endregion");
 96            _output.autoTabLn("");
 97            _output.autoTabLn("#region MVC Actions");
 98            _output.autoTabLn("");
 99            _output.autoTabLn("public ActionResult Index()");
100            _output.autoTabLn("{");
101            _output.tabLevel++;
102            _output.autoTabLn("<#= ControllerRootName #>ViewData viewData = ViewDataFactory.CreateBaseViewData<<#= ControllerRootName #>ViewData>(\"<#= ControllerRootName #> List\");");
103            _output.autoTabLn("viewData.<#= ControllerRootName #>List = <#= camelCaseServiceNameWithPrefix #>Service.Get().ToList();");
104            _output.tabLevel--;
105            _output.autoTabLn("");
106            _output.tabLevel++;
107            _output.autoTabLn("return View(viewData);");
108            _output.tabLevel--;
109            _output.autoTabLn("}");
110            _output.autoTabLn("");
111            _output.autoTabLn("public ActionResult Details(int id)");
112            _output.autoTabLn("{");
113            _output.tabLevel++;
114            _output.autoTabLn("<#= ControllerRootName #>ViewData viewData = ViewDataFactory.CreateBaseViewData<<#= ControllerRootName #>ViewData>(\"<#= ControllerRootName #> Details\");");
115            _output.autoTabLn("viewData.<#= ControllerRootName #> = <#= camelCaseServiceNameWithPrefix #>Service.GetById(id);");
116            _output.tabLevel--;
117            _output.autoTabLn("");
118            _output.tabLevel++;
119            _output.autoTabLn("return View(viewData);");
120            _output.tabLevel--;
121            _output.autoTabLn("}");
122            _output.autoTabLn("");
123            _output.autoTabLn("public ActionResult Create()");
124            _output.autoTabLn("{");
125            _output.tabLevel++;
126            _output.autoTabLn("<#= ControllerRootName #>ViewData viewData = ViewDataFactory.CreateBaseViewData<<#= ControllerRootName #>ViewData>(\"Create <#= ControllerRootName #>\");");
127            _output.autoTab("viewData.<#= ControllerRootName #> = new <#= ControllerRootName #>() { ");
128            
129            if (_criteria.UseTracking)
130	        {
131                _output.write("UpdatedBy = User.Identity.Name, UpdatedOn = DateTime.Now");
132	        }
133            _output.writeln("};");
134            
135        
136        
137            _output.tabLevel--;
138            _output.autoTabLn("");
139            _output.tabLevel++;
140            _output.autoTabLn("return View(viewData);");
141            _output.tabLevel--;
142            _output.autoTabLn("} ");
143            _output.autoTabLn("");
144            _output.autoTabLn("[HttpPost]");
145            _output.autoTabLn("[ValidateAntiForgeryToken]");
146            _output.autoTabLn("public ActionResult Create([Bind(Exclude=\"Id,rowversion\")]<#= ControllerRootName #> model)");
147            _output.autoTabLn("{");
148            _output.tabLevel++;
149            _output.autoTabLn("try");
150            _output.autoTabLn("{");
151            _output.tabLevel++;
152            _output.autoTabLn("if (!ModelState.IsValid)");
153            _output.tabLevel++;
154            _output.autoTabLn("return RedirectToAction(\"Create\");");
155            _output.tabLevel--;
156            _output.tabLevel--;
157            _output.autoTabLn("");
158            _output.tabLevel++;
159            _output.autoTabLn("<#= camelCaseServiceNameWithPrefix #>Service.Insert(model);");
160            _output.autoTabLn("_unitOfWork.Commit();");
161            _output.autoTabLn("");
162            _output.autoTabLn("return RedirectToAction(\"Index\");");
163            _output.tabLevel--;
164            _output.autoTabLn("}");
165            _output.autoTabLn("catch (Exception ex)");
166            _output.autoTabLn("{");
167            _output.tabLevel++;
168            _output.autoTabLn("ModelState.AddModelError(\"\", ex.Message);");
169            _output.autoTabLn("return RedirectToAction(\"Create\");");
170            _output.tabLevel--;
171            _output.autoTabLn("}");
172            _output.tabLevel--;
173            _output.autoTabLn("}");
174            _output.autoTabLn("");
175            _output.autoTabLn("public ActionResult Edit(int id)");
176            _output.autoTabLn("{");
177            _output.tabLevel++;
178            _output.autoTabLn("<#= ControllerRootName #>ViewData viewData = ViewDataFactory.CreateBaseViewData<<#= ControllerRootName #>ViewData>(\"Edit <#= ControllerRootName #>\");");
179            _output.autoTabLn("viewData.<#= ControllerRootName #> = <#= camelCaseServiceNameWithPrefix #>Service.GetById(id);");
180            if (_criteria.UseTracking)
181            {
182                _output.autoTabLn("viewData.<#= ControllerRootName #>.UpdatedOn = DateTime.Now;");
183            }
184            _output.autoTabLn("");
185            _output.autoTabLn("return View(viewData);");
186            _output.tabLevel--;
187            _output.autoTabLn("}");
188            _output.autoTabLn("");
189            _output.autoTabLn("[HttpPost]");
190            _output.autoTabLn("[ValidateAntiForgeryToken]");
191            _output.autoTabLn("public ActionResult Edit(" + ((_criteria.UseTracking) ? "[Bind(Prefix=\"<#= ControllerRootName #>\")]" : "") + "<#= ControllerRootName #> model)");
192            _output.autoTabLn("{");
193            _output.tabLevel++;
194            _output.autoTabLn("try");
195            _output.autoTabLn("{");
196            _output.tabLevel++;
197            _output.autoTabLn("if (!ModelState.IsValid)");
198            _output.tabLevel++;
199            _output.autoTabLn("return RedirectToAction(\"Edit\", new { id = model.Id });");
200            _output.tabLevel--;
201            _output.autoTabLn("");
202            _output.autoTabLn("");
203            _output.autoTabLn("<#= camelCaseServiceNameWithPrefix #>Service.Update(model);");
204            _output.autoTabLn("_unitOfWork.Commit();");
205            _output.autoTabLn("");
206            _output.autoTabLn("return RedirectToAction(\"Index\");");
207            _output.tabLevel--;
208            _output.autoTabLn("}");
209            _output.autoTabLn("catch (Exception ex)");
210            _output.autoTabLn("{");
211            _output.tabLevel++;
212            _output.autoTabLn("ModelState.AddModelError(\"\", ex.Message);");
213            _output.autoTabLn("return RedirectToAction(\"Edit\", new { id = model.Id });");
214            _output.tabLevel--;
215            _output.autoTabLn("}");
216            _output.tabLevel--;
217            _output.autoTabLn("}");
218            _output.autoTabLn("");
219            _output.autoTabLn("");
220            _output.autoTabLn("public ActionResult Delete(int id)");
221            _output.autoTabLn("{");
222            _output.tabLevel++;
223            _output.autoTabLn("<#= ControllerRootName #>ViewData viewData = ViewDataFactory.CreateBaseViewData<<#= ControllerRootName #>ViewData>(\"Delete <#= ControllerRootName #>\");");
224            _output.autoTabLn("viewData.<#= ControllerRootName #> = <#= camelCaseServiceNameWithPrefix #>Service.GetById(id);");
225            _output.tabLevel--;
226            _output.autoTabLn("");
227            _output.tabLevel++;
228            _output.autoTabLn("return View(viewData);");
229            _output.tabLevel--;
230            _output.autoTabLn("}");
231            _output.autoTabLn("");
232            _output.autoTabLn("[HttpPost]");
233            _output.autoTabLn("[ValidateAntiForgeryToken]");
234            _output.autoTabLn("public ActionResult Delete([Bind(Prefix=\"<#= ControllerRootName #>\")]<#= ControllerRootName #> model)");
235            _output.autoTabLn("{");
236            _output.tabLevel++;
237            _output.autoTabLn("try");
238            _output.autoTabLn("{");
239            _output.tabLevel++;
240            _output.autoTabLn("");
241            _output.autoTabLn("<#= camelCaseServiceNameWithPrefix #>Service.Delete(model.Id);");
242            _output.autoTabLn("_unitOfWork.Commit();");
243            _output.autoTabLn("");
244            _output.autoTabLn("return RedirectToAction(\"Index\");");
245            _output.tabLevel--;
246            _output.autoTabLn("}");
247            _output.autoTabLn("catch (Exception)");
248            _output.autoTabLn("{");
249            _output.tabLevel++;
250            _output.autoTabLn("return RedirectToAction(\"Delete\", new { id = model.Id });");
251            _output.tabLevel--;
252            _output.autoTabLn("}");
253            _output.tabLevel--;
254            _output.autoTabLn("}");
255            _output.autoTabLn("");
256            _output.autoTabLn("");
257            _output.autoTabLn("#endregion");
258            _output.autoTabLn("");
259            _output.autoTabLn("#region Ajax Grid Paging Methods");
260            _output.autoTabLn("");
261            _output.autoTabLn("//Used with KendoMvc.UI components");
262            _output.autoTabLn("//public ActionResult _Get<#= ControllerRootName #>List([DataSourceRequest] DataSourceRequest request)");
263            _output.autoTabLn("//{");
264            _output.autoTabLn("//	return Json(<#= camelCaseServiceNameWithPrefix #>Service.Get<#= ControllerRootName #>ListForIndex().ToDataSourceResult(request), JsonRequestBehavior.AllowGet);");
265            _output.autoTabLn("//}");
266            _output.autoTabLn("");
267            _output.autoTabLn("#endregion");
268            _output.tabLevel--;
269            _output.autoTabLn("");
270            _output.autoTabLn("}");
271            _output.tabLevel--;
272            _output.autoTabLn("}");
273            _output.autoTabLn("<#+");
274            _output.autoTabLn("");
275            _output.tabLevel++;
276            _output.autoTabLn("public string CamelCaseString(string input)");
277            _output.autoTabLn("{");
278            _output.tabLevel++;
279            _output.autoTabLn("return CamelCaseString(input, \"\");");
280            _output.tabLevel--;
281            _output.autoTabLn("}");
282            _output.autoTabLn("");
283            _output.autoTabLn("public string CamelCaseString(string input, string prefix)");
284            _output.autoTabLn("{");
285            _output.tabLevel++;
286            _output.autoTabLn("string firstChar = input.Substring(0, 1);");
287            _output.autoTabLn("string remaining = input.Substring(1);");
288            _output.autoTabLn("");
289            _output.autoTabLn("string result = firstChar.ToLower() + remaining;");
290            _output.autoTabLn("");
291            _output.autoTabLn("if (!string.IsNullOrEmpty(prefix))");
292            _output.tabLevel++;
293            _output.autoTabLn("result = prefix + result;");
294            _output.tabLevel--;
295            _output.autoTabLn("");
296            _output.autoTabLn("return result;");
297            _output.tabLevel--;
298            _output.autoTabLn("}");
299            _output.autoTabLn("");
300            _output.tabLevel--;
301            _output.autoTabLn("#>");
302
303            _context.FileList.Add("    MvcControllerWithActions\\Controller.cs.t4");
304            SaveOutput(CreateFullPath(_script.Settings.UI.UINamespace + "\\CodeTemplates\\MvcControllerWithActions", "Controller.cs.t4"), SaveActions.DontOverwrite);	
305        }
306
307        public void RenderRazorViewTemplates()
308        {
309            
310        } 
311
312        public void RenderWebApiTemplates()
313        {
314
315
316            _output.autoTabLn("<#@ template language=\"C#\" HostSpecific=\"True\" #>");
317            _output.autoTabLn("<#@ output extension=\"cs\" #>");
318            _output.autoTabLn("<#@ parameter type=\"System.String\" name=\"ControllerName\" #>");
319            _output.autoTabLn("<#@ parameter type=\"System.String\" name=\"ControllerRootName\" #>");
320            _output.autoTabLn("<#@ parameter type=\"System.String\" name=\"Namespace\" #>");
321            _output.autoTabLn("<#");
322            _output.autoTabLn("");
323            _output.autoTabLn("string camelCaseServiceName = CamelCaseString(ControllerRootName);");
324            _output.autoTabLn("string camelCaseServiceNameWithPrefix = CamelCaseString(ControllerRootName, \"_\");");
325            _output.autoTabLn("#>");
326            _output.autoTabLn("using System;");
327            _output.autoTabLn("using System.Collections.Generic;");
328            _output.autoTabLn("using System.Linq;");
329            _output.autoTabLn("using System.Net;");
330            _output.autoTabLn("using System.Net.Http;");
331            _output.autoTabLn("using System.Web.Http;");
332            _output.autoTabLn("using " + _script.Settings.BusinessObjects.BusinessObjectsNamespace + ";");
333            _output.autoTabLn("using " + _script.Settings.DataOptions.DataObjectsNamespace + ";");
334            _output.autoTabLn("using " + _script.Settings.ServiceLayer.ServiceNamespace + ".Interfaces;");
335            _output.autoTabLn("");
336            _output.autoTabLn("namespace <#= Namespace #>");
337            _output.autoTabLn("{");
338            _output.tabLevel++;
339            _output.autoTabLn("public class <#= ControllerName #> : BaseController<<#= ControllerRootName #>>");
340            _output.autoTabLn("{");
341            _output.autoTabLn("");
342            _output.tabLevel++;
343            _output.autoTabLn("#region ctors");
344            _output.autoTabLn("");
345            _output.autoTabLn("private readonly I<#= ControllerRootName #>Service <#= camelCaseServiceNameWithPrefix #>Service;");
346            _output.autoTabLn("");
347            _output.autoTabLn("private readonly IUnitOfWork _unitOfWork;");
348            _output.autoTabLn("");
349            _output.autoTabLn("public <#= ControllerName #>(I<#= ControllerRootName #>Service <#= camelCaseServiceName #>Service, IUnitOfWork unitOfWork)");
350            _output.tabLevel++;
351            _output.autoTabLn(":base(<#= camelCaseServiceName #>Service, unitOfWork)");
352            _output.tabLevel--;
353            _output.autoTabLn("{");
354            _output.tabLevel++;
355            _output.autoTabLn("this.<#= camelCaseServiceNameWithPrefix #>Service = <#= camelCaseServiceName #>Service;");
356            _output.autoTabLn("this._unitOfWork = unitOfWork;");
357            _output.tabLevel--;
358            _output.autoTabLn("}");
359            _output.autoTabLn("");
360            _output.autoTabLn("#endregion");
361            _output.autoTabLn("");
362            _output.autoTabLn("#region Public Methods");
363            _output.tabLevel--;
364            _output.autoTabLn("");
365            _output.autoTabLn("");
366            _output.autoTabLn("");
367            _output.tabLevel++;
368            _output.autoTabLn("#endregion");
369            _output.tabLevel--;
370            _output.autoTabLn("");
371            _output.autoTabLn("}");
372            _output.tabLevel--;
373            _output.autoTabLn("}");
374            _output.autoTabLn("<#+");
375            _output.autoTabLn("");
376            _output.tabLevel++;
377            _output.autoTabLn("public string CamelCaseString(string input)");
378            _output.autoTabLn("{");
379            _output.tabLevel++;
380            _output.autoTabLn("return CamelCaseString(input, \"\");");
381            _output.tabLevel--;
382            _output.autoTabLn("}");
383            _output.autoTabLn("");
384            _output.autoTabLn("public string CamelCaseString(string input, string prefix)");
385            _output.autoTabLn("{");
386            _output.tabLevel++;
387            _output.autoTabLn("string firstChar = input.Substring(0, 1);");
388            _output.autoTabLn("string remaining = input.Substring(1);");
389            _output.autoTabLn("");
390            _output.autoTabLn("string result = firstChar.ToLower() + remaining;");
391            _output.autoTabLn("");
392            _output.autoTabLn("if (!string.IsNullOrEmpty(prefix))");
393            _output.tabLevel++;
394            _output.autoTabLn("result = prefix + result;");
395            _output.tabLevel--;
396            _output.autoTabLn("");
397            _output.autoTabLn("return result;");
398            _output.tabLevel--;
399            _output.autoTabLn("}");
400            _output.autoTabLn("");
401            _output.tabLevel--;
402            _output.autoTabLn("#>");
403
404            _context.FileList.Add("    ApiControllerEmpty\\Controller.cs.t4");
405            SaveOutput(CreateFullPath(_script.Settings.UI.UINamespace + "\\CodeTemplates\\ApiControllerEmpty", "Controller.cs.t4"), SaveActions.DontOverwrite);	
406        }
407        #endregion
408    }
409}