Published: Sunday 18 December 2022
ChatGPT is a new free artificial intelligence model from OpenAI.
It is trained so it can interact in a conversational way.
Using the Playground, we asked it to build an ecommerce application using ASP.NET Core Web API. The answers it gave included a high-level overview of how to build it, and getting it to write code for the classes, controllers and unit tests.
Could this model be a threat to the job as a .NET developer?
The first thing we asked ChatGPT to do is to build an ecommerce system with orders, customers and products in ASP.NET Core Web API.
It gave us a high-level overview of how to build it, which included:
A good start! But what about writing code?
Next, we asked it to write the code for the classes of the model. This included a class for
It did well in writing the syntax as well as coming up with some common properties for the entities. But, there were some questions around how it stored details about the items ordered.
Rather than creating a separate entity for order items, it tried to store it all in the
Let's see if we can separate the order items into a separate class.
The results were a lot better when we asked it to separate the order items out to it's own class.
It created a new
OrderItem class, and added the
Price into that class.
Order class then had a list of order items so it was able to have that relationship between the two.
We noticed it created the order items as a
List. We asked it why it didn't use an
It gave us a very detailed explanation on the benefits of using
This included giving a concrete implementation of all methods and properties in the
IList interface, and storing the elements internally. This means that element access is faster and memory overhead is lower compared to other implementations.
However, it did give some reasons for using
IList, such as using it for a mock implementaiton, or passing a list to a method that expects an
With the entities created, the controllers was the next thing we got it to code.
Order entity, it wrote a controller with common CRUD methods needed, such as reading all orders, or a single order, as well as creating, updating and deleting an order.
To reference the services that populate the data for this controller, it created a unspecified interface. The
IUnitOfWork seemed to have methods that supplies the input and output of the order data.
However, there was no sign of how this interface was constructed in the final result.
Another thing was when it started to write the controller for the
Customer, it suddenly stopped half way through.
It might be because of the load of the server, or maybe it was unable to complete the task. Maybe we were asking it for too much?
We did ask it to try it again, but it stopped at the same place. So there are definitely some limitations there.
We asked it to write unit tests using XUnit.
It had some good use of mocking by creating a mock object of our
IUnitOfWork interface. Afterwards, it went through and wrote some good unit tests for our controllers. It called the endpoints in the controllers and set an assert as to what type should be returned.
Once again though, it got stuck half way through writing the tests in a similar fashion to last time. It looked like we used ChatGPT at the wrong time of day.
At this point, we were getting a number of errors when asking it questions. Errors ranged from the server experience an error while processing the request, to us sending it too many requests.
At this point, we decided to conclude our experiment.
Watch our video where you can see us using ChatGPT to build an ecommerce application with ASP.NET Core Web API.
In-addition, we created an ASP.NET Core Web API in Visual Studio and see if the code that ChatGPT generates actually compiles.
At the moment, ChatGPT can only really be used as a guide. It takes a .NET developer to question the code quality that it writes.
In-addition, there are a number of occassions were it was unable to complete the answer to a question that was asked. This was particular with lengthy pieces of code.
In our example, it was felt that it was unable to actually run the code, particularly as it created an unknown
IUnitOfWork interface which would never compile.
So for now in our opinion, a .NET developer's job is safe.
But this is a huge step towards writing code using artificial intellegence. Where will this technology be in 5-10 years time?