Milan Jovanović 4/18/2026

Why I Switched to Primary Constructors for DI in C#

Read Original

This article discusses the author's transition to using C# 12 primary constructors for dependency injection in service classes. It contrasts the traditional approach with field declarations and constructor bodies against the concise primary constructor syntax. The author explains how primary constructors eliminate boilerplate in ASP.NET Core service classes, using examples like OrderService and CheckoutService. They also address initial skepticism about mutable captures versus readonly fields, concluding that the benefits outweigh the risks when used appropriately in DI scenarios.

Why I Switched to Primary Constructors for DI in C#

Comments

No comments yet

Be the first to share your thoughts!

Browser Extension

Get instant access to AllDevBlogs from your browser

Top of the Week

No top articles yet