Sie sind auf Seite 1von 2

Dependency Injection Scenarios in Asp.

Net
Mvc

Nitij, 16 Jun 2016 CPOL


4.00 (6 votes)
A couple of Dependency Injection scenarios in Asp.Net Mvc
DIScenarios (GitHub)

Introduction
Dependency injection is a programming and application design pattern which is being used by
developers for so many years now. This pattern comes as a default programming syntax in many
frameworks like Angular and we must follow them in order to use the framework. I will not go
much into explaining what Dependency Injection actually is because there is much information
available on the subject which could be accessed using a simple web search. Here is the formal
definition from Wikipedia:
Quote:
In software engineering, dependency injection is a software design pattern that
implements inversion of control for resolving dependencies. A dependency is an object
that can be used (a service). An injection is the passing of a dependency to a dependent
object (a client) that would use it.
Dependency Injection promotes loose coupling between application modules and provides us means
to switch code modules being used without building and redeploying the entire application or parts
of it. This could be done by using either application configuration file or by reading dependency
information from a database table.
We can create a DI code framework of our own for custom scenarios. But in Asp.Net Mvc in order
to use DI in certain areas we must follow the .Net approach. This could be done by implementing
in-built interfaces into classes and wiring them up from Global.asax when the application starts.
I am assuming that you are aware of dependency injection as a design pattern and as a programming
concept before you start reading further. In this article I will be giving examples of DI in different
areas of Asp.Net Mvc application architecture. There will be partial code demonstration in this
article and the entire example could be downloaded from either here or from GitHub where I have
uploaded it in a public repository.

Using the code


Create an empty Asp.Net Mvc application. Add a single HomeController, Index view and a Web
Api Controller.
Controller Class Dependency Injection
Controller classes are extensively used in the Asp.Net Mvc from simply returning the html view to
the browser to providing Get and Post methods which could be invoked from the client side. So
many times we need to do things like data access, error logging, caching etc. from a controller class.
Now there are a couple of ways to do this. The standard way is to just create an object out of the
manager class which contains the data access logic and use it directly. But there are several issues in
this approach.
The manager class will be tightly bound to the controller class and in order to use a different
manager class implementation we would have to make changes to the controller code and re-deploy
the application after building it. There could be several such classes that the controller class could
need to perform the essential business logic operations which means more changes.

Das könnte Ihnen auch gefallen