Dependency Injection

Services can be conditionally injected, or conditionally decorate existing services.

AddTransient / AddScoped

services.AddTransientForFeature<ILinkService, GlobalLinkService>("GlobalLink");

Decorate

The decorator pattern injects the existing implementation of a service into the constructor of the decorator service

services.AddScoped<ITaxService, LocalTaxService>();
services.DecorateForFeature<ITaxService, AvalaraTaxService>("Avalara");

Transient, scoped and middleware flags take effect immediately and don't require an application restart. They are usually preferred for this reason over singleton implementations, which live for the entire lifetime of the application.

Last updated