'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:
There is no Guid type in telosys.
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 1:[1]
Regarding the class, for the moment there are no annotations (or tags) at the entity level (it will happen soon). 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)
Solution 2:[2]
Regarding the attribute type, as you mentioned in your comment, the easiest way is to manage it with a tag.
In the model set a "guid" tag for the attribute having the GUID :
MyEntity {
myattribute : string { #Guid } ;
}
In the template you can use this way:
#if ( $attrib.hasTag("Guid ") )
do something
#end
Solution 3:[3]
Since Telosys v 4.0.0 you can use the annotation @Extends(SuperClass)
see https://doc.telosys.org/dsl-model/annotations#extends-string
You can also use a "tag" at entity level, eg : #EntityWithGuid or #Entity(Guid)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | lgu |
Solution 3 | lgu |