简体   繁体   English

是否有本机方式或通过 VS Extension for VS 2017/2019 从 C# POCO 自动生成 typescript .d 文件

[英]Is there a native way or via VS Extension for VS 2017/2019 to auto generate typescript .d files from a C# POCO

In my Razor .cshtml file I need to pass a JSON-serialized data from some complex C# POCO model objects that can be used in the Javascript space.在我的 Razor .cshtml 文件中,我需要从一些可以在 Javascript 空间中使用的复杂 C# POCO 模型对象传递一个 JSON 序列化数据。 But I want to use type-safe Typescript instead, which VS 2017/2019 now has great support (by auto-transpile during the build).但我想改用类型安全的 Typescript,VS 2017/2019 现在有很大的支持(通过在构建过程中自动转换)。 That way I can also use ES6 features.这样我也可以使用 ES6 特性。

I am looking for tips on generating typescript .d files so the typescript scripts I use has the benefit of type checking when operating on the JSON data derived from the C# POCO model.我正在寻找有关生成打字稿 .d 文件的提示,因此我使用的打字稿脚本在对源自 C# POCO 模型的 JSON 数据进行操作时具有类型检查的好处。

There is an SO answer recommending Web Essentials but it has not been updated for VS 2017/2019, and I suspect now that VS 2017/2019 has Roslyn built-in and support Typescript a lot more than VS 2015, I'm hoping it has a way to do this without installing any extensions or requiring any nuget libraries.有一个推荐Web EssentialsSO 答案,但它尚未针对 VS 2017/2019 进行更新,我现在怀疑 VS 2017/2019 内置了 Roslyn 并且比 VS 2015 支持 Typescript 多得多,我希望它有一种无需安装任何扩展或不需要任何 nuget 库即可执行此操作的方法。

I can settle for manually generating it (maybe by right-clicking on the POCO root class from the Solution Explorer tab inside the VS 2017/2019 IDE).我可以解决手动生成它(也许通过在 VS 2017/2019 IDE 内的解决方案资源管理器选项卡中右键单击 POCO 根类)。 But what is even better if the generation of the .d files are automatic so that whenever the C# POCO class is updated, the syntax helper in the .ts editing window is updated automatically.但是,如果 .d 文件的生成是自动的,那么无论何时更新 C# POCO 类,.ts 编辑窗口中的语法帮助程序都会自动更新,这会更好。 I'm hoping that the integrated Rosyln feature somehow makes it work like magic :-).我希望集成的 Rosyln 功能以某种方式使它像魔术一样工作:-)。

Since POCO objects often represent tables, I have my solution to create classes based on SQL table in MS SQL Management Studio.由于 POCO 对象通常表示表,因此我有我的解决方案,可以在 MS SQL Management Studio 中基于 SQL 表创建类。

First create this stored proc:首先创建这个存储过程:

CREATE PROC [dbo].[CreateClass](
    @Schema VARCHAR(100),
    @TableName VARCHAR(100),
    @Type VARCHAR(2) = NULL

)
AS
IF @Type IS NULL
    SET @Type = 'T'

SET NOCOUNT ON
DECLARE @TableInfo TABLE (
    ColumnName VARCHAR(100),
    ColumnPosition INT,
    ColumnTypeCS VARCHAR(100),
    ColumnTypeTS VARCHAR(100),
    NullableSign CHAR(1)
)

