Search code examples
c#uuiddslguidtelosys

Generating Class in c# that inherits a generic class which uses type parameter using Telosys code generation tool


I have a need to generate C# code for my Country entity that will inherit from a base Entity class providing stong typed argument to denote the fact that my PK (@id) is of type Guid that is Id property on the base class having implictly type Guid. So I have 2 problems:

  1. There is no Guid type in telosys.

  2. How to define PK using Generic base class typed argument?

    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

For example a special property name: metaproperty I can parse to get $entity inheritance typed argument. I need other metadata. Entity class as Id property.It can be string, int, long etc

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.

Solution

  • Regarding the class, annotations and tags at the entity level are available since Telosys 4.0.

    Before Telosys 4.0 there are no annotations (or tags) at the entity level. But you can use a file to define the entities which should extend another class.

    Step 1 - in your model folder define a list of entities in a file "variables.txt"

    Example for entities "Foo", "Bar" and "Country"

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

    Step 2 - in your template evaluate the content of this file in order to define a variable for the list and use it to check if the current entity must extend Entity

    Example:

    ## 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 
    
    

    Note: The list is defined in a separate file, located in the model folder as it can be seen as part of the model definition (and it can be used in multiple templates)