subsonic 3.0.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. data/lib/LICENSE.txt +41 -0
  2. data/lib/SubSonic.Core.dll +0 -0
  3. data/lib/T4 Templates/ActiveRecord/ActiveRecord.tt +620 -0
  4. data/lib/T4 Templates/ActiveRecord/Context.tt +295 -0
  5. data/lib/T4 Templates/ActiveRecord/SQLServer.ttinclude +347 -0
  6. data/lib/T4 Templates/ActiveRecord/Settings.ttinclude +552 -0
  7. data/lib/T4 Templates/ActiveRecord/StoredProcedures.tt +30 -0
  8. data/lib/T4 Templates/ActiveRecord/Structs.tt +67 -0
  9. data/lib/T4 Templates/LinqTemplates/Classes.tt +117 -0
  10. data/lib/T4 Templates/LinqTemplates/Context.tt +276 -0
  11. data/lib/T4 Templates/LinqTemplates/SQLServer.ttinclude +349 -0
  12. data/lib/T4 Templates/LinqTemplates/Settings.ttinclude +551 -0
  13. data/lib/T4 Templates/LinqTemplates/StoredProcedures.tt +30 -0
  14. data/lib/T4 Templates/LinqTemplates/Structs.tt +67 -0
  15. data/lib/T4 Templates/README.txt +7 -0
  16. data/lib/T4 Templates/SubSonic.TemplatesVB/ActiveRecord/ActiveRecord.tt +560 -0
  17. data/lib/T4 Templates/SubSonic.TemplatesVB/ActiveRecord/ActiveRecord.vb +6447 -0
  18. data/lib/T4 Templates/SubSonic.TemplatesVB/ActiveRecord/Context.tt +258 -0
  19. data/lib/T4 Templates/SubSonic.TemplatesVB/ActiveRecord/Context.vb +349 -0
  20. data/lib/T4 Templates/SubSonic.TemplatesVB/ActiveRecord/SQLServer.ttinclude +332 -0
  21. data/lib/T4 Templates/SubSonic.TemplatesVB/ActiveRecord/Settings.ttinclude +550 -0
  22. data/lib/T4 Templates/SubSonic.TemplatesVB/ActiveRecord/StoredProcedures.tt +30 -0
  23. data/lib/T4 Templates/SubSonic.TemplatesVB/ActiveRecord/StoredProcedures.vb +55 -0
  24. data/lib/T4 Templates/SubSonic.TemplatesVB/ActiveRecord/Structs.tt +57 -0
  25. data/lib/T4 Templates/SubSonic.TemplatesVB/ActiveRecord/Structs.vb +1478 -0
  26. data/lib/T4 Templates/SubSonic.TemplatesVB/ActiveRecord/Tests/FetchTests.vb +52 -0
  27. data/lib/T4 Templates/SubSonic.TemplatesVB/App.config +9 -0
  28. data/lib/T4 Templates/SubSonic.TemplatesVB/My Project/Application.Designer.vb +13 -0
  29. data/lib/T4 Templates/SubSonic.TemplatesVB/My Project/Application.myapp +10 -0
  30. data/lib/T4 Templates/SubSonic.TemplatesVB/My Project/AssemblyInfo.vb +35 -0
  31. data/lib/T4 Templates/SubSonic.TemplatesVB/My Project/Resources.Designer.vb +63 -0
  32. data/lib/T4 Templates/SubSonic.TemplatesVB/My Project/Resources.resx +117 -0
  33. data/lib/T4 Templates/SubSonic.TemplatesVB/My Project/Settings.Designer.vb +73 -0
  34. data/lib/T4 Templates/SubSonic.TemplatesVB/My Project/Settings.settings +7 -0
  35. data/lib/T4 Templates/SubSonic.TemplatesVB/SubSonic.TemplatesVB.vbproj +171 -0
  36. data/lib/T4 Templates/SubSonic.TemplatesVB/SubSonic.TemplatesVB.vbproj.user +5 -0
  37. data/lib/T4 Templates/TemplateProviders/MySQL.ttinclude +278 -0
  38. data/lib/T4 Templates/TemplateProviders/MySQL.ttinclude.orig +303 -0
  39. data/lib/T4 Templates/TemplateProviders/MySQLTest.tt +27 -0
  40. data/lib/T4 Templates/TemplateProviders/SQLite.ttinclude +194 -0
  41. data/lib/T4 Templates/TemplateProviders/SQLite.ttinclude.orig +236 -0
  42. data/lib/T4 Templates/TemplateProviders/SQLiteTest.tt +27 -0
  43. data/lib/T4 Templates/TemplateProviders/Settings.ttinclude +551 -0
  44. metadata +112 -0
