Developer knowledge network · moderated exchange

Супольнасць UnreliableCode

Супольнасць распрацоўшчыкаў даследаванняў, зваротнага праектавання і кадавання

Knowledge indexжыць
4Categories
919Threads
2.8KПаведамленні
Tutorial

C# 12 Primary Constructors on Classes and Structs: Clean Dependency Injection [StackOverflow Architecture Guide]

roslyn_source_gen
Roslyn Compiler Dev
MEMBER
прадстаўнік: 120
Дата далучэння: Feb 2020
Паведамленні: 12
Дзякуй: 75
1 месяцаў таму · Jul 8, 2026 1:51 AM
#1

Simplifying boilerplate dependency injection with C# 12 Primary Constructors:

CSHARP
// C# 12 Primary Constructor syntax
public class PaymentProcessor(ILogger<PaymentProcessor> logger, IPaymentGateway gateway, IOrderRepository repo)
{
    public async Task ProcessAsync(OrderId id)
    {
        logger.LogInformation("Processing order {Id}", id);
        await gateway.ChargeAsync(id);
        await repo.MarkAsPaidAsync(id);
    }
}

Eliminates private field declarations (private readonly ILogger _logger;) and constructor assignment boilerplate, making service classes concise and readable!

generic_math_geek
C# 11 Math Guru
MEMBER
прадстаўнік: 138
Дата далучэння: Jul 2021
Паведамленні: 10
Дзякуй: 88
1 месяцаў таму · Jul 8, 2026 4:34 AM
#2

Primary constructors make ASP.NET Core service and controller definitions 50% shorter.

dotnet_runtime_architect
.NET Core Specialist
MEMBER
прадстаўнік: 103
Дата далучэння: Apr 2018
Паведамленні: 40
Дзякуй: 24
1 месяцаў таму · Jul 8, 2026 8:56 PM
#3

Works on records, classes, and struct types across the board.