UnitOfWorkTemplate.ttinclude 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <#+
  2. // <copyright file="DalTemplate.tt" company="Yue@China">
  3. // Copyright © Yue. All Rights Reserved.
  4. // </copyright>
  5. public class UnitOfWorkTemplate : CSharpTemplate
  6. {
  7. private T4ModelInfo[] _models;
  8. public UnitOfWorkTemplate(T4ModelInfo[] models)
  9. {
  10. _models=models;
  11. }
  12. string name;
  13. ///<summary>
  14. ///获取 生成的文件名,根据模型名定义
  15. ///</summary>
  16. public string FileName {get {return string.Format("UnitOfWork.gen.cs");}}
  17. public override string TransformText()
  18. {
  19. #>
  20. //------------------------------------------------------------------------------
  21. // <auto-generated>
  22. // 此代码由工具生成。
  23. // 对此文件的更改可能会导致不正确的行为,并且如果
  24. // 重新生成代码,这些更改将会丢失。
  25. // 如存在本生成代码外的新需求,请在相同命名空间下创建同名分部类实现 UnitOfWork.cs 分部方法。
  26. // </auto-generated>
  27. //
  28. // <copyright file="UnitOfWork.gen.cs">
  29. // Copyright(c)2013 YUECN.All rights reserved.
  30. // CLR版本: v0.8
  31. // 开发组织:IWB
  32. // 公司网站:www.iwbnet.com
  33. // 所属工程: YZXYH
  34. // 生成时间:<#= DateTime.Now.ToString("yyyy-MM-dd HH:mm") #>
  35. // </copyright>
  36. //------------------------------------------------------------------------------
  37. using System;
  38. using YZXYH.Repository.Models;
  39. using YZXYH.Repository.Interface;
  40. namespace YZXYH.Repository
  41. {
  42. public class UnitOfWork:IUnitOfWork
  43. {
  44. public Yzxyh2017Context Context { get;}
  45. public UnitOfWork(Yzxyh2017Context context)
  46. {
  47. if (Context == null)
  48. {
  49. if(context==null)
  50. Context = new Yzxyh2017Context();
  51. else
  52. Context = context;
  53. }
  54. }
  55. #region BaseRepository<T>
  56. <#+
  57. foreach(T4ModelInfo _model in _models){
  58. #>
  59. <#+
  60. name = char.ToLower( _model.Name[0]) +_model.Name.Substring(1);
  61. #>
  62. private I<#= _model.Name #>Repository _<#= name #>Repository;
  63. public I<#= _model.Name #>Repository <#= _model.Name #>Repository
  64. {
  65. get { return _<#= name #>Repository ?? (_<#= name #>Repository = new <#= _model.Name #>Repository(Context)); }
  66. set { _<#= name #>Repository=value; }
  67. }
  68. <#+}#>
  69. #endregion
  70. #region Save & Dispose
  71. public bool Save()
  72. {
  73. try
  74. {
  75. Context.SaveChanges();
  76. return true;
  77. }
  78. catch
  79. {
  80. return false;
  81. }
  82. }
  83. //关闭连接
  84. private bool _disposed;
  85. protected virtual void Dispose(bool disposing)
  86. {
  87. if (!_disposed)
  88. if (disposing)
  89. Context.Dispose();
  90. _disposed = true;
  91. }
  92. public void Dispose()
  93. {
  94. Dispose(true);
  95. GC.SuppressFinalize(this);
  96. }
  97. #endregion
  98. }
  99. }
  100. <#+
  101. return this.GenerationEnvironment.ToString();
  102. }
  103. }
  104. #>