Dependency Injection implements two related concepts
Inversion of control
Dependency inversion principal
Benefits:
Promotes loose coupling of components.
Promotes logical abstraction of components.
Improve unit testing
Cleaner and more readable code.
Microsoft Dependency Injection Container:
Services are registered at startup and resolve from the container at run time.
The container is responsible of creating and disposing of instances of required services on
demand
What To register with DI:
Locate new keyword
Is the object a dependency
Apply dependency inversion principal
Register the service
Rins and repeat
What NOT To register with DI:
POCOS which only return model to make some view.
Primitive types and strings.
Injection Configuration in .Net Core:
Create a POCO class which hold the object of configuration like
o Public bool EnableEmailFunctionality
Then add those variables in json file
Register the configuration in ConfigureService Method and Get Section of configuration
Inject the Ioption in Controller or any where you need like this and get value
Service Lifetime:
o Transient Services:
o Not required to be thread safe
o Potentially less efficient
o Easiest to reason about
o SingleTon Services:
o Generally more performant
o Must be thread-safe
o Suited to functional stateless service. Like function accept input and return output
and no shared state is used.
o Scoped Services:
o Create scoped services per request e.g dbContext
Avoiding Captive Dependencies (Chose proper service lifetime)
o A service should not depend on a service with a lifetime shorter than its own.
o
Scope Validation
o It is introduced in Core 2.0.
o Enables by default for development
o Validate container scopes at startup
Service Descriptors:
It contain information about registered services.
Multiple Registration:
In multiple registration case for the same contract. It’s the last registration always wins. Consider using
TryAdd method.
TryAdd and TrayAddEnumerable:
Both will ensure that no duplication occurred while registering services.
Replace And Remove Method:
o Use replace method to replace the already registered service with new one. But it accept service
descriptor.
o Use remove method to remove the service registration
Registering Multiple Implementation Of An interface:
If one interface has multiple implementation then inject using IEnumerable in constructor so that It will
attempt to resolve all implementation of an interface
Open Generic Registeration:
This allow us to define single registration which can handle any type arguments of contract
Cleaning Registration Of Dependency Injection Services:
o Create another class with extension method under the namespace of
Microsoft.Extension.DependencyInjection which will return IServiceCollection
Action Injection:
Only work when action specifies the dependency invoked to handle the request.
Middleware Injection:
Factory Based Middleware:
Factory based middleware is constructed once per request.
Scrutor:
It’s a 3rd party library which full fills two gaps that Microsoft lacks
o Scanning Assemblies
o Using the decorator pattern