ChatGPT generated .NET CRUD controller with DTO mapped to domain model using AutoMapper

It’s not perfect but I’m absolutely loving ChatGPT. ChatGPT-3 (current version as of writing) is trained on 175 billion parameters, while ChatGPT-4 is supposedly trained on 100 trillion parameters, so what’s coming next will be even better.

I played around with it and asked it a few .NET questions and also got it to generate different kinds of code. One thing I asked it to create was a full CRUD ProductController. Specifically I asked it to …

Generate a .NET API ProductController which has hardcoded Products (in the ProductController constructor) and full crud actions using strongly typed DTOs which map to domain models using AutoMapper.

This is what it gave me ….

Click on the image to open a larger view in a new window.

Product/ProductDTO classes not shown above to save space but were generated fine too.

Obviously it’s not ‘runnable’ and Visual Studio can scaffold basic controllers from models like this too, but this is seriously impressive. It even injected IMapper into the constructor 👏 …

You can of course ask it to amend something if there’s something not right or you want to change something.

The prompt I used above wasn’t the first prompt I put in, sometimes you have to refine what you ask it and be more explicit. In this case for example it initially put the hardcoded products in a separate class and so I asked it to specifically put them in the controller constructor and it did.

ChatGPT should reduce the need for us to write so much boilerplate code. I can see it being particularly useful in the early stages of projects. I can’t wait for it to be fully integrated into Visual Studio.

2 thoughts on “ChatGPT generated .NET CRUD controller with DTO mapped to domain model using AutoMapper”

  1. As a developer about 80% of my time is spent supporting existing applications, making changes, checking issues, etc. I spend only a small proportion of my time creating new applications from scratch, and even less time on boilerplate code like this. So if ChatGPT can do 5% of my job for me, that’s great, but not a significant time-saver

Leave a Reply

Your email address will not be published. Required fields are marked *