-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathTestTemplate.tt
More file actions
216 lines (190 loc) · 6.53 KB
/
Copy pathTestTemplate.tt
File metadata and controls
216 lines (190 loc) · 6.53 KB
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<#@ template debug="true" hostspecific="true" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.IO" #>
<#@ output extension=".cs" #>
// ============================================================================
// Auto-generated code - do not modify manually
// Generated at: <#= DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") #>
// ============================================================================
using System;
using System.Collections.Generic;
using System.Linq;
namespace Generated.Models
{
<#
var entities = new[]
{
new { Name = "Customer", Properties = new[] { "Id:int", "FirstName:string", "LastName:string", "Email:string", "CreatedAt:DateTime", "IsActive:bool" } },
new { Name = "Order", Properties = new[] { "Id:int", "CustomerId:int", "OrderDate:DateTime", "TotalAmount:decimal", "Status:string", "ShippingAddress:string" } },
new { Name = "Product", Properties = new[] { "Id:int", "Name:string", "Description:string", "Price:decimal", "StockCount:int", "Category:string", "IsAvailable:bool" } },
new { Name = "Invoice", Properties = new[] { "Id:int", "OrderId:int", "InvoiceNumber:string", "IssuedDate:DateTime", "DueDate:DateTime", "Amount:decimal", "IsPaid:bool" } },
};
foreach (var entity in entities)
{
#>
/// <summary>
/// Represents a <#= entity.Name #> entity in the system.
/// </summary>
public class <#= entity.Name #>
{
<#
foreach (var prop in entity.Properties)
{
var parts = prop.Split(':');
var propName = parts[0];
var propType = parts[1];
#>
public <#= propType #> <#= propName #> { get; set; }
<#
}
#>
public override string ToString()
{
return $"<#= entity.Name #> [Id={Id}]";
}
public override bool Equals(object obj)
{
return obj is <#= entity.Name #> other && other.Id == Id;
}
public override int GetHashCode()
{
return Id.GetHashCode();
}
}
<#
}
#>
// ========================================================================
// Repository interfaces
// ========================================================================
<#
foreach (var entity in entities)
{
#>
public interface I<#= entity.Name #>Repository
{
<#= entity.Name #> GetById(int id);
IEnumerable<<#= entity.Name #>> GetAll();
void Add(<#= entity.Name #> entity);
void Update(<#= entity.Name #> entity);
void Delete(int id);
IEnumerable<<#= entity.Name #>> Find(Func<<#= entity.Name #>, bool> predicate);
}
<#
}
#>
// ========================================================================
// Enum definitions
// ========================================================================
<#
var enums = new Dictionary<string, string[]>
{
{ "OrderStatus", new[] { "Pending", "Processing", "Shipped", "Delivered", "Cancelled", "Refunded" } },
{ "PaymentMethod", new[] { "CreditCard", "DebitCard", "PayPal", "BankTransfer", "Crypto", "Cash" } },
{ "UserRole", new[] { "Admin", "Manager", "Employee", "Customer", "Guest" } },
};
foreach (var kvp in enums)
{
#>
public enum <#= kvp.Key #>
{
<#
for (int i = 0; i < kvp.Value.Length; i++)
{
var comma = i < kvp.Value.Length - 1 ? "," : "";
#>
<#= kvp.Value[i] #> = <#= i #><#= comma #>
<#
}
#>
}
<#
}
#>
// ========================================================================
// Service layer
// ========================================================================
<#
foreach (var entity in entities)
{
#>
public class <#= entity.Name #>Service
{
private readonly I<#= entity.Name #>Repository _repository;
public <#= entity.Name #>Service(I<#= entity.Name #>Repository repository)
{
_repository = repository ?? throw new ArgumentNullException(nameof(repository));
}
public <#= entity.Name #> Get(int id)
{
if (id <= 0) throw new ArgumentException("Id must be positive", nameof(id));
return _repository.GetById(id);
}
public IEnumerable<<#= entity.Name #>> GetAll() => _repository.GetAll();
public void Create(<#= entity.Name #> entity)
{
if (entity == null) throw new ArgumentNullException(nameof(entity));
_repository.Add(entity);
}
public void Remove(int id)
{
if (id <= 0) throw new ArgumentException("Id must be positive", nameof(id));
_repository.Delete(id);
}
}
<#
}
#>
}
<#+
// ========================================================================
// Class feature block - helper methods available to all code above
// ========================================================================
private string ToCamelCase(string input)
{
if (string.IsNullOrEmpty(input)) return input;
return char.ToLowerInvariant(input[0]) + input.Substring(1);
}
private string ToPlural(string input)
{
if (string.IsNullOrEmpty(input)) return input;
if (input.EndsWith("y"))
return input.Substring(0, input.Length - 1) + "ies";
if (input.EndsWith("s") || input.EndsWith("x") || input.EndsWith("ch"))
return input + "es";
return input + "s";
}
private string GetDefaultValue(string typeName)
{
switch (typeName)
{
case "int": return "0";
case "decimal": return "0m";
case "bool": return "false";
case "DateTime": return "DateTime.MinValue";
case "string": return "string.Empty";
default: return "default";
}
}
private IEnumerable<string> WrapLines(string text, int maxLength)
{
var words = text.Split(' ');
var currentLine = new StringBuilder();
foreach (var word in words)
{
if (currentLine.Length + word.Length + 1 > maxLength)
{
yield return currentLine.ToString();
currentLine.Clear();
}
if (currentLine.Length > 0) currentLine.Append(' ');
currentLine.Append(word);
}
if (currentLine.Length > 0)
yield return currentLine.ToString();
}
#>