【问题标题】:T4 -> C# class to Knockout View Model in Typescript [closed]T4-> C# 类到 Typescript 中的 Knockout View Model [关闭]
【发布时间】:2013-10-26 14:07:23
【问题描述】:

我正在寻找某种 T4 或类似方式来获取 C# 类,并为将通过线路的数据和以强类型方式的视图模型生成接口。

我发现 typelite 对接口很有效,但我正在寻找可以处理视图模型的东西(或除此之外)

我发现这个项目看起来很棒:Code Project View Models to JS

但它当然会生成 javascript,并且在键入时看起来并不简单,包括枚举和继承等内容,并使其与 typescript 一起使用

有谁知道这样做的项目吗?

如果没有,关于如何构建它的任何提示?我不是 T4 专家,即使使用工具使 Visual Studio 中的 T4 编辑变得不那么糟糕,它看起来也相当强大。

谢谢!

【问题讨论】:

  • T4 真的很简单。我建议你使用有形 T4 编辑器。他们有一个免费版,它突出了与“生成代码”不同的“模板代码”。这真的很有用。我还推荐 T4 ToolBox。我已经编写了几十个 T4 模板,但没有一个与您要查找的内容相关。
  • 您是否考虑过使用像 Breeze.js 这样的客户端数据库,这样您就不必在客户端和服务器上创建模型?
  • breeze 的问题是它仍然不是强类型的。整个意图是让所有东西都被强类型化,因此任何地方的任何更改都会引发编译时错误,而不是随机出现的东西。通过使用 t4 视图模型生成器,即使您使用的是 Breeze,您也可以强转换您的对象,以便在 Typescript 中始终强类型化所有内容。
  • 至于有形,是的,这就是我正在考虑做的事情。只是一直在搞乱它,它只是一个非常丑陋的文档,关于做一些简单的事情,比如遍历类库中的所有内容并寻找一个属性,然后我需要做一些事情,比如创建枚举,记住我已经创建了它们,处理继承等。尽量偷懒,不要重新发明轮子。
  • “尽量偷懒,不要重新发明轮子。” - 有时您必须发明一个不同的轮子以使其适合您的业务需求(您已经注意到 Breeze.js 轮子并不真正适合)。

标签: c# knockout.js typescript t4 typelite


【解决方案1】:

我做这个 T4 是为了让我的 DTO 合同可以从 javascript 获得,也许它可以帮助你

<#@ template debug="true" hostSpecific="true" #>
<#@ output extension=".js" #>
<#@ Assembly Name="System.Core" #>
<#@ Assembly Name="System.Windows.Forms" #>
<#@ import namespace="System" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Diagnostics" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Collections" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="EnvDTE" #>
<#@ include file="..\T4\Automation.ttinclude"#><#
var project = VisualStudioHelper.GetProject("MyApp.Core.Contracts");      
var contracts = GetSubClasses("MyApp.Core.Contracts.Commands.Command", project)
    .Concat(GetSubClasses("MyApp.Core.Contracts.Queries.Query", project));

#>(function(MyApp) {
    function buildContract(contract) {
        return { type: contract.constructor.type, data: ko.toJSON(contract) };
    }
    var url = "api/commandQuery";
    MyApp.cqrs = {
        sendQuery: function(query, callback) {
            $.getJSON(url, buildContract(query), callback);
        },
        sendCommand: function(command) {
            MyApp.utils.post(url, buildContract(command));
        }
    };
<#


foreach(var contract in contracts) {
        #>  
    <#
    foreach(var part in BuildNameSpace(contract)) {
        #><#= part #>
    <#
    }

    var properties = GetProperties(contract).Select(p => CamelCased(p.Name)).ToList();
    var args = string.Join(", ", properties);

    #>

    window.<#= contract.FullName #> = function(<#= args #>) {<#
    foreach(var property in properties) {#>

        this.<#= property #> = <#= property #>;<#
    }
    #>

    };
    window.<#= contract.FullName #>.type = "<#= contract.FullName #>";
<#
}
#>
})(window.MyApp = window.MyApp || {});
<#+

private static IEnumerable<string> BuildNameSpace(CodeClass @class)
{
    return BuildNameSpace(@class.Namespace.Name.Split('.'), "window", new List<string>());
}            

private static IEnumerable<string> BuildNameSpace(IEnumerable<string> @namespace, string parent, List<string> parts)
{
    var part = @namespace.FirstOrDefault();
    if (part == null) return parts;

    var current = string.Format("{0}.{1}", parent, part);
    parts.Add(string.Format("{0} = ({0} || {{}});", current));
    return BuildNameSpace(@namespace.Skip(1), current, parts);
}

public IEnumerable<CodeClass> GetSubClasses(string baseClass, Project project)
{
    return VisualStudioHelper       
        .CodeModel
        .GetAllCodeElementsOfType(project.CodeModel.CodeElements, EnvDTE.vsCMElement.vsCMElementClass, false)
        .Cast<CodeClass>()
        .Where(c => GetInheritance(c).Any(b => b.FullName == baseClass) && !c.IsAbstract)
        .ToList(); 
}


public IEnumerable<CodeClass> GetInheritance(CodeClass @class) 
{
    return GetInheritance(@class, new List<CodeClass>());
}

public IEnumerable<CodeClass> GetInheritance(CodeClass @class, List<CodeClass> collection) 
{
    foreach(CodeClass @base in @class.Bases) 
    {
        collection.Add(@base);
        GetInheritance(@base, collection);
    }

    return collection;
}

public string CamelCased(string pascalCased) {
    return pascalCased.Substring(0, 1).ToLower() + pascalCased.Substring(1);
}

public IEnumerable<CodeProperty> GetProperties(CodeClass @class)
{
    if (@class == null) 
        return new List<CodeProperty>();

    var baseProperties = GetProperties(@class.Bases.Cast<CodeClass>().FirstOrDefault());

    return baseProperties.Concat(@class
        .Members
        .Cast<CodeElement>()
        .Where(ce => ce.Kind == vsCMElement.vsCMElementProperty)
        .Cast<CodeProperty>()
        .Where(p => p.Access == vsCMAccess.vsCMAccessPublic));
    }
 #>

它输出一个看起来像这样的js

(function(MyApp) {
    function buildContract(contract) {
        return { type: contract.constructor.type, data: ko.toJSON(contract) };
    }
    var url = "api/commandQuery";
    MyApp.cqrs = {
        sendQuery: function(query, callback) {
            $.getJSON(url, buildContract(query), callback);
        },
        sendCommand: function(command) {
            MyApp.utils.post(url, buildContract(command));
        }
    };

    window.MyApp = (window.MyApp || {});
    window.MyApp.Core = (window.MyApp.Core || {});
    window.MyApp.Core.Contracts = (window.MyApp.Core.Contracts || {});
    window.MyApp.Core.Contracts.Commands = (window.MyApp.Core.Contracts.Commands || {});

    window.MyApp.Core.Contracts.Commands.FooCommand = function(bar) {
        this.bar = bar;
    };
    window.MyApp.Core.Contracts.Commands.FooCommand.type = "MyApp.Core.Contracts.Commands.FooCommand";
})(window.MyApp = window.MyApp || {});

请注意,它依赖于有形 T4 编辑器模板

<#@ include file="..\T4\Automation.ttinclude"#>

【讨论】:

  • 我找不到“T4\Automation.ttinclude”文件。你能给我一个提示吗?
猜你喜欢
  • 2013-10-26
  • 2014-04-26
  • 2012-11-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-09
  • 1970-01-01
  • 2015-05-14
  • 2014-12-17
相关资源
最近更新 更多