C
Join ServerC#
help
Creating and using an incremental source generator
EEro11/27/2022
I wanna create an incremental source generator for my web API project, but I cannot for the life of me understand how to create a source generator at all. I'm even having a hard time understanding the documentation that does exist. Is there anyone that's able to help?
I have a pretty clear idea of what I wanna do: I have a collection of HTTP endpoints in one class, which get turned into controllers, commands, and DTOs.
I have a pretty clear idea of what I wanna do: I have a collection of HTTP endpoints in one class, which get turned into controllers, commands, and DTOs.
namespace Endpoints;
[GenerateHttpEndpoints] // better name ideas?
public partial class User
{
[HttpPost("/api/users")]
public async Task<ActionResult<Entities.User>> Create(
string name,
string email,
string password)
{
// ...
return Ok(createdUser);
}
}
UserEndpoints.g.cs
file record CreateUserDto(
string Name,
string Email,
string Password);
// other things related to handling the command
Ppatrickk11/27/2022
what are you actually trying to achieve with
GenerateHttpEndpoints
EEro11/27/2022
generate controllers, commands, command handlers, dtos
Ppatrickk11/27/2022
From what
EEro11/27/2022
what do you mean from what? it's all laid out in the post
Ppatrickk11/27/2022
You already have a controller there
Ppatrickk11/27/2022
You can’t just generate something from nothing
Ppatrickk11/27/2022
How would it know what the DTO is supposed to even be?
Ppatrickk11/27/2022
Based on what?
EEro11/27/2022
from the method arguments...?
Ppatrickk11/27/2022
So who’s using the DTO then?
EEro11/27/2022
like


Ppatrickk11/27/2022
You’re going to need the DTO in the first place to post to an endpoint if you’re not getting these from query parameters
EEro11/27/2022
the
Endpoints.User
class is basically nothing. it's not used anywhere. it's not a controller. it only exists to generate the actual controllerPpatrickk11/27/2022
What you’ve posted looks like a controller
EEro11/27/2022
and i'm saying it isn't one
Ppatrickk11/27/2022
So you’re going to write a class that looks like a controller, is supposed to be a controller but isn’t because you want to… source generate it
EEro11/27/2022
it's supposed to look like one so people reading the source have an idea of what's going on
EEro11/27/2022
but i simply cannot be bothered writing full controllers and services and commands and command handlers and dtos for every single thing i implement
Ppatrickk11/27/2022
But what you have is 90% a controller….
Ppatrickk11/27/2022
I don’t get it
Ppatrickk11/27/2022
If you don’t want to write any code, write a swagger doc and generate all controllers and types
EEro11/27/2022
look if you don't wanna help, you don't have to
EEro11/27/2022
that's fine
Ppatrickk11/27/2022
i don't think you're understanding what you want to do is backward
Ppatrickk11/27/2022
this is 99% a controller which you say you don't want to write

EEro11/27/2022
it's not anything. you can't post to an endpoint like that, can you? it doesn't take a DTO after all
Ppatrickk11/27/2022
right... so just change it to take a DTO and then inherit
ControllerBase
and put [ApiController]
on the classPpatrickk11/27/2022
99%
EEro11/27/2022
and then i need a command. and a command handler. and maybe a service if i feel like it
EEro11/27/2022
like there's so much more that needs to be generated
EEro11/27/2022
i don't know what you're talking about
Ppatrickk11/27/2022
i dont know what a command or command handler is
Ppatrickk11/27/2022
i assume you mean mediatr
EEro11/27/2022
or something
Ppatrickk11/27/2022
that's a completely different topic and no you don't need that
EEro11/27/2022
i decided i want it
Ppatrickk11/27/2022
people building APIs only to have controllers => mediatr => handler => service end up building a lot of bloat
Ppatrickk11/27/2022
your desire to generate code comes from over engineering a solution
EEro11/27/2022
this is meant to be a completely feature rich, public api, everything open source, with potentially thousands of daily users
Ppatrickk11/27/2022
ok
Ppatrickk11/27/2022
where does mediatr come into play
EEro11/27/2022
you really make people wanna ask questions in this server you know
Ppatrickk11/27/2022
"potentially thousands of daily users"
Ppatrickk11/27/2022
you haven't got 1 yet
Ppatrickk11/27/2022
controller => service
Ppatrickk11/27/2022
what's wrong with that?