Skip to main content

Dependency Injection(DI) Inversion of Control(IOC) and IOC Framewoks


Dependency Injection
To understand DI better, lets  develop a hypothetical   role based application where the rendered view for the user depends upon the ROLE the user possesses. In a typical ASP.Net Application(c’mon I have  been asp.net developer for 6+ years…will take some time ),membership services will do the trick or if the developer is enthusiast enough, he/she will write some custom classes.

In either of the scenarios, the Code Behind file will render view depending upon the role. Let’s say there are 4 views (AdminView, DeveloperView, Manager View, SupervisorView).
If I develop a pseudocode  for administering admin user view, it wil be something like the following
If (userRole==Admin)RenderView= AdminView

To get into some details, it’s the code-behind(or rather say controller) that decides which view to render. How about the Views being intelligent enough, to know which controller (class/objects) to call and give the required results. This is where comes the Dependency Injection….injecting the dependencies into the view.

What exactly  is  Dependency Injection?

 According to WikiPedia, Dependency injection (DI) is  nothing but a design pattern in object-oriented programming whose primary purpose is to  improve testability and simplify deployment of components in enterprise  software development environment.
The Dependency Injection pattern involves at least three elements:
  • dependent consumer,
  • A declaration of a component's dependencies,, defined as interface contracts,
  • An injector (sometimes referred to as a provider or container) that creates instances of classes that implement a given dependency interfaces on request.


The dependent object describes what components it depends on to do its work.
The injector decides what concrete classes satisfy the requirements of the dependent object, and provides them to the dependent.
Note:
In conventional software development the dependent object decides for itself what concrete classes it will use; in the dependency injection pattern, this decision is delegated to the "injector" which can choose to substitute different concrete class implementations of a dependency contract interface at run time rather than at compile time.

If you feel like Class inheritance would have solved the purpose, please clarify the doubts by reading this Dependency Injection(DI )vs Class Inheriance article.

Types of Dependency Injection

There are three common forms of dependency injection:

  1. Constructor Injection(uses parameters to inject dependencies in constructor)
  2. Setter Injection (setter methods,are used  to inject the object's dependencies)
  3. Interface-based injection (an interface is used  to inject dependencies)

The example puts a small demonstration of how these types are implemented.
This link can help you understand DI better from SilverLight perspective.

Are Dependency Injection(DI) and Inversion of Control (IOC) inter-related
The relation between these two patterns can be understood from these lines  article
“Inversion of Control and Dependency Injection are two related ways to break apart dependencies in your applications. 

Inversion of Control (IoC) means that objects do not create other objects on which they rely to do their work. Instead, they get the objects that they need from an outside source (for example, an xml configuration file).
Dependency Injection (DI) means that this is done without the object intervention, usually by a framework component that passes constructor parameters and set properties.”

If you still need some more clearance, refer to this article.

Dependency injection/IOC Frameworks(containers)
To implement IOC, the developers have been thrown up with lots of IOC frameworks that have been listed here.
Which one to use is entirely your call or may be your project’s requirements or simply your boss’s wish.
A very interesting way to decide would be to user the poll results mentioned here

Hope this was useful.
Till next we connect…
Happy reading….




Comments

Popular posts from this blog

Asp.Net 4.0: An Overview-Part-III

This is the last post in the series which will explore the following new features of ASP.Net 4.0  Performance Monitoring for Individual Applications in a Single Worker Process Web.config File Refactoring Permanently Redirecting a Page Expanding the Range of Allowable URLs Performance Monitoring for Individual Applications in a Single Worker Process It is a common practice to host multiple ASP.NET applications in a single worker process, In order to increase the number of Web sites that can be hosted on a single server. This practice results in difficulties for server administrators to identify an individual application that is experiencing problems. ASP.NET 4 introduces new resource-monitoring functionality introduced by the CLR. To enable this functionality, following XML configuration snippet is added to the aspnet.config configuration file.(This file is located in the directory where the .NET Framework is installed ) <?xml version="1.0" encoding="UTF-8"

WCF-REST Services-Part-II

HOW REST is implemented in WCF Part-I of the series explored the REST conceptually and this post will explore how REST is implemented in WCF. For REST implementation in WCF, 2 new attributes namely WebGetAttribute and WebInvokeAttribute are introduced in WCF along with a URI template mechanism that enables you to declare the URI and verb to which each method is going to respond. The infrastructure comes in the form of a binding ( WebHttpBinding ) and a behavior ( WebHttpBehavior ) that provide the correct networking stack for using REST. Also, there is some hosting infrastructure help from a custom Service¬Host ( WebServiceHost ) and a ServiceHostFactory ( WebServiceHostFactory ). How WCF Routes messages WCF routes network messages to methods on instances of the classes defined as implementations of the service. Default behavior ( Dispatching ) for WCF is to do this routing based on the concept of action. For this dispatching to work, an action needs to be present in ev

SOLID principles -Code Samples and Free Ebook

I planned to write code samples for SOLID principle implementations, however I am a firm believer of " NOT RE-INVENTING THE WHEEL ", when all you need is use the wheels and make a new CAR. Going by the ideology, I have found an excellent  SOLID principles FREE -Ebook ( covering all aspects of SOLID design principles, with Code sample). This book is an excellent visual aid to remember these principles, as it uses Motivational posters for explaining SOLID design principles. One additional advantage to the above mentioned book is the Code-Refactoring ebook . Both of these books can be downloaded from this EBOOK download Link Both of these books can be downloaded form here. Hope this book proves useful... Till next we connect.... Happy Learning..