Skip to content
Back to overview

Refactoring from Python to .NET with AI

Joshua Heller · April 25, 2025 · 8 min.

Refactoring from Python to .NET with AI

Refactoring – the word alone already sounds like a mammoth task, especially when it comes to migrating between two completely different technology stacks. This is exactly the challenge I took on when I decided to port my Python project to .NET. Without any prior .NET knowledge, but with plenty of curiosity and the right AI tools, I wanted to test what was possible.

The idea came from a practical occasion: a project I had originally developed in Python was supposed to be implemented in a language that is better supported in our professional environment. The client relies heavily on .NET. Specifically, it was a console application that scraped content from websites and summarized it using the ChatGPT API. The application originally came about for our tool **imagetocaption.ai**.

Goal of the Experiment

My experiment had two central goals:

  1. Technical feasibility: Can I manage to develop a functioning .NET version of my project within just four hours – and that without any .NET experience?
  2. AI as a game changer: How much support can AI tools like ChatGPT, CursorAI, or GitHub Copilot provide to accelerate the process while delivering a solid result at the same time?

I wanted to find out not only whether it is technically possible, but also whether this approach is worthwhile for others. The question was: Can AI revolutionize the development process, especially for developers getting started with new technologies?

With these goals and a clear motivation, I set off into this experiment. And I was curious: Would I manage to deliver a clean solution – even if it isn’t 100% perfect? In the next chapters, I’ll tell you how it went.

The Project

Description of the Application

The project in question was, at first glance, simple, but it had a clear and useful function: it was a console application that scrapes websites, processes the collected content, and extracts relevant information using the ChatGPT API. The application primarily served as a prototype for a tool like imagetocaption.ai, which automatically summarizes or analyzes texts. Here is the original repository: https://github.com/joshuaheller/web-summarizer

There are a few parameters: the language of the summary, a focus on a specific topic (e.g. company, products, or technologies), and the length of the answer (bullet points, short, long…).

Although the Python version of the project worked, I saw potential for optimization: on the one hand regarding long-term maintainability, and on the other hand because the team prefers to work with .NET. The migration therefore promised not only technical advantages but also better integration into the existing development ecosystem.

Original Python Code

The original Python codebase was not overly complex. It consisted of a manageable number of modules and functions, all geared toward the main task – scraping and data processing.

In addition, though, I had to keep in mind that Python is dynamically typed, while .NET offers a strongly typed ecosystem. This meant that during the migration I had to not only translate the code, but also possibly design new types and class structures.

Objective for the .NET Version

The goal was clear: a functional .NET version that does the same thing as the Python original – just more structured and more maintainable in the long term. For this, I had set myself the following requirements:

  1. Feature parity: The application should, if possible, cover all features of the Python version.
  2. Clear structure: I wanted to use the opportunity to create a clean project structure with clear classes and methods from the very start.
  3. Use of AI tools: AI should support not only the refactoring, but also the architecture and implementation.

I was also aware that perfection was not the goal. This was about an experiment: Can I develop a usable solution in a short time with little experience and AI support?

In the next chapter, I’ll explain step by step how I proceeded and which tools and methods were used.

Approach to the Refactoring

Preparation and Planning with OpenAI

Before I even wrote a single line of .NET code, preparation was the main focus. My first step was to involve the AI (in this case ChatGPT o1) in the planning process. I had it create a step-by-step guide based on a short description of the Python project, its main functions, and my goals for the .NET version.

The result was good: the AI delivered a clear structure with tasks such as project initialization, the definition of classes and methods, as well as tips for dealing with specific problems like the API integration. This plan was my guideline for the entire process.

Creating the Empty .NET Structure

Next, I built the basic structure for the .NET project. The point here was to create the directories and files according to the guide created earlier.

  • Main directories: I created folders for Configuration, Models, and Services (OpenAI and Scraping).
  • File structure: For each function of the Python code, I created a corresponding class or file.
  • Tools: CursorAI was my main tool. CursorAI opened up the possibility of integrating AI directly into the process and generating the code with powerful models, such as Anthropic Claude Sonnet.

This step was done quickly and gave me a solid basis to continue working.

AI Tools

Now came the exciting part: the actual implementation. Here I used two key tools that made the process enormously easier for me.

