Well done! You’ve learned how to apply the DIP and IoC principles. So how does this factor into ASP.NET development?

If you’ve ever logged information or added a database in your application, you’ve already used these principles! Within Razor Pages, each page model depends on certain tools, such as loggers, databases, and authentication tools. We call these tools services.

Instead of instantiating the relevant objects and injecting them ourselves, ASP.NET gives us a built-in feature to do it for us, called an IoC container or DI container (Inversion of control and Dependency Injection, respectively).

Each page model “requests” and accesses services by listing them in its constructor. Here’s an example…

public class IndexModel : PageModel { private readonly IService service; public IndexModel(IService _service) { service = _service; } }

The interface IService here is the service. The IoC container will eventually provide an object that fulfills that service (in other words, an object that implements the interface).


Take a look at IndexModel.cshtml.cs. Where does the class declare its dependencies?

Sign up to start coding

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?