Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Source Code Generator #431

Closed
iammukeshm opened this issue Feb 1, 2022 · 11 comments
Closed

Source Code Generator #431

iammukeshm opened this issue Feb 1, 2022 · 11 comments
Labels
enhancement New feature or request

Comments

@iammukeshm
Copy link
Member

iammukeshm commented Feb 1, 2022

It will be nice to have a feature to generate code and further reduce the time taken by a developer.

The scope -

DomainEntities and related configurations
Service Layers
Validators
Generic Specification
Mediatr handlers / Dtos
Controller

All these files should be generated on the basis of a new entity.

Should be under src/Utils/SourceGenerator
with assembly name as FSH.WebApi.SourceGenerator

@iammukeshm iammukeshm added the enhancement New feature or request label Feb 1, 2022
@iammukeshm iammukeshm modified the milestones: Release 1.0, Release 2.0 Feb 1, 2022
@frankyjquintero
Copy link
Contributor

Initially snippets could be created
https://code.visualstudio.com/docs/editor/userdefinedsnippets

https://docs.microsoft.com/en-us/visualstudio/ide/code-snippets?view=vs-2022

@CanMehmetK
Copy link

CanMehmetK commented Feb 2, 2022

@frankyjquintero snippets is nice for many requirement

roslyn-sdk this guy is reflecting whenever your model changed
to avoid boiling ..

as you can see in samples as easy as preparing snipped and in some senarios compile time is faster then written code
roslyn-sdk SourceGenerators samples

@12dollar
Copy link

I'm so looking forward to that one as I'm pissed writing repeating code over and over again :)

@PedroVentura235
Copy link
Contributor

Hi, as an temporary alternative we can use this : #598

@Stonne
Copy link

Stonne commented Apr 14, 2022

I Followed the suggestions of Mukesh

Should be under src/Utils/SourceGenerator
with assembly name as FSH.WebApi.SourceGenerator

The code is based on the product and brand entities
I used the same approach as I did some years ago so bear with me should the whole thing is well 'not that modern' .
But at the end of the 'run' all the files are created.
After a rerun fullstackhero sets the permissions (as it does now) .

with the new directories in place (and if not completely integrated in fullstackhero)
Add a reference to the Utils project in the host project

in program.cs
Add 'using FSH.WebApi.Utils;'
Add 'builder.Services.AddUtils(builder.Configuration);'
Add 'await app.Services.InitializeGenerateSources();' '

approach :
I transformed the classes to text files.
the entityrelated words are replaced with specific "terms"
With System.IO when creating the files the terms are replaced with the new entityrelated words.
(in i.e. productdto product is replaced with <&Entity&> : <& will not be accepted when creating a new entity )
All these files reside in a subfolder of Sourcegenerator => Basicsources
These text files are based on the brand and product ones (The image in product.cs is left out in the newly created class).
The different classes to generate the sourcefiles reside in SourceGenerator => SourceGeneratorClasses
The main program is GenerateSources
To give developers the opportunity to change foldernames etc There is a settings file called GenerateSourceSettings.cs.
First I had a json file but the sourcesettings.cs was needed anyway so I skipped json and added the settings values directly.

How it works.
After the creation of a new entity (see product.cs or brand.cs) Add a migration, run it and update database.

If you restart the program
If => await app.Services.InitializeGenerateSources(); is commented out in program.cs it does not run
A secondary check is in the generatesourcessettings.cs.

The code will get the entityTypes from EF-Core
and will foreach through the entitylist and check if it is one of the entities in created in FSH.WebApi.Domain.Catalog.
THose are the entities we are interested in.
Then the code will check if the directory for the entity exists in : FSH.WebApi.Application.Catalog
If not the sources are generated.
the permission file is adapted
the controllers are made.

for the validators a validation as in createproductsrequestvalidator is made but commented out so the appropriate fieldname(s) can be copied in and code eventually adapted.

If any alterations are made to an entity and a new migration is run, delete the appropriate entity folder in FSH.WebApi.Application.Catalog.
Restart the program and the altered sourcefiles are made.
the same goes if one of the class code is changed and accordingly a new txtfile is created : delete the contents of the entities folder in FSH.WebApi.Application.Catalog

<&Entity&> : the entity
<&EntityToLower&> : lowercaes entity (could be replaced with <&Entity&>.ToLower()
<&theusings&> : generated usings path
<&EventsPath&> : path to the Event (using <&EventsPath&>)
<&StringNameSpace&> : namespace
<&PropertyLines&> : the properties from the entity in & object
<&RelationalLines&> : the relation in the entity in object
<&DetailLines&> : the detail lines as used in the DTO
<&ReadRepository&> : the repository object
<&validator_name&> : validator name as in the validator code
<&validator_length&>: length as used in the validator code.
<&childEntity&> : name of the child (deletion check for parent)
<&childEntityPLural&>: plural
<&Parent&> : Parent entity
<&ParentToLower&> : Parent to lower

@Stonne
Copy link

Stonne commented Apr 15, 2022

Code is in my github Repository Name : FSH.WebApi

@Stonne
Copy link

Stonne commented Apr 15, 2022

Working on some bugs my repository generates some errors in the end result... Sorry

@PedroVentura235
Copy link
Contributor

It looks like a very good start, probbably create a PR, to get more opinions, but nice work

@Stonne
Copy link

Stonne commented Apr 19, 2022

Thx wIll do so asap. But first I'm still working on the validator stuff (getting close).

@Julianh9
Copy link

Julianh9 commented May 6, 2022

iammukeshm and @Stonne
This is amazing.
I have used Blazor Hero so far and I am really looking forward to using FSH.
Best regards to you both and thanks for your tireless work.

@iammukeshm iammukeshm removed this from the Release 2.0 milestone Apr 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

8 participants