? 5 Mistakes I Made When Starting with .NET (and How to Avoid Them)

? 5 Mistakes I Made When Starting with .NET (and How to Avoid Them)

When I started working with .NET, I made several mistakes that made my code less efficient, harder to maintain, and even caused performance issues. If I had known about these pitfalls earlier, I would have saved a lot of time!

Here are 5 mistakes I made early in my .NET journey and how you can avoid them:


1?? Blocking Asynchronous Calls

?? One of the first mistakes I made was mixing synchronous and asynchronous code without understanding how async programming actually works. I tried calling async methods synchronously, which often resulted in deadlocks and unexpected application freezes.

? Wrong Code:

The problem here is that .Result blocks the main thread while waiting for the async method to complete, which can lead to deadlocks in environments with a synchronization context (like ASP.NET).

? Correct Code:

?? Tip: Always use await to keep the async flow and avoid unnecessary blocking.


2?? Using ORM Without Understanding SQL

?? At first, I thought Entity Framework (EF) would handle everything for me, and I wouldn't have to worry about SQL at all. But after analyzing auto-generated queries, I realized some of them were slow and inefficient. This directly affected my application’s performance.

? Wrong Code:

This may seem harmless, but depending on the data volume and the lack of indexes in the database, it can be very inefficient.

? Correct Code:

?? Tip:

  • Use AsNoTracking() if you don’t need to modify the data. This improves performance.
  • Always monitor EF-generated queries. In EF Core, you can use:

  • For complex queries, consider optimizing with FromSqlRaw() and database indexing.


3?? Ignoring Dependency Injection

?? At first, I used to instantiate services directly inside classes, without considering coupling and testability. This made maintenance difficult and unit testing almost impossible.

? Wrong Code:

Here, OrderService directly depends on the concrete implementation of EmailService, making it difficult to replace this dependency with a mock in tests.

? Correct Code:

And in Program.cs (for .NET 6+):

?? Tip: Use .NET's dependency injection to keep your code modular and testable.


4?? Not Writing Automated Tests

?? In the beginning, I relied on the famous "it works on my machine", but when bugs started appearing in production, I realized the importance of automated tests.

? Wrong Code (no tests):

Without tests, any changes can break the code without anyone noticing.

? Correct Code (with tests using xUnit and FluentAssertions):

?? Tip: Start with unit tests, then add integration tests to cover full application flows.


5?? Poor Project Structure

?? At first, I mixed business logic, data access, and controllers, making the code hard to scale and maintain.

? Wrong Code (everything in the Controller):

This makes the controller directly responsible for database access, violating the separation of concerns principle.

? Correct Code (separating responsibilities):

?? Tip: Use a layered architecture to keep the code clean and scalable.


?? Conclusion: We all make mistakes when starting out, but the important thing is to learn and improve. Have you made any of these mistakes? Or do you have others to share? Drop a comment below!


#DotNet #CSharp #DotNetCore #AspNetCore #DotNetDeveloper #SoftwareDevelopment #Coding #CleanCode #BestPractices #CodeQuality #SoftwareEngineering #TechTips #DevTips #LearnToCode #CareerGrowth #ProgrammingTips #DeveloperLife #SoftwareCraftsmanship


Marcos Borges

Software engineer | Developer | DevOps | .NET | C# | Angular

1 个月

Thanks for sharing!! Great tips.

回复
Leandro Henrique M.

C# Engineer | BackEnd Developer | .NET | SQL | SCRUM Certified - (SFC) | KANBAN | Agile Methodologies |

1 个月

Congrats! Very Nice Article!

回复
Fabio Ribeiro

Senior Software Engineer | Java | Spring | AWS

1 个月

Great advice!

回复
Leandro Veiga

Senior Software Engineer | Full Stack Developer | C# | .NET | .NET Core | React | Amazon Web Service (AWS)

1 个月

Useful tips. Thanks for sharing! ??

回复
Gabriel Levindo

Android Developer | Mobile Software Engineer | Kotlin | Jetpack Compose | XML

1 个月

Well done!!

回复

要查看或添加评论,请登录

Willian Soares da Silva的更多文章

社区洞察

其他会员也浏览了