>
333 |
334 | @{
335 | ViewData["Title"] = "Index";
336 | }
337 |
338 | Index
339 |
340 |
341 | Create New
342 |
343 |
344 |
345 |
346 | <#
347 | foreach(string property in propList)
348 | {
349 | #>
350 | |
351 | @Html.DisplayNameFor(model => model.<#= property #>)
352 | |
353 | <#
354 | }
355 | #>
356 | |
357 |
358 |
359 |
360 | @foreach (var item in Model) {
361 |
362 | <#
363 | foreach(string property in propList)
364 | {
365 | #>
366 | |
367 | @Html.DisplayFor(modelItem => item.<#= property #>)
368 | |
369 | <#
370 | }
371 | #>
372 |
373 | >Edit |
374 | >Details |
375 | >Delete
376 | |
377 |
378 | }
379 |
380 |
381 | <# CreateFile(StartupProject, viewfolder, "Index.cshtml"); #>
382 | <#
383 | /////////////////
384 | // Edit.cshtml //
385 | /////////////////
386 | #>
387 | @model Domain.Entities.<#= properClassName #>
388 |
389 | @{
390 | ViewData["Title"] = "Edit";
391 | }
392 |
393 | Edit
394 |
395 | <#= properClassName #>
396 |
397 |
438 |
439 |
442 |
443 | @section Scripts {
444 | @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
445 | }
446 | <# CreateFile(StartupProject, viewfolder, "Edit.cshtml"); #>
447 | <#
448 | ////////////////////
449 | // Details.cshtml //
450 | ////////////////////
451 | #>
452 | @model Domain.Entities.<#= properClassName #>
453 |
454 | @{
455 | ViewData["Title"] = "Details";
456 | }
457 |
458 | Details
459 |
460 |
461 |
<#= properClassName #>
462 |
463 |
464 | <#
465 | foreach(string property in propList)
466 | {
467 | #>
468 | -
469 | @Html.DisplayNameFor(model => model.<#= property #>)
470 |
471 | -
472 | @Html.DisplayFor(model => model.<#= property #>)
473 |
474 | <#
475 | }
476 | #>
477 |
478 |
479 |
483 | <# CreateFile(StartupProject, viewfolder, "Details.cshtml"); #>
484 | <#
485 | ///////////////////
486 | // Delete.cshtml //
487 | ///////////////////
488 | #>
489 | @model Domain.Entities.<#= properClassName #>
490 |
491 | @{
492 | ViewData["Title"] = "Delete";
493 | }
494 |
495 | Delete
496 |
497 | Are you sure you want to delete this?
498 |
499 |
<#= properClassName #>
500 |
501 |
502 | <#
503 | foreach(string property in propList)
504 | {
505 | #>
506 | -
507 | @Html.DisplayNameFor(model => model.<#= property #>)
508 |
509 | -
510 | @Html.DisplayFor(model => model.<#= property #>)
511 |
512 | <#
513 | }
514 | #>
515 |
516 |
517 |
522 |
523 | <# CreateFile(StartupProject, viewfolder, "Delete.cshtml"); #>
524 | <#
525 | ///////////////////
526 | // Create.cshtml //
527 | ///////////////////
528 | #>
529 | @model Domain.Entities.<#= properClassName #>
530 |
531 | @{
532 | ViewData["Title"] = "Create";
533 | }
534 |
535 | Create
536 |
537 | <#= properClassName #>
538 |
539 |
561 |
562 |
565 |
566 | @section Scripts {
567 | @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
568 | }
569 | <# CreateFile(StartupProject, viewfolder, "Create.cshtml"); #>
570 |
571 | <#
572 | }
573 | ////////////////////////////////////
574 | // end loop of generate views //
575 | ////////////////////////////////////
576 | #>
577 | <#
578 | ////////////////////
579 | // _Layout.cshtml //
580 | ////////////////////
581 | #>
582 |
583 |
584 |
585 |
586 |
587 | @ViewData["Title"] - WebMVC
588 |
589 |
590 |
591 |
592 |
593 |
594 |
624 |
625 |
626 |
627 | @RenderBody()
628 |
629 |
630 |
631 |
636 |
637 |
638 |
639 | @await RenderSectionAsync("Scripts", required: false)
640 |
641 |
642 | <# OverwriteFile(StartupProject, "Views/Shared", "_Layout.cshtml"); #>
643 |
--------------------------------------------------------------------------------
/Clean Architecture Database Access Generator.tt:
--------------------------------------------------------------------------------
1 | <#@ template language="C#" debug="true" hostspecific="true" #>
2 | <#@ output extension=".txt" #>
3 | <#@ assembly name="System" #>
4 | <#@ assembly name="System.Core" #>
5 | <#@ assembly name="System.Data.Entity" #>
6 | <#@ assembly name="EnvDTE" #>
7 | <#@ import namespace="EnvDTE" #>
8 | <#@ include file="T4Helper.ttinclude" #>
9 | <#@ import namespace="System" #>
10 | <#@ import namespace="System.Collections" #>
11 | <#@ import namespace="System.Collections.Generic" #>
12 | <#@ import namespace="System.Linq" #>
13 | <#@ import namespace="System.Reflection" #>
14 | <#
15 | /////////////////////////////
16 | // Generate Domain project //
17 | /////////////////////////////
18 | CreateClassLibraryProject("Domain");
19 | #>
20 |
21 |
22 |
23 | net6.0
24 | enable
25 | enable
26 | preview
27 |
28 |
29 |
30 |
31 |
32 | <# OverwriteFile("Domain", "","Domain.csproj"); #>
33 | /////////////////////////
34 | // generated Sample.cs //
35 | /////////////////////////
36 | using System.ComponentModel.DataAnnotations;
37 |
38 | namespace Domain.Entities
39 | {
40 | public class Sample
41 | {
42 | [Key]
43 | public int Id1 { get; set; }
44 | [Key]
45 | public int Id2 { get; set; }
46 | [Key]
47 | public int Id3 { get; set; }
48 | public string? Name { get; set; }
49 | public string? Description { get; set; }
50 | }
51 | }
52 | <# CreateFile("Domain", "Entities","Sample.cs"); #>
53 | <#
54 | //////////////////////////////////
55 | // Generate Application project //
56 | //////////////////////////////////
57 | CreateClassLibraryProject("Application");
58 | #>
59 |
60 |
61 |
62 | net6.0
63 | enable
64 | enable
65 | preview
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 | <# OverwriteFile("Application", "","Application.csproj"); #>
83 | //////////////////////////////////////////
84 | // generated AddApplicationExtension.cs //
85 | //////////////////////////////////////////
86 | using Application.ServiceInterfaces;
87 | using Application.Services;
88 | using Microsoft.Extensions.DependencyInjection;
89 |
90 | namespace Application
91 | {
92 | public static class AddApplicationExtension
93 | {
94 | public static IServiceCollection AddApplication(this IServiceCollection services)
95 | {
96 | services.AddScoped();
97 | return services;
98 | }
99 | }
100 | }
101 | <# CreateFile("Application", "","AddApplicationExtension.cs"); #>
102 | <#
103 | //////////////////////////////////
104 | // Generate Persistence project //
105 | //////////////////////////////////
106 | CreateClassLibraryProject("Persistence");
107 | #>
108 |
109 |
110 |
111 | net6.0
112 | enable
113 | enable
114 | preview
115 |
116 |
117 |
118 |
119 |
120 | all
121 | runtime; build; native; contentfiles; analyzers; buildtransitive
122 |
123 |
124 |
125 | all
126 | runtime; build; native; contentfiles; analyzers; buildtransitive
127 |
128 |
129 |
130 |
131 |
132 |
133 |
134 |
135 |
136 | <# OverwriteFile("Persistence", "","Persistence.csproj"); #>
137 | ///////////////////////////////////////
138 | // generated ApplicationDbContext.cs //
139 | ///////////////////////////////////////
140 | using Domain.Entities;
141 | using Microsoft.EntityFrameworkCore;
142 |
143 | namespace Persistence.Context
144 | {
145 | public partial class ApplicationDbContext : DbContext
146 | {
147 | public ApplicationDbContext()
148 | {
149 | }
150 |
151 | public ApplicationDbContext(DbContextOptions options) : base(options)
152 | {
153 |
154 | }
155 |
156 | public virtual DbSet Samples { get; set; }
157 |
158 | protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
159 | {
160 | if (!optionsBuilder.IsConfigured)
161 | {
162 | optionsBuilder.UseSqlServer("Name=SqlServerDB");
163 | }
164 | }
165 |
166 | protected override void OnModelCreating(ModelBuilder modelBuilder)
167 | {
168 |
169 | modelBuilder.Entity(entity =>
170 | {
171 | entity.HasKey(e => new { e.Id1, e.Id2, e.Id3 });
172 | });
173 |
174 | OnModelCreatingPartial(modelBuilder);
175 | }
176 |
177 | partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
178 | }
179 | }
180 | <# CreateFile("Persistence", "Context","ApplicationDbContext.cs"); #>
181 | //////////////////////////////////////////
182 | // generated AddPersistenceExtension.cs //
183 | //////////////////////////////////////////
184 | using Application.RepositoryInterfaces;
185 | using Persistence.Context;
186 | using Persistence.Repositories;
187 | using Microsoft.EntityFrameworkCore;
188 | using Microsoft.Extensions.DependencyInjection;
189 |
190 | namespace Persistence
191 | {
192 | public static class AddPersistenceExtension
193 | {
194 | public static IServiceCollection AddPersistence(this IServiceCollection services)
195 | {
196 | services.AddDbContext(options => options.UseSqlServer("Name=SqlServerDB"));
197 | services.AddScoped();
198 | return services;
199 | }
200 | }
201 | }
202 | <# CreateFile("Persistence", "","AddPersistenceExtension.cs"); #>
203 | <#
204 | //////////////////////////////
205 | // Generate Startup project //
206 | //////////////////////////////
207 | CreateConsoleAppProject("Startup");
208 | #>
209 |
210 |
211 |
212 | Exe
213 | net6.0
214 | enable
215 | enable
216 |
217 |
218 |
219 |
220 | all
221 | runtime; build; native; contentfiles; analyzers; buildtransitive
222 |
223 |
224 |
225 |
226 |
227 | PreserveNewest
228 |
229 |
230 |
231 |
232 |
233 |
234 |
235 |
236 | <# OverwriteFile("Startup", "","Startup.csproj"); #>
237 | <#
238 | ////////////////////////////////
239 | // generated appsettings.json //
240 | ////////////////////////////////
241 | #>
242 | {
243 | //define connection here
244 | "ConnectionStrings": {
245 | "SqlServerDB": "Server=DESKTOP-GBANT4V; Database=BookStoresDB; Trusted_Connection=True;"
246 | }
247 | }
248 | <# OverwriteFile("Startup", "","appsettings.json"); #>
249 | //////////////////////////
250 | // generated Program.cs //
251 | /////////////////////////
252 | using Microsoft.EntityFrameworkCore;
253 | using Microsoft.Extensions.DependencyInjection;
254 | using Microsoft.Extensions.Hosting;
255 | using Persistence.Context;
256 | using System.Reflection;
257 |
258 | namespace startup
259 | {
260 | public class Program
261 | {
262 | public static async Task Main(string[] args)
263 | {
264 | await Host.CreateDefaultBuilder(args)
265 | .UseContentRoot(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location))
266 | .ConfigureServices((hostContext, services) =>
267 | {
268 | //required if you are using entity framework
269 | services.AddDbContext(options => options.UseSqlServer("Name=SqlServerDB"));
270 | })
271 | .RunConsoleAsync();
272 | }
273 | }
274 | }
275 | <# OverwriteFile("Startup", "","Program.cs"); #>
276 | <#
277 | ////////////////////////////////////////////////////////
278 | // Get defined Entities details and DbContext details //
279 | ////////////////////////////////////////////////////////
280 | string EntityNamespace = "Domain.Entities";
281 | string DbContextNamespace = "Persistence.Context";
282 | string efContext = "ApplicationDbContext";
283 |
284 | IServiceProvider serviceProvider = (IServiceProvider)this.Host;
285 | DTE dte = serviceProvider.GetService(typeof(DTE)) as DTE;
286 | var project1 = FindProjectByName(dte, "Domain");
287 | var classes = FindClasses(project1, EntityNamespace, "");
288 | var classNames = classes
289 | .Select(currentClass => currentClass.Name)
290 | .ToList();
291 | var project2 = FindProjectByName(dte, "Persistence");
292 | var dbcclass = FindClasses(project2, DbContextNamespace, "");
293 | var classWithDbContext = dbcclass.FirstOrDefault(currentClass => currentClass.Bases.OfType().Any(currentBase => currentBase.Name == "DbContext"));
294 | if(classWithDbContext != null)
295 | {
296 | efContext = classWithDbContext.Name;
297 | }
298 | #>
299 | /////////////////////////////////////
300 | // generated IGenericRepository.cs //
301 | /////////////////////////////////////
302 | using System.Linq.Expressions;
303 |
304 | namespace Application.RepositoryInterfaces
305 | {
306 | public interface IGenericRepository where T : class
307 | {
308 | Task> GetAll();
309 | Task> Find(Expression> expression);
310 | Task Add(T entity);
311 | void Remove(T entitiy);
312 | void Update(T entitiy);
313 | }
314 | }
315 | <# CreateFile("Application", "RepositoryInterfaces","IGenericRepository.cs"); #>
316 | ////////////////////////////////////
317 | // generated GenericRepository.cs //
318 | ////////////////////////////////////
319 | using Application.RepositoryInterfaces;
320 | using Persistence.Context;
321 | using Microsoft.EntityFrameworkCore;
322 | using System.Linq.Expressions;
323 |
324 | namespace Persistence.Repositories
325 | {
326 | public class GenericRepository : IGenericRepository where T : class
327 | {
328 | //replace DbContex with ApplicationDbContext
329 | public ApplicationDbContext _context;
330 |
331 | public GenericRepository(ApplicationDbContext context)
332 | {
333 | _context = context;
334 | }
335 |
336 | public virtual async Task> GetAll()
337 | {
338 | return await _context.Set().ToListAsync();
339 | }
340 | public virtual async Task> Find(Expression> expression)
341 | {
342 | return await _context.Set().Where(expression).ToListAsync();
343 | }
344 | public virtual async Task Add(T entity)
345 | {
346 | await _context.Set().AddAsync(entity);
347 | }
348 | public virtual void Remove(T entity)
349 | {
350 | _context.Set().Remove(entity);
351 |
352 | }
353 | public virtual void Update(T entity)
354 | {
355 | _context.Set().Update(entity);
356 | }
357 | }
358 | }
359 | <# CreateFile("Persistence", "Repositories","GenericRepository.cs"); #>
360 | //////////////////////////////
361 | // generated IUnitOfWork.cs //
362 | //////////////////////////////
363 | using Microsoft.EntityFrameworkCore;
364 | namespace Application.RepositoryInterfaces
365 | {
366 | public interface IUnitOfWork
367 | {
368 | DbContext GetContext();
369 | Task BeginTransactionAsync(CancellationToken cancellationToken = default);
370 | Task CommitTransactionAsync(CancellationToken cancellationToken = default);
371 | Task CompleteAsync(CancellationToken cancellationToken = default);
372 | }
373 | }
374 | <# CreateFile("Application", "RepositoryInterfaces","IUnitOfWork.cs"); #>
375 | /////////////////////////////
376 | // generated UnitOfWork.cs //
377 | /////////////////////////////
378 | using Application.RepositoryInterfaces;
379 | using Persistence.Context;
380 | using Microsoft.EntityFrameworkCore;
381 | using Microsoft.EntityFrameworkCore.Storage;
382 |
383 |
384 | namespace Persistence.Repositories
385 | {
386 | public class UnitOfWork : IUnitOfWork
387 | {
388 | public readonly ApplicationDbContext _context;
389 |
390 | private IDbContextTransaction _transaction;
391 |
392 | public UnitOfWork(ApplicationDbContext context)
393 | {
394 | _context = context;
395 | }
396 |
397 | public DbContext GetContext()
398 | {
399 | return _context;
400 | }
401 |
402 | public async Task BeginTransactionAsync(CancellationToken cancellationToken = default)
403 | {
404 | _transaction = await _context.Database.BeginTransactionAsync(cancellationToken);
405 | }
406 |
407 | public async Task CommitTransactionAsync(CancellationToken cancellationToken = default)
408 | {
409 | await _transaction.CommitAsync(cancellationToken);
410 | }
411 |
412 | public async Task CompleteAsync(CancellationToken cancellationToken = default)
413 | {
414 | return await _context.SaveChangesAsync(cancellationToken);
415 | }
416 |
417 | public void Dispose()
418 | {
419 | _transaction?.Dispose();
420 |
421 | _context.Dispose();
422 | }
423 | }
424 | }
425 | <# CreateFile("Persistence", "Repositories","UnitOfWork.cs"); #>
426 | //////////////////////////////////////
427 | // generated BadRequestException.cs //
428 | //////////////////////////////////////
429 | namespace Domain.Exceptions
430 | {
431 | public abstract class BadRequestException : Exception
432 | {
433 | protected BadRequestException(string message) : base(message)
434 | {
435 | }
436 | }
437 | }
438 | <# CreateFile("Domain", "Exceptions","BadRequestException.cs"); #>
439 | //////////////////////////////////
440 | // generated BadKeyException.cs //
441 | //////////////////////////////////
442 | namespace Domain.Exceptions
443 | {
444 | public sealed class BadKeyException : BadRequestException
445 | {
446 | public BadKeyException(string entity, string? entityId, string? passedId)
447 | : base(String.Format("Passed key {2} not matching Entity {0} key {1}.", entity, entityId, passedId))
448 | {
449 | }
450 | }
451 | }
452 | <# CreateFile("Domain", "Exceptions","BadKeyException.cs"); #>
453 | ////////////////////////////////////
454 | // generated NotFoundException.cs //
455 | ////////////////////////////////////
456 | namespace Domain.Exceptions
457 | {
458 | public abstract class NotFoundException : Exception
459 | {
460 | protected NotFoundException(string message) : base(message)
461 | {
462 | }
463 | }
464 | }
465 | <# CreateFile("Domain", "Exceptions","NotFoundException.cs"); #>
466 | //////////////////////////////////////////
467 | // generated EntityKeyFoundException.cs //
468 | //////////////////////////////////////////
469 | namespace Domain.Exceptions
470 | {
471 | public sealed class EntityKeyFoundException : BadRequestException
472 | {
473 | public EntityKeyFoundException(string entity, string? id)
474 | : base(String.Format("Entity {0} with the identifier {1} exist.", entity, id))
475 | {
476 | }
477 | }
478 | }
479 | <# CreateFile("Domain", "Exceptions","EntityKeyFoundException.cs"); #>
480 | /////////////////////////////////////////////
481 | // generated EntityKeyNotFoundException.cs //
482 | /////////////////////////////////////////////
483 | namespace Domain.Exceptions
484 | {
485 | public sealed class EntityKeyNotFoundException : NotFoundException
486 | {
487 | public EntityKeyNotFoundException(string entity, string? id)
488 | : base(String.Format("Entity {0} with the identifier {1} was not found.", entity, id))
489 | {
490 | }
491 | }
492 | }
493 | <# CreateFile("Domain", "Exceptions","EntityKeyNotFoundException.cs"); #>
494 | <#foreach(string className in classNames)
495 | {
496 | var properClassName = GetProperClassName(className);
497 | var repositoryName = properClassName + "Repository";
498 | (var keyType, var key, var keyValue, var checkKeyEqual, var entityKeys, var entityValue, var httpKeys, var asprouteItem, var asprouteModel, var aspfors)
499 | = GetKeyAndTypeStrings(dte,project1,EntityNamespace+"."+properClassName);
500 | #>
501 | /////////////////////////////////////////
502 | // generated I<#= repositoryName #>.cs //
503 | /////////////////////////////////////////
504 | using Domain.Entities;
505 |
506 | namespace Application.RepositoryInterfaces
507 | {
508 | public interface I<#= repositoryName #> : IGenericRepository<<#= properClassName #>>
509 | {
510 | ValueTask<<#= properClassName #>?> GetById(<#= keyType #>);
511 | }
512 | }
513 | <# CreateFile("Application", "RepositoryInterfaces", "I"+repositoryName+".cs"); #>
514 | <#
515 | }
516 | #>
517 | <#foreach(string className in classNames)
518 | {
519 | var properClassName = GetProperClassName(className);
520 | var repositoryName = properClassName + "Repository";
521 | (var keyType, var key, var keyValue, var checkKeyEqual, var entityKeys, var entityValue, var httpKeys, var asprouteItem, var asprouteModel, var aspfors)
522 | = GetKeyAndTypeStrings(dte,project1,EntityNamespace+"."+properClassName);
523 | #>
524 | ////////////////////////////////////////
525 | // generated <#= repositoryName #>.cs //
526 | ////////////////////////////////////////
527 | using Application.RepositoryInterfaces;
528 | using Domain.Entities;
529 | using Persistence.Context;
530 |
531 | namespace Persistence.Repositories
532 | {
533 | public class <#= properClassName #>Repository : GenericRepository<<#= properClassName #>>, I<#= properClassName #>Repository
534 | {
535 | public <#= properClassName #>Repository(ApplicationDbContext context) : base(context)
536 | {
537 | _context = context;
538 | }
539 | public async ValueTask<<#= properClassName #>?> GetById(<#= keyType #>)
540 | {
541 | return await _context.Set<<#= properClassName #>>().FindAsync(<#= key #>);
542 | }
543 | }
544 | }
545 | <# CreateFile("Persistence", "Repositories", repositoryName+".cs"); #>
546 | <#
547 | }
548 | #>
549 | <#foreach(string className in classNames)
550 | {
551 | var properClassName = GetProperClassName(className);
552 | var serviceName = properClassName + "Service";
553 | (var keyType, var key, var keyValue, var checkKeyEqual, var entityKeys, var entityValue, var httpKeys, var asprouteItem, var asprouteModel, var aspfors)
554 | = GetKeyAndTypeStrings(dte,project1,EntityNamespace+"."+properClassName);
555 | #>
556 | //////////////////////////////////////
557 | // generated I<#= serviceName #>.cs //
558 | //////////////////////////////////////
559 | using Domain.Entities;
560 | using System.Linq.Expressions;
561 |
562 | namespace Application.ServiceInterfaces
563 | {
564 | public interface I<#= properClassName #>Service
565 | {
566 | Task Add<#= properClassName #>(<#= properClassName #> entity, CancellationToken cancellationToken = default);
567 | Task<(IEnumerable<<#= properClassName #>> entities, string Message)> Find<#= properClassName #>(Expression, bool>> expression, CancellationToken cancellationToken = default);
568 | Task<(IEnumerable<<#= properClassName #>> entities, string Message)> GetAll<#= properClassName #>(CancellationToken cancellationToken = default);
569 | Task<(<#= properClassName #>? entity, string Message)> Get<#= properClassName #>ById(<#= keyType #>, CancellationToken cancellationToken = default);
570 | Task Remove<#= properClassName #>(<#= keyType #>, CancellationToken cancellationToken = default);
571 | Task Update<#= properClassName #>(<#= keyType #>, <#= properClassName #> entity, CancellationToken cancellationToken = default);
572 | }
573 | }
574 | <# CreateFile("Application", "ServiceInterfaces", "I"+serviceName+".cs"); #>
575 | <#
576 | }
577 | #>
578 | <#foreach(string className in classNames)
579 | {
580 | var properClassName = GetProperClassName(className);
581 | var serviceName = properClassName + "Service";
582 | var repositoryName = properClassName + "Repository";
583 | (var keyType, var key, var keyValue, var checkKeyEqual, var entityKeys, var entityValue, var httpKeys, var asprouteItem, var asprouteModel, var aspfors)
584 | = GetKeyAndTypeStrings(dte,project1,EntityNamespace+"."+properClassName);
585 | #>
586 | /////////////////////////////////////
587 | // generated <#= serviceName #>.cs //
588 | /////////////////////////////////////
589 | using Domain.Entities;
590 | using Domain.Exceptions;
591 | using Application.ServiceInterfaces;
592 | using Application.RepositoryInterfaces;
593 | using System.Linq.Expressions;
594 | using Microsoft.EntityFrameworkCore;
595 |
596 | namespace Application.Services
597 | {
598 | public class <#= properClassName #>Service : I<#= properClassName #>Service
599 | {
600 | private readonly IRepositoryManager _repositoryManager;
601 |
602 | public <#= properClassName #>Service(IRepositoryManager repositoryManager)
603 | {
604 | _repositoryManager = repositoryManager;
605 | }
606 | public async Task<(IEnumerable<<#= properClassName #>> entities, string Message)> Find<#= properClassName #>(Expression, bool>> expression, CancellationToken cancellationToken = default)
607 | {
608 | var items = await _repositoryManager.<#= properClassName #>Repository.Find(expression);
609 | return (items, "<#= properClassName #> records retrieved");
610 | }
611 |
612 | public async Task<(IEnumerable<<#= properClassName #>> entities, string Message)> GetAll<#= properClassName #>(CancellationToken cancellationToken = default)
613 | {
614 | var items = await _repositoryManager.<#= properClassName #>Repository.GetAll();
615 | return (items, "<#= properClassName #> records retrieved");
616 | }
617 |
618 | public async Task<(<#= properClassName #>? entity, string Message)> Get<#= properClassName #>ById(<#= keyType #>, CancellationToken cancellationToken = default)
619 | {
620 | var item = await _repositoryManager.<#= properClassName #>Repository.GetById(<#= key #>);
621 | if (item == null)
622 | {
623 | throw new EntityKeyNotFoundException("<#= properClassName #>", <#= keyValue #>);
624 | }
625 | return (item, "<#= properClassName #> record retrieved");
626 | }
627 |
628 | public async Task Add<#= properClassName #>(<#= properClassName #> entity, CancellationToken cancellationToken = default)
629 | {
630 | if (entity != null)
631 | {
632 | var item = await _repositoryManager.<#= properClassName #>Repository.GetById(<#= entityKeys #>);
633 | if (item != null)
634 | {
635 | throw new EntityKeyFoundException("<#= properClassName #>", <#= entityValue #>);
636 | }
637 | else
638 | {
639 | await _repositoryManager.<#= properClassName #>Repository.Add(entity);
640 | await _repositoryManager.UnitOfWork.CompleteAsync(cancellationToken);
641 | return "<#= properClassName #> record added";
642 | }
643 | }
644 | throw new Exception("Add Error");
645 | }
646 |
647 | public async Task Remove<#= properClassName #>(<#= keyType #>, CancellationToken cancellationToken = default)
648 | {
649 | var item = await _repositoryManager.<#= properClassName #>Repository.GetById(<#= key #>);
650 | if (item == null)
651 | {
652 | throw new EntityKeyNotFoundException("<#= properClassName #>", <#= keyValue #>);
653 | }
654 | else
655 | {
656 | _repositoryManager.<#= properClassName #>Repository.Remove(item);
657 | await _repositoryManager.UnitOfWork.CompleteAsync(cancellationToken);
658 | return "<#= properClassName #> record deleted";
659 | }
660 | }
661 |
662 | public async Task Update<#= properClassName #>(<#= keyType #>, <#= properClassName #> entity, CancellationToken cancellationToken = default)
663 | {
664 | if(!(<#= checkKeyEqual #>))
665 | {
666 | throw new BadKeyException("<#= properClassName #>", <#= entityValue #>, <#= keyValue #>);
667 | }
668 |
669 | _repositoryManager.UnitOfWork.GetContext().Entry(entity).State = EntityState.Modified;
670 |
671 | try
672 | {
673 | _repositoryManager.UnitOfWork.CompleteAsync(cancellationToken);
674 | }
675 | catch (DbUpdateConcurrencyException)
676 | {
677 | var item = await _repositoryManager.<#= properClassName #>Repository.GetById(<#= key #>);
678 | if (item == null)
679 | {
680 | throw new EntityKeyNotFoundException("<#= properClassName #>", <#= keyValue #>);
681 | }
682 | else
683 | {
684 | throw new Exception("Update Error");
685 | }
686 | }
687 | return "<#= properClassName #> record updated";
688 | }
689 | }
690 | }
691 | <# CreateFile("Application", "Services", serviceName+".cs"); #>
692 | <#
693 | }
694 | #>
695 | //////////////////////////////////
696 | // generated IServiceManager.cs //
697 | //////////////////////////////////
698 | namespace Application.ServiceInterfaces
699 | {
700 | public interface IServiceManager
701 | {
702 | <#foreach(string className in classNames)
703 | {
704 | var properClassName = GetProperClassName(className);
705 | #>
706 | I<#= properClassName #>Service <#= properClassName #>Service { get; }
707 | <#
708 | }
709 | #>
710 | }
711 | }
712 | <# OverwriteFile("Application", "ServiceInterfaces","IServiceManager.cs"); #>
713 | /////////////////////////////////
714 | // generated ServiceManager.cs //
715 | /////////////////////////////////
716 | using Application.RepositoryInterfaces;
717 | using Application.ServiceInterfaces;
718 |
719 | namespace Application.Services
720 | {
721 | public class ServiceManager : IServiceManager
722 | {
723 | <#foreach(string className in classNames)
724 | {
725 | var properClassName = GetProperClassName(className);
726 | #>
727 | private readonly LazyService> _lazy<#= properClassName #>Service;
728 | <#
729 | }
730 | #>
731 | public ServiceManager(IRepositoryManager repositoryManager)
732 | {
733 | <#foreach(string className in classNames)
734 | {
735 | var properClassName = GetProperClassName(className);
736 | #>
737 | _lazy<#= properClassName #>Service = new LazyService>(() => new <#= properClassName #>Service(repositoryManager));
738 | <#
739 | }
740 | #>
741 | }
742 | <#foreach(string className in classNames)
743 | {
744 | var properClassName = GetProperClassName(className);
745 | #>
746 | public I<#= properClassName #>Service <#= properClassName #>Service => _lazy<#= properClassName #>Service.Value;
747 | <#
748 | }
749 | #>
750 | }
751 | }
752 | <# OverwriteFile("Application", "Services","ServiceManager.cs"); #>
753 | /////////////////////////////////////
754 | // generated IRepositoryManager.cs //
755 | /////////////////////////////////////
756 | namespace Application.RepositoryInterfaces
757 | {
758 | public interface IRepositoryManager
759 | {
760 | <#foreach(string className in classNames)
761 | {
762 | var properClassName = GetProperClassName(className);
763 | #>
764 | I<#= properClassName #>Repository <#= properClassName #>Repository { get; }
765 | <#
766 | }
767 | #>
768 | IUnitOfWork UnitOfWork { get; }
769 | }
770 | }
771 | <# OverwriteFile("Application", "RepositoryInterfaces","IRepositoryManager.cs"); #>
772 | ////////////////////////////////////
773 | // generated RepositoryManager.cs //
774 | ////////////////////////////////////
775 | using Application.RepositoryInterfaces;
776 | using Persistence.Context;
777 |
778 | namespace Persistence.Repositories
779 | {
780 | public class RepositoryManager : IRepositoryManager
781 | {
782 | <#foreach(string className in classNames)
783 | {
784 | var properClassName = GetProperClassName(className);
785 | #>
786 | private readonly LazyRepository> _lazy<#= properClassName #>Repository;
787 | <#
788 | }
789 | #>
790 | private readonly Lazy _lazyUnitOfWork;
791 |
792 | public RepositoryManager(ApplicationDbContext context)
793 | {
794 | <#foreach(string className in classNames)
795 | {
796 | var properClassName = GetProperClassName(className);
797 | #>
798 | _lazy<#= properClassName #>Repository = new LazyRepository>(() => new <#= properClassName #>Repository(context));
799 | <#
800 | }
801 | #>
802 | _lazyUnitOfWork = new Lazy(() => new UnitOfWork(context));
803 | }
804 | <#foreach(string className in classNames)
805 | {
806 | var properClassName = GetProperClassName(className);
807 | #>
808 | public I<#= properClassName #>Repository <#= properClassName #>Repository => _lazy<#= properClassName #>Repository.Value;
809 | <#
810 | }
811 | #>
812 | public IUnitOfWork UnitOfWork => _lazyUnitOfWork.Value;
813 | }
814 | }
815 | <# OverwriteFile("Persistence", "Repositories","RepositoryManager.cs"); #>
--------------------------------------------------------------------------------