Open Closed

Entity generation boilerplate #2986


User avatar
0
jpietkiewicz_ created

Hello Support!

Is there code generation tools for entities? It seems like there is alot of boilerplate when it comes to adding entities.

Our domain has many entities, so we would like to know if there are tools in the abp cli to save us time by generating code.

If there is not, I am thinking of using jinja as I have been successful in the past generating boilerplate with such a tool.

Do you have suggestions or plans on adding more code generation? Is there a tool like jinja but for dotnet which I could explore (and possibly submit to the abp framework)?

Thanks!


11 Answer(s)
  • User Avatar
    0
    gterdem created
    Support Team Senior .NET Developer

    Do you mean something like this?

  • User Avatar
    0
    alper created
    Support Team Director

    watch https://www.youtube.com/watch?v=RFArBh60RSA

  • User Avatar
    0
    Radoslav created

    I have started to build a abp code generation tool around telsoys cli and velocity engine. We can collaborate on it. I have one template/page ready: Domain entity. It is based on DSL language that telosys offers. You can use vs code telosys extension to write your entities or Eclipse plugin. It allows both generation from a model class or from DB table. You can see videos: https://www.youtube.com/channel/UCX5-ypQygEHMCGXVTTbhfNQ I basically build a tooling to enable telosys tool to be abp aware. My generation allows code re-generation, so you can preserve your custom code. Let me know if you are interested.

    Rad

  • User Avatar
    0
    jpietkiewicz_ created

    I have started to build a abp code generation tool around telsoys cli and velocity engine. We can collaborate on it. I have one template/page ready: Domain entity. It is based on DSL language that telosys offers. You can use vs code telosys extension to write your entities or Eclipse plugin. It allows both generation from a model class or from DB table. You can see videos:
    https://www.youtube.com/channel/UCX5-ypQygEHMCGXVTTbhfNQ I basically build a tooling to enable telosys tool to be abp aware. My generation allows code re-generation, so you can preserve your custom code. Let me know if you are interested.

    Rad

    This sounds amazing. I will look into it more and get back to you.

  • User Avatar
    0
    alper created
    Support Team Director

    All these generators have 1 big disadvantage; inject code into an existing classes' existing method. so there are DbSets, permissions, and menuProviders in an ABP project which need to be parsed carefully and inject into the new code. this sometimes needs to be parsed the current code (like parent item name) to be able to add the new child. do these general generators work in these cases?

  • User Avatar
    0
    Radoslav created

    @albert. It is easier than you think. I am using this strategy https://efg.loresoft.com/en/latest/regeneration/ I manage to extend telosys velocity code and connect my custom C# method using Java to .Net bridge and my c# code uses native c # classes to parse c# regions and simply only affect #region …. #end region area into which I only generate things from the model taking any new changes and rendering them in those regions. All other code is under your control to customize and it won’t be touched by this method

  • User Avatar
    0
    alper created
    Support Team Director

    but this is only for EF Core, there are other resources like HTML, CSS, JavaScript, DTO mappings, permissions, menuProviders, and you need to make it idempotent.

  • User Avatar
    0
    Radoslav created

    You can do the same techniquest for any c# code that can have #region .... #end region segments. If you are sure that whatever goes into these "code generation" regions depends on your DSL model fields you can do it for c# only code.

  • User Avatar
    0
    alper created
    Support Team Director

    what about Angular and Razor pages?

  • User Avatar
    0
    Radoslav created

    @albert You can do generation in any type of files. Only in c# classes you can have region merge (comming from the model) into your customized code. I use some of these files: https://github.com/loresoft/EntityFrameworkCore.Generator/tree/master/src/EntityFrameworkCore.Generator.Core/Parsing

    If you see the project references I am using: <PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.1.0" /> <PackageReference Include="Microsoft.CodeAnalysis.CSharp.Scripting" Version="4.1.0" /> for merge strategy. You can see how do my entities look like (with REG_REGION prefixed c# regions - making them ready for code re-generation) You can see 3 entities from the domain project being generated and re-generated (when there is a change to model) https://github.com/radrad/Acme.HelpDesk/blob/master/Acme.Bookstore.Domain.zip Your custom code can go anywhere outside REG_REGIONs and it will be preserved

    I chose to have the merge strategy within once file instead of having a partial class and making custom changes in another related partial class becuase I wanted to see the whole file and properties.

    These are model definiton in Telosys DSL language: `Deal.entity: Deal { Id : string { @Id #ignore }; DealName : string { @NotNull @NotBlank @SizeMin(20) @SizeMax(30) }; Zip : string { @Pattern("\d{5}") }; GameId : string { #optional @FK(FK_GAME, Game.Id) }; Game : Game; Players : Player[]; Author : Author { @NotNull }; }

    Game.entity: Game { Id : string { @Id #ignore }; GameName : string { @NotNull @NotBlank @SizeMin(5) @SizeMax(20) }; Rating : int { @Min(1) @Max(5) }; StartDate : date { @Optional }; Email : string { @NotNull @NotBlank #ValidationEmail }; }

    Player.entity: Player { Id : string { @Id #ignore }; FirstName : string { @SizeMax(20) }; LastName : string { @SizeMax(20) }; BirthDate : date { @Past } ; Certified : boolean ; }`

    There could be some custom "preserve" region for other kind of file types where you can use smart commenting to discover/register re-generatable regions. I did't work on it, but you can always use some smart c# parsing (maybe regex: https://csharp.net-tutorials.com/it/419/regular-expressions-regex/search-replace-with-the-regex-class/) to replace these regions with new incomming regions. (C# class is given pre-generation output that you can manipulate in any ways possible). There could be some libraries that can do the job for you.

    Server side Comments:

    Razor Pages .cshtml @* Gen Start (Don't change, it will be re-generated) @ ... will be re-generated <input name="firstName">... loop over all properties in your dsl model and output <input name="City">... loop over all properties in your dsl model and output @ Gen End *@

    Web form .aspx <%-- Gen Start (Don't change, it will be re-generated) --%> ... will be re-generated <%-- Gen End --%>

    Client Side Comments

    HTML Comments <!-- Gen Start (Don't change, it will be re-generated) --> ... will be re-generated <!-- Gen End -->

    Javascript Comment //Gen Start (Don't change, it will be re-generated) ... will be re-generated //Gen End

    /* Gen Start (Don't change, it will be re-generated) / ... will be re-generated / Gen End */

    CSS comments: /* This is a single-line comment */ But I would not envision any CSS styling based on the model

  • User Avatar
    0
    alper created
    Support Team Director

    thanks for the info. I'll check

Made with ❤️ on ABP v8.2.0-preview Updated on March 25, 2024, 15:11