Created
May 2, 2016 20:02
-
-
Save vendettamit/e721a56b57f79afa29ade8c7148f1f00 to your computer and use it in GitHub Desktop.
A T4Template to auto generate Unit of work factory over repository pattern (Entity framework)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<#@ template debug="true" hostSpecific="true" #> | |
<#@ output extension=".cs" #> | |
<#@ Assembly Name="System.Core.dll" #> | |
<#@ Assembly Name="System.Xml.dll" #> | |
<#@ Assembly Name="System.Xml.Linq.dll" #> | |
<#@ Assembly Name="System.Windows.Forms.dll" #> | |
<#@ import namespace="System" #> | |
<#@ import namespace="System.IO" #> | |
<#@ import namespace="System.Diagnostics" #> | |
<#@ import namespace="System.Linq" #> | |
<#@ import namespace="System.Xml.Linq" #> | |
<#@ import namespace="System.Collections" #> | |
<#@ import namespace="System.Collections.Generic" #> | |
<# | |
/******************************************/ | |
// Add model entities in below array, template will automatically update the UnitOfWork template. | |
var entities = new String[] { | |
"Course", | |
"Test" | |
}; | |
/******************************************/ | |
#> | |
/***************************** | |
Generated by <#= Host.TemplateFile #> | |
Warning (AutoGenerated) - This file is autogenerated by UnitOfWork template file. DO NOT update code here to add a new entity. | |
Add new entity in the UnitOfWorkFactory.tt code. Follow instructions in the source. | |
******************************/ | |
using System; | |
using System.Collections.Generic; | |
using Domain_Repository.Domain; | |
using Domain_Repository.Infrastructre; | |
using System.Data.Entity; | |
using Domain_Repository.Repository; | |
using Domain_Repository.Repository.EF; | |
namespace Domain_Repository.DataAccess | |
{ | |
public static class UnitOfWorkFactory | |
{ | |
private static IUnitOfWork _instance = null; | |
public static IUnitOfWork Create() | |
{ | |
return _instance ?? new UnitOfWork(); | |
} | |
private partial class UnitOfWork : DbContext, IUnitOfWork | |
{ | |
<# foreach(var entity in entities) {#> | |
private DbSet<<#=entity #>> <#=entity #>s { get; set; } | |
<#}#> | |
<# foreach(var entity in entities) {#> | |
private Repository<<#=entity #>> _<#=entity.Replace(entity[0].ToString(), entity[0].ToString().ToLower()) #>Repository; | |
<#}#> | |
internal UnitOfWork() | |
{ | |
<# foreach(var entity in entities) {#> | |
_<#=entity.Replace(entity[0].ToString(), entity[0].ToString().ToLower()) #>Repository = new Repository<<#=entity #>>(<#=entity #>s); | |
<#}#> | |
} | |
<# foreach(var entity in entities) {#> | |
public IRepository<<#=entity #>> <#=entity #>Repository | |
{ | |
get { return _<#=entity.Replace(entity[0].ToString(), entity[0].ToString().ToLower()) #>Repository; } | |
} | |
<#}#> | |
public void Save() | |
{ | |
SaveChanges(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment