Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,150,600 members, 7,809,179 topics. Date: Friday, 26 April 2024 at 03:22 AM

Dependency Injection - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Dependency Injection (954 Views)

Help Out! On SQL INJECTION / Xpath Injection / Hacker Stole $100,000 From Users Of California Based ISP Using SQL Injection (2) (3) (4)

(1) (Reply) (Go Down)

Dependency Injection by harryobas: 11:19pm On May 23, 2012
In designing an object-oriented application, a major tenet of design is "loose coupling". Loose coupling means that objects should only have as many dependencies as is needed to do their job - and the dependencies should be few. Furthermore, an object's dependencies should be on interfaces and not on "concrete" objects, when possible. Loose coupling promotes greater reusability, easier maintainability, it also enhances unit testability by allowing you to easily provide "mock" objects in place of expensive services, such as a socket-communicator."Dependency Injection" (DI), also known as "Inversion of Control" (IoC), can be used as a technique for encouraging this loose coupling. There are two primary approaches to implementing DI: constructor injection and setter injection. Obviously, at some point, something must be responsible for creating the concrete objects that will be injected into another object. The injector can be a parent object, or can be externalized and handled by a "DI container" framework. There are scores of open source DI container framework such as castle windsor, microsoft unity, ninject to name a few. DI is worth giving a shot if you are serious about developing loosely coupled applications.
Re: Dependency Injection by naijaswag1: 11:41pm On May 23, 2012
Spring champions IoC and DI in Java.
Re: Dependency Injection by harryobas: 11:58pm On May 23, 2012
Spring is good but there are others for java which i think are equally very good like pico container and google-guice.
Re: Dependency Injection by ektbear: 1:04am On May 24, 2012
This post would be much better with a concrete example.
Re: Dependency Injection by harryobas: 1:41am On May 24, 2012
@ekt bear I have not provided any concrete example because i wanted to discuss DI from a language agnostic standpoint. You can always google around for concrete examples of DI in your preferred language.
Re: Dependency Injection by ektbear: 6:44am On May 24, 2012
Eh, just pick an arbitrary language. Most of us can get the gist from any language you'll likely pick.

I'm not likely to google around to learn more unless I'm convinced first of its usefulness..
Re: Dependency Injection by harryobas: 9:25am On May 24, 2012
@ekt bear Like i said earlier DI promotes loose coupling here is a small example of constructor DI using c#:

Interface ISomeInterface{

void SomeMethod(); }

Public Class SomeClass{

Private ISomeInterface _somefield;

Public SomeClass(ISomeInterface somefield){
this._somefield = somefield;}
}

As u can see from the small example above the dependency for SomeClass is injected from outside SomeClass via its constructor. Hope this helps.
Re: Dependency Injection by harryobas: 11:22am On May 27, 2012
DI is the antidote for highly-coupled code.
Re: Dependency Injection by Chimanet(m): 12:40pm On May 27, 2012
DI is very important wen designing an enterprise level app. In java is all about defining some important components of your app in an extenal xml file and and loading d apropraite component without affecting other part of d application. Jst imagine a shoping site dat uses only paypal payment initially, i can abstract d payment component into a payment class and and inject paypal payment object which is a kind of payment 2 my app via xml. Later on d shop decides 2 introduce a payment method dat will accept masta card, paypal, nigerians atm cards etc. We simply create anoda payment object dat can handle all these different means of payment and inject it through d xml in place of d original only paypal payment object. Now our new payment component can handle different payments jst by making a new class and changing a single element of an xml file. We dnt need 2 touch other parts of our app. Also anoda example can b d case of wen we want 2 swap two different data layers of an app

(1) (Reply)

Pls How Do I Run My Java Program Using Jdk / Learn Php Mysql The Wordpress Way / Please Lead Me How To Create My Web Site For My Business

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 17
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.