Use the T4 template to generate the constructors and parameters of the ASP. NET Core controller, t4core
Preface
In ASP.. NET Core introduces DI, and through the constructor injection parameters, the Controller will inject a large number of configuration parameters using DI. If there are many configuration injection parameters, in addition, if the configuration parameters required by each controller are basically the same, the repeated copying and pasting Code will provide the corresponding constructor, Which is inefficient. Therefore, the T4 template is used to generate the constructor of the controller, this also benefits from C #'s support for the sub-class (partial.
T4 template Generation Controller Constructor
In the figure, ctrltemplate.ttis the template file. ctrlnames.txt is the Controller name file that needs to generate code using T4, and CtrlTemplate. cs is the file generated by the T4 template.
CtrlNames.txt file content:
ValuesAccount
CtrlTemplate. tt code:
1 <# @ template language = "C #" debug = "false" hostspecific = "true" #> 2 <# @ assembly name = "System. core "#> 3 <# @ import namespace =" System. linq "#> 4 <# @ import namespace =" System. text "#> 5 <# @ import namespace =" System. collections. generic "#> 6 <# @ output extension = ". cs "#> 7 using ApiCoreTest; 8 using EFDbContext; 9 using Microsoft. aspNetCore. mvc; 10 using Microsoft. extensions. logging; 11 using Microsoft. extensions. options; 12 13 namespace ApiCoreTest. controllers14 {15 <#16 // obtain the project's root directory 17 var solutionsPath = Host. resolveAssemblyReference ("$ (SolutionDir)"); 18 // get the configuration file 19 var lines = System. IO. file. readAllLines (solutionsPath + @ "/src/ApiCoreTest/Controllers/Template/CtrlNames.txt"); 20 foreach (var name in lines) 21 {#> 22 public partial class <# = name #> Controller: Controller23 {24 IOptions <ConfigModel> _ config; 25 ILogger <# = name #> Controller> _ logger; 26 ApplicationDbContext _ db; 27 public <# = name #> Controller (IOptions <ConfigModel> config, ILogger <# = name #> Controller> logger, ApplicationDbContext db) 28 {29 _ config = config; 30 _ logger = logger; 31 _ db = db; 32} 33} 34 <#}#> 35}
Template generated file CtrlTemplate. cs content:
1 using ApiCoreTest; 2 using EFDbContext; 3 using Microsoft.AspNetCore.Mvc; 4 using Microsoft.Extensions.Logging; 5 using Microsoft.Extensions.Options; 6 7 namespace ApiCoreTest.Controllers 8 { 9 public partial class ValuesController : Controller10 { 11 IOptions<ConfigModel> _config;12 ILogger<ValuesController> _logger;13 ApplicationDbContext _db;14 public ValuesController(IOptions<ConfigModel> config, ILogger<ValuesController> logger, ApplicationDbContext db)15 {16 _config = config;17 _logger = logger;18 _db = db;19 }20 }21 public partial class AccountController : Controller22 { 23 IOptions<ConfigModel> _config;24 ILogger<AccountController> _logger;25 ApplicationDbContext _db;26 public AccountController(IOptions<ConfigModel> config, ILogger<AccountController> logger, ApplicationDbContext db)27 {28 _config = config;29 _logger = logger;30 _db = db;31 }32 }33 }
The template controller is used:
1 namespace ApiCoreTest.Controllers 2 { 3 [Route("api/[controller]")] 4 public partial class ValuesController : Controller 5 { 6 [HttpGet] 7 public string Gets() 8 { 9 var val = JsonConvert.SerializeObject(_config.Value);10 _logger.LogDebug(val);11 return val;12 }13 }14 }
1 namespace ApiCoreTest.Controllers 2 { 3 public partial class AccountController : Controller 4 { 5 public IActionResult Test() 6 { 7 var val = JsonConvert.SerializeObject(_config.Value); 8 _logger.LogDebug(val); 9 return Content(val);10 }11 }12 }
Note:
The controller that uses the T4 template must be set as a Division class and the namespace must be consistent.