I love ChatGPT and previously posted an example of some ChatGPT generated code to create a controller, with DTO and which used AutoMapper to map from DTO to model …
This time instead of using AutoMapper to convert DTOs into models and vice versa I asked it to use the explicit operator/keyword. I also asked it to use the repository pattern.
Specifically my prompt to ChatGPT was…
Generate a .NET API ProductController which has hardcoded Products (in a repository which is injected into the ProductController constructor) and full crud actions using strongly typed DTOs which map to domain models using the explicit operator in C#.
Here’s the code it gave me…
ChatGPT nailed it. I didn’t explicitly ask it to inject an interface as opposed to a concrete instance of a repository but this is the way it’s done in pretty much all code bases I see.
DTO, Model and Mappings
Perfect again, not sure I would write anything better.
The code it did generate looks solid.
It did seem to get cut off at the end which happens very frequently on ChatGPT I found. However subsequent to me taking the screenshots I learnt that when this happens you can just prompt it to continue by typing in ‘continue’ and it will resume from where it was.
What do you think? Pretty cool right?
This is so powerful, I’m kind of blown away by it. ChatGPT created a controller, repository (partial pending continue prompt), classes and mappings in seconds.
Once developers learn how to ask for things they want in the right way I think this tool has the potential to save a lot of time.
Going forward I expect ChatGPT will allow us to spend more time working on ‘higher value add’ stuff like talking to clients to understand what they really want, complex domain logic, the UX or maybe improving the performance of an app. I’ve literally zero interest in typing boilerplate code out if ChatGPT can do it for me in seconds.
I am curious though, how does it compare to GitHub CoPilot? I’ve not used CP much so can’t compare them myself. Has anyone tried both?
Full code on GitHub Gist
ChatGPT controller with injected repository example. (github.com)