data/lib/LICENSE.txt ADDED
@@ -0,0 +1,41 @@
1
+ New BSD License
2
+ http://www.opensource.org/licenses/bsd-license.php
3
+ Copyright (c) 2009, Rob Conery (robconery@gmail.com)
4
+ All rights reserved.
5
+
6
+ Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
7
+
8
+ Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
9
+ Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
10
+ Neither the name of the SubSonic nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
11
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
12
+
13
+
14
+ Code in the "Linq" directory was created by Matt Warren under MS-PL. Using SubSonic means that you agree with the terms of this license:
15
+ http://www.opensource.org/licenses/ms-pl.html
16
+
17
+ Microsoft Public License (Ms-PL)
18
+ This license governs use of the accompanying software. If you use the software, you
19
+ accept this license. If you do not accept the license, do not use the software.
20
+
21
+ 1. Definitions
22
+ The terms "reproduce," "reproduction," "derivative works," and "distribution" have the
23
+ same meaning here as under U.S. copyright law.
24
+ A "contribution" is the original software, or any additions or changes to the software.
25
+ A "contributor" is any person that distributes its contribution under this license.
26
+ "Licensed patents" are a contributor's patent claims that read directly on its contribution.
27
+
28
+ 2. Grant of Rights
29
+ (A) Copyright Grant- Subject to the terms of this license, including the license conditions and limitations in section 3, each contributor grants you a non-exclusive, worldwide, royalty-free copyright license to reproduce its contribution, prepare derivative works of its contribution, and distribute its contribution or any derivative works that you create.
30
+ (B) Patent Grant- Subject to the terms of this license, including the license conditions and limitations in section 3, each contributor grants you a non-exclusive, worldwide, royalty-free license under its licensed patents to make, have made, use, sell, offer for sale, import, and/or otherwise dispose of its contribution in the software or derivative works of the contribution in the software.
31
+
32
+ 3. Conditions and Limitations
33
+ (A) No Trademark License- This license does not grant you rights to use any contributors' name, logo, or trademarks.
34
+ (B) If you bring a patent claim against any contributor over patents that you claim are infringed by the software, your patent license from such contributor to the software ends automatically.
35
+ (C) If you distribute any portion of the software, you must retain all copyright, patent, trademark, and attribution notices that are present in the software.
36
+ (D) If you distribute any portion of the software in source code form, you may do so only under this license by including a complete copy of this license with your distribution. If you distribute any portion of the software in compiled or object code form, you may only do so under a license that complies with this license.
37
+ (E) The software is licensed "as-is." You bear the risk of using it. The contributors give no express warranties, guarantees or conditions. You may have additional consumer rights under your local laws which this license cannot change. To the extent permitted under your local laws, the contributors exclude the implied warranties of merchantability, fitness for a particular purpose and non-infringement.
38
+
39
+
40
+
41
+
Binary file
@@ -0,0 +1,620 @@
1
+ <#@ include file="SQLServer.ttinclude" #>
2
+ using System;
3
+ using System.Collections.Generic;
4
+ using System.Linq;
5
+ using System.Text;
6
+ using System.Data;
7
+ using SubSonic.DataProviders;
8
+ using SubSonic.Extensions;
9
+ using System.Linq.Expressions;
10
+ using SubSonic.Schema;
11
+ using System.Collections;
12
+ using SubSonic;
13
+ using SubSonic.Repository;
14
+ using System.ComponentModel;
15
+ using System.Data.Common;
16
+
17
+ namespace <#=Namespace #>
18
+ {
19
+ <#
20
+
21
+ var tables = LoadTables();
22
+
23
+ foreach(Table tbl in tables)
24
+ {
25
+ if(!ExcludeTables.Contains(tbl.Name))
26
+ {
27
+ #>
28
+
29
+
30
+ /// <summary>
31
+ /// A class which represents the <#=tbl.Name #> table in the <#=DatabaseName#> Database.
32
+ /// </summary>
33
+ public partial class <#=tbl.ClassName#>: IActiveRecord
34
+ {
35
+
36
+ #region Built-in testing
37
+ static TestRepository<<#=tbl.ClassName#>> _testRepo;
38
+
39
+
40
+
41
+ static void SetTestRepo(){
42
+ _testRepo = _testRepo ?? new TestRepository<<#=tbl.ClassName#>>(new <#=Namespace#>.<#=DatabaseName#>DB());
43
+ }
44
+ public static void ResetTestRepo(){
45
+ _testRepo = null;
46
+ SetTestRepo();
47
+ }
48
+ public static void Setup(List<<#=tbl.ClassName#>> testlist){
49
+ SetTestRepo();
50
+ foreach (var item in testlist)
51
+ {
52
+ _testRepo._items.Add(item);
53
+ }
54
+ }
55
+ public static void Setup(<#=tbl.ClassName#> item) {
56
+ SetTestRepo();
57
+ _testRepo._items.Add(item);
58
+ }
59
+ public static void Setup(int testItems) {
60
+ SetTestRepo();
61
+ for(int i=0;i<testItems;i++){
62
+ <#=tbl.ClassName#> item=new <#=tbl.ClassName#>();
63
+ _testRepo._items.Add(item);
64
+ }
65
+ }
66
+
67
+ public bool TestMode = false;
68
+
69
+
70
+ #endregion
71
+
72
+ IRepository<<#=tbl.ClassName#>> _repo;
73
+ ITable tbl;
74
+ bool _isNew;
75
+ public bool IsNew(){
76
+ return _isNew;
77
+ }
78
+
79
+ public void SetIsLoaded(bool isLoaded){
80
+ _isLoaded=isLoaded;
81
+ if(isLoaded)
82
+ OnLoaded();
83
+ }
84
+
85
+ public void SetIsNew(bool isNew){
86
+ _isNew=isNew;
87
+ }
88
+ bool _isLoaded;
89
+ public bool IsLoaded(){
90
+ return _isLoaded;
91
+ }
92
+
93
+ List<IColumn> _dirtyColumns;
94
+ public bool IsDirty(){
95
+ return _dirtyColumns.Count>0;
96
+ }
97
+
98
+ public List<IColumn> GetDirtyColumns (){
99
+ return _dirtyColumns;
100
+ }
101
+
102
+ <#=Namespace#>.<#=DatabaseName#>DB _db;
103
+ public <#=tbl.ClassName#>(string connectionString, string providerName) {
104
+
105
+ _db=new <#=Namespace#>.<#=DatabaseName#>DB(connectionString, providerName);
106
+ Init();
107
+ }
108
+ void Init(){
109
+ TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
110
+ _dirtyColumns=new List<IColumn>();
111
+ if(TestMode){
112
+ <#=tbl.ClassName#>.SetTestRepo();
113
+ _repo=_testRepo;
114
+ }else{
115
+ _repo = new SubSonicRepository<<#=tbl.ClassName#>>(_db);
116
+ }
117
+ tbl=_repo.GetTable();
118
+ SetIsNew(true);
119
+ OnCreated();
120
+
121
+ }
122
+
123
+ public <#=tbl.ClassName#>(){
124
+ _db=new <#=Namespace#>.<#=DatabaseName#>DB();
125
+ Init();
126
+ }
127
+
128
+
129
+ partial void OnCreated();
130
+
131
+ partial void OnLoaded();
132
+
133
+ partial void OnSaved();
134
+
135
+ partial void OnChanged();
136
+
137
+ public IList<IColumn> Columns{
138
+ get{
139
+ return tbl.Columns;
140
+ }
141
+ }
142
+
143
+ public <#=tbl.ClassName#>(Expression<Func<<#=tbl.ClassName#>, bool>> expression):this() {
144
+
145
+ SetIsLoaded(_repo.Load(this,expression));
146
+ }
147
+
148
+
149
+
150
+ internal static IRepository<<#=tbl.ClassName#>> GetRepo(string connectionString, string providerName){
151
+ <#=Namespace#>.<#=DatabaseName#>DB db;
152
+ if(String.IsNullOrEmpty(connectionString)){
153
+ db=new <#=Namespace#>.<#=DatabaseName#>DB();
154
+ }else{
155
+ db=new <#=Namespace#>.<#=DatabaseName#>DB(connectionString, providerName);
156
+ }
157
+ IRepository<<#=tbl.ClassName#>> _repo;
158
+
159
+ if(db.TestMode){
160
+ <#=tbl.ClassName#>.SetTestRepo();
161
+ _repo=_testRepo;
162
+ }else{
163
+ _repo = new SubSonicRepository<<#=tbl.ClassName#>>(db);
164
+ }
165
+ return _repo;
166
+ }
167
+
168
+ internal static IRepository<<#=tbl.ClassName#>> GetRepo(){
169
+ return GetRepo("","");
170
+ }
171
+
172
+ public static <#=tbl.ClassName#> SingleOrDefault(Expression<Func<<#=tbl.ClassName#>, bool>> expression) {
173
+
174
+ var repo = GetRepo();
175
+ var results=repo.Find(expression);
176
+ <#=tbl.ClassName#> single=null;
177
+ if(results.Count() > 0){
178
+ single=results.ToList()[0];
179
+ single.OnLoaded();
180
+ single.SetIsLoaded(true);
181
+ single.SetIsNew(false);
182
+ }
183
+
184
+ return single;
185
+ }
186
+
187
+ public static <#=tbl.ClassName#> SingleOrDefault(Expression<Func<<#=tbl.ClassName#>, bool>> expression,string connectionString, string providerName) {
188
+ var repo = GetRepo(connectionString,providerName);
189
+ var results=repo.Find(expression);
190
+ <#=tbl.ClassName#> single=null;
191
+ if(results.Count() > 0){
192
+ single=results.ToList()[0];
193
+ }
194
+
195
+ return single;
196
+
197
+
198
+ }
199
+
200
+
201
+ public static bool Exists(Expression<Func<<#=tbl.ClassName#>, bool>> expression,string connectionString, string providerName) {
202
+
203
+ return All(connectionString,providerName).Any(expression);
204
+ }
205
+ public static bool Exists(Expression<Func<<#=tbl.ClassName#>, bool>> expression) {
206
+
207
+ return All().Any(expression);
208
+ }
209
+
210
+ public static IList<<#=tbl.ClassName#>> Find(Expression<Func<<#=tbl.ClassName#>, bool>> expression) {
211
+
212
+ var repo = GetRepo();
213
+ return repo.Find(expression).ToList();
214
+ }
215
+
216
+ public static IList<<#=tbl.ClassName#>> Find(Expression<Func<<#=tbl.ClassName#>, bool>> expression,string connectionString, string providerName) {
217
+
218
+ var repo = GetRepo(connectionString,providerName);
219
+ return repo.Find(expression).ToList();
220
+
221
+ }
222
+ public static IQueryable<<#=tbl.ClassName#>> All(string connectionString, string providerName) {
223
+ return GetRepo(connectionString,providerName).GetAll();
224
+ }
225
+ public static IQueryable<<#=tbl.ClassName#>> All() {
226
+ return GetRepo().GetAll();
227
+ }
228
+
229
+ public static PagedList<<#=tbl.ClassName#>> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
230
+ return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
231
+ }
232
+
233
+ public static PagedList<<#=tbl.ClassName#>> GetPaged(string sortBy, int pageIndex, int pageSize) {
234
+ return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
235
+ }
236
+
237
+ public static PagedList<<#=tbl.ClassName#>> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
238
+ return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
239
+
240
+ }
241
+
242
+
243
+ public static PagedList<<#=tbl.ClassName#>> GetPaged(int pageIndex, int pageSize) {
244
+ return GetRepo().GetPaged(pageIndex, pageSize);
245
+
246
+ }
247
+
248
+ public string KeyName()
249
+ {
250
+ return "<#=tbl.PK.CleanName #>";
251
+ }
252
+
253
+ public object KeyValue()
254
+ {
255
+ return this.<#=tbl.PK.CleanName#>;
256
+ }
257
+
258
+ public void SetKeyValue(object value) {
259
+ if (value != null && value!=DBNull.Value) {
260
+ var settable = value.ChangeTypeTo<<#=tbl.PK.SysType#>>();
261
+ this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
262
+ }
263
+ }
264
+
265
+ public override string ToString(){
266
+ <# if (tbl.ClassName == tbl.Descriptor.CleanName){ #>
267
+ return this.<#=tbl.Descriptor.CleanName+"X" #>.ToString();
268
+ <# } else { #>
269
+ return this.<#=tbl.Descriptor.CleanName #>.ToString();
270
+ <# } #>
271
+ }
272
+
273
+ public override bool Equals(object obj){
274
+ if(obj.GetType()==typeof(<#=tbl.ClassName#>)){
275
+ <#=tbl.ClassName#> compare=(<#=tbl.ClassName#>)obj;
276
+ return compare.KeyValue()==this.KeyValue();
277
+ }else{
278
+ return base.Equals(obj);
279
+ }
280
+ }
281
+
282
+ <# if(tbl.PK.SysType=="int"){#>
283
+
284
+ public override int GetHashCode() {
285
+ return this.<#=tbl.PK.CleanName #>;
286
+ }
287
+
288
+ <# }#>
289
+ public string DescriptorValue()
290
+ {
291
+ <# if (tbl.ClassName == tbl.Descriptor.CleanName){ #>
292
+ return this.<#=tbl.Descriptor.CleanName+"X" #>.ToString();
293
+ <# } else { #>
294
+ return this.<#=tbl.Descriptor.CleanName #>.ToString();
295
+ <# } #>
296
+ }
297
+
298
+ public string DescriptorColumn() {
299
+ return "<#=tbl.Descriptor.CleanName #>";
300
+ }
301
+ public static string GetKeyColumn()
302
+ {
303
+ return "<#=tbl.PK.CleanName #>";
304
+ }
305
+ public static string GetDescriptorColumn()
306
+ {
307
+ return "<#=tbl.Descriptor.CleanName #>";
308
+ }
309
+
310
+ #region ' Foreign Keys '
311
+ <#
312
+ List<string> fkCreated = new List<string>();
313
+ foreach(FKTable fk in tbl.FKTables)
314
+ {
315
+
316
+ if(!ExcludeTables.Contains(fk.OtherTable)){
317
+ string propName=fk.OtherQueryable;
318
+ if(fkCreated.Contains(propName))
319
+ {
320
+ propName=fk.OtherQueryable+fkCreated.Count.ToString();
321
+ }
322
+
323
+ fkCreated.Add(fk.OtherQueryable);
324
+
325
+
326
+ #>
327
+ public IQueryable<<#=fk.OtherClass #>> <#=propName #>
328
+ {
329
+ get
330
+ {
331
+
332
+ var repo=<#=Namespace #>.<#=fk.OtherClass#>.GetRepo();
333
+ return from items in repo.GetAll()
334
+ where items.<#=CleanUp(fk.OtherColumn)#> == _<#=CleanUp(fk.ThisColumn)#>
335
+ select items;
336
+ }
337
+ }
338
+
339
+ <#
340
+ }
341
+ }
342
+
343
+ #>
344
+ #endregion
345
+
346
+
347
+ <#
348
+ foreach(Column col in tbl.Columns)
349
+ {
350
+
351
+ if (tbl.ClassName == col.CleanName)
352
+ {
353
+ col.CleanName += "X";
354
+ }
355
+ #>
356
+ <#=col.SysType #><#=CheckNullable(col)#> _<#=col.CleanName #>;
357
+ public <#=col.SysType #><#=CheckNullable(col)#> <#=col.CleanName #>
358
+ {
359
+ get { return _<#=col.CleanName #>; }
360
+ set
361
+ {
362
+ if(_<#=col.CleanName #>!=value){
363
+ _<#=col.CleanName #>=value;
364
+ var col=tbl.Columns.SingleOrDefault(x=>x.Name=="<#=col.Name #>");
365
+ if(col!=null){
366
+ if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
367
+ _dirtyColumns.Add(col);
368
+ }
369
+ }
370
+ OnChanged();
371
+ }
372
+ }
373
+ }
374
+
375
+ <#
376
+ }
377
+ #>
378
+
379
+
380
+ public DbCommand GetUpdateCommand() {
381
+ <#if(tbl.Columns.Any(x=>x.Name.ToLower()=="modifiedon")){#>
382
+ if (!_dirtyColumns.Any(x => x.Name.ToLower() == "modifiedon")) {
383
+ this.<#=tbl.Columns.Single(x=>x.Name.ToLower()=="modifiedon").CleanName#>=<#=DatabaseName#>DB.DateTimeNowTruncatedDownToSecond();
384
+ }
385
+ <#}#>
386
+ if(TestMode)
387
+ return _db.DataProvider.CreateCommand();
388
+ else
389
+ return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
390
+
391
+ }
392
+ public DbCommand GetInsertCommand() {
393
+
394
+ if(TestMode)
395
+ return _db.DataProvider.CreateCommand();
396
+ else
397
+ return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
398
+ }
399
+
400
+ public DbCommand GetDeleteCommand() {
401
+ if(TestMode)
402
+ return _db.DataProvider.CreateCommand();
403
+ else
404
+ return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
405
+ }
406
+
407
+
408
+ public void Update(){
409
+ Update(_db.DataProvider);
410
+ }
411
+
412
+ public void Update(IDataProvider provider){
413
+
414
+ <#if(tbl.Columns.Any(x=>x.Name=="ModifiedBy")){#>
415
+ if(String.IsNullOrEmpty(this.ModifiedBy))
416
+ this.ModifiedBy=Environment.UserName;
417
+ <#}#>
418
+ <#if(tbl.Columns.Any(x=>x.Name=="ModifiedOn")){#>
419
+ this.ModifiedOn=<#=DatabaseName#>DB.DateTimeNowTruncatedDownToSecond();
420
+ <#}#>
421
+
422
+ if(this._dirtyColumns.Count>0){
423
+ _repo.Update(this,provider);
424
+ _dirtyColumns.Clear();
425
+ }
426
+ OnSaved();
427
+ }
428
+
429
+ public void Add(){
430
+ Add(_db.DataProvider);
431
+ }
432
+
433
+ <#if(tbl.Columns.Any(x=>x.Name=="ModifiedBy")){#>
434
+ public void Update(string username){
435
+
436
+ this.ModifiedBy=username;
437
+ Update();
438
+
439
+ }
440
+ public void Update(string username, IDataProvider provider){
441
+
442
+ this.ModifiedBy=username;
443
+ Update(provider);
444
+ }
445
+ <#}#>
446
+
447
+
448
+ public void Add(IDataProvider provider){
449
+
450
+ <#if(tbl.Columns.Any(x=>x.Name=="CreatedOn")){#>
451
+
452
+ this.CreatedOn=<#=DatabaseName#>DB.DateTimeNowTruncatedDownToSecond();
453
+ <#}#>
454
+ <#if(tbl.Columns.Any(x=>x.Name=="CreatedBy")){#>
455
+ if(String.IsNullOrEmpty(this.CreatedBy))
456
+ this.CreatedBy=Environment.UserName;
457
+ <#}#>
458
+ <#if(tbl.Columns.Any(x=>x.Name=="ModifiedOn")){#>
459
+ this.ModifiedOn=<#=DatabaseName#>DB.DateTimeNowTruncatedDownToSecond();
460
+ <#}#>
461
+ <#if(tbl.Columns.Any(x=>x.Name=="ModifiedBy")){#>
462
+ if(String.IsNullOrEmpty(this.ModifiedBy))
463
+ this.ModifiedBy=Environment.UserName;
464
+ <#}#>
465
+
466
+ var key=KeyValue();
467
+ if(key==null){
468
+ var newKey=_repo.Add(this,provider);
469
+ this.SetKeyValue(newKey);
470
+ }else{
471
+ _repo.Add(this,provider);
472
+ }
473
+ SetIsNew(false);
474
+ OnSaved();
475
+ }
476
+
477
+ <#if(tbl.Columns.Any(x=>x.Name=="CreatedBy")){#>
478
+ public void Add(string username){
479
+
480
+ this.CreatedBy=username;
481
+ Add();
482
+
483
+ }
484
+ public void Add(string username, IDataProvider provider){
485
+
486
+ this.CreatedBy=username;
487
+ Add(provider);
488
+ }
489
+ <#}#>
490
+
491
+
492
+ public void Save() {
493
+ Save(_db.DataProvider);
494
+ }
495
+ public void Save(IDataProvider provider) {
496
+
497
+
498
+ if (_isNew) {
499
+ Add(provider);
500
+
501
+ } else {
502
+ Update(provider);
503
+ }
504
+
505
+ }
506
+
507
+ <#if(tbl.Columns.Any(x=>x.Name=="CreatedBy" || x.Name=="ModifiedBy")){#>
508
+ public void Save(string username, IDataProvider provider) {
509
+
510
+
511
+ if (_isNew) {
512
+ <#if(tbl.Columns.Any(x=>x.Name=="CreatedBy")){#>
513
+ Add(username,provider);
514
+ <#}else{#>
515
+ Add(provider);
516
+ <#}#>
517
+ } else {
518
+ <#if(tbl.Columns.Any(x=>x.Name=="ModifiedBy")){#>
519
+ Update(username,provider);
520
+ <#}else{#>
521
+ Update(provider);
522
+ <#}#>
523
+
524
+ }
525
+
526
+ }
527
+ <#}#>
528
+
529
+
530
+ public void Delete(IDataProvider provider) {
531
+ <#if(tbl.HasLogicalDelete()){#>
532
+
533
+ this.<#=tbl.DeleteColumn.CleanName#>=true;
534
+ _repo.Update(this,provider);
535
+
536
+ <#}else{#>
537
+
538
+ _repo.Delete(KeyValue());
539
+
540
+ <#}#>
541
+ }
542
+
543
+
544
+ public void Delete() {
545
+ Delete(_db.DataProvider);
546
+ }
547
+
548
+
549
+ public static void Delete(Expression<Func<<#=tbl.ClassName#>, bool>> expression) {
550
+ var repo = GetRepo();
551
+
552
+ <#if(tbl.HasLogicalDelete()){#>
553
+
554
+ List<<#=tbl.ClassName#>> items=repo.GetAll().Where(expression).ToList();
555
+ items.ForEach(x=>x.<#=tbl.DeleteColumn.CleanName#>=true);
556
+ repo.Update(items);
557
+
558
+ <#}else{#>
559
+
560
+ repo.DeleteMany(expression);
561
+
562
+ <#}#>
563
+ }
564
+
565
+ <#if(tbl.HasLogicalDelete()){#>
566
+
567
+ public static void Destroy(Func<<#=tbl.ClassName#>, bool> expression) {
568
+ var repo = GetRepo();
569
+ repo.Delete(expression);
570
+ }
571
+
572
+ public static void Destroy(object key) {
573
+ var repo = GetRepo();
574
+ repo.Delete(key);
575
+ }
576
+
577
+ public static void Destroy(object key, IDataProvider provider) {
578
+
579
+ var repo = GetRepo();
580
+ repo.Delete(key,provider);
581
+
582
+ }
583
+
584
+ public void Destroy() {
585
+ _repo.Delete(KeyValue());
586
+ }
587
+ public void Destroy(IDataProvider provider) {
588
+ _repo.Delete(KeyValue(), provider);
589
+ }
590
+ <#}#>
591
+
592
+
593
+ public void Load(IDataReader rdr) {
594
+ Load(rdr, true);
595
+ }
596
+ public void Load(IDataReader rdr, bool closeReader) {
597
+ if (rdr.Read()) {
598
+
599
+ try {
600
+ rdr.Load(this);
601
+ SetIsNew(false);
602
+ SetIsLoaded(true);
603
+ } catch {
604
+ SetIsLoaded(false);
605
+ throw;
606
+ }
607
+ }else{
608
+ SetIsLoaded(false);
609
+ }
610
+
611
+ if (closeReader)
612
+ rdr.Dispose();
613
+ }
614
+
615
+
616
+ }
617
+ <# }
618
+ }
619
+ #>
620
+ }