Using Claude AI during .NET development

Published on

We’re in the midst of summer, and vacation season is in full swing. It’s the perfect time to dive into some AI experimentation at work and share the journey with everyone including you, whether you’re at the (home)office or lounging on a beach chair.

What or who is Claude?

Meet Claude, as envisioned by Midjourney.

Claude is an AI service similar to the more well-known ChatGPT. It functions as an assistant through text input, helping to brainstorm ideas, generate text and code, understand subjects, and more. Currently in Open Beta, Claude offers both free and paid plans. For this introduction I opted for the pro plan to gain (almost unlimited) access to the Claude 3.5 model, which is touted as the most intelligent version available (though this will undoubtedly change with future updates). Claude allows you to create chat threads to continue conversations based on context, or you can create projects where you can upload documents and code for contextual assistance. This is useful for including only relevant, non-personal documents (so you know what items Claude has access to). You can integrate Claude into development environments like Visual Studio Code using plugins (API available), but for now, I’ll be experimenting with the web-based version. Check out: https://claude.ai/ for more info.

Claude Welcome screen

Scenario

I was fortunate enough to be given the opportunity to start a fresh green-field .NET project (a rare occurrence!). The project involves importing data, adding user authorization/authentication, and creating a dashboard for data exposure and search functionality. For now, I’ll focus mainly on the backend: creating classes (objects), setting up roles, populating the database, and exposing data through APIs. How can Claude assist me in this process?

Usage

I’m not inclined to generate a complete codebase from scratch. At this point, I want to maintain control and use the AI as an assistant. Let’s say we need to build a bit of mutual trust first. Therefore, we decided on the architecture ourselves, choosing Core Identity and Entity Framework. Core Identity provides an ‘out of the box’ framework for logging-in and Entity Framework is used to communicate with the new database from code. I created a default .NET 8 project with identity support in Visual Studio and then turned to Claude for assistance with the implementation. How did it go? Let’s find out!

Findings So Far

  • Getting Started with Entity Framework: I asked Claude for guidance on how to get started with Entity Framework on .NET Core. It provided a clean, step-by-step guide, including the necessary packages, DbContext class, and migration setup. Could I have used a search engine or done this by memory? Absolutely! But Claude made it fast and easy, helping me remember all the steps.

All the steps needed to setup the database and related code

  • Database Classes and config from file: I challenged Claude with a modified source file containing (anonimized) data and asked it to create database classes and configuration for Entity Framework based on it. I gave directions, such as placing the ‘location’ object in a separate table and ensuring that a ‘training’ object can have multiple locations. Claude responded promptly with the .NET classes, including the relation setup (including DbContext configuration) and necessary attributes to define for example the Primary Key (unique ID). This was a helpful starting point since I don’t do this daily. It was able to handle updates to the code and configuration as well.
  • Kickstarting the Project: To verify my project structure assumptions, I asked Claude to generate all the code for an application that imports data from a file and stores it in SQL Server using Entity Framework Core, applying best practices (SOLID). Claude provided a clean project structure with an async service and a re-usable database repository class supporting dependency injection. Keeping the context, I then asked it to generate unit tests based on the delivered code. Claude provided relevant test examples, ready to run, and noted the use of an in-memory database for Entity Framework to avoid polluting the actual database. Excellent!
  • Setting Up Roles: For the authentication/authorization part, I asked Claude to add two roles to the application. It provided the necessary configuration and a class to initially ‘seed’ roles and users in the database. Sweet!
  • API Definitions: Based on the earlier code context, Claude generated API definitions and actual implementation to expose and search the newly created database, along with some example commands to call the API (documentation).

‘Certainly!’ is what i love to hear. Luckily Claude kept saying it.

Conclusion

Claude 3.5 has proven to be a highly useful assistant during coding. None of the code snippets for this experiment are used in its entirety: I cherry-picked the best practices from Claude and filled in the gaps of my own implementation. The AI service required less tweaking and delivered better results compared to ChatGPT (though I haven’t conducted a direct comparison here). In summary:

  • Save Time on Project Foundations, Spend More on Features: With Claude, you can quickly set up your project’s foundation, giving you more time to focus on adding those extra features that make your project stand out.
  • Best Practices Built Right In: The code generated (depending on the prompt) comes with best practices embedded and explained, ensuring your project follows developer standards. Please review all output carefully though.
  • Boost Productivity with Auto-Generated Documentation and Unit Tests: Generating documentation and unit tests is a breeze, reducing the workload on developers. This automation frees up your energy for other critical parts of the project.
  • Your Always-Available Code Buddy: Claude is like having a coding buddy who’s always ready to brainstorm and verify ideas. It’s a valid and efficient alternative to asking a colleague or going on a search-engine spree.

Integrating Claude into my coding environment, using it for debugging, and exploring its potential in frontend development are next on my to-do list. I’m sure there are also still many things Claude can’t do, so stay tuned for more updates on my journey!

Have you tried using Claude? Or are you using another AI service? Let me know your experiences!

Thank you for being a part of the community! Before you go: