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...

Covariance and Contravariance-General Discussion

If you have just started the exploration of .Net Framework 4.0, two terms namely Covariance and Contravariance might have been heard. The concept that these terms encapsulate are used by most developer almost daily, however there has never been any botheration about the terminologies. Now, what actually these terms mean and how are these going to affect us as a developer, if we dive in to the details. The simple answer is it’s always good to know your tools before actually using them. Enough philosophy, let’s get to the business. Starting the discussion let me reiterate that in addition to Covariance and Contravariance, there is another terminology, Invariance. I’ll by start here by diving into the details of Invariance and then proceed further. Invariance: Invariance can be better understood by considering the types in .Net.>net has basically two type, value-types and reference-types. Value types (int, double etc) are invariant i.e. the types can’t be interchanged either ...

Advanced WCF

In this post, I am sharing the link of articles about  advanced topics in WCF. The List of articles is exhaustive and can serve as your repository for all WCF queries. Concurrency,Throttling & Callbacks  WCF Concurrency (Single, Multiple and Re entrant) and Throttling   WCF-Interop and BinarySecurityToken  WCF Callbacks  Creating Web Services From WSDL Link1 Link2 Link3 Link4 WCF-Security WCF over HTTPS   Transport Security(basic)/HTTPS UserNamePasswordValidator ServerCertificateValidationCallback 9 simple steps to enable X.509 certificates on WCF - CodeProject http://www.codeproject.com/KB/WCF/9StepsWCF.aspx?display=Print Message Security(Certificate)/PeerTrust Securing WCF Services with Certificates. - CodeProject http://www.codeproject.com/KB/WCF/wcf_certificates.aspx Message Security(Certificate)/ChainTrust How To Configure WCF Security Using Only X.509 Certificates - CodePr...