PageRenderTime 54ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/DemoApp.BusinessModel.ImplCSLA/CSLABusinessObjectsTemplate.tt

http://entmodapp.codeplex.com
Unknown | 1023 lines | 890 code | 133 blank | 0 comment | 0 complexity | b9a01ab11e2a73fda228d99641338723 MD5 | raw file
  1. <#@ template language="c#" hostspecific="true" debug="true" #>
  2. <#@ output extension="log" #>
  3. <#@ assembly name="T4Toolbox.10.0"#>
  4. <#@ assembly Name="System.Core.dll" #>
  5. <#@ assembly Name="NetFrameworkExtensions.dll" #>
  6. <#@ assembly Name="SoftwareEngineering.dll" #>
  7. <#@ assembly Name="System.ComponentModel.DataAnnotations.dll" #>
  8. <#@ import namespace="System" #>
  9. <#@ import namespace="System.IO" #>
  10. <#@ import namespace="System.Diagnostics" #>
  11. <#@ import namespace="System.Linq" #>
  12. <#@ import namespace="System.Collections" #>
  13. <#@ import namespace="System.Collections.Generic" #>
  14. <#@ import namespace="NetFrameworkExtensions" #>
  15. <#@ import namespace="NetFrameworkExtensions.Reflection" #>
  16. <#@ import namespace="SoftwareEngineering.Modeling" #>
  17. <#@ import namespace="SoftwareEngineering.Modeling.DataModeling" #>
  18. <#@ import namespace="System.ComponentModel.DataAnnotations" #>
  19. <#@ include file="t4toolbox.tt" #>
  20. <#
  21. var templateFolder = Path.GetDirectoryName(Host.TemplateFile);
  22. var modelFileName = File.ReadAllLines(Path.Combine(templateFolder, "ModelLocation.txt"));
  23. var modelPath = Path.Combine(templateFolder, modelFileName[0]);
  24. #>
  25. /* Log file generated on <#= DateTime.Now #>
  26. * Template file: <#= Path.GetFileName(Host.TemplateFile) #>
  27. * Model file: <#= Path.GetFullPath(modelPath) #>
  28. */
  29. Generating Business Object Implementation classes ...
  30. <#
  31. var model = (DataModel)ModelReader.ReadModel(modelPath);
  32. var validation = new List<ValidationResult>();
  33. if (!model.TryValidate(null, validation))
  34. {#>
  35. Error validating model:
  36. <# foreach (var item in validation)
  37. { #>
  38. <#= item.PrintUnlocalized() #>
  39. <# }
  40. return this.GenerationEnvironment.ToString();
  41. }
  42. #>
  43. <#
  44. foreach (var entity in model.EntityTypes)
  45. {
  46. var fileName = string.Format("{0}Info.cs", entity.Name);
  47. #>Generating CSLA Object Info for <#= entity.Name #> on file <#= fileName #> ... <#
  48. new CslaObjectInfoTemplate(entity).RenderToFile(fileName);
  49. #>OK!
  50. <#
  51. fileName = string.Format("{0}Edit.cs", entity.Name);
  52. #>Generating CSLA Object Edit for <#= entity.Name #> on file <#= fileName #> ... <#
  53. new CslaObjectEditTemplate(entity).RenderToFile(fileName);
  54. #>OK!
  55. <#
  56. fileName = string.Format("{0}InfoCollection.cs", entity.Name);
  57. #>Generating CSLA Object Info Collection for <#= entity.Name #> on file <#= fileName #> ... <#
  58. new CslaObjectInfoCollectionTemplate(entity).RenderToFile(fileName);
  59. #>OK!
  60. <#
  61. fileName = string.Format("{0}EditCollection.cs", entity.Name);
  62. #>Generating CSLA Object Edit Collection for <#= entity.Name #> on file <#= fileName #> ... <#
  63. new CslaObjectEditCollectionTemplate(entity).RenderToFile(fileName);
  64. #>OK!
  65. <#
  66. }
  67. #>
  68. <#+
  69. public class CslaObjectInfoTemplate : T4Toolbox.Template
  70. {
  71. public EntityType Model { get; set; }
  72. public CslaObjectInfoTemplate(EntityType model)
  73. {
  74. Model = model;
  75. }
  76. public override string TransformText()
  77. {
  78. #>
  79. using System;
  80. using System.Collections.Generic;
  81. using Csla;
  82. using EntModApp.EntServices.Business;
  83. using <#= Model.DataModel.Namespace #>.DataAccess;
  84. namespace <#= Model.DataModel.Namespace #>.BusinessLayer
  85. {
  86. public partial class Csla<#= Model.Name #>Info :
  87. <#+ if (Model.BaseEntityTypeRef != null) { #>
  88. Csla<#= Model.BaseEntityTypeRef.Name #>Info,
  89. <#+ } else { #>
  90. ReadOnlyBase<Csla<#= Model.Name #>Info>,
  91. <#+ } #>
  92. I<#= Model.Name #>
  93. {
  94. #region [ Business Properties ]
  95. <#+ foreach (var prop in Model.PlainProperties) {
  96. var propType = prop.Type.ToCommonString(prop.AllowNull);
  97. #>
  98. public static PropertyInfo<<#= propType #>> <#= prop.Name #>PropertyInfo = RegisterProperty(new PropertyInfo<<#= propType #>>("<#= prop.Name #>"));
  99. public <#= propType #> <#= prop.Name #>
  100. {
  101. get
  102. {
  103. return ReadProperty(<#= prop.Name #>PropertyInfo);
  104. }
  105. private set
  106. {
  107. LoadProperty(<#= prop.Name #>PropertyInfo, value);
  108. }
  109. }
  110. <#+ } #>
  111. <#+ foreach (var prop in Model.References) {
  112. var propType = string.Format("Csla{0}Info", prop.EntityTypeRef.Name);
  113. if (prop.IsMultiple) propType = string.Format("Csla{0}InfoCollection", prop.EntityTypeRef.Name);
  114. #>
  115. public static PropertyInfo<<#= propType #>> <#= prop.Name #>PropertyInfo = RegisterProperty(new PropertyInfo<<#= propType #>>("<#= prop.Name #>"));
  116. public <#= propType #> <#= prop.Name #>
  117. {
  118. get
  119. {
  120. return ReadProperty(<#= prop.Name #>PropertyInfo);
  121. }
  122. <#+ if (prop.IsMultiple) { #>private <#+ } #>set
  123. {
  124. LoadProperty(<#= prop.Name #>PropertyInfo, value);
  125. }
  126. }
  127. <#+ } #>
  128. #endregion [ Business Properties ]
  129. #region [ Factory Methods ]
  130. protected internal static Csla<#= Model.Name #>Info Get<#= Model.Name #>Info(<#= Model.Name #> data, <#= Model.Name #>LoadSettings loadSettings)
  131. {
  132. if (data == null) throw new ArgumentNullException("data");
  133. if (loadSettings == null) throw new ArgumentNullException("loadSettings");
  134. <#+ if (Model.DirectDerivedTypes.Any()) { #>
  135. Csla<#= Model.Name #>Info result;
  136. <#+ foreach (var derivedType in Model.DirectDerivedTypes) { #>
  137. if (data is <#= derivedType.Name #>)
  138. result = Csla<#= derivedType.Name #>Info.Get<#= derivedType.Name #>Info((<#= derivedType.Name #>)data, <#= derivedType.Name #>LoadSettings.From<#= Model.Name #>LoadSettings(loadSettings));
  139. else
  140. <#+ } #>
  141. {
  142. result = new Csla<#= Model.Name #>Info();
  143. result.Fetch<#= Model.Name #>(data, loadSettings);
  144. }
  145. <#+ } else { #>
  146. Csla<#= Model.Name #>Info result = new Csla<#= Model.Name #>Info();
  147. result.Fetch<#= Model.Name #>(data, loadSettings);
  148. <#+ } #>
  149. return result;
  150. }
  151. private static int ForceInitField = 0;
  152. protected Csla<#= Model.Name #>Info()
  153. {
  154. ForceInitField = 1;
  155. }
  156. #endregion [ Factory Methods ]
  157. #region [ Data Access ]
  158. protected void Fetch<#= Model.Name #>(<#= Model.Name #> data, <#= Model.Name #>LoadSettings loadSettings)
  159. {
  160. Fetch<#= Model.Name #>Data(data, loadSettings);
  161. Fetch<#= Model.Name #>Children(data, loadSettings);
  162. }
  163. protected void Fetch<#= Model.Name #>Data(<#= Model.Name #> data, <#= Model.Name #>LoadSettings loadSettings)
  164. {
  165. <#+ if (Model.BaseEntityTypeRef != null) { #>
  166. base.Fetch<#= Model.BaseEntityTypeRef.Name #>Data(data, loadSettings);
  167. <#+ } #>
  168. <#+ foreach (var prop in Model.PlainProperties) { #>
  169. this.<#= prop.Name #> = data.<#= prop.Name #>;
  170. <#+ } #>
  171. }
  172. protected void Fetch<#= Model.Name #>Children(<#= Model.Name #> data, <#= Model.Name #>LoadSettings loadSettings)
  173. {
  174. <#+ if (Model.BaseEntityTypeRef != null) { #>
  175. base.Fetch<#= Model.BaseEntityTypeRef.Name #>Data(data, loadSettings);
  176. <#+ } #>
  177. <#+ foreach (var prop in Model.References) {
  178. var propType = string.Format("Csla{0}Info", prop.EntityTypeRef.Name);
  179. if (prop.IsMultiple)
  180. {
  181. propType = string.Format("Csla{0}InfoCollection", prop.EntityTypeRef.Name);
  182. #>
  183. if (loadSettings.Inclusion.Include<#= prop.Name #>)
  184. this.<#= prop.Name #> = <#= propType #>.Get<#= prop.EntityTypeRef.Name #>InfoCollection(data.<#= prop.Name #>, loadSettings.Inclusion.Included<#= prop.Name #>Settings);
  185. <#+ } #>
  186. <#+ } #>
  187. }
  188. #endregion [ Data Access ]
  189. #region [ Explicit I<#= Model.Name #> implementation ]
  190. <#+ foreach (var prop in Model.PlainProperties) {
  191. var propType = prop.Type.ToCommonString(prop.AllowNull);
  192. #>
  193. <#= propType #> I<#= Model.Name #>.<#= prop.Name #>
  194. {
  195. get { return this.<#= prop.Name #>; }
  196. <#+ if (!prop.IsKey) { #>
  197. set
  198. {
  199. throw new NotSupportedException();
  200. }
  201. <#+ } #>
  202. }
  203. <#+ } #>
  204. <#+ foreach (var prop in Model.References) {
  205. var propType = string.Format("I{0}", prop.EntityTypeRef.Name);
  206. if (prop.IsMultiple) propType = string.Format("IBusinessObjectCollection<I{0}>", prop.EntityTypeRef.Name);
  207. #>
  208. <#= propType #> I<#= Model.Name #>.<#= prop.Name #>
  209. {
  210. get
  211. {
  212. return this.<#= prop.Name #>;
  213. }
  214. <#+ if (!prop.IsMultiple) { #>
  215. set
  216. {
  217. throw new NotSupportedException();
  218. }
  219. <#+ } #>
  220. }
  221. <#+ } #>
  222. bool IBusinessObject.IsReadonly
  223. {
  224. get { return true; }
  225. }
  226. bool IBusinessObject.IsDirty
  227. {
  228. get { return false; }
  229. }
  230. #endregion [ Explicit I<#= Model.Name #> implementation ]
  231. }
  232. }
  233. <#+
  234. return this.GenerationEnvironment.ToString();
  235. }
  236. }
  237. #>
  238. <#+
  239. public class CslaObjectEditTemplate : T4Toolbox.Template
  240. {
  241. public EntityType Model { get; set; }
  242. public CslaObjectEditTemplate(EntityType model)
  243. {
  244. Model = model;
  245. }
  246. public override string TransformText()
  247. {
  248. #>
  249. using System;
  250. using System.Collections.Generic;
  251. using System.Linq;
  252. using Csla;
  253. using EntModApp.EntServices.Business;
  254. using EntModApp.EntServices.IoC;
  255. using <#= Model.DataModel.Namespace #>.DataAccess;
  256. namespace <#= Model.DataModel.Namespace #>.BusinessLayer
  257. {
  258. [Serializable]
  259. public partial class Csla<#= Model.Name #>Edit :
  260. <#+ if (Model.BaseEntityTypeRef != null) { #>
  261. Csla<#= Model.BaseEntityTypeRef.Name #>Edit,
  262. <#+ } else { #>
  263. BusinessBase<Csla<#= Model.Name #>Edit>,
  264. <#+ } #>
  265. I<#= Model.Name #>
  266. {
  267. #region [ Business Properties ]
  268. <#+ foreach (var prop in Model.PlainProperties) {
  269. var propType = prop.Type.ToCommonString(prop.AllowNull);
  270. #>
  271. public static PropertyInfo<<#= propType #>> <#= prop.Name #>PropertyInfo = RegisterProperty(new PropertyInfo<<#= propType #>>("<#= prop.Name #>"));
  272. public <#= propType #> <#= prop.Name #>
  273. {
  274. get
  275. {
  276. return GetProperty(<#= prop.Name #>PropertyInfo);
  277. }
  278. <#+ if (prop.IsKey) { #>private <#+ } #>set
  279. {
  280. LoadProperty(<#= prop.Name #>PropertyInfo, value);
  281. }
  282. }
  283. <#+ } #>
  284. <#+ foreach (var prop in Model.References) {
  285. var propType = string.Format("Csla{0}Edit", prop.EntityTypeRef.Name);
  286. if (prop.IsMultiple) propType = string.Format("Csla{0}EditCollection", prop.EntityTypeRef.Name);
  287. #>
  288. public static PropertyInfo<<#= propType #>> <#= prop.Name #>PropertyInfo = RegisterProperty(new PropertyInfo<<#= propType #>>("<#= prop.Name #>"));
  289. public <#= propType #> <#= prop.Name #>
  290. {
  291. get
  292. {
  293. return GetProperty(<#= prop.Name #>PropertyInfo);
  294. }
  295. private set
  296. {
  297. LoadProperty(<#= prop.Name #>PropertyInfo, value);
  298. }
  299. }
  300. <#+ if (!prop.IsMultiple) { #>
  301. <#+ foreach (var key in prop.ConformKey()) { #>
  302. public static PropertyInfo<<#= key.KeyType() #>> <#= key.KeyName() #>PropertyInfo = RegisterProperty(new PropertyInfo<<#= key.KeyType() #>>("<#= key.KeyName() #>"));
  303. public <#= key.KeyType() #> <#= key.KeyName() #>
  304. {
  305. get
  306. {
  307. return GetProperty(<#= key.KeyName() #>PropertyInfo);
  308. }
  309. <#+ if (prop.IsKey) { #>private <#+ } #>set
  310. {
  311. LoadProperty(<#= key.KeyName() #>PropertyInfo, value);
  312. }
  313. }
  314. <#+ } #>
  315. <#+ } #>
  316. <#+ } #>
  317. #endregion [ Business Properties ]
  318. #region [ Validation Rules ]
  319. #endregion [ Validation Rules ]
  320. #region [ Authorization Rules ]
  321. #endregion [ Authorization Rules ]
  322. #region [ Factory Methods ]
  323. #region [ Root Factory Methods ]
  324. public static Csla<#= Model.Name #>Edit New()
  325. {
  326. return DataPortal.Create<Csla<#= Model.Name #>Edit>();
  327. }
  328. public static void Delete(<#= Model.RootEntityType.Name #>Key key)
  329. {
  330. DataPortal.Delete<<#= Model.Name #>>(new SingleCriteria<<#= Model.Name #>, <#= Model.RootEntityType.Name #>Key>(key));
  331. }
  332. public static void Delete(<#= Model.RootEntityType.ConformKey().ToStringList(", ", k => string.Format("{0} {1}", k.KeyType(), k.KeyName())) #>)
  333. {
  334. Delete(new <#= Model.RootEntityType.Name #>Key { <#= Model.RootEntityType.ConformKey().ToStringList(", ", k => string.Format("{0} = {0}", k.KeyName())) #> });
  335. }
  336. #endregion [ Root Factory Methods ]
  337. #region [ Child Factory Methods ]
  338. internal static Csla<#= Model.Name #>Edit NewChild()
  339. {
  340. return DataPortal.CreateChild<Csla<#= Model.Name #>Edit>();
  341. }
  342. internal static Csla<#= Model.Name #>Edit Get<#= Model.Name #>Edit(<#= Model.Name #> data, <#= Model.Name #>LoadSettings loadSettings)
  343. {
  344. if (data == null) throw new ArgumentNullException("data");
  345. if (loadSettings == null) throw new ArgumentNullException("loadSettings");
  346. <#+ if (Model.DirectDerivedTypes.Any()) { #>
  347. Csla<#= Model.Name #>Edit result;
  348. <#+ foreach (var derivedType in Model.DirectDerivedTypes) { #>
  349. if (data is <#= derivedType.Name #>)
  350. result = Csla<#= derivedType.Name #>Edit.Get<#= derivedType.Name #>Edit((<#= derivedType.Name #>)data, <#= derivedType.Name #>LoadSettings.From<#= Model.Name #>LoadSettings(loadSettings));
  351. else
  352. <#+ } #>
  353. {
  354. result = new Csla<#= Model.Name #>Edit();
  355. result.Fetch<#= Model.Name #>(data, loadSettings);
  356. }
  357. <#+ } else { #>
  358. Csla<#= Model.Name #>Edit result = new Csla<#= Model.Name #>Edit();
  359. result.Fetch<#= Model.Name #>(data, loadSettings);
  360. <#+ } #>
  361. result.MarkAsChild();
  362. return result;
  363. }
  364. #endregion [ Child Factory Methods ]
  365. private static int ForceInitField = 0;
  366. protected Csla<#= Model.Name #>Edit()
  367. {
  368. ForceInitField = 1;
  369. }
  370. #endregion [ Factory Methods ]
  371. #region [ Data Access ]
  372. #region [ Root Data Access ]
  373. protected override void DataPortal_Create()
  374. {
  375. base.DataPortal_Create();
  376. }
  377. [Transactional(TransactionalTypes.TransactionScope)]
  378. protected override void DataPortal_Insert()
  379. {
  380. }
  381. [Transactional(TransactionalTypes.TransactionScope)]
  382. protected override void DataPortal_Update()
  383. {
  384. }
  385. [Transactional(TransactionalTypes.TransactionScope)]
  386. protected override void DataPortal_DeleteSelf()
  387. {
  388. DataPortal_Delete(new SingleCriteria<<#= Model.Name #>, <#= Model.RootEntityType.Name #>Key>(new <#= Model.RootEntityType.Name #>Key { <#= Model.RootEntityType.ConformKey().ToStringList(", ", k => string.Format("{0} = this.{0}", k.KeyName())) #> }));
  389. }
  390. [Transactional(TransactionalTypes.TransactionScope)]
  391. private void DataPortal_Delete(SingleCriteria<<#= Model.Name #>, <#= Model.RootEntityType.Name #>Key> criteria)
  392. {
  393. }
  394. #endregion [ Root Data Access ]
  395. #region [ Child Data Access ]
  396. protected override void Child_Create()
  397. {
  398. base.Child_Create();
  399. }
  400. private void Child_Fetch(object childData)
  401. {
  402. }
  403. private void Child_Insert(object parent)
  404. {
  405. }
  406. private void Child_Update(object parent)
  407. {
  408. }
  409. private void Child_DeleteSelf(object parent)
  410. {
  411. }
  412. #endregion [ Child Data Access ]
  413. protected void Fetch<#= Model.Name #>(<#= Model.Name #> data, <#= Model.Name #>LoadSettings loadSettings)
  414. {
  415. Fetch<#= Model.Name #>Data(data, loadSettings);
  416. Fetch<#= Model.Name #>Children(data, loadSettings);
  417. }
  418. protected void Fetch<#= Model.Name #>Data(<#= Model.Name #> data, <#= Model.Name #>LoadSettings loadSettings)
  419. {
  420. <#+ if (Model.BaseEntityTypeRef != null) { #>
  421. base.Fetch<#= Model.BaseEntityTypeRef.Name #>Data(data, loadSettings);
  422. <#+ } #>
  423. <#+ foreach (var prop in Model.PlainProperties) { #>
  424. this.<#= prop.Name #> = data.<#= prop.Name #>;
  425. <#+ } #>
  426. <#+ foreach (var prop in Model.References) {
  427. if (!prop.IsMultiple)
  428. foreach (var key in prop.ConformKey())
  429. {
  430. #>
  431. this.<#= key.KeyName() #> = data.<#= key.KeyName() #>;
  432. <#+ } #>
  433. <#+ } #>
  434. }
  435. protected void Fetch<#= Model.Name #>Children(<#= Model.Name #> data, <#= Model.Name #>LoadSettings loadSettings)
  436. {
  437. <#+ if (Model.BaseEntityTypeRef != null) { #>
  438. base.Fetch<#= Model.BaseEntityTypeRef.Name #>Data(data, loadSettings);
  439. <#+ } #>
  440. <#+ foreach (var prop in Model.References) {
  441. var propType = string.Format("Csla{0}Edit", prop.EntityTypeRef.Name);
  442. if (prop.IsMultiple)
  443. {
  444. propType = string.Format("Csla{0}EditCollection", prop.EntityTypeRef.Name);
  445. #>
  446. if (loadSettings.Inclusion.Include<#= prop.Name #>)
  447. this.<#= prop.Name #> = <#= propType #>.Get<#= prop.EntityTypeRef.Name #>EditCollection(data.<#= prop.Name #>, loadSettings.Inclusion.Included<#= prop.Name #>Settings);
  448. <#+ } else { #>
  449. if (loadSettings.Inclusion.Include<#= prop.Name #>)
  450. this.<#= prop.Name #> = <#= propType #>.Get<#= prop.EntityTypeRef.Name #>Edit(data.<#= prop.Name #>, loadSettings.Inclusion.Included<#= prop.Name #>Settings);
  451. <#+ } #>
  452. <#+ } #>
  453. }
  454. #endregion [ Data Access ]
  455. #region [ Explicit I<#= Model.Name #> implementation ]
  456. <#+ foreach (var prop in Model.PlainProperties) {
  457. var propType = prop.Type.ToCommonString(prop.AllowNull);
  458. #>
  459. <#= propType #> I<#= Model.Name #>.<#= prop.Name #>
  460. {
  461. get { return this.<#= prop.Name #>; }
  462. <#+ if (!prop.IsKey) { #>
  463. set
  464. {
  465. this.<#= prop.Name #> = value;
  466. }
  467. <#+ } #>
  468. }
  469. <#+ } #>
  470. <#+ foreach (var prop in Model.References) {
  471. var propType = string.Format("I{0}", prop.EntityTypeRef.Name);
  472. if (prop.IsMultiple) propType = string.Format("IBusinessObjectCollection<I{0}>", prop.EntityTypeRef.Name);
  473. #>
  474. <#= propType #> I<#= Model.Name #>.<#= prop.Name #>
  475. {
  476. get
  477. {
  478. return this.<#= prop.Name #>;
  479. }
  480. <#+ if (!prop.IsMultiple) { #>
  481. set
  482. {
  483. this.<#= prop.Name #> = (Csla<#= prop.EntityTypeRef.Name #>Edit)value;
  484. }
  485. <#+ } #>
  486. }
  487. <#+ } #>
  488. bool IBusinessObject.IsReadonly
  489. {
  490. get { return false; }
  491. }
  492. bool IBusinessObject.IsDirty
  493. {
  494. get { return this.IsDirty; }
  495. }
  496. #endregion [ Explicit I<#= Model.Name #> implementation ]
  497. }
  498. }
  499. <#+
  500. return this.GenerationEnvironment.ToString();
  501. }
  502. }
  503. #>
  504. <#+
  505. public class CslaObjectInfoCollectionTemplate : T4Toolbox.Template
  506. {
  507. public EntityType Model { get; set; }
  508. public CslaObjectInfoCollectionTemplate(EntityType model)
  509. {
  510. Model = model;
  511. }
  512. public override string TransformText()
  513. {
  514. #>
  515. using System;
  516. using System.Collections.Generic;
  517. using System.Linq;
  518. using Csla;
  519. using EntModApp.EntServices.Business;
  520. using EntModApp.EntServices.IoC;
  521. using <#= Model.DataModel.Namespace #>.DataAccess;
  522. namespace <#= Model.DataModel.Namespace #>.BusinessLayer
  523. {
  524. [Serializable]
  525. public partial class Csla<#= Model.Name #>InfoCollection
  526. : ReadOnlyListBase<Csla<#= Model.Name #>InfoCollection, Csla<#= Model.Name #>Info>,
  527. IBusinessObjectCollection<I<#= Model.Name #>>
  528. {
  529. #region [ Business Properties ]
  530. public int TotalItems { get; private set; }
  531. #endregion [ Business Properties ]
  532. #region [ Factory Methods ]
  533. public static int GetCount(<#= Model.Name #>LoadSettings loadSettings = null)
  534. {
  535. loadSettings = <#= Model.Name #>LoadSettings.From<#= Model.Name #>LoadSettings(loadSettings);
  536. var cmd = new GetCountCommand { LoadSettings = loadSettings };
  537. DataPortal.Execute(cmd);
  538. return cmd.Count;
  539. }
  540. public static Csla<#= Model.Name #>InfoCollection Get<#= Model.Name #>InfoById(<#= Model.RootEntityType.ConformKey().ToStringList(", ", k => string.Format("{0} {1}", k.KeyType(), k.KeyName())) #>, <#= Model.Name #>LoadSettings loadSettings = null)
  541. {
  542. return Get<#= Model.Name #>InfoById(new <#= Model.RootEntityType.Name #>Key { <#= Model.RootEntityType.ConformKey().ToStringList(", ", k => string.Format("{0} = {0}", k.KeyName())) #> }, loadSettings);
  543. }
  544. public static Csla<#= Model.Name #>InfoCollection Get<#= Model.Name #>InfoById(<#= Model.RootEntityType.Name #>Key key, <#= Model.Name #>LoadSettings loadSettings = null)
  545. {
  546. if (key == null) throw new ArgumentNullException("key");
  547. loadSettings = <#= Model.Name #>LoadSettings.From<#= Model.Name #>LoadSettings(loadSettings);
  548. loadSettings.Filter.ByKey = key;
  549. var criteria = new GetAllCriteria
  550. {
  551. LoadSettings = loadSettings,
  552. };
  553. var result = DataPortal.Fetch<Csla<#= Model.Name #>InfoCollection>(criteria);
  554. return result;
  555. }
  556. public static Csla<#= Model.Name #>InfoCollection Get<#= Model.Name #>InfoCollection(<#= Model.Name #>LoadSettings loadSettings = null)
  557. {
  558. loadSettings = <#= Model.Name #>LoadSettings.From<#= Model.Name #>LoadSettings(loadSettings);
  559. var criteria = new GetAllCriteria
  560. {
  561. LoadSettings = loadSettings,
  562. };
  563. var result = DataPortal.Fetch<Csla<#= Model.Name #>InfoCollection>(criteria);
  564. return result;
  565. }
  566. protected internal static Csla<#= Model.Name #>InfoCollection Get<#= Model.Name #>InfoCollection(IEnumerable<<#= Model.Name #>> data, <#= Model.Name #>LoadSettings loadSettings)
  567. {
  568. if (data == null) throw new ArgumentNullException("data");
  569. if (loadSettings == null) throw new ArgumentNullException("loadSettings");
  570. var result = new Csla<#= Model.Name #>InfoCollection();
  571. result.FetchAll(data, loadSettings);
  572. result.TotalItems = result.Count;
  573. return result;
  574. }
  575. private static int ForceInit = 0;
  576. protected Csla<#= Model.Name #>InfoCollection()
  577. {
  578. ForceInit = 1;
  579. }
  580. #region [ Criterias ]
  581. [Serializable]
  582. class GetAllCriteria : CriteriaBase<GetAllCriteria>//, ICriteriaBase
  583. {
  584. public <#= Model.Name #>LoadSettings LoadSettings { get; set; }
  585. }
  586. #endregion
  587. #endregion
  588. #region [ Commands ]
  589. [Serializable]
  590. class GetCountCommand : CommandBase<GetCountCommand>
  591. {
  592. public <#= Model.Name #>LoadSettings LoadSettings { get; set; }
  593. public int Count { get; private set; }
  594. protected override void DataPortal_Execute()
  595. {
  596. var dbFactory = ServiceLocator.Current.GetInstance<I<#= Model.DataModel.Name #>DataRepositoryFactory>();
  597. using (var db = dbFactory.CreateRepository())
  598. {
  599. Count = db.<#= Model.EntitySetName #>.Count(LoadSettings);
  600. }
  601. }
  602. }
  603. #endregion [ Commands ]
  604. #region [ Data Access ]
  605. private void DataPortal_Fetch(GetAllCriteria criteria)
  606. {
  607. var dbFactory = ServiceLocator.Current.GetInstance<I<#= Model.DataModel.Name #>DataRepositoryFactory>();
  608. IEnumerable<<#= Model.Name #>> data;
  609. using (var db = dbFactory.CreateRepository())
  610. {
  611. int total;
  612. data = db.<#= Model.RootEntityType.EntitySetName #>.Retrieve(criteria.LoadSettings, out total)<#+ if (Model.BaseEntityTypeRef != null) { #>.Cast<<#= Model.Name #>>()<#+ } #>;
  613. TotalItems = total;
  614. }
  615. FetchAll(data, criteria.LoadSettings);
  616. }
  617. private void FetchAll(IEnumerable<<#= Model.Name #>> data, <#= Model.Name #>LoadSettings loadSettings)
  618. {
  619. var oldIsReadOnly = IsReadOnly;
  620. var oldRaiseListChangedEvents = RaiseListChangedEvents;
  621. try
  622. {
  623. IsReadOnly = false;
  624. RaiseListChangedEvents = false;
  625. foreach (var item in data)
  626. {
  627. var element = Csla<#= Model.Name #>Info.Get<#= Model.Name #>Info(item, loadSettings);
  628. this.Add(element);
  629. }
  630. }
  631. finally
  632. {
  633. IsReadOnly = oldIsReadOnly;
  634. RaiseListChangedEvents = oldRaiseListChangedEvents;
  635. }
  636. }
  637. #endregion
  638. #region [ Explicit IBusinessObjectCollection<I<#= Model.Name #>> implementation ]
  639. void ICollection<I<#= Model.Name #>>.Add(I<#= Model.Name #> item)
  640. {
  641. this.Add((Csla<#= Model.Name #>Info)item);
  642. }
  643. void ICollection<I<#= Model.Name #>>.Clear()
  644. {
  645. this.Clear();
  646. }
  647. bool ICollection<I<#= Model.Name #>>.Contains(I<#= Model.Name #> item)
  648. {
  649. return this.Contains((Csla<#= Model.Name #>Info)item);
  650. }
  651. void ICollection<I<#= Model.Name #>>.CopyTo(I<#= Model.Name #>[] array, int arrayIndex)
  652. {
  653. int size = Math.Min(array.Length - arrayIndex, this.Count);
  654. if (size > 0)
  655. {
  656. var a = new Csla<#= Model.Name #>Info[size];
  657. this.CopyTo(a, 0);
  658. Array.Copy(a, 0, array, arrayIndex, size);
  659. }
  660. }
  661. bool ICollection<I<#= Model.Name #>>.IsReadOnly
  662. {
  663. get { return true; }
  664. }
  665. bool ICollection<I<#= Model.Name #>>.Remove(I<#= Model.Name #> item)
  666. {
  667. return this.Remove((Csla<#= Model.Name #>Info)item);
  668. }
  669. IEnumerator<I<#= Model.Name #>> IEnumerable<I<#= Model.Name #>>.GetEnumerator()
  670. {
  671. return this.GetEnumerator();
  672. }
  673. System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
  674. {
  675. return this.GetEnumerator();
  676. }
  677. bool IBusinessObject.IsReadonly
  678. {
  679. get { return true; }
  680. }
  681. bool IBusinessObject.IsDirty
  682. {
  683. get { return false; }
  684. }
  685. I<#= Model.Name #> IBusinessObjectCollection<I<#= Model.Name #>>.this[int index]
  686. {
  687. get
  688. {
  689. return this[index];
  690. }
  691. set
  692. {
  693. this[index] = (Csla<#= Model.Name #>Info)value;
  694. }
  695. }
  696. #endregion [ Explicit IBusinessObjectCollection<I<#= Model.Name #>> implementation ]
  697. }
  698. }
  699. <#+
  700. return this.GenerationEnvironment.ToString();
  701. }
  702. }
  703. #>
  704. <#+
  705. public class CslaObjectEditCollectionTemplate : T4Toolbox.Template
  706. {
  707. public EntityType Model { get; set; }
  708. public CslaObjectEditCollectionTemplate(EntityType model)
  709. {
  710. Model = model;
  711. }
  712. public override string TransformText()
  713. {
  714. #>
  715. using System;
  716. using System.Collections.Generic;
  717. using System.Linq;
  718. using Csla;
  719. using EntModApp.EntServices.Business;
  720. using EntModApp.EntServices.IoC;
  721. using <#= Model.DataModel.Namespace #>.DataAccess;
  722. namespace <#= Model.DataModel.Namespace #>.BusinessLayer
  723. {
  724. [Serializable]
  725. public partial class Csla<#= Model.Name #>EditCollection
  726. : BusinessListBase<Csla<#= Model.Name #>EditCollection, Csla<#= Model.Name #>Edit>,
  727. IBusinessObjectCollection<I<#= Model.Name #>>
  728. {
  729. #region [ Business Properties ]
  730. public int TotalItems { get; private set; }
  731. #endregion [ Business Properties ]
  732. #region [ Factory Methods ]
  733. #region [ Root Factory Methods ]
  734. public static Csla<#= Model.Name #>EditCollection New()
  735. {
  736. return DataPortal.Create<Csla<#= Model.Name #>EditCollection>();
  737. }
  738. public static Csla<#= Model.Name #>EditCollection Get<#= Model.Name #>EditById(<#= Model.RootEntityType.ConformKey().ToStringList(", ", k => string.Format("{0} {1}", k.KeyType(), k.KeyName())) #>, <#= Model.Name #>LoadSettings loadSettings = null)
  739. {
  740. return Get<#= Model.Name #>EditById(new <#= Model.RootEntityType.Name #>Key { <#= Model.RootEntityType.ConformKey().ToStringList(", ", k => string.Format("{0} = {0}", k.KeyName())) #> }, loadSettings);
  741. }
  742. public static Csla<#= Model.Name #>EditCollection Get<#= Model.Name #>EditById(<#= Model.RootEntityType.Name #>Key key, <#= Model.Name #>LoadSettings loadSettings = null)
  743. {
  744. if (key == null) throw new ArgumentNullException("key");
  745. loadSettings = <#= Model.Name #>LoadSettings.From<#= Model.Name #>LoadSettings(loadSettings);
  746. loadSettings.Filter.ByKey = key;
  747. var criteria = new GetAllCriteria
  748. {
  749. LoadSettings = loadSettings,
  750. };
  751. var result = DataPortal.Fetch<Csla<#= Model.Name #>EditCollection>(criteria);
  752. return result;
  753. }
  754. public static Csla<#= Model.Name #>EditCollection Get<#= Model.Name #>EditCollection(<#= Model.Name #>LoadSettings loadSettings = null)
  755. {
  756. loadSettings = <#= Model.Name #>LoadSettings.From<#= Model.Name #>LoadSettings(loadSettings);
  757. var criteria = new GetAllCriteria
  758. {
  759. LoadSettings = loadSettings,
  760. };
  761. var result = DataPortal.Fetch<Csla<#= Model.Name #>EditCollection>(criteria);
  762. return result;
  763. }
  764. #endregion [ Root Factory Methods ]
  765. #region [ Child Factory Methods ]
  766. internal static Csla<#= Model.Name #>EditCollection Get<#= Model.Name #>EditCollection(IEnumerable<<#= Model.Name #>> data, <#= Model.Name #>LoadSettings loadSettings)
  767. {
  768. if (data == null) throw new ArgumentNullException("data");
  769. if (loadSettings == null) throw new ArgumentNullException("loadSettings");
  770. var result = new Csla<#= Model.Name #>EditCollection();
  771. result.MarkAsChild();
  772. result.FetchAll(data, loadSettings);
  773. result.TotalItems = result.Count;
  774. return result;
  775. }
  776. #endregion [ Child Factory Methods ]
  777. private static int ForceInit = 0;
  778. protected Csla<#= Model.Name #>EditCollection()
  779. {
  780. ForceInit = 1;
  781. }
  782. #region [ Criterias ]
  783. [Serializable]
  784. class GetAllCriteria : CriteriaBase<GetAllCriteria>
  785. {
  786. public <#= Model.Name #>LoadSettings LoadSettings { get; set; }
  787. }
  788. #endregion
  789. #endregion
  790. #region [ Data Access ]
  791. private void DataPortal_Fetch(GetAllCriteria criteria)
  792. {
  793. var dbFactory = ServiceLocator.Current.GetInstance<I<#= Model.DataModel.Name #>DataRepositoryFactory>();
  794. IEnumerable<<#= Model.Name #>> data;
  795. using (var db = dbFactory.CreateRepository())
  796. {
  797. int total;
  798. data = db.<#= Model.RootEntityType.EntitySetName #>.Retrieve(criteria.LoadSettings, out total)<#+ if (Model.BaseEntityTypeRef != null) { #>.Cast<<#= Model.Name #>>()<#+ } #>;
  799. TotalItems = total;
  800. }
  801. FetchAll(data, criteria.LoadSettings);
  802. }
  803. private void FetchAll(IEnumerable<<#= Model.Name #>> data, <#= Model.Name #>LoadSettings loadSettings)
  804. {
  805. var oldRaiseListChangedEvents = RaiseListChangedEvents;
  806. try
  807. {
  808. RaiseListChangedEvents = false;
  809. foreach (var item in data)
  810. {
  811. var element = Csla<#= Model.Name #>Edit.Get<#= Model.Name #>Edit(item, loadSettings);
  812. this.Add(element);
  813. }
  814. }
  815. finally
  816. {
  817. RaiseListChangedEvents = oldRaiseListChangedEvents;
  818. }
  819. }
  820. #endregion
  821. #region [ Explicit IBusinessObjectCollection<I<#= Model.Name #>> implementation ]
  822. void ICollection<I<#= Model.Name #>>.Add(I<#= Model.Name #> item)
  823. {
  824. this.Add((Csla<#= Model.Name #>Edit)item);
  825. }
  826. void ICollection<I<#= Model.Name #>>.Clear()
  827. {
  828. this.Clear();
  829. }
  830. bool ICollection<I<#= Model.Name #>>.Contains(I<#= Model.Name #> item)
  831. {
  832. return this.Contains((Csla<#= Model.Name #>Edit)item);
  833. }
  834. void ICollection<I<#= Model.Name #>>.CopyTo(I<#= Model.Name #>[] array, int arrayIndex)
  835. {
  836. int size = Math.Min(array.Length - arrayIndex, this.Count);
  837. if (size > 0)
  838. {
  839. var a = new Csla<#= Model.Name #>Edit[size];
  840. this.CopyTo(a, 0);
  841. Array.Copy(a, 0, array, arrayIndex, size);
  842. }
  843. }
  844. int ICollection<I<#= Model.Name #>>.Count
  845. {
  846. get { return this.Count; }
  847. }
  848. bool ICollection<I<#= Model.Name #>>.IsReadOnly
  849. {
  850. get { return false; }
  851. }
  852. bool ICollection<I<#= Model.Name #>>.Remove(I<#= Model.Name #> item)
  853. {
  854. return this.Remove((Csla<#= Model.Name #>Edit)item);
  855. }
  856. IEnumerator<I<#= Model.Name #>> IEnumerable<I<#= Model.Name #>>.GetEnumerator()
  857. {
  858. return this.GetEnumerator();
  859. }
  860. System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
  861. {
  862. return this.GetEnumerator();
  863. }
  864. bool IBusinessObject.IsReadonly
  865. {
  866. get { return false; }
  867. }
  868. bool IBusinessObject.IsDirty
  869. {
  870. get { return this.IsDirty; }
  871. }
  872. I<#= Model.Name #> IBusinessObjectCollection<I<#= Model.Name #>>.this[int index]
  873. {
  874. get
  875. {
  876. return this[index];
  877. }
  878. set
  879. {
  880. this[index] = (Csla<#= Model.Name #>Edit)value;
  881. }
  882. }
  883. #endregion [ Explicit IBusinessObjectCollection<I<#= Model.Name #>> implementation ]
  884. }
  885. }
  886. <#+
  887. return this.GenerationEnvironment.ToString();
  888. }
  889. }
  890. #>