INSERT INTO @TableInfo
    SELECT 
        replace(COLUMN_NAME, ' ', '_') ,
        ORDINAL_POSITION AS ColumnPosition,
        CASE DATA_TYPE
            WHEN 'bigint' THEN 'long'
            WHEN 'binary' THEN 'byte[]'
            WHEN 'bit' THEN 'bool'
            WHEN 'char' THEN 'string'
            WHEN 'date' THEN 'DateTime'
            WHEN 'datetime' THEN 'DateTime'
            WHEN 'datetime2' THEN 'DateTime'
            WHEN 'datetimeoffset' THEN 'DateTimeOffset'
            WHEN 'decimal' THEN 'decimal'
            WHEN 'float' THEN 'double'
            WHEN 'image' THEN 'byte[]'
            WHEN 'int' THEN 'int'
            WHEN 'money' THEN 'decimal'
            WHEN 'nchar' THEN 'string'
            WHEN 'ntext' THEN 'string'
            WHEN 'numeric' THEN 'decimal'
            WHEN 'nvarchar' THEN 'string'
            WHEN 'real' THEN 'double'
            WHEN 'smalldatetime' THEN 'DateTime'
            WHEN 'smallint' THEN 'short'
            WHEN 'smallmoney' THEN 'decimal'
            WHEN 'text' THEN 'string'
            WHEN 'time' THEN 'TimeSpan'
            WHEN 'timestamp' THEN 'DateTime'
            WHEN 'tinyint' THEN 'byte'
            WHEN 'uniqueidentifier' THEN 'Guid'
            WHEN 'varbinary' THEN 'byte[]'
            WHEN 'varchar' THEN 'string'
            ELSE 'UNKNOWN_' + DATA_TYPE
        END AS ColumnTypeCS,
        CASE DATA_TYPE
            WHEN 'bigint' THEN 'number'
            WHEN 'binary' THEN 'any'
            WHEN 'bit' THEN 'boolean'
            WHEN 'char' THEN 'string'
            WHEN 'date' THEN 'Date'
            WHEN 'datetime' THEN 'Date'
            WHEN 'datetime2' THEN 'Date'
            WHEN 'datetimeoffset' THEN 'Date'
            WHEN 'decimal' THEN 'number'
            WHEN 'float' THEN 'number'
            WHEN 'image' THEN 'any'
            WHEN 'int' THEN 'number'
            WHEN 'money' THEN 'number'
            WHEN 'nchar' THEN 'string'
            WHEN 'ntext' THEN 'string'
            WHEN 'numeric' THEN 'number'
            WHEN 'nvarchar' THEN 'string'
            WHEN 'real' THEN 'number'
            WHEN 'smalldatetime' THEN 'Date'
            WHEN 'smallint' THEN 'number'
            WHEN 'smallmoney' THEN 'number'
            WHEN 'text' THEN 'string'
            WHEN 'time' THEN 'number'
            WHEN 'timestamp' THEN 'number'
            WHEN 'tinyint' THEN 'number'
            WHEN 'uniqueidentifier' THEN 'string'
            WHEN 'varbinary' THEN 'any'
            WHEN 'varchar' THEN 'string'
            ELSE 'UNKNOWN_' + DATA_TYPE
        END ColumnTypeTS,
        CASE 
            WHEN IS_NULLABLE = 'YES' and DATA_TYPE in ('bigint', 'bit', 'date', 'datetime', 'datetime2', 'datetimeoffset', 'decimal', 'float', 'int', 'money', 'numeric', 'real', 'smalldatetime', 'smallint', 'smallmoney', 'time', 'tinyint', 'uniqueidentifier') 
            THEN '?' 
            ELSE '' 
        END NullableSign
    FROM INFORMATION_SCHEMA.COLUMNS 
    WHERE TABLE_NAME = @TableName AND TABLE_SCHEMA = @Schema 


DECLARE @Result VARCHAR(MAX) = 'public class ' + @TableName + '
{'

SELECT @Result = @Result + '
    public ' + ColumnTypeCS + NullableSign + ' ' + ColumnName + ' { get; set; }
'
FROM @TableInfo
ORDER BY ColumnPosition

SET @Result = @Result  + '
}'

IF CHARINDEX('C', @Type) > 0
    PRINT @Result


UPDATE @TableInfo
    SET ColumnName = LOWER(SUBSTRING(ColumnName, 1, 1)) + SUBSTRING(ColumnName, 2, LEN(ColumnName))

SET @Result = '
export interface I' + @TableName + '
{'

SELECT @Result = @Result + '
    '+ColumnName + '?: ' + ColumnTypeTS + ';'
FROM @TableInfo
ORDER BY ColumnPosition

SET @Result = @Result  + '
}
'

IF CHARINDEX('T', @Type) > 0
    PRINT @Result


SET @Result = '
export class ' + @TableName + ' implements I'+@TableName+'
{'

SELECT @Result = @Result + '
    '+ColumnName + '?: ' + ColumnTypeTS + ';'
FROM @TableInfo
ORDER BY ColumnPosition

SET @Result = @Result  + '

    constructor(recoverFrom: '+@TableName+' | I'+@TableName+') {
        super(recoverFrom);
    }
}
'
PRINT @Result
GO

to use, simply execute this要使用,只需执行此

EXEC dbo.CreateClass @Schema = 'dbo', @TableName = 'MyTable', @Type = 'T'
or
EXEC dbo.CreateClass @Schema = 'dbo', @TableName = 'MyTable', @Type = 'C'
or
EXEC dbo.CreateClass @Schema = 'dbo', @TableName = 'MyTable'

and see the result in C#, Typescript or both并在 C#、Typescript 或两者中查看结果

TypeScript Definition Generator , a Visual Studio Extension, has been working very well for the past few months. TypeScript Definition Generator是一个 Visual Studio 扩展,在过去几个月中一直运行良好。 It works for the latest Visual Studio 2019 update as well (as of version 16.4.6).它也适用于最新的 Visual Studio 2019 更新(从 16.4.6 版开始)。

Once installed you can designate the POCO .cs file that you want a .ts.d file auto generated for you simply by right clicking on it in Solution Explorer.安装后,您可以指定要自动生成 .ts.d 文件的 POCO .cs 文件,只需在解决方案资源管理器中右键单击它即可。 From then on, everytime you update that POCO .cs file, the associated .ts.d file is automatically updated.从那时起,每次更新 POCO .cs 文件时,关联的 .ts.d 文件都会自动更新。

In my usage I put several POCO into one .cs file so I only have to right click on a few files for the dozens of POCO that I need.在我的使用中,我将几个 POCO 放入一个 .cs 文件中,因此我只需右键单击几个文件即可获得我需要的数十个 POCO。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM