Objective
With the introduction of Di in ASP., and by the constructor injection of parameters, the controller will be a large number of Di injection of various configuration parameters, if the configuration injection parameters are more, and each controller needs the configuration parameters are basically the same, then the repeated copy of the paste code to provide the corresponding constructor , inefficient also, so use the T4 template to generate the controller's constructor, which also benefits from C # support for partial classes (partial).
T4 Template Generation Controller constructors
The diagram ctrltemplate.tt as a template file, CtrlNames.txt for the controller name file that needs to generate code with T4, and CtrlTemplate.cs the file generated for the T4 template.
CtrlNames.txt File Contents:
Valuesaccount
CTRLTEMPLATE.TT Code:
1<#@ Template language="C #"debug="false"Hostspecific="true"#>2<#@ Assembly Name="System.core"#>3<#@ Importnamespace="System.Linq"#>4<#@ Importnamespace="System.Text"#>5<#@ Importnamespace="System.Collections.Generic"#>6<#@ Output extension=". CS"#>7 usingapicoretest;8 usingEfdbcontext;9 usingMICROSOFT.ASPNETCORE.MVC;Ten usingMicrosoft.Extensions.Logging; One usingMicrosoft.Extensions.Options; A - namespaceapicoretest.controllers - { the<# - //get the root directory of the project - varSolutionspath = Host.resolveassemblyreference ("$ (SolutionDir)"); - //Get configuration file + varLines = System.IO.File.ReadAllLines (Solutionspath +@"/src/apicoretest/controllers/template/ctrlnames.txt"); - foreach(varNameinchlines) +{#> A Public Partial class<#= name#>Controller:controller at { -Ioptions<configmodel>_config; -ilogger<<#= name#>controller>_logger; - Applicationdbcontext _db; - Public<#= Name#>controller (ioptions<configmodel> config, ilogger<<#= name#>controller>logger, Applicationdbcontext db) - { in_config =config; -_logger =logger; to_db =db; + } - } the<#}#> *}
Template-Generated file CtrlTemplate.cs content:
1 usingapicoretest;2 usingEfdbcontext;3 usingMICROSOFT.ASPNETCORE.MVC;4 usingMicrosoft.Extensions.Logging;5 usingMicrosoft.Extensions.Options;6 7 namespaceapicoretest.controllers8 {9 Public Partial classValuescontroller:controllerTen { OneIoptions<configmodel>_config; AIlogger<valuescontroller>_logger; - Applicationdbcontext _db; - PublicValuescontroller (ioptions<configmodel> config, ilogger<valuescontroller>logger, Applicationdbcontext db) the { -_config =config; -_logger =logger; -_db =db; + } - } + Public Partial classAccountcontroller:controller A { atIoptions<configmodel>_config; -Ilogger<accountcontroller>_logger; - Applicationdbcontext _db; - PublicAccountController (ioptions<configmodel> config, ilogger<accountcontroller>logger, Applicationdbcontext db) - { -_config =config; in_logger =logger; -_db =db; to } + } -}
A controller using a template:
1 namespaceapicoretest.controllers2 {3[Route ("Api/[controller]")]4 Public Partial classValuescontroller:controller5 {6 [HttpGet]7 Public stringGets ()8 {9 varval =Jsonconvert.serializeobject (_config. Value);Ten _logger. Logdebug (val); One returnVal; A } - } -}
1 namespaceapicoretest.controllers2 {3 Public Partial classAccountcontroller:controller4 {5 Publiciactionresult Test ()6 {7 varval =Jsonconvert.serializeobject (_config. Value);8 _logger. Logdebug (val);9 returnContent (val);Ten } One } A}
Attention
controllers that use the T4 template must be set to partial classes, and the namespace is consistent.
Generating constructors and parameters for an ASP. NET core controller using the T4 template