-
Notifications
You must be signed in to change notification settings - Fork 5
/
RepositoryGenerator.tt
190 lines (164 loc) · 5.09 KB
/
RepositoryGenerator.tt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<#@ template language="C#" debug="false" hostspecific="true" #>
<#@ output extension=".cs" #>
<#@ assembly name="System" #>
<#@ assembly name="System.Core" #>
<#@ assembly name="System.Data.Entity" #>
<#@ assembly name="EnvDTE" #>
<#@ import namespace="EnvDTE" #>
<#@ include file="T4Helper.ttinclude" #>
<#@ import namespace="System" #>
<#@ import namespace="System.Collections" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Reflection" #>
<# //Set these options
string targetNamespace = "(ENTER THE FULL NAMESPACE TO YOUR MODELS)";
string efContext = "DefaultModel"; //If Context is not in the same namespace rename this to the name of the context
IServiceProvider serviceProvider = (IServiceProvider)this.Host;
DTE dte = serviceProvider.GetService(typeof(DTE)) as DTE;
var project = GetActiveProject(dte);
var classes = FindClasses(project, targetNamespace, "");
var classWithDbContext = classes.FirstOrDefault(currentClass => currentClass.Bases.OfType<CodeElement>().Any(currentBase => currentBase.Name == "DbContext"));
var classNames = classes
.Select(currentClass => currentClass.Name)
.ToList();
if(classWithDbContext != null)
{
efContext = classWithDbContext.Name;
classNames.Remove(efContext);
}
GenerateRepositoryFromPoco(project, targetNamespace, classNames, efContext);
#>
using System;
public interface IUnitOfWork : IDisposable
{
<#
foreach(string className in classNames)
{
var properClassName = GetProperClassName(className);
var repositoryInterfaceName = "I" + properClassName + "Repository";
#>
<#= repositoryInterfaceName + " " + properClassName+"s" #> { get; }
<#
}
#>
void Save();
}
<# CreateFile("IUnitofWork.cs"); #>
using System;
using <#= targetNamespace #>;
public sealed class UnitOfWork : IUnitOfWork
{
private <#= efContext #> _context;
public UnitOfWork(<#= efContext #> context)
{
_context = context;
}
//Delete this default constructor if using an IoC container
public UnitOfWork()
{
_context = new <#= efContext #>();
}
<#foreach(string className in classNames)
{
var properClassName = GetProperClassName(className);
var repositoryInterfaceName = "I" + properClassName + "Repository";
#>
public <#= repositoryInterfaceName + " " + properClassName+"s"#>
{
get { return new <#= properClassName + "Repository" #>(_context); }
}
<#}#>
public void Save()
{
_context.SaveChanges();
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
if (_context != null)
{
_context.Dispose();
_context = null;
}
}
}
}
<# CreateFile("UnitofWork.cs"); #>
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using <#= targetNamespace #>;
public abstract class Repository<TEntity> : IRepository<TEntity> where TEntity : class
{
private readonly DbSet<TEntity> _entities;
protected Repository(<#= efContext #> context)
{
_entities = context.Set<TEntity>();
}
public IEnumerable<TEntity> GetAll()
{
return _entities.ToList();
}
public TEntity GetById(int id)
{
return _entities.Find(id);
}
public void Add(TEntity entity)
{
_entities.Add(entity);
}
public void Delete(TEntity entity)
{
_entities.Remove(entity);
}
}
<# CreateFile("Repository.cs"); #>
using System.Collections.Generic;
public interface IRepository<T> where T : class
{
IEnumerable<T> GetAll();
T GetById(int id);
void Add(T entity);
void Delete(T entity);
}
<# CreateFile("IRepository.cs"); #>
<#+
public void GenerateRepositoryFromPoco(Project project, string targetNamespace, List<string> classNames, string efContext)
{
foreach(string className in classNames)
{
var properClassName = GetProperClassName(className);
var repositoryName = properClassName + "Repository";
#>
using System.Collections.Generic;
using System.Linq;
using <#= targetNamespace #>;
public class <#= repositoryName #> : Repository<<#= className #>>, I<#= properClassName #>Repository
{
private <#= efContext #> _context;
public <#= repositoryName #>(<#= efContext #> context) : base(context)
{
_context = context;
}
//Override any generic method for your own custom implemention, add new repository methods to the I<#= repositoryName #>.cs file
}
<#+ CreateFile(repositoryName + ".cs"); #>
using System.Collections.Generic;
using <#= targetNamespace #>;
public interface I<#= properClassName #>Repository : IRepository<<#= className #>>
{
//Add any additional repository methods other than the generic ones (GetAll, GetById, Delete, Add)
}
<#+ CreateFile(@"I" + repositoryName + ".cs"); #>
<#+
}
}
#>