#+
//
// Copyright © Yue. All Rights Reserved.
//
public class UnitOfWorkTemplate : CSharpTemplate
{
private T4ModelInfo[] _models;
public UnitOfWorkTemplate(T4ModelInfo[] models)
{
_models=models;
}
string name;
///
///获取 生成的文件名,根据模型名定义
///
public string FileName {get {return string.Format("UnitOfWork.gen.cs");}}
public override string TransformText()
{
#>
//------------------------------------------------------------------------------
//
// 此代码由工具生成。
// 对此文件的更改可能会导致不正确的行为,并且如果
// 重新生成代码,这些更改将会丢失。
// 如存在本生成代码外的新需求,请在相同命名空间下创建同名分部类实现 UnitOfWork.cs 分部方法。
//
//
//
// Copyright(c)2013 YUECN.All rights reserved.
// CLR版本: v0.8
// 开发组织:IWB
// 公司网站:www.iwbnet.com
// 所属工程: YZXYH
// 生成时间:<#= DateTime.Now.ToString("yyyy-MM-dd HH:mm") #>
//
//------------------------------------------------------------------------------
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
<#+
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();
}
}
#>