PageRenderTime 51ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/Plugins/WebApi.Components/WebApiServiceLayerObjects/WebApiServiceLayerObjects.cs

http://github.com/kahanu/CondorXE
C# | 534 lines | 476 code | 55 blank | 3 comment | 1 complexity | 5f255c7f70e824d11ed6b397813c7e59 MD5 | raw file
  1. using System;
  2. using System.Linq;
  3. using Condor.Core;
  4. using Condor.Core.Interfaces;
  5. using MyMeta;
  6. namespace WebApi.Components.ServiceLayer
  7. {
  8. public class WebApiServiceLayerObjects : RenderBase, IServiceObjects
  9. {
  10. #region ctors
  11. private readonly RequestContext _context;
  12. public WebApiServiceLayerObjects(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(5);
  22. _context.FileList.Add("");
  23. _context.FileList.Add("Generated WebApi Service Layer App_Start classes: ");
  24. _context.Dialog.Display("Adding NinjectLoader() class");
  25. RenderNinjectLoaderClass();
  26. _context.Dialog.Display("Adding NinjectWebApiConfiguration() class");
  27. RenderNinjectWebApiConfigurationClass();
  28. _context.Dialog.Display("Adding RouteTableLoader() class");
  29. RenderRouteTableLoaderClass();
  30. _context.Dialog.Display("Adding WebApiStart() class");
  31. RenderWebApiStartClass();
  32. _context.Dialog.Display("Adding Extensions class");
  33. _context.FileList.Add("");
  34. _context.FileList.Add("Generated WebApi Extensions classes: ");
  35. RenderExtensionsClass();
  36. _context.Dialog.InitDialog(_script.Tables.Count);
  37. _context.FileList.Add("");
  38. _context.FileList.Add("Generated WebApi Service base classes: ");
  39. foreach (string tableName in _script.Tables)
  40. {
  41. _context.Dialog.Display("Adding " + tableName + "Service Base class");
  42. ITable table = _context.Database.Tables[tableName];
  43. RenderServiceBaseClass(table);
  44. }
  45. _context.Dialog.InitDialog(_script.Tables.Count);
  46. _context.FileList.Add("");
  47. _context.FileList.Add("Generated WebApi Service Layer concrete classes: ");
  48. foreach (string tableName in _script.Tables)
  49. {
  50. _context.Dialog.Display("Adding " + tableName + "Service Custom class");
  51. ITable table = _context.Database.Tables[tableName];
  52. RenderConcreteClass(table);
  53. }
  54. _context.FileList.Add("");
  55. _context.FileList.Add("Generated WebApi configuration: ");
  56. RenderWebConfig();
  57. }
  58. private void RenderWebConfig()
  59. {
  60. string connectionStringName = _script.Settings.DataOptions.ORMFramework.Selected + "." + _script.Settings.DataOptions.DataStore.Selected;
  61. bool isEF = connectionStringName.ToLower().Contains("entityframework");
  62. string xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
  63. "<configuration>" +
  64. "<appSettings>" +
  65. "<!-- The ClientTag is for use with Patterns In Action 4.0 applications, otherwise ignored. -->" +
  66. "<add key=\"ClientTag\" value=\"" + _script.Settings.Namespace + "Tag\" />" +
  67. "<add key=\"ConnectionStringName\" value=\"" + connectionStringName + "\" />" +
  68. "<add key=\"DataProvider\" value=\"System.Data.SqlClient\" />" +
  69. "</appSettings>" +
  70. "<connectionStrings>" +
  71. BuildConnectionString(connectionStringName, isEF) +
  72. "</connectionStrings>" +
  73. "<system.web>" +
  74. "<compilation debug=\"true\" targetFramework=\"4.0\"/>" +
  75. "</system.web>" +
  76. "<system.serviceModel>" +
  77. "<serviceHostingEnvironment aspNetCompatibilityEnabled=\"true\"/>" +
  78. "</system.serviceModel>" +
  79. "<system.webServer>" +
  80. "<modules runAllManagedModulesForAllRequests=\"true\">" +
  81. "</modules>" +
  82. "</system.webServer>" +
  83. "</configuration>";
  84. _output.write(xml);
  85. _context.FileList.Add(" web.config");
  86. SaveOutput(CreateFullPath(_script.Settings.ServiceLayer.ServiceNamespace, "web.config"), SaveActions.DontOverwrite);
  87. }
  88. private string BuildConnectionString(string connectionStringName, bool isEF)
  89. {
  90. string efConnectionString = "metadata=res://*/" + _script.Settings.DataOptions.ORMFramework.Selected + "." + _script.Settings.DataOptions.DataContext.Name + ".csdl|res://*/" + _script.Settings.DataOptions.ORMFramework.Selected + "." + _script.Settings.DataOptions.DataContext.Name + ".ssdl|res://*/" + _script.Settings.DataOptions.ORMFramework.Selected + "." + _script.Settings.DataOptions.DataContext.Name + ".msl;provider=System.Data.SqlClient;provider connection string=&quot;";
  91. //string connectionString = string.Format("Data Source=localhost;Initial Catalog={0};Integrated Security=True;MultipleActiveResultSets=True", _script.DatabaseName);
  92. string connectionString = _script.Settings.DataOptions.ConnectionString + "MultipleActiveResultSets=True";
  93. string addNode = "<add name=\"{0}\" connectionString=\"{1}\" {2}/>";
  94. if (!isEF)
  95. {
  96. // Linq-To-Sql connection string
  97. addNode = string.Format(addNode, connectionStringName, connectionString, "");
  98. }
  99. else
  100. {
  101. // Entity Framework connection string
  102. addNode = string.Format(addNode, connectionStringName, efConnectionString + connectionString + "&quot;", "providerName=\"System.Data.EntityClient\"");
  103. }
  104. return addNode;
  105. }
  106. private void RenderExtensionsClass()
  107. {
  108. _hdrUtil.WriteClassHeader(_output);
  109. _output.autoTabLn("using System;");
  110. _output.autoTabLn("using System.Linq;");
  111. _output.autoTabLn("");
  112. _output.autoTabLn("namespace " + _script.Settings.ServiceLayer.ServiceNamespace + ".Extensions");
  113. _output.autoTabLn("{");
  114. _output.tabLevel++;
  115. _output.autoTabLn("public static class StringExtensions");
  116. _output.autoTabLn("{");
  117. _output.tabLevel++;
  118. _output.autoTabLn("public static string GetPrefix(this string source, string stringToChop)");
  119. _output.autoTabLn("{");
  120. _output.tabLevel++;
  121. _output.autoTabLn("return source.Replace(stringToChop, \"\");");
  122. _output.tabLevel--;
  123. _output.autoTabLn("}");
  124. _output.tabLevel--;
  125. _output.autoTabLn("}");
  126. _output.tabLevel--;
  127. _output.autoTabLn("}");
  128. _context.FileList.Add(" StringExtensions.cs");
  129. SaveOutput(CreateFullPath(_script.Settings.ServiceLayer.ServiceNamespace + "\\Extensions", "StringExtensions.cs"), SaveActions.DontOverwrite);
  130. }
  131. private void RenderWebApiStartClass()
  132. {
  133. _hdrUtil.WriteClassHeader(_output);
  134. _output.autoTabLn("using System;");
  135. _output.autoTabLn("using System.Linq;");
  136. _output.autoTabLn("using System.Web.Routing;");
  137. _output.autoTabLn("using Microsoft.ApplicationServer.Http.Activation;");
  138. _output.autoTabLn("");
  139. _output.autoTabLn("[assembly: WebActivator.PreApplicationStartMethod(typeof(" + _script.Settings.ServiceLayer.ServiceNamespace + ".App_Start.WebApi), \"Start\")]");
  140. _output.autoTabLn("");
  141. _output.autoTabLn("namespace " + _script.Settings.ServiceLayer.ServiceNamespace + ".App_Start {");
  142. _output.tabLevel++;
  143. _output.autoTabLn("public static class WebApi {");
  144. _output.tabLevel++;
  145. _output.autoTabLn("");
  146. _output.autoTabLn("public static void Start()");
  147. _output.autoTabLn("{ ");
  148. _output.tabLevel++;
  149. _output.autoTabLn("HttpServiceHostFactory _factory = null;");
  150. _output.autoTabLn("");
  151. _output.autoTabLn("var config = new NinjectWebApiConfiguration();");
  152. _output.autoTabLn("_factory = new HttpServiceHostFactory() { Configuration = config };");
  153. _output.autoTabLn("");
  154. _output.autoTabLn("RouteTable.Routes.ServicesLoader(_factory);");
  155. _output.tabLevel--;
  156. _output.autoTabLn("}");
  157. _output.tabLevel--;
  158. _output.autoTabLn("}");
  159. _output.tabLevel--;
  160. _output.autoTabLn("}");
  161. _context.FileList.Add(" WebApi.cs");
  162. SaveOutput(CreateFullPath(_script.Settings.ServiceLayer.ServiceNamespace + "\\App_Start", "WebApi.cs"), SaveActions.DontOverwrite);
  163. }
  164. private void RenderRouteTableLoaderClass()
  165. {
  166. _hdrUtil.WriteClassHeader(_output);
  167. _output.autoTabLn("using System;");
  168. _output.autoTabLn("using System.Collections.Generic;");
  169. _output.autoTabLn("using System.Linq;");
  170. _output.autoTabLn("using System.Reflection;");
  171. _output.autoTabLn("using System.ServiceModel;");
  172. _output.autoTabLn("using System.ServiceModel.Activation;");
  173. _output.autoTabLn("using System.Web.Routing;");
  174. _output.autoTabLn("using Microsoft.ApplicationServer.Http.Activation;");
  175. _output.autoTabLn("using " + _script.Settings.ServiceLayer.ServiceNamespace + ".Extensions;");
  176. _output.autoTabLn("");
  177. _output.autoTabLn("namespace " + _script.Settings.ServiceLayer.ServiceNamespace + ".App_Start");
  178. _output.autoTabLn("{");
  179. _output.tabLevel++;
  180. _output.autoTabLn("public static class RouteTableLoader");
  181. _output.autoTabLn("{");
  182. _output.tabLevel++;
  183. _output.tabLevel++;
  184. _output.autoTabLn("/// <summary>");
  185. _output.autoTabLn("/// This static class loads the services dynamically based on naming conventions.");
  186. _output.autoTabLn("/// </summary>");
  187. _output.autoTabLn("public static void ServicesLoader(this RouteCollection routes, HttpServiceHostFactory factory)");
  188. _output.autoTabLn("{");
  189. _output.tabLevel++;
  190. _output.autoTabLn("IEnumerable<Type> types = Assembly.GetExecutingAssembly().GetTypes()");
  191. _output.tabLevel++;
  192. _output.autoTabLn(".Where(type => type.GetCustomAttributes(typeof(ServiceContractAttribute), false).Length > 0);");
  193. _output.tabLevel--;
  194. _output.autoTabLn("");
  195. _output.autoTabLn("foreach (var item in types)");
  196. _output.autoTabLn("{");
  197. _output.tabLevel++;
  198. _output.autoTabLn("string preFix = item.Name.GetPrefix(\"Service\");");
  199. _output.autoTabLn("string routePreFix = string.Format(\"{0}/\", preFix.ToLower());");
  200. _output.autoTabLn("");
  201. _output.autoTabLn("routes.Add(new ServiceRoute(routePreFix, factory, item));");
  202. _output.tabLevel--;
  203. _output.autoTabLn("}");
  204. _output.tabLevel--;
  205. _output.autoTabLn("}");
  206. _output.tabLevel--;
  207. _output.autoTabLn("}");
  208. _output.tabLevel--;
  209. _output.autoTabLn("}");
  210. _context.FileList.Add(" RouteTableLoader.cs");
  211. SaveOutput(CreateFullPath(_script.Settings.ServiceLayer.ServiceNamespace + "\\App_Start", "RouteTableLoader.cs"), SaveActions.DontOverwrite);
  212. }
  213. private void RenderNinjectWebApiConfigurationClass()
  214. {
  215. _hdrUtil.WriteClassHeader(_output);
  216. _output.autoTabLn("using System;");
  217. _output.autoTabLn("using System.Linq;");
  218. _output.autoTabLn("using Microsoft.ApplicationServer.Http;");
  219. _output.autoTabLn("using Ninject;");
  220. _output.autoTabLn("");
  221. _output.autoTabLn("namespace " + _script.Settings.ServiceLayer.ServiceNamespace);
  222. _output.autoTabLn("{");
  223. _output.tabLevel++;
  224. _output.autoTabLn("public class NinjectWebApiConfiguration : WebApiConfiguration");
  225. _output.autoTabLn("{");
  226. _output.tabLevel++;
  227. _output.autoTabLn("private IKernel kernel = new StandardKernel(new NinjectLoader());");
  228. _output.autoTabLn("");
  229. _output.autoTabLn("public NinjectWebApiConfiguration()");
  230. _output.autoTabLn("{");
  231. _output.tabLevel++;
  232. _output.autoTabLn("CreateInstance = (serviceType, context, request) => kernel.Get(serviceType);");
  233. _output.tabLevel--;
  234. _output.autoTabLn("}");
  235. _output.tabLevel--;
  236. _output.autoTabLn("}");
  237. _output.tabLevel--;
  238. _output.autoTabLn("}");
  239. _context.FileList.Add(" NinjectWebApiConfiguration.cs");
  240. SaveOutput(CreateFullPath(_script.Settings.ServiceLayer.ServiceNamespace + "\\App_Start", "NinjectWebApiConfiguration.cs"), SaveActions.DontOverwrite);
  241. }
  242. private void RenderNinjectLoaderClass()
  243. {
  244. _hdrUtil.WriteClassHeader(_output);
  245. _output.autoTabLn("using System;");
  246. _output.autoTabLn("using System.Linq;");
  247. _output.autoTabLn("using " + _script.Settings.DataOptions.DataObjectsNamespace + ".Interfaces;");
  248. _output.autoTabLn("using " + _script.Settings.DataOptions.DataObjectsNamespace + ".SQLServer;");
  249. _output.autoTabLn("");
  250. _output.autoTabLn("namespace " + _script.Settings.ServiceLayer.ServiceNamespace);
  251. _output.autoTabLn("{");
  252. _output.tabLevel++;
  253. _output.autoTabLn("public class NinjectLoader: Ninject.Modules.NinjectModule");
  254. _output.autoTabLn("{");
  255. _output.tabLevel++;
  256. _output.autoTabLn("public override void Load()");
  257. _output.autoTabLn("{");
  258. _output.tabLevel++;
  259. foreach (string tableName in _script.Tables)
  260. {
  261. ITable table = _context.Database.Tables[tableName];
  262. _output.autoTabLn("Bind<I" + StringFormatter.CleanUpClassName(table.Name) + _script.Settings.DataOptions.ClassSuffix.Name + ">().To<" + _script.Settings.DataOptions.DataStore.Selected + StringFormatter.CleanUpClassName(table.Name) + _script.Settings.DataOptions.ClassSuffix.Name + ">();");
  263. }
  264. _output.tabLevel--;
  265. _output.autoTabLn("}");
  266. _output.tabLevel--;
  267. _output.autoTabLn("}");
  268. _output.tabLevel--;
  269. _output.autoTabLn("}");
  270. _context.FileList.Add(" NinjectLoader.cs");
  271. SaveOutput(CreateFullPath(_script.Settings.ServiceLayer.ServiceNamespace + "\\App_Start", "NinjectLoader.cs"), SaveActions.Overwrite);
  272. }
  273. #endregion
  274. #region Private Methods
  275. private void RenderConcreteClass(ITable table)
  276. {
  277. _hdrUtil.WriteClassHeader(_output);
  278. try
  279. {
  280. _output.autoTabLn("using System;");
  281. _output.autoTabLn("using System.Linq;");
  282. _output.autoTabLn("using System.Net;");
  283. _output.autoTabLn("using System.Net.Http;");
  284. _output.autoTabLn("using System.ServiceModel;");
  285. _output.autoTabLn("using System.ServiceModel.Web;");
  286. _output.autoTabLn("using Microsoft.ApplicationServer.Http.Dispatcher;");
  287. _output.autoTabLn("using System.Text;");
  288. _output.autoTabLn("");
  289. _output.autoTabLn("using " + _script.Settings.BusinessObjects.BusinessObjectsNamespace + ";");
  290. _output.autoTabLn("using " + _script.Settings.DataOptions.DataObjectsNamespace + ".Interfaces;");
  291. _output.autoTabLn("");
  292. _output.autoTabLn("namespace " + _script.Settings.ServiceLayer.ServiceNamespace);
  293. _output.autoTabLn("{");
  294. _output.tabLevel++;
  295. _output.autoTabLn("[ServiceContract(Namespace = \"" + _script.Settings.ServiceLayer.DataContract + "\")]");
  296. _output.autoTabLn("public partial class " + StringFormatter.CleanUpClassName(table.Name) + "Service ");
  297. _output.autoTabLn("{");
  298. _output.autoTabLn("");
  299. _output.tabLevel++;
  300. _output.autoTabLn("#region ctors");
  301. _output.autoTabLn("private readonly I" + StringFormatter.CleanUpClassName(table.Name) + _script.Settings.DataOptions.ClassSuffix.Name + " " + StringFormatter.CleanUpClassName(StringFormatter.CamelCasing(table.Name)) + _script.Settings.DataOptions.ClassSuffix.Name + ";");
  302. _output.autoTabLn("");
  303. _output.autoTabLn("public " + StringFormatter.CleanUpClassName(table.Name) + "Service(I" + StringFormatter.CleanUpClassName(table.Name) + _script.Settings.DataOptions.ClassSuffix.Name + " " + StringFormatter.CleanUpClassName(StringFormatter.CamelCasing(table.Name)) + _script.Settings.DataOptions.ClassSuffix.Name + ")");
  304. _output.autoTabLn("{");
  305. _output.tabLevel++;
  306. _output.autoTabLn("this." + StringFormatter.CleanUpClassName(StringFormatter.CamelCasing(table.Name)) + _script.Settings.DataOptions.ClassSuffix.Name + " = " + StringFormatter.CleanUpClassName(StringFormatter.CamelCasing(table.Name)) + _script.Settings.DataOptions.ClassSuffix.Name + ";");
  307. _output.tabLevel--;
  308. _output.autoTabLn("} ");
  309. _output.autoTabLn("#endregion");
  310. _output.autoTabLn("");
  311. _output.autoTabLn("");
  312. _output.autoTabLn("//[WebGet(UriTemplate = \"/title/{title}\")]");
  313. _output.autoTabLn("//public HttpResponseMessage<" + StringFormatter.CleanUpClassName(table.Name) + "> GetByTitle(string title)");
  314. _output.autoTabLn("//{");
  315. _output.autoTabLn("// if (string.IsNullOrEmpty(title))");
  316. _output.autoTabLn("// throw new HttpResponseException(HttpStatusCode.NotFound);");
  317. _output.tabLevel++;
  318. _output.autoTabLn("");
  319. _output.tabLevel--;
  320. _output.autoTabLn("// var " + StringFormatter.CleanUpClassName(StringFormatter.CamelCasing(table.Name)) + " = " + StringFormatter.CleanUpClassName(StringFormatter.CamelCasing(table.Name)) + _script.Settings.DataOptions.ClassSuffix.Name + ".GetAll().Where(b => b.Title.ToLower() == title.ToLower()).SingleOrDefault();");
  321. _output.autoTabLn("// if (" + StringFormatter.CleanUpClassName(StringFormatter.CamelCasing(table.Name)) + " == null)");
  322. _output.autoTabLn("// {");
  323. _output.autoTabLn("// var response = new HttpResponseMessage();");
  324. _output.autoTabLn("// response.StatusCode = HttpStatusCode.NotFound;");
  325. _output.autoTabLn("// response.Content = new StringContent(\"" + StringFormatter.CleanUpClassName(table.Name) + " not found\");");
  326. _output.autoTabLn("// throw new HttpResponseException(response);");
  327. _output.autoTabLn("// }");
  328. _output.autoTabLn("// var " + StringFormatter.CleanUpClassName(StringFormatter.CamelCasing(table.Name)) + "Response = new HttpResponseMessage<" + StringFormatter.CleanUpClassName(table.Name) + ">(" + StringFormatter.CleanUpClassName(StringFormatter.CamelCasing(table.Name)) + ");");
  329. _output.tabLevel++;
  330. _output.autoTabLn("");
  331. _output.tabLevel--;
  332. _output.autoTabLn("// //set it to expire in 5 minutes");
  333. _output.autoTabLn("// " + StringFormatter.CleanUpClassName(StringFormatter.CamelCasing(table.Name)) + "Response.Content.Headers.Expires = new DateTimeOffset(DateTime.Now.AddSeconds(30));");
  334. _output.autoTabLn("// return " + StringFormatter.CleanUpClassName(StringFormatter.CamelCasing(table.Name)) + "Response;");
  335. _output.autoTabLn("//}");
  336. _output.tabLevel--;
  337. _output.autoTabLn("}");
  338. _output.tabLevel--;
  339. _output.autoTabLn("}");
  340. _context.FileList.Add(" " + StringFormatter.CleanUpClassName(table.Name) + "Service.cs");
  341. SaveOutput(CreateFullPath(_script.Settings.ServiceLayer.ServiceNamespace + "\\api\\v1.0", StringFormatter.CleanUpClassName(table.Name) + "Service.cs"), SaveActions.DontOverwrite);
  342. }
  343. catch (Exception ex)
  344. {
  345. throw new Exception("Error rendering ServiceLayer Concrete class - " + ex.Message);
  346. }
  347. }
  348. private void RenderServiceBaseClass(ITable table)
  349. {
  350. _hdrUtil.WriteClassHeader(_output);
  351. try
  352. {
  353. _output.autoTabLn("using System;");
  354. _output.autoTabLn("using System.Linq;");
  355. _output.autoTabLn("using System.Net;");
  356. _output.autoTabLn("using System.Net.Http;");
  357. _output.autoTabLn("using System.ServiceModel.Web;");
  358. _output.autoTabLn("using Microsoft.ApplicationServer.Http.Dispatcher;");
  359. _output.autoTabLn("");
  360. _output.autoTabLn("using " + _script.Settings.BusinessObjects.BusinessObjectsNamespace + ";");
  361. _output.autoTabLn("");
  362. _output.autoTabLn("namespace " + _script.Settings.ServiceLayer.ServiceNamespace);
  363. _output.autoTabLn("{");
  364. _output.tabLevel++;
  365. _output.autoTabLn("");
  366. _output.autoTabLn("public partial class " + StringFormatter.CleanUpClassName(table.Name) + "Service");
  367. _output.autoTabLn("{");
  368. _output.tabLevel++;
  369. _output.autoTabLn("");
  370. _output.autoTabLn("#region " + StringFormatter.CleanUpClassName(table.Name) + " Members");
  371. _output.autoTabLn("");
  372. _output.autoTabLn("[WebGet(UriTemplate = \"{id}\")]");
  373. _output.autoTabLn("public HttpResponseMessage<BusinessObjects." + StringFormatter.CleanUpClassName(table.Name) + "> GetById(int id)");
  374. _output.autoTabLn("{");
  375. _output.tabLevel++;
  376. _output.autoTabLn("var " + StringFormatter.CleanUpClassName(StringFormatter.CamelCasing(table.Name)) + " = " + StringFormatter.CleanUpClassName(StringFormatter.CamelCasing(table.Name)) + _script.Settings.DataOptions.ClassSuffix.Name + ".GetById(id);");
  377. _output.autoTabLn("if (" + StringFormatter.CleanUpClassName(StringFormatter.CamelCasing(table.Name)) + " == null)");
  378. _output.autoTabLn("{");
  379. _output.tabLevel++;
  380. _output.autoTabLn("var response = new HttpResponseMessage();");
  381. _output.autoTabLn("response.StatusCode = HttpStatusCode.NotFound;");
  382. _output.autoTabLn("response.Content = new StringContent(\"Contact not found\");");
  383. _output.autoTabLn("throw new HttpResponseException(response);");
  384. _output.tabLevel--;
  385. _output.autoTabLn("}");
  386. _output.autoTabLn("var " + StringFormatter.CleanUpClassName(StringFormatter.CamelCasing(table.Name)) + "Response = new HttpResponseMessage<" + StringFormatter.CleanUpClassName(table.Name) + ">(" + StringFormatter.CleanUpClassName(StringFormatter.CamelCasing(table.Name)) + ");");
  387. _output.autoTabLn("");
  388. _output.autoTabLn("//set it to expire in 5 minutes");
  389. _output.autoTabLn(StringFormatter.CleanUpClassName(StringFormatter.CamelCasing(table.Name)) + "Response.Content.Headers.Expires = new DateTimeOffset(DateTime.Now.AddSeconds(30));");
  390. _output.autoTabLn("return " + StringFormatter.CleanUpClassName(StringFormatter.CamelCasing(table.Name)) + "Response;");
  391. _output.tabLevel--;
  392. _output.autoTabLn("}");
  393. _output.autoTabLn("");
  394. _output.autoTabLn("[WebGet(UriTemplate=\"\")]");
  395. _output.autoTabLn("public IQueryable<BusinessObjects." + StringFormatter.CleanUpClassName(table.Name) + "> Get()");
  396. _output.autoTabLn("{");
  397. _output.tabLevel++;
  398. _output.autoTabLn("return " + StringFormatter.CleanUpClassName(StringFormatter.CamelCasing(table.Name)) + _script.Settings.DataOptions.ClassSuffix.Name + ".GetAll().AsQueryable();");
  399. _output.tabLevel--;
  400. _output.autoTabLn("}");
  401. _output.autoTabLn("");
  402. _output.autoTabLn("[WebInvoke(UriTemplate = \"\", Method = \"POST\")]");
  403. _output.autoTabLn("public HttpResponseMessage<BusinessObjects." + StringFormatter.CleanUpClassName(table.Name) + "> Post(BusinessObjects." + StringFormatter.CleanUpClassName(table.Name) + " model)");
  404. _output.autoTabLn("{");
  405. _output.tabLevel++;
  406. _output.autoTabLn("if (model == null)");
  407. _output.autoTabLn("{");
  408. _output.tabLevel++;
  409. _output.autoTabLn("throw new HttpResponseException(HttpStatusCode.NotFound);");
  410. _output.tabLevel--;
  411. _output.autoTabLn("}");
  412. _output.autoTabLn("");
  413. _output.autoTabLn(StringFormatter.CleanUpClassName(StringFormatter.CamelCasing(table.Name)) + _script.Settings.DataOptions.ClassSuffix.Name + ".Insert(model);");
  414. _output.autoTabLn("");
  415. _output.autoTabLn("var response = new HttpResponseMessage<" + StringFormatter.CleanUpClassName(table.Name) + ">(model);");
  416. _output.autoTabLn("response.StatusCode = HttpStatusCode.Created;");
  417. _output.autoTabLn("return response;");
  418. _output.tabLevel--;
  419. _output.autoTabLn("}");
  420. _output.autoTabLn("");
  421. _output.autoTabLn("[WebInvoke(UriTemplate = \"{id}\", Method = \"PUT\")]");
  422. _output.autoTabLn("public BusinessObjects." + StringFormatter.CleanUpClassName(table.Name) + " Put(int id, BusinessObjects." + StringFormatter.CleanUpClassName(table.Name) + " model)");
  423. _output.autoTabLn("{");
  424. _output.tabLevel++;
  425. _output.autoTabLn("if (id <= 0)");
  426. _output.tabLevel++;
  427. _output.autoTabLn("throw new HttpResponseException(\"id is missing\");");
  428. _output.tabLevel--;
  429. _output.autoTabLn("");
  430. _output.autoTabLn("if (model == null)");
  431. _output.autoTabLn("{");
  432. _output.tabLevel++;
  433. _output.autoTabLn("var response = new HttpResponseMessage();");
  434. _output.autoTabLn("response.StatusCode = HttpStatusCode.NotFound;");
  435. _output.autoTabLn("response.Content = new StringContent(\"Contact not found\");");
  436. _output.autoTabLn("throw new HttpResponseException(response);");
  437. _output.tabLevel--;
  438. _output.autoTabLn("}");
  439. _output.autoTabLn("");
  440. _output.autoTabLn(StringFormatter.CleanUpClassName(StringFormatter.CamelCasing(table.Name)) + _script.Settings.DataOptions.ClassSuffix.Name + ".Update(model);");
  441. _output.autoTabLn("model = " + StringFormatter.CleanUpClassName(StringFormatter.CamelCasing(table.Name)) + _script.Settings.DataOptions.ClassSuffix.Name + ".GetById(model.Id);");
  442. _output.autoTabLn("");
  443. _output.autoTabLn("return model;");
  444. _output.tabLevel--;
  445. _output.autoTabLn("}");
  446. _output.autoTabLn("");
  447. _output.autoTabLn("[WebInvoke(UriTemplate = \"{id}\", Method = \"DELETE\")]");
  448. _output.autoTabLn("public BusinessObjects." + StringFormatter.CleanUpClassName(table.Name) + " Delete(int id)");
  449. _output.autoTabLn("{");
  450. _output.tabLevel++;
  451. _output.autoTabLn("if (id <= 0)");
  452. _output.tabLevel++;
  453. _output.autoTabLn("throw new HttpResponseException(\"id is missing\");");
  454. _output.tabLevel--;
  455. _output.autoTabLn("");
  456. _output.autoTabLn("var model = " + StringFormatter.CleanUpClassName(StringFormatter.CamelCasing(table.Name)) + _script.Settings.DataOptions.ClassSuffix.Name + ".GetById(id);");
  457. _output.autoTabLn(StringFormatter.CleanUpClassName(StringFormatter.CamelCasing(table.Name)) + _script.Settings.DataOptions.ClassSuffix.Name + ".Delete(model);");
  458. _output.autoTabLn("");
  459. _output.autoTabLn("return model;");
  460. _output.tabLevel--;
  461. _output.autoTabLn("}");
  462. _output.autoTabLn("");
  463. _output.autoTabLn("#endregion");
  464. _output.autoTabLn("");
  465. _output.tabLevel--;
  466. _output.autoTabLn("}");
  467. _output.tabLevel--;
  468. _output.autoTabLn("}");
  469. _context.FileList.Add(" " + StringFormatter.CleanUpClassName(table.Name) + "Service.cs");
  470. SaveOutput(CreateFullPath(_script.Settings.ServiceLayer.ServiceNamespace + "\\Generated", StringFormatter.CleanUpClassName(table.Name) + "Service.cs"), SaveActions.Overwrite);
  471. }
  472. catch (Exception ex)
  473. {
  474. throw new Exception("Error rendering ServiceBase class - " + ex.Message);
  475. }
  476. }
  477. #endregion
  478. }
  479. }