Dependency Injection in MVC
As we know, dependency injection is defined as a design pattern that allows an application to remove hard-coded dependencies. The Dependency Injection (DI, Wikipedia) is a design pattern that reduces hard-coded dependencies between your classes by injecting these dependencies at run-time, instead of during design-time. In Technically, Dependency Injection is a mechanism that allows the implementation of another, more high-level, design pattern called Inversion of Control (IoC, Wikipedia). That is the purpose of both patterns is to reduce hard-coded dependencies (or ‘coupling’) between your classes. Whether you ever developed ASP.NET MVC applications you probably have come across this term - Dependency Injection. The Dependency Injection is a way to implement the Dependency Inversion Principle. Into simply put, Dependency Injection is a design pattern that helps a class separate the logic of creating dependent objects. That is the result of this separation is a loosely coupled system where there is no rigid dependency between two concrete implementations. It article discusses what Dependency Injection is and illustrates its use in an ASP.NET MVC application. Like a Developers, you may have come across the term ‘Dependency Injection’ as a design pattern that helps develop maintainable and de-coupled code. The DI is also a way to implement the ‘D’ in SOLID principles.
There are the following three types of dependencies:
1. Constructor
2. Setter (Properties)
3. Method.
The Dependency Injection (DI) is as a design pattern that takes away the responsibility of creating dependencies from a class thus resulting in a loosely coupled system. Into the order to understand DI you need to be aware of the following terms. –
- Dependency .
- Dependency Inversion Principle .
- Inversion of Control (IoC) .
Agenda
Dependency Inversion Principle.
Inversion of Control.
Dependency Injection.
Creating a console application.
Adding Reference to Microsoft Unity Framework.
Adding the BL class.
Adding the DL class.
Adding Interface.
How to configure the Unity Container.
Running and debugging an application.
Final output.
Dependency Injection Pros and Cons.
Inversion of Control (IoC)
The Inversion of Control (IoC) refers to a programming style where a framework controls the program flow with the help of Dependency Injection. In software engineering, inversion of control is a design principle used to increase the modularity of the program and make it extensible. There are several basic techniques to implement inversion of control (i.e. factory pattern, service locator pattern, dependency injection and so on).
Dependency Injection
The DI is a software design pattern that allows us to develop loosely coupled code.
A. High-level modules should not depend on low-level modules. Both should depend on abstractions.
B. Abstractions should not depend on details. Details should depend on abstractions.
There is one major point to remember:
“Inversion of control is principal and Dependency Injection is implementation”.
Note: - Even though this article illustrates Dependency Injection using ASP.NET MVC, the underlying principle can be applied to any platform or programming framework. It’s Also, keeps in mind that this article is intended to explain the Dependency Injection principle and not its specific implementation under ASP.NET MVC. Therefore, topics like containers are beyond the scope of this article.
Example - 1
public interface ICustomerRepository
{
List<CustomerViewModel> SelectAll();
CustomerViewModel SelectByID(string id);
void Insert(CustomerViewModel obj);
void Update(CustomerViewModel obj);
void Delete(CustomerViewModel obj);
}
Example - 2
public class HomeController : Controller
{
ICustomerRepository repository = null;
public HomeController(ICustomerRepository repository)
{
this.repository = repository;
}
public ActionResult Index()
{
List<CustomerViewModel> data = repository.SelectAll();
return View(data);
}
}
Thank You For reading this article!!!
Shrikant Mishra
19-Aug-2020There is no doubt that this is a great article for a beginner level developer.
Thank you for posting this article !!
https://www.mindstick.com/articles/33673/insert-update-delete-in-mvc-via-webgrid-and-modal-popup