❔ Source generator for creating REST Api
I want to create a REST endpoint for my entity classes.
Structure:
Backend.Api (contains asp net core logic, endpoints etc.)
Backend.Database (DB-Layer using EF Core, Entity classes)
Now I want to annotate my entity classes with a specific annotation (lets say
[HasEndpoint]
) and create the specific endpoint in the Backend.Api
project. But the source generator will create the endpoints in the Backend.Database
project.. How would it be possible to create the endpoints in Backend.Api
using the entities in Backend.Database
?5 Replies
If the backend api project references the backend database project, then the generator can look through the symbols from the referenced assembly, and generate whatever you like:
It's probably not the best fit for a source generator though. It would be easier to write a standalone tool that used reflection to generate the same thing, by looking at the same symbols from the database dll
If you think about it, your generator is going to run whenever the api project is compiled, but the api project cannot, by definition, affect the outcome of the generated code. So it's just unnecessary work
look through the symbols from the referenced assemblythat does sound to efficient, right?
The compiler has to load them anyway, so it's not inefficient per se, it's just work that doesn't need to happen as often as source generators run, and it's also work that doesn't really benefit from being in a source generator
The unique value prop, in my opinion, of source generators is that they can get at the compilers object model for source code. If you're not using that, then an external tool that you run in your build pipeline, is probably just as good
In a past life I wrote a rest API that was generated from database models, and it worked really well. I think it's a great idea. We just ran the generator manually when we changed the schema, because it happened pretty rarely.
Thank you very much @davidwengier ! You are probably right that a source generator would not be a good fit, the api has to be re-compiled "non-stop" and the underlying database would only change pretty infrequently at some point. I'll probably go with a standalone tool which may be part of a CI pipeline
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.