Skip to content

Feature Request: Use C# Source Generators to reduce boilerplate controllers #732

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

Closed
jeff-becker-projekt202 opened this issue May 4, 2020 · 7 comments · Fixed by #1117
Closed

Comments

@jeff-becker-projekt202
Copy link

Description

One of my current annoyances is )that I have a lot of boilerplate controllers. This could be addressed with a Source Generator which inspects a data context and emits controllers for the configured resources.

I'll implement & PR this, I'd just like feedback on what it needs to look like.
...

Environment

  • JsonApiDotNetCore Version: 4.x
  • Other Relevant Package Versions:
@bart-degreed
Copy link
Contributor

This sounds interesting. I'm wondering how you would know which controllers to generate, based on the syntax trees that represent the project source code. In the past, all resources had to be exposed as DbSet properties on DbContext, but we've recently switched to using EF Core's DbContext.Model instead, which is populated at application runtime.

@jeff-becker-projekt202
Copy link
Author

So my thinking was to determine the set of resources based on classes marked with the resource attribute. Then only emit controllers for those if there is not already a controller defined for that resource.

@bart-degreed
Copy link
Contributor

I think that could work. ResourceAttribute currently requires to specify a resource name. but we can make that optional.

@wayne-o
Copy link
Contributor

wayne-o commented May 19, 2020 via email

@bart-degreed
Copy link
Contributor

@KirovAir
Copy link

@jeff-becker-projekt202
Copy link
Author

@wayne-o @KirovAir just to be clear, this is proposing using the new Source Generators feature in the C# compiler. Source Generators are a hook into the compiler pipeline that allows you to generate the code during the compile without having to write it out to disk. They're a much more powerful form of code generation in that they have access to the compilation pipeline as well as all the traditional types of code generation sources. The thought here was that I could write a source generator that inspects the code-base for classes tagged with the ResourceAttribute and if no corresponding controller class is found, emit a boilerplate controller. This would prevent having to implement complicated generic / inheritance / DI based strategies which would make the controller interfaces more complicated than they already are.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging a pull request may close this issue.

5 participants