| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- <#+
- // <copyright file="DalTemplate.tt" company="Yue@China">
- // Copyright © Yue. All Rights Reserved.
- // </copyright>
- public class UnitOfWorkTemplate : CSharpTemplate
- {
- private T4ModelInfo[] _models;
- public UnitOfWorkTemplate(T4ModelInfo[] models)
- {
- _models=models;
- }
- string name;
- ///<summary>
- ///获取 生成的文件名,根据模型名定义
- ///</summary>
- public string FileName {get {return string.Format("UnitOfWork.gen.cs");}}
- public override string TransformText()
- {
- #>
- //------------------------------------------------------------------------------
- // <auto-generated>
- // 此代码由工具生成。
- // 对此文件的更改可能会导致不正确的行为,并且如果
- // 重新生成代码,这些更改将会丢失。
- // 如存在本生成代码外的新需求,请在相同命名空间下创建同名分部类实现 UnitOfWork.cs 分部方法。
- // </auto-generated>
- //
- // <copyright file="UnitOfWork.gen.cs">
- // Copyright(c)2013 YUECN.All rights reserved.
- // CLR版本: v0.8
- // 开发组织:IWB
- // 公司网站:www.iwbnet.com
- // 所属工程: YZXYH
- // 生成时间:<#= DateTime.Now.ToString("yyyy-MM-dd HH:mm") #>
- // </copyright>
- //------------------------------------------------------------------------------
- using System;
- using YZXYH.Repository.Models;
- using YZXYH.Repository.Interface;
- namespace YZXYH.Repository
- {
-
- public class UnitOfWork:IUnitOfWork
- {
- public Yzxyh2017Context Context { get;}
- public UnitOfWork(Yzxyh2017Context context)
- {
- if (Context == null)
- {
- if(context==null)
- Context = new Yzxyh2017Context();
- else
- Context = context;
- }
- }
- #region BaseRepository<T>
- <#+
- foreach(T4ModelInfo _model in _models){
- #>
- <#+
- name = char.ToLower( _model.Name[0]) +_model.Name.Substring(1);
- #>
- private I<#= _model.Name #>Repository _<#= name #>Repository;
- public I<#= _model.Name #>Repository <#= _model.Name #>Repository
- {
- get { return _<#= name #>Repository ?? (_<#= name #>Repository = new <#= _model.Name #>Repository(Context)); }
- set { _<#= name #>Repository=value; }
- }
- <#+}#>
-
- #endregion
- #region Save & Dispose
- public bool Save()
- {
- try
- {
- Context.SaveChanges();
- return true;
- }
- catch
- {
- return false;
- }
- }
- //关闭连接
- private bool _disposed;
- protected virtual void Dispose(bool disposing)
- {
- if (!_disposed)
- if (disposing)
- Context.Dispose();
- _disposed = true;
- }
- public void Dispose()
- {
- Dispose(true);
- GC.SuppressFinalize(this);
- }
- #endregion
- }
- }
- <#+
- return this.GenerationEnvironment.ToString();
- }
- }
- #>
|