jeudi 30 mai 2019

Why doesn't TryAddTransient return an IServiceCollection?

Most of the Microsoft Dot Net Core Dependency Injection static extensions return the IServiceCollection, for example AddTransient

Returning the IServiceCollection allows building up the dependencies fluently:

services
    .AddTransient<IThing,Thing>()
    .AddTransient<IOtherThing,OtherThing>()

But the TryAdd variants of these return void, for example TryAddTransient. This is irksome because it breaks the method chaining approach.

I can't think of a reason why the API is designed this way, but I doubt it's an omission. Am I missing something, why does it return null?

Aucun commentaire:

Enregistrer un commentaire