开发者

Having some situations with T4 Templates

开发者 https://www.devze.com 2023-04-06 06:21 出处:网络
I am trying to create a T4 Template for a custom controller (with services). However, I am having some issues setting things up. I currently use tangible free T4 tool editor and TextTemplatingFileGene

I am trying to create a T4 Template for a custom controller (with services). However, I am having some issues setting things up. I currently use tangible free T4 tool editor and TextTemplatingFileGenerator as a Custom Tool. I am running into a couple issues when experimenting:

1: I try using MvcTextTemplateHost mvcHost = (MvcTextTemplateHost)(Host); and it says "The type or namespace name MvcTextTemplateHost could not be found (are you missing a using directive or an assembly reference?). It says to clear the Custom tool, but I was wondering how I can view the generated T4 file if I do clear the tool?

2: Another approach was Steve Sanderson's CustomTemplate but I am having issues there with the DynamicTransform not being found and was wondering where I can get the file needed to use it. If i need to clear the tool for this, then how do I view a generated sample file? Thank you very much.

Max Gilman

Some sample code:

<#@ Template Language="C#" HostSpecific="True" Inherits="DynamicTransform" #>
<#@ Output Extension="cs" #>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;

namespace someNamespace
{ 

<#  
    var modelType = (CodeType)Model.ModelType; 
    var modelName = modelType.Name; 
    var modelNamePlural = Model.ModelTypePluralized; 
    var modelVariable = modelName.ToLower(); 
    var relatedEntities = ((IEnumerable)Model.RelatedEntities).OfType<RelatedEntityInfo>();
    var primaryKeyProperty = modelType.VisibleMem开发者_如何学运维bers().OfType<CodeProperty>().Single(x => x.Name == Model.PrimaryKey); 
    var routingName = Regex.Replace(Model.ControllerName, "Controller$", "", RegexOptions.IgnoreCase);
    var isObjectContext = ((CodeType)Model.DbContextType).IsAssignableTo<System.Data.Objects.ObjectContext>();
#>
    public class foo
    {
        //This is a basic comment.
    }
}


In both cases it looks like you are trying to use a T4 template in your Visual Studio project and use the TextTemplatingFileGenerator as the custom tool to generate the code from the template.

1: The MvcTextTemplatingHost is a custom T4 host that is only available when you use the ASP.NET MVC Add View or Add Controller dialogs. It will not be available nor properly initialized when you use the TextTemplatingFileGenerator custom tool.

2: Steve Sanderson's MvcScaffolding/T4Scaffolding uses its own custom T4 host so you have a similar problem here. If you use the NuGet package manager console with your custom T4 scaffolding template then it should work but it will not work with the TextTemplatingFileGenerator custom tool. The DynamicTransform class itself does not exist. If you take a look at the T4Scaffolding source code on CodePlex, in the InvokeScaffoldTemplateCmdlet class, you will see that the Inherits="DynamicTransform" directive is actually removed and replaced with a reference to a dynamic model object. When you scaffold a controller various properties (e.g. such as Model.ModelType) are setup on the custom host and the dynamic model object before it is passed to the T4 template for processing.

In both the above cases the use of the T4 templates with the TextTemplatingFileGenerator is not going to work since they use a custom templating host that needs to be initialized before it can be used by the T4 templates.

I think your options are:

  1. Create a custom ASP.NET MVC T4 template and use the ASP.NET tooling to generate your custom controller using the Add Controller dialog.
  2. Create a custom scaffolding T4 template and use the NuGet Package Manager console to generate your custom controller.
  3. Create your own custom tool that initializes a custom host that can be used from your T4 template.

Option 2) is probably your best bet since I believe MvcScaffolding can update existing controller code without removing the existing code.

0

精彩评论

暂无评论...
验证码 换一张
取 消