【问题标题】:Generating Class in c# that inherits a generic class which uses type parameter using Telosys code generation tool在 C# 中生成类,该类继承使用 Telosys 代码生成工具使用类型参数的泛型类
【发布时间】:2022-01-02 14:57:29
【问题描述】:

我需要为我的 Country 实体生成 C# 代码,该代码将从提供强类型参数的基实体类继承,以表示我的 PK (@id) 属于 Guid 类型,即基类上的 Id 属性具有隐式类型 Guid。 所以我有两个问题:

  1. telosys 中没有 Guid 类型。

  2. 如何使用通用基类类型化参数定义 PK?

    public class Country : Entity<Guid>
    {

    }

    public abstract class Entity<TKey> : Entity, IEntity<TKey>
    {
        public virtual TKey Id { get; protected set; }
        protected Entity(TKey id)
        {
            Id = id;
        }
    }

https://www.telosys.org/dsl-syntax.html

  . binary
  . boolean
  . byte
  . date
  . decimal
  . double
  . float
  . int
  . long
  . short
  . string
  . time
  . timestamp

https://doc.telosys.org/dsl-model/tags

例如一个特殊的属性名称:metaproperty 我可以解析得到 $entity 继承类型的参数。我需要其他元数据。 实体类作为Id属性,可以是string、int、long等

User {
  metaproperty: string {#base       
  @Label("typed_param:Guid;name:Id;form_sections:Info section~1|Contact sec~2;display_layout:rows(n)_cols(12)")}
  FirstName : string {@Label("form_section:~1;display_layout:row(1)col(1)colspan(3)")};
  LastName: string {@Label("form_section:~1;display_layout:row(1)col(2)colspan(9)")};
  Phone: string {@Label("form_section:~2;display_layout:row(1)col(1)colspan(12)")};
}

I need some mechanizam to display the layout of fields in the form for each property I want in view/edit screens
I can certaily generate some .json structure and add metadata there as well. Even have a GUI with drag and drop feature to define rows, cols and row or col spans.

【问题讨论】:

  • 你不能制作自己的 Guid 类吗?
  • 什么是 telosys ?上课?
  • 如果我正确理解您的需求,Telosys 中的“类型”不是问题(类型用于属性),更多的是关于如何表达“国家”类必须扩展“实体" ?
  • 供参考:Telosys 是一个代码生成器 (telosys.org)
  • @Igu。你说的对。它是关于 telosys 可以提供的元数据。这是dsl模型的语法:telosys.org/dsl-syntax.html

标签: c# uuid dsl guid telosys


【解决方案1】:

关于类,目前在实体级别没有注释(或标签)(很快就会发生)。但是你可以使用一个文件来定义应该扩展另一个类的实体。

第 1 步 - 在您的模型文件夹中文件“variables.txt”

中定义实体列表

实体“Foo”、“Bar”和“Country”的示例

#set ( $guidEntities = ["Foo", "Bar", "Country" ] )

第 2 步 - 在您的模板中评估此文件的内容,以便为列表定义一个变量并使用它来检查当前实体是否必须扩展 Entity

例子:

## Load content from file "variables.txt" located in current model folder
#set( $file = $fn.fileFromModel("variables.txt") )
#set( $fileContent = $file.loadContent() ) 

## Use 'evaluate(statement)' to convert the file content in list variable
#evaluate( $fileContent )

## Now the list $guidEntities is defined and we can use it
## to check if it contains the current entity name
#if ( $guidEntities.contains($entity.name) ) 
public class $entity.name : Entity<Guid>  
#else 
public class $entity.name
#end 

注意:列表定义在一个单独的文件中,位于模型文件夹中,因为它可以被视为模型定义的一部分(并且可以在多个模板中使用)

【讨论】:

  • 谢谢 Laurent 教授 :) 我想同时使用那个 #Guid 标签,我可以将它附加到一个特别命名的属性: metaproperty : string 并且我计划在输出 $entity 继承时找到这个属性提供特定类型的参数。
  • 例如一个特殊的属性名称:metaproperty 我可以解析得到 $entity 继承类型的参数。我需要其他元数据。 Entity 类作为 Id 属性。它可以是 string、int、long 等 User { metaproperty: string {#base @Label("typed_pa​​ram:Guid;name:Id;form_sections:Info section~1|Contact sec~2; display_layout:rows(n)_cols(12)")} 名字:字符串 {@Label("form_section:~1;display_layout:row(1)col(1)colspan(3)")} 姓氏:字符串 {@Label( "form_section:~1;display_layout:row(1)col(2)colspan(9)")} 电话:字符串 {@Label("form_section:~2;display_layout:row(1)col(1)colspan(3) ")}
  • 洛朗。我们可以在 Linkedin 上继续对话。我需要支持。我正在构建一个高级 abp.io asp.net 核心生成器,并重新生成代码以保留开发人员所做的更改,并且我正在使用特定的 java 类来运行一些 abp.io cli 命令来创建解决方案和模块,我可以在其中生成我的代码。我想要 Azure Desired State Configuration (DSC) 之类的东西用于基础设施,以表达将命名的主要应用程序解决方案以及创建它的位置以及需要哪些额外模块来创建和添加到解决方案中。这是为了构建模块化单体应用
  • 我确实有一个使用 swagger 文档的原型,我可以在其中选择有效负载来创建/编辑表单,并且可以在表单上拖放字段,比如说 3 列和 n 行。我可以构建一些可用于生成代码的元数据 json,将字段放在适当的列中。
【解决方案2】:

关于属性类型,正如您在评论中提到的,最简单的方法是使用标签进行管理。

在模型中为具有 GUID 的属性设置一个“guid”标签:

MyEntity {
 myattribute : string { #Guid } ;
}

在模板中可以这样使用:

#if ( $attrib.hasTag("Guid ") )  
 do something
#end

【讨论】:

    猜你喜欢
    • 2014-11-13
    • 1970-01-01
    • 2022-10-15
    • 1970-01-01
    • 1970-01-01
    • 2021-11-02
    • 1970-01-01
    • 1970-01-01
    • 2011-03-16
    相关资源
    最近更新 更多