Dependency Injection
Dependency Injection
https://www.codecademy.com/learn/learn-asp-net/modules/asp-net-dependency-injection/cheatsheet 1/8
19/01/2024, 13:10 Learn ASP.NET: ASP.NET: Dependency Injection Cheatsheet | Codecademy
Dependency Injection
When a service (dependency object) is created outside //The client class that depends on a
of its client (the object that depends on it) that service
service
can be passed into, or injected, into the client, typically
through the client’s constructor. class Reviewer
This process is called dependency injection, where {
services are not created by the clients that use (and
public EmailSender _sender {get; set;}
depend on) them, but rather are created and managed
in other code, and are injected into the client.
//EmailSender is injected into the
Reviewer object
public Reviewer(EmailSender sender)
{
_sender = sender;
}
public SendReview(string lesson, string
comments)
{
_sender.SendReview(lesson, comments);
}
}
https://www.codecademy.com/learn/learn-asp-net/modules/asp-net-dependency-injection/cheatsheet 2/8
19/01/2024, 13:10 Learn ASP.NET: ASP.NET: Dependency Injection Cheatsheet | Codecademy
}
IoC Container
Registering Services
https://www.codecademy.com/learn/learn-asp-net/modules/asp-net-dependency-injection/cheatsheet 3/8
19/01/2024, 13:10 Learn ASP.NET: ASP.NET: Dependency Injection Cheatsheet | Codecademy
AddDbContext
https://www.codecademy.com/learn/learn-asp-net/modules/asp-net-dependency-injection/cheatsheet 4/8
19/01/2024, 13:10 Learn ASP.NET: ASP.NET: Dependency Injection Cheatsheet | Codecademy
Dependencies As Interfaces
In order to satisfy the Dependency Inversion Principle, public class ReviewModel : PageModel
injected services are typically referenced as interfaces.
{
This allows the client class to use an implemented
service whose behavior is well defined via its interface, // Notice IFormSender interface
and not have any knowledge or be concerned with the private readonly IFormSender _Sender;
actual concrete class that implements that interface.
Any changes to the concrete class’s methods or
properties would not require any modifications to the [BindProperty]
client class since it only knows of the interface and its public string Review {get;set;}
well defined abstract methods.
[BindProperty]
public int ProductID {get;set}
https://www.codecademy.com/learn/learn-asp-net/modules/asp-net-dependency-injection/cheatsheet 5/8
19/01/2024, 13:10 Learn ASP.NET: ASP.NET: Dependency Injection Cheatsheet | Codecademy
Dependency
When one object (Object A) references another object // Object A - the class that depends on
(Object B), using its properties and methods, it means
that the first object (A) depends on the second (B),
Object B
making the second object (B) a dependency. class Reviewer
{
private EmailSender Sender = new
EmailSender();
public SendReview(string lesson, string
comments)
{
Sender.SendReview(lesson, comments);
}
}
https://www.codecademy.com/learn/learn-asp-net/modules/asp-net-dependency-injection/cheatsheet 6/8
19/01/2024, 13:10 Learn ASP.NET: ASP.NET: Dependency Injection Cheatsheet | Codecademy
AddRazorPages()
https://www.codecademy.com/learn/learn-asp-net/modules/asp-net-dependency-injection/cheatsheet 7/8
19/01/2024, 13:10 Learn ASP.NET: ASP.NET: Dependency Injection Cheatsheet | Codecademy
Print Share
https://www.codecademy.com/learn/learn-asp-net/modules/asp-net-dependency-injection/cheatsheet 8/8