Tool Tip: CursorAI

CursorAI was a real game changer, especially for code generation and editing. Since I had already developed the original Python project with CursorAI, I could seamlessly use it for the .NET version as well. It not only offered context-based suggestions, but also helped me avoid typical mistakes like wrong data types or syntax problems. For better context, I also kept the original Python project in a folder for now and used it as a reference in my prompts.

Using Claude 3.5

Claude 3.5 (a large, advanced language model) helped me translate the Python code into C# piece by piece. The model was particularly helpful for understanding more complex functions and transferring them into a .NET-typical syntax. I gave the model the original Python code and the project description, and it delivered very precise and almost error-free code suggestions.

Of course, alternatives like Visual Studio or Rider with GitHub Copilot would also have been possible, but I was already familiar with CursorAI, making it the obvious choice.

Foundations for the Implementation

I provided the following information in the chat in CursorAI:

  • The guide from step 1 as a foundation
  • The complete original codebase in Python (piece by piece)

I indexed relevant documentation in Cursor. In my case, that was the OpenAI SDK documentation.

Step-by-Step Implementation

The implementation followed a clear structure:

  1. File by file: I went systematically through each file of the Python version and implemented its functionality in the corresponding .NET class.
  2. Focus on functions: I first concentrated on the core logic, such as scraping and API integration.
  3. Incremental tests: After each major change, I ran initial manual tests to make sure the code worked.

Debugging and Fine-Tuning

Debugging was unavoidable – especially because I’m not a .NET expert. But this is exactly where the AI tools proved to be invaluably helpful.

Manual Review vs. Unit Tests

Since the original Python project did not contain any unit tests, I had to rely on manual reviews for validation. For a small project like this, that was fine, but I quickly noticed that unit tests would have made the work considerably easier. My approach:

  • Focus on main functions: I specifically checked whether the core features such as scraping and API calls were running correctly.
  • AI support: The AI helped me identify typical mistakes like wrong return values or unused variables.

In the end, this iterative approach – implement, test, debug – was time-consuming, but incredibly instructive and successful.

In the next chapter, I’ll share the biggest challenges I encountered and how I solved them.

Challenges and Solutions

Dealing with a Lack of .NET Experience

The biggest challenge for me was that I had no experience with .NET whatsoever. While I feel at home in Python, working with a strongly typed language like C# is a completely different world. Two problems came up here:

  1. Concepts and syntax: Many .NET-specific concepts like LINQ, delegates, or the exact handling of types were somewhat foreign to me.
  2. Project structure: Unlike Python, which often works with a simple module structure, .NET requires a clearly defined architecture with classes, namespaces, and strict conventions.

Solutions:

  • AI as a tutor: I used the OpenAI models not only to translate the code, but also to have unfamiliar concepts explained to me. For example, I asked: “What is the difference between List<T> and IEnumerable<T>?” The answers were concise and helped me quickly grasp the fundamentals.
  • Documentation and resources: The official .NET documentation also proved to be a gold mine. With the help of CursorAI, I could index these documents and use them in a targeted way. This means I don’t have to deal in detail with the specifications of the OpenAI-DotNet NuGet.

Ensuring Clean Code Without Comprehensive Tests

A central point in the migration from Python to .NET was the question of how much I could focus on clean-code practices – and where I deliberately made compromises. In an experiment like this, geared toward speed and feasibility, perfection took a back seat. Nevertheless, clean code played a role, because in the long term it is decisive for the maintainability and extensibility of the project.

DRY – Don’t Repeat Yourself

One of the cornerstones of clean code is the DRY principle: avoid repetition in the code. During the migration, I tried to minimize redundant sections by extracting frequently recurring procedures – for example for API requests or data processing – into separate methods. But with the tight time frame and my limited experience in .NET, this wasn’t always consistently feasible. Some redundant code blocks remained and could be cleaned up in a subsequent refactoring.

The connection is clear: repetition leads to unnecessary maintenance effort, since every change has to be made in several places. This aspect should be a priority in the post-processing of the code.

SRP – Single Responsibility Principle

The SRP states that each class or method should have exactly one task. While I tried to adhere to this rule in the .NET version, the result wasn’t always ideal. Some classes and methods currently take on more responsibility than they should, which is due to the fast development process and my limited experience with the .NET architecture.

The problem with this is that such structures are hard to test and even harder to extend. After the project is finished, I could address this specifically by separating the responsibilities more clearly and splitting up methods or classes.

IOSP – Interface Segregation Principle

The IOSP requires that interfaces only contain the methods that a particular class actually needs. In the current version of my project, I didn’t actively pay attention to this, since the main task was feature parity with the Python version.

A focus on IOSP could later help make the code more flexible and maintainable. For example, smaller, more specific interfaces could create a better foundation for extensions without affecting existing implementations.

KISS – Keep It Simple, Stupid

The KISS principle was the most important clean-code guideline during the project. I tried to keep the functions and structures as simple as possible in order to maintain an overview and put the focus on feasibility. But simplicity doesn’t equate to cleanliness. Some parts of the code currently seem simple, but are not optimally structured or documented.

This shows how important a subsequent refactoring is: only with some distance can you rework simple structures so that they also meet the demands of clean code.

Post-Processing: Clean Code as the Next Step

Even if not everything was implemented perfectly, that is completely fine in an initial prototype. The focus was on feasibility, not on perfection. Now that the project works, I could tackle the following steps:

  1. Analysis and improvement: Use a clean-code tool or an agent to specifically identify weak points in the code.
  2. Refactoring according to principles: Eliminate repetition, assign responsibilities more clearly, and make interfaces leaner.
  3. Add tests: Automated tests would help carry out future changes safely and maintain the clean-code level.

The most important thing is: this experiment has shown that AI tools enable rapid implementation, but human thinking and targeted post-processing remain irreplaceable when it comes to sustainable code quality.

Conclusion and Outlook

Summary of the Results

The experiment proved it: with the right tools and approaches, it is possible to successfully migrate a Python project to .NET in a short time – even without prior knowledge of the target technology. Within just four hours, I was able to develop a functioning .NET version of my application that covers all the core functions of the original.

The process was anything but perfect, though. The code quality still left something to be desired in some places, and the application was designed more for functionality at first than for long-term maintainability. Nevertheless, the experiment was a success: it showed how powerful AI tools can be for shortening development times and closing knowledge gaps. Here you can see the refactored version: https://github.com/genericdeAG/web-summarizer-dotnet

Potential of AI Tools in Refactoring

The role of AI in this project was not just supportive, but decisive. Tools like ChatGPT o1 or CursorAI helped me master typical challenges:

  • Fast translation: The Python code could be quickly ported to C#.
  • Error reduction: Typical beginner mistakes were identified and fixed early (through the step-by-step approach).
  • Knowledge expansion: The AI served as a tutor that explained new concepts to me and delivered concrete solution suggestions.

This experiment illustrates the potential of AI-assisted refactoring for software teams or individual developers. It allows us to work more efficiently and learn new technologies faster, without losing sight of quality entirely.

Next Steps: Further Refactoring and Optimization

The current result is a prototype – functional, but with room for growth. The next steps could aim to systematically optimize and further develop the code:

  1. Clean-code refactoring: In the next step, I would specifically rework the existing classes and methods in order to implement principles like DRY, SRP, and IOSP more consistently.
  2. Add tests: Automated unit tests could increase the stability of the application and make future changes safer.
  3. Performance optimization: Once the codebase is cleaned up, I could check whether the performance of the application can be improved in .NET.
  4. Extensions: Finally, the application could be enhanced with new features, such as more dynamic parsing (for JavaScript-based content) or scraping multiple subpages at once.

What Remains?

The experiment has shown that AI tools are not just a shortcut, but also open up new perspectives. They don’t take the work off our hands, but expand our possibilities – whether through automation, knowledge transfer, or the reduction of errors.

For me, this project was a step into a new world. It showed me that I don’t have to be afraid of new technologies, as long as I’m willing to learn and use the right tools. And that may be the most important insight: with a clear objective and a bit of a willingness to experiment, almost anything can be achieved.

The AI Software Company supports small and medium-sized software firms in the DACH region in making their development processes more efficient, faster, and future-proof with AI.

Curious how you can use AI meaningfully in your software team? Sign up for our newsletter and receive valuable tips, insights, and updates!

Your direct line to our AI specialists

Book a free consultation