PageRenderTime 38ms CodeModel.GetById 3ms app.highlight 29ms RepoModel.GetById 1ms app.codeStats 0ms

/Plugins/WebApi4.Components/WebApi4.Components/ServiceLayer/WebApiServiceLayerObjects.cs

http://github.com/kahanu/CondorXE
C# | 620 lines | 538 code | 42 blank | 40 comment | 0 complexity | 440b956dd03ea8100e487fcb23116e13 MD5 | raw file
  1using System;
  2using System.Linq;
  3using Condor.Core;
  4using Condor.Core.Interfaces;
  5using MyMeta;
  6
  7namespace WebApi4.Components.ServiceLayer
  8{
  9    /// <summary>
 10    /// OBSOLETE!!! This class isn't being used any more in favor of
 11    /// WebApiServiceLayerObjectsForDbContext.
 12    /// </summary>
 13    public class WebApiServiceLayerObjects : RenderBase, IServiceObjects
 14    {
 15        #region ctors
 16        private readonly RequestContext _context;
 17
 18        public WebApiServiceLayerObjects(RequestContext context)
 19            : base(context.Zeus.Output)
 20        {
 21            this._context = context;
 22        }
 23        #endregion
 24
 25        #region IRenderObject Members
 26
 27        public void Render()
 28        {
 29            _context.Dialog.InitDialog(_script.Tables.Count);
 30            _context.FileList.Add("");
 31
 32            //RenderFormatterClass();
 33            //RenderRouteConfigClass();
 34            //RenderGlobalAsaxClass();
 35            RenderUnityWebApiConfigurationClass();
 36            RenderBaseControllerClass();
 37
 38            _context.Dialog.InitDialog(_script.Tables.Count);
 39            _context.FileList.Add("");
 40            _context.FileList.Add("Generated WebApi4 Controller classes: ");
 41            foreach (string tableName in _script.Tables)
 42            {
 43                _context.Dialog.Display("Adding " + tableName + "Controller class");
 44                ITable table = _context.Database.Tables[tableName];
 45                RenderApiControllerClass(table);
 46            }
 47        }
 48
 49
 50        #endregion
 51
 52        #region Private Methods
 53
 54        private void RenderApiControllerClass(ITable table)
 55        {
 56            _hdrUtil.WriteClassHeader(_output);
 57
 58            _output.autoTabLn("using System;");
 59            _output.autoTabLn("using System.Collections.Generic;");
 60            _output.autoTabLn("using System.Linq;");
 61            _output.autoTabLn("using System.Linq.Expressions;");
 62            _output.autoTabLn("");
 63            _output.autoTabLn("using " + _script.Settings.BusinessObjects.BusinessObjectsNamespace + ";");
 64            _output.autoTabLn("using " + _script.Settings.DataOptions.DataObjectsNamespace + ";");
 65            _output.autoTabLn("using " + _script.Settings.DataOptions.DataObjectsNamespace + ".Interfaces;");
 66            _output.autoTabLn("");
 67            _output.autoTabLn("namespace " + _script.Settings.ServiceLayer.ServiceNamespace + ".Controllers");
 68            _output.autoTabLn("{");
 69            _output.tabLevel++;
 70            _output.autoTabLn("/// <summary>");
 71            _output.autoTabLn("/// This is the WebApi controller for the " + StringFormatter.CleanUpClassName(table.Name) + " entity.");
 72            _output.autoTabLn("/// </summary>");
 73            _output.autoTabLn("public class " + StringFormatter.CleanUpClassName(table.Name) + "Controller : BaseController<" + StringFormatter.CleanUpClassName(table.Name) + ">");
 74            _output.autoTabLn("{");
 75            _output.tabLevel++;
 76            _output.autoTabLn("#region ctors");
 77            _output.autoTabLn("private readonly I" + StringFormatter.CleanUpClassName(table.Name) + _script.Settings.DataOptions.ClassSuffix.Name + " _" + StringFormatter.CamelCasing(StringFormatter.CleanUpClassName(table.Name)) + _script.Settings.DataOptions.ClassSuffix.Name + ";");
 78            _output.autoTabLn("private readonly IUnitOfWork _unitOfWork;");
 79            _output.autoTabLn("");
 80            _output.autoTabLn("public " + StringFormatter.CleanUpClassName(table.Name) + "Controller(I" + StringFormatter.CleanUpClassName(table.Name) + _script.Settings.DataOptions.ClassSuffix.Name + " " + StringFormatter.CamelCasing(StringFormatter.CleanUpClassName(table.Name)) + _script.Settings.DataOptions.ClassSuffix.Name + ", IUnitOfWork unitOfWork) : base(" + StringFormatter.CamelCasing(StringFormatter.CleanUpClassName(table.Name)) + _script.Settings.DataOptions.ClassSuffix.Name + ", unitOfWork)");
 81            _output.autoTabLn("{");
 82            _output.tabLevel++;
 83            _output.autoTabLn("this._unitOfWork = unitOfWork;");
 84            _output.autoTabLn("this._" + StringFormatter.CamelCasing(StringFormatter.CleanUpClassName(table.Name)) + _script.Settings.DataOptions.ClassSuffix.Name + " = " + StringFormatter.CamelCasing(StringFormatter.CleanUpClassName(table.Name)) + _script.Settings.DataOptions.ClassSuffix.Name + ";");
 85            _output.tabLevel--;
 86            _output.autoTabLn("}");
 87            _output.autoTabLn("");
 88            _output.autoTabLn("#endregion");
 89            _output.autoTabLn("");
 90            _output.autoTabLn("#region Custom Actions");
 91            _output.autoTabLn("");
 92            _output.autoTabLn("///// <summary>");
 93            _output.autoTabLn("///// This is a custom action that returns Product by category.");
 94            _output.autoTabLn("///// Check how the route is created in the RouteConfig.cs file in the App_Start folder.");
 95            _output.autoTabLn("///// </summary>");
 96            _output.autoTabLn("///// <param name=\"category\">Category Name</param>");
 97            _output.autoTabLn("///// <returns></returns>");
 98            _output.autoTabLn("//public List<Product> GetProductsByCategory(string category)");
 99            _output.autoTabLn("//{");
100            _output.autoTabLn("//	Expression<Func<Product, bool>> exp = null;");
101            _output.autoTabLn("//	if (!string.IsNullOrEmpty(category))");
102            _output.autoTabLn("//		exp = c => c.Category.CategoryName.ToLower() == category.ToLower();");
103            _output.autoTabLn("");
104            _output.autoTabLn("//	return _productRepository.Get(exp).ToList();");
105            _output.autoTabLn("//}");
106            _output.autoTabLn("");
107            _output.autoTabLn("///// <summary>");
108            _output.autoTabLn("///// Get a Product by name.  Check the RouteConfig.cs file for routing.");
109            _output.autoTabLn("///// </summary>");
110            _output.autoTabLn("///// <param name=\"name\">Product Name</param>");
111            _output.autoTabLn("///// <returns></returns>");
112            _output.autoTabLn("//public Product GetProductByName(string name)");
113            _output.autoTabLn("//{");
114            _output.autoTabLn("");
115            _output.autoTabLn("//	Product product = _productRepository.Get(p => p.Name.ToLower() == name.ToLower()).SingleOrDefault();");
116            _output.autoTabLn("//	if (product == null)");
117            _output.autoTabLn("//		throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));");
118            _output.autoTabLn("");
119            _output.autoTabLn("//	return product;");
120            _output.autoTabLn("//}");
121            _output.autoTabLn("");
122            _output.autoTabLn("///// <summary>");
123            _output.autoTabLn("///// Calculate the tax for a product. (Example PUT method.)");
124            _output.autoTabLn("///// </summary>");
125            _output.autoTabLn("///// <param name=\"name\">Product model</param>");
126            _output.autoTabLn("///// <returns></returns>");
127            _output.autoTabLn("//[HttpPut]");
128            _output.autoTabLn("//public Product CalculateTax(Product model)");
129            _output.autoTabLn("//{");
130            _output.autoTabLn("//	if (ModelState.IsValid)");
131            _output.autoTabLn("//	{");
132            _output.autoTabLn("//		try");
133            _output.autoTabLn("//		{");
134            _output.autoTabLn("//			var tax = model.Amount * _taxRate;");
135            _output.autoTabLn("//			_groupDao.UpdateTax(tax);");
136            _output.autoTabLn("//			_unitOfWork.ActionEntities.Commit();");
137            _output.autoTabLn("//			");
138            _output.autoTabLn("//		}");
139            _output.autoTabLn("//		catch (Exception)");
140            _output.autoTabLn("//		{");
141            _output.autoTabLn("//			return Request.CreateResponse(HttpStatusCode.NotFound);");
142            _output.autoTabLn("//		}");
143            _output.autoTabLn("//		return Request.CreateResponse(HttpStatusCode.OK, model);");
144            _output.autoTabLn("//	}");
145            _output.autoTabLn("//	else");
146            _output.autoTabLn("//	{");
147            _output.autoTabLn("//		return Request.CreateResponse(HttpStatusCode.BadRequest);");
148            _output.autoTabLn("//	}");
149            _output.autoTabLn("//}");
150            _output.autoTabLn("");
151            _output.autoTabLn("#endregion");
152            _output.tabLevel--;
153            _output.autoTabLn("}");
154            _output.tabLevel--;
155            _output.autoTabLn("}");
156
157            _context.FileList.Add("    " + StringFormatter.CleanUpClassName(table.Name) + "Controller.cs");
158            SaveOutput(CreateFullPath(_script.Settings.ServiceLayer.ServiceNamespace + "\\Controllers", StringFormatter.CleanUpClassName(table.Name) + "Controller.cs"), SaveActions.DontOverwrite);
159        }
160
161        private void RenderBaseControllerClass()
162        {
163            _hdrUtil.WriteClassHeader(_output);
164
165            _output.autoTabLn("using System;");
166            _output.autoTabLn("using System.Collections.Generic;");
167            _output.autoTabLn("using System.Linq;");
168            _output.autoTabLn("using System.Net;");
169            _output.autoTabLn("using System.Net.Http;");
170            _output.autoTabLn("using System.Web.Http;");
171            _output.autoTabLn("using " + _script.Settings.BusinessObjects.BusinessObjectsNamespace + ";");
172            _output.autoTabLn("using " + _script.Settings.DataOptions.DataObjectsNamespace + ";");
173            _output.autoTabLn("using " + _script.Settings.DataOptions.DataObjectsNamespace + ".Generated;");
174            _output.autoTabLn("");
175            _output.autoTabLn("namespace " + _script.Settings.ServiceLayer.ServiceNamespace + ".Controllers");
176            _output.autoTabLn("{");
177            _output.tabLevel++;
178            _output.autoTabLn("/// <summary>");
179            _output.autoTabLn("/// This is the common base ApiController used for all controllers.");
180            _output.autoTabLn("/// </summary>");
181            _output.autoTabLn("/// <typeparam name=\"T\"></typeparam>");
182            _output.autoTabLn("public class BaseController<T> : ApiController where T : Entity");
183            _output.autoTabLn("{");
184            _output.tabLevel++;
185            _output.autoTabLn("#region ctors");
186            _output.autoTabLn("private readonly IRepository<T> _repository;");
187            _output.autoTabLn("private readonly IUnitOfWork _unitOfWork;");
188            _output.autoTabLn("");
189            _output.autoTabLn("public BaseController(IRepository<T> repository, IUnitOfWork unitOfWork)");
190            _output.autoTabLn("{");
191            _output.tabLevel++;
192            _output.autoTabLn("this._unitOfWork = unitOfWork;");
193            _output.autoTabLn("this._repository = repository;");
194            _output.tabLevel--;
195            _output.autoTabLn("}");
196            _output.autoTabLn("");
197            _output.autoTabLn("#endregion");
198            _output.autoTabLn("");
199            _output.autoTabLn("#region Basic CRUD");
200            _output.autoTabLn("");
201            _output.autoTabLn("/// <summary>");
202            _output.autoTabLn("/// Get all the entities in the repository.");
203            _output.autoTabLn("/// </summary>");
204            _output.autoTabLn("/// <returns></returns>");
205            _output.autoTabLn("public IEnumerable<T> Get()");
206            _output.autoTabLn("{");
207            _output.tabLevel++;
208            _output.autoTabLn("return _repository.Get();");
209            _output.tabLevel--;
210            _output.autoTabLn("}");
211            _output.autoTabLn("");
212            _output.autoTabLn("/// <summary>");
213            _output.autoTabLn("/// Get the selected entity.");
214            _output.autoTabLn("/// </summary>");
215            _output.autoTabLn("/// <param name=\"id\">Id</param>");
216            _output.autoTabLn("/// <returns></returns>");
217            _output.autoTabLn("public T Get(int id)");
218            _output.autoTabLn("{");
219            _output.tabLevel++;
220            _output.autoTabLn("T model = _repository.GetById(id);");
221            _output.autoTabLn("if (model == null)");
222            _output.tabLevel++;
223            _output.autoTabLn("throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));");
224            _output.tabLevel--;
225            _output.autoTabLn("");
226            _output.autoTabLn("return model;");
227            _output.tabLevel--;
228            _output.autoTabLn("}");
229            _output.autoTabLn("");
230            _output.autoTabLn("/// <summary>");
231            _output.autoTabLn("/// Insert a new model into the repository.");
232            _output.autoTabLn("/// </summary>");
233            _output.autoTabLn("/// <param name=\"model\">model</param>");
234            _output.autoTabLn("/// <returns></returns>");
235            _output.autoTabLn("public HttpResponseMessage Post(T model)");
236            _output.autoTabLn("{");
237            _output.tabLevel++;
238            _output.autoTabLn("if (ModelState.IsValid)");
239            _output.autoTabLn("{");
240            _output.tabLevel++;
241            _output.autoTabLn("_repository.Insert(model);");
242            _output.autoTabLn("_unitOfWork." + _script.Settings.DataOptions.DataContext.Name + ".Commit();");
243            _output.autoTabLn("");
244            _output.autoTabLn("var response = Request.CreateResponse(HttpStatusCode.Created, model);");
245            _output.autoTabLn("response.Headers.Location = new Uri(Url.Link(\"DefaultApi\", new { id = model.Id }));");
246            _output.autoTabLn("");
247            _output.autoTabLn("return response;");
248            _output.tabLevel--;
249            _output.autoTabLn("}");
250            _output.autoTabLn("else");
251            _output.autoTabLn("{");
252            _output.tabLevel++;
253            _output.autoTabLn("return Request.CreateResponse(HttpStatusCode.BadRequest);");
254            _output.tabLevel--;
255            _output.autoTabLn("}");
256            _output.tabLevel--;
257            _output.autoTabLn("}");
258            _output.autoTabLn("");
259            _output.autoTabLn("/// <summary>");
260            _output.autoTabLn("/// Update the selected model.");
261            _output.autoTabLn("/// </summary>");
262            _output.autoTabLn("/// <param name=\"model\">model</param>");
263            _output.autoTabLn("/// <returns></returns>");
264            _output.autoTabLn("public HttpResponseMessage Put(T model)");
265            _output.autoTabLn("{");
266            _output.tabLevel++;
267            _output.autoTabLn("if (ModelState.IsValid)");
268            _output.autoTabLn("{");
269            _output.tabLevel++;
270            _output.autoTabLn("try");
271            _output.autoTabLn("{");
272            _output.tabLevel++;
273            _output.autoTabLn("_repository.Update(model);");
274            _output.autoTabLn("_unitOfWork." + _script.Settings.DataOptions.DataContext.Name + ".Commit();");
275            _output.tabLevel--;
276            _output.autoTabLn("}");
277            _output.autoTabLn("catch (Exception)");
278            _output.autoTabLn("{");
279            _output.tabLevel++;
280            _output.autoTabLn("return Request.CreateResponse(HttpStatusCode.NotFound);");
281            _output.tabLevel--;
282            _output.autoTabLn("}");
283            _output.autoTabLn("return Request.CreateResponse(HttpStatusCode.OK, model);");
284            _output.tabLevel--;
285            _output.autoTabLn("}");
286            _output.autoTabLn("else");
287            _output.autoTabLn("{");
288            _output.tabLevel++;
289            _output.autoTabLn("return Request.CreateResponse(HttpStatusCode.BadRequest);");
290            _output.tabLevel--;
291            _output.autoTabLn("}");
292            _output.tabLevel--;
293            _output.autoTabLn("}");
294            _output.autoTabLn("");
295
296            _output.autoTabLn("/// <summary>");
297            _output.autoTabLn("/// Alternate generic Put method with an Id argument.");
298            _output.autoTabLn("/// </summary>");
299            _output.autoTabLn("/// <param name=\"id\"></param>");
300            _output.autoTabLn("/// <param name=\"model\"></param>");
301            _output.autoTabLn("/// <returns></returns>");
302            _output.autoTabLn("public HttpResponseMessage Put(int id, T model)");
303            _output.autoTabLn("{");
304            _output.tabLevel++;
305            _output.autoTabLn("if (ModelState.IsValid && id == model.Id)");
306            _output.autoTabLn("{");
307            _output.tabLevel++;
308            _output.autoTabLn("try");
309            _output.autoTabLn("{");
310            _output.tabLevel++;
311            _output.autoTabLn("_repository.Update(model);");
312            _output.autoTabLn("_unitOfWork." + _script.Settings.DataOptions.DataContext.Name + ".Commit();");
313            _output.tabLevel--;
314            _output.autoTabLn("}");
315            _output.autoTabLn("catch (Exception)");
316            _output.autoTabLn("{");
317            _output.tabLevel++;
318            _output.autoTabLn("return Request.CreateResponse(HttpStatusCode.NotFound);");
319            _output.tabLevel--;
320            _output.autoTabLn("}");
321            _output.autoTabLn("return Request.CreateResponse(HttpStatusCode.OK, model);");
322            _output.tabLevel--;
323            _output.autoTabLn("}");
324            _output.autoTabLn("else");
325            _output.autoTabLn("{");
326            _output.tabLevel++;
327            _output.autoTabLn("return Request.CreateResponse(HttpStatusCode.BadRequest);");
328            _output.tabLevel--;
329            _output.autoTabLn("}");
330            _output.tabLevel--;
331            _output.autoTabLn("}");
332
333
334
335            _output.autoTabLn("/// <summary>");
336            _output.autoTabLn("/// Delete the selected model by Id.");
337            _output.autoTabLn("/// </summary>");
338            _output.autoTabLn("/// <param name=\"id\">Id</param>");
339            _output.autoTabLn("/// <returns></returns>");
340            _output.autoTabLn("public HttpResponseMessage Delete(int id)");
341            _output.autoTabLn("{");
342            _output.tabLevel++;
343            _output.autoTabLn("T model = _repository.GetById(id);");
344            _output.autoTabLn("return Delete(model);");
345            _output.tabLevel--;
346            _output.autoTabLn("}");
347            _output.autoTabLn("");
348            _output.autoTabLn("/// <summary>");
349            _output.autoTabLn("/// Delete the selected model via the model.");
350            _output.autoTabLn("/// </summary>");
351            _output.autoTabLn("/// <param name=\"model\"></param>");
352            _output.autoTabLn("/// <returns></returns>");
353            _output.autoTabLn("public HttpResponseMessage Delete(T model)");
354            _output.autoTabLn("{");
355            _output.tabLevel++;
356            _output.autoTabLn("T localModel = _repository.GetById(model.Id);");
357            _output.autoTabLn("if (localModel == null)");
358            _output.autoTabLn("{");
359            _output.tabLevel++;
360            _output.autoTabLn("return Request.CreateResponse(HttpStatusCode.NotFound);");
361            _output.tabLevel--;
362            _output.autoTabLn("}");
363            _output.autoTabLn("");
364            _output.autoTabLn("try");
365            _output.autoTabLn("{");
366            _output.tabLevel++;
367            _output.autoTabLn("_repository.Delete(localModel);");
368            _output.autoTabLn("_unitOfWork." + _script.Settings.DataOptions.DataContext.Name + ".Commit();");
369            _output.tabLevel--;
370            _output.autoTabLn("}");
371            _output.autoTabLn("catch (Exception)");
372            _output.autoTabLn("{");
373            _output.tabLevel++;
374            _output.autoTabLn("return Request.CreateResponse(HttpStatusCode.NotFound);");
375            _output.tabLevel--;
376            _output.autoTabLn("}");
377            _output.autoTabLn("");
378            _output.autoTabLn("return Request.CreateResponse(HttpStatusCode.OK, localModel);");
379            _output.tabLevel--;
380            _output.autoTabLn("}");
381
382
383
384            //_output.autoTabLn("/// <summary>");
385            //_output.autoTabLn("/// Delete the selected model.");
386            //_output.autoTabLn("/// </summary>");
387            //_output.autoTabLn("/// <param name=\"id\">Id</param>");
388            //_output.autoTabLn("/// <returns></returns>");
389            //_output.autoTabLn("public HttpResponseMessage Delete(int id)");
390            //_output.autoTabLn("{");
391            //_output.tabLevel++;
392            //_output.autoTabLn("T model = _repository.GetById(id);");
393            //_output.autoTabLn("if (model == null)");
394            //_output.autoTabLn("{");
395            //_output.tabLevel++;
396            //_output.autoTabLn("return Request.CreateResponse(HttpStatusCode.NotFound);");
397            //_output.tabLevel--;
398            //_output.autoTabLn("}");
399            //_output.autoTabLn("");
400            //_output.autoTabLn("try");
401            //_output.autoTabLn("{");
402            //_output.tabLevel++;
403            //_output.autoTabLn("_repository.Delete(model);");
404            //_output.autoTabLn("_unitOfWork." + _script.Settings.DataOptions.DataContext.Name + ".Commit();");
405            //_output.tabLevel--;
406            //_output.autoTabLn("}");
407            //_output.autoTabLn("catch (Exception)");
408            //_output.autoTabLn("{");
409            //_output.tabLevel++;
410            //_output.autoTabLn("return Request.CreateResponse(HttpStatusCode.NotFound);");
411            //_output.tabLevel--;
412            //_output.autoTabLn("}");
413            //_output.autoTabLn("");
414            //_output.autoTabLn("return Request.CreateResponse(HttpStatusCode.OK, model);");
415            //_output.tabLevel--;
416            //_output.autoTabLn("}");
417
418
419
420            _output.autoTabLn("#endregion");
421            _output.autoTabLn("");
422            _output.tabLevel--;
423            _output.autoTabLn("}");
424            _output.tabLevel--;
425            _output.autoTabLn("}");
426
427            _context.FileList.Add("    BaseController.cs");
428            SaveOutput(CreateFullPath(_script.Settings.ServiceLayer.ServiceNamespace + "\\Controllers", "BaseController.cs"), SaveActions.Overwrite);
429        }
430
431        private void RenderGlobalAsaxClass()
432        {
433            _output.autoTabLn("using System;");
434            _output.autoTabLn("using System.Linq;");
435            _output.autoTabLn("using System.Web.Http;");
436            _output.autoTabLn("using System.Web.Routing;");
437            _output.autoTabLn("using " + _script.Settings.ServiceLayer.ServiceNamespace + ".App_Start;");
438            _output.autoTabLn("");
439            _output.autoTabLn("namespace " + _script.Settings.ServiceLayer.ServiceNamespace);
440            _output.autoTabLn("{");
441            _output.tabLevel++;
442            _output.autoTabLn("public class Global : System.Web.HttpApplication");
443            _output.autoTabLn("{");
444            _output.autoTabLn("");
445            _output.tabLevel++;
446            _output.autoTabLn("protected void Application_Start(object sender, EventArgs e)");
447            _output.autoTabLn("{");
448            _output.tabLevel++;
449            _output.autoTabLn("RouteConfig.RegisterRoutes(RouteTable.Routes);");
450            _output.autoTabLn("FormattersConfig.RegisterFormatters(GlobalConfiguration.Configuration);");
451            _output.tabLevel--;
452            _output.autoTabLn("}");
453            _output.tabLevel--;
454            _output.autoTabLn("}");
455            _output.tabLevel--;
456            _output.autoTabLn("}");
457
458            _context.FileList.Add("    Global.asax.cs");
459            SaveOutput(CreateFullPath(_script.Settings.ServiceLayer.ServiceNamespace, "Global.asax.cs"), SaveActions.DontOverwrite);
460
461            _output.writeln("<%@ Application Codebehind=\"Global.asax.cs\" Inherits=\"" + _script.Settings.ServiceLayer.ServiceNamespace + ".Global\" Language=\"C#\" %>");
462            SaveOutput(CreateFullPath(_script.Settings.ServiceLayer.ServiceNamespace, "Global.asax"), SaveActions.DontOverwrite);
463        }
464
465        private void RenderUnityWebApiConfigurationClass()
466        {
467            _hdrUtil.WriteClassHeader(_output);
468
469            _output.autoTabLn("[assembly: WebActivator.PreApplicationStartMethod(typeof(" + _script.Settings.ServiceLayer.ServiceNamespace + ".App_Start.UnityWebApi), \"Start\")]");
470            _output.autoTabLn("[assembly: WebActivator.ApplicationShutdownMethodAttribute(typeof(" + _script.Settings.ServiceLayer.ServiceNamespace + ".App_Start.UnityWebApi), \"Stop\")]");
471            _output.autoTabLn("");
472            _output.autoTabLn("");
473            _output.autoTabLn("namespace " + _script.Settings.ServiceLayer.ServiceNamespace + ".App_Start");
474            _output.autoTabLn("{");
475            _output.tabLevel++;
476            _output.autoTabLn("using System;");
477            _output.autoTabLn("using System.Linq;");
478            _output.autoTabLn("using System.Web.Http;");
479            _output.autoTabLn("using " + _script.Settings.DataOptions.DataObjectsNamespace + ";");
480            _output.autoTabLn("using " + _script.Settings.DataOptions.DataObjectsNamespace + ".Interfaces;");
481            _output.autoTabLn("using " + _script.Settings.DataOptions.DataObjectsNamespace + ".Repositories;");
482            _output.autoTabLn("using Microsoft.Practices.Unity;");
483            _output.autoTabLn("");
484            _output.autoTabLn("public static class UnityWebApi");
485            _output.autoTabLn("{");
486            _output.tabLevel++;
487            _output.autoTabLn("private static IUnityContainer container;");
488            _output.autoTabLn("");
489            _output.autoTabLn("#region Public Methods");
490            _output.autoTabLn("public static void Start()");
491            _output.autoTabLn("{");
492            _output.tabLevel++;
493            _output.autoTabLn("Initialise();");
494            _output.tabLevel--;
495            _output.autoTabLn("}");
496            _output.autoTabLn("");
497            _output.autoTabLn("public static void Stop()");
498            _output.autoTabLn("{");
499            _output.autoTabLn("");
500            _output.autoTabLn("} ");
501            _output.autoTabLn("#endregion");
502            _output.autoTabLn("");
503            _output.autoTabLn("#region Private Methods");
504            _output.autoTabLn("private static void Initialise()");
505            _output.autoTabLn("{");
506            _output.tabLevel++;
507            _output.autoTabLn("container = BuildUnityContainer();");
508            _output.autoTabLn("");
509            _output.autoTabLn("GlobalConfiguration.Configuration.DependencyResolver = new Unity.WebApi.UnityDependencyResolver(container);");
510            _output.tabLevel--;
511            _output.autoTabLn("}");
512            _output.autoTabLn("");
513            _output.autoTabLn("private static IUnityContainer BuildUnityContainer()");
514            _output.autoTabLn("{");
515            _output.tabLevel++;
516            _output.autoTabLn("var container = new UnityContainer();");
517            _output.autoTabLn("");
518
519            foreach (string tableName in _script.Tables)
520            {
521                ITable table = _context.Database.Tables[tableName];
522                _output.autoTabLn("container.RegisterType<I" + StringFormatter.CleanUpClassName(table.Name) + _script.Settings.DataOptions.ClassSuffix.Name + ", " + StringFormatter.CleanUpClassName(table.Name) + _script.Settings.DataOptions.ClassSuffix.Name + ">();");
523            }
524            _output.autoTabLn("container.RegisterType<IUnitOfWork, UnitOfWork>(new HierarchicalLifetimeManager());");
525
526            _output.autoTabLn("");
527            _output.autoTabLn("return container;");
528            _output.tabLevel--;
529            _output.autoTabLn("} ");
530            _output.autoTabLn("#endregion");
531            _output.tabLevel--;
532            _output.autoTabLn("}");
533            _output.tabLevel--;
534            _output.autoTabLn("}");
535
536            _context.FileList.Add("    UnityWebApi.cs");
537            SaveOutput(CreateFullPath(_script.Settings.ServiceLayer.ServiceNamespace + "\\App_Start", "UnityWebApi.cs"), SaveActions.Overwrite);
538        }
539
540        private void RenderRouteConfigClass()
541        {
542            _hdrUtil.WriteClassHeader(_output);
543
544            _output.autoTabLn("using System;");
545            _output.autoTabLn("using System.Linq;");
546            _output.autoTabLn("using System.Web.Http;");
547            _output.autoTabLn("using System.Web.Routing;");
548            _output.autoTabLn("");
549            _output.autoTabLn("namespace " + _script.Settings.ServiceLayer.ServiceNamespace + ".App_Start");
550            _output.autoTabLn("{");
551            _output.tabLevel++;
552            _output.autoTabLn("/// <summary>");
553            _output.autoTabLn("/// Register your routes here.");
554            _output.autoTabLn("/// </summary>");
555            _output.autoTabLn("public class RouteConfig");
556            _output.autoTabLn("{");
557            _output.tabLevel++;
558            _output.autoTabLn("public static void RegisterRoutes(RouteCollection routes)");
559            _output.autoTabLn("{");
560            _output.autoTabLn("");
561            _output.tabLevel++;
562            _output.autoTabLn("// This is the default route.");
563            _output.autoTabLn("routes.MapHttpRoute(");
564            _output.tabLevel++;
565            _output.autoTabLn("name: \"DefaultApi\",");
566            _output.autoTabLn("routeTemplate: \"api/{controller}/{id}\",");
567            _output.autoTabLn("defaults: new { id = RouteParameter.Optional }");
568            _output.tabLevel--;
569            _output.autoTabLn(");");
570            _output.autoTabLn("");
571            _output.tabLevel--;
572            _output.autoTabLn("}");
573            _output.tabLevel--;
574            _output.autoTabLn("}");
575            _output.tabLevel--;
576            _output.autoTabLn("}");
577
578            _context.FileList.Add("    RouteConfig.cs");
579            SaveOutput(CreateFullPath(_script.Settings.ServiceLayer.ServiceNamespace + "\\App_Start", "RouteConfig.cs"), SaveActions.DontOverwrite);
580        }
581
582        private void RenderFormatterClass()
583        {
584            _hdrUtil.WriteClassHeader(_output);
585
586            _output.autoTabLn("using System;");
587            _output.autoTabLn("using System.Linq;");
588            _output.autoTabLn("using System.Web.Http;");
589            _output.autoTabLn("using WebApiContrib.Formatting.Jsonp;");
590            _output.autoTabLn("");
591            _output.autoTabLn("namespace " + _script.Settings.ServiceLayer.ServiceNamespace + ".App_Start");
592            _output.autoTabLn("{");
593            _output.tabLevel++;
594            _output.autoTabLn("/// <summary>");
595            _output.autoTabLn("/// Register your formatters here.");
596            _output.autoTabLn("/// For the Jsonp formatter, install the NuGet package: install-package WebApiContrib.Formatting.Jsonp");
597            _output.autoTabLn("/// </summary>");
598            _output.autoTabLn("public class FormattersConfig");
599            _output.autoTabLn("{");
600            _output.tabLevel++;
601            _output.autoTabLn("public static void RegisterFormatters(HttpConfiguration config)");
602            _output.autoTabLn("{");
603            _output.tabLevel++;
604            _output.autoTabLn("config.Formatters.Insert(0, new JsonpMediaTypeFormatter());");
605            _output.tabLevel--;
606            _output.autoTabLn("}");
607            _output.tabLevel--;
608            _output.autoTabLn("}");
609            _output.tabLevel--;
610            _output.autoTabLn("}");
611
612            _context.FileList.Add("    FormattersConfig.cs");
613            SaveOutput(CreateFullPath(_script.Settings.ServiceLayer.ServiceNamespace + "\\App_Start", "FormattersConfig.cs"), SaveActions.DontOverwrite);
614        }
615
616
617        #endregion
618    }
619}
620