S.O.L.I.D is an acronym for the first five object-oriented design (OOD) principles by Robert C. Martin, popularly known as Uncle Bob. These principles, when combined together, make it easy for a programmer to develop software that are easy to maintain and extend.
The name S.O.L.I.D is very interesting, When I heard this name I found it to be very funny. Actually at that time I did not understand what is the importance. Each letter in S.O.L.I.D represent a principle which is very important for software development. Here is what exactly the principle means:
- S – Single-responsibility principle
- O – Open-closed principle
- L – Liskov substitution principle
- I – Interface segregation principle
- D – Dependency Inversion Principle
In this article I am going to describe an important principle, that is the last one: Dependency Inversion.
When I was new to this topic I was searching Google for the accurate definition of this principle. I got several definitions but I found one from Wikipedia that is appropriate for this concept:
In object-oriented programming, the dependency inversion principle refers to a specific form of decoupling software modules.
There are two main concepts of the Dependency Inversion Principle and these are:
1. The Dependency Inversion Principle (DIP) states that high-level modules/classes should not depend upon low-level modules/classes. Both should depend upon abstractions.
2. Abstractions should not depend upon details. Details should depend upon abstractions.
Dependency injection is a technique to develop software in an independent way. Independent in the sensethe no module will depend on another module in the software development.
Sothe dependency injection technique will inject all dependencies of lower modules and provide these dependency to the higher module without direct contact.
High-level modules/classes implement business rules or logic in an application. Low-level modules/classes deal with different operations; in other words, they may deal with writing information to databases or passing messages to the operating system or services, etc.
The fully dependent application will look something like this. All modules are tightly coupled with each other.

As per Dependency Inversion Principle there is someone between them to decouple these models.

Here now I will explain how we can implement this in our MVC Project.
Dependency Injection using Microsoft Unity
2. Select the template MVC.
3. Now create a class Employee.cs and an Interface Iemployee.cs as shown in the project.

4. Now go to Manage NuGet Package and install Unity.Mvc3.
After installing we will get two references in our project as follows.
After installing unity we get these two things in the references.
- private static IUnityContainer BuildUnityContainer()
- {
- var container = new UnityContainer();
- container.RegisterType<IEmployee,Employee>();
- // register all your components with the container here
- // it is NOT necessary to register your controllers
- // e.g. container.RegisterType<ITestService, TestService>();
- return container;
- }
- public class MvcApplication : System.Web.HttpApplication
- protected void Application_Start()
- {
- AreaRegistration.RegisterAllAreas();
- FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
- RouteConfig.RegisterRoutes(RouteTable.Routes);
- BundleConfig.RegisterBundles(BundleTable.Bundles);
- Bootstrapper.Initialise();
- }
- public interface IEmployee
- {
- string savedata();
- }
- public class Employee:IEmployee
- {
- public string savedata()
- {
- return "Data Saved...";
- }
- }
So our aim here is to decouple each class, so here without creating any instance, I will call another class method. Let's see how.
- public class HomeController : Controller
- {
- IEmployee iemployee;
- public HomeController(IEmployee _iemployee)
- {
- iemployee = _iemployee;
- }
Now when you try to access the employee class method you can do as follows in another action method.
Here is the code of the screenshot.
- public class HomeController : Controller
- {
- IEmployee iemployee;
- public HomeController(IEmployee _iemployee)
- {
- iemployee = _iemployee;
- }
- public ActionResult Index()
- {
- string result= iemployee.savedata();
- return Content(result);
- }


Gowtham RajamanickamPosted Apr 25, 2016, 2:55 AM
ncie share
Mohammad KhalidPosted Apr 1, 2016, 6:39 AM
You have made this article simple to understand...Keep posting
Asfend YarPosted Feb 29, 2016, 10:54 AM
nice
Vikram ChaudharyPosted Feb 8, 2016, 3:55 AM
Clear and simple.Thanks
Manas MohapatraPosted Feb 8, 2016, 1:44 AM
Simplified
Sibeesh VenuPosted Feb 8, 2016, 12:08 AM
Nice Share
Santhakumar MunuswamyPosted Feb 7, 2016, 12:53 PM
Thanks for good article. Keep it up
Ehsan SajjadPosted Feb 7, 2016, 12:13 PM
Well explained
sreenivasa kPosted Feb 7, 2016, 10:23 AM
nice 1
Humayun Kabir MamunPosted Feb 7, 2016, 7:35 AM
Nice...