11

I have been reading for and practicing dependency injection for the past two days but nothing is working out, and suddenly I found out that there were some frameworks required in order for dependency injection to work. Is that true? Isnt it a bad practice to make my project depend on some framework? Could it be done without the use of a framework?

EDIT: Im new to programming so I dont understand what is the difference between instatiating a class and using its methods (i dont need a framework for that) and using dependency injection and what is better about it

EDIT: Here is an example of me not using a framework and things not working TestNG @Factory annotation + not enough knowledge on Dependency Injection

Community
  • 1
  • 1
Kaloyan Roussev
  • 14,515
  • 21
  • 98
  • 180
  • I think you should use spring framework. – ihsan kocak Aug 12 '13 at 11:19
  • but do I MUST use a framework? – Kaloyan Roussev Aug 12 '13 at 11:20
  • Guice is not exactly a framework ... but it is way better than what Spring dependency injection can do. – le-doude Aug 12 '13 at 11:21
  • Frameworks does not mean "depending on".By using a framework your project does not depend on anything. Frameworks are examples of reusability.And AFAIK you must use framework for DI. – ihsan kocak Aug 12 '13 at 11:23
  • If you don't use a framework/library, you must implements your own dependecy injection system, which may be boring... I advice you to use Guice too, here is the [getting started](https://code.google.com/p/google-guice/wiki/GettingStarted) – NiziL Aug 12 '13 at 11:24
  • Using Guice makes sense for me because you can avoid lots of conditional logic when you select what type of implementation to be used in a particular situation – Sebin Eapen George Aug 13 '13 at 11:27

3 Answers3

8

No, you don't have to use a framework:

Dependency Injection

Of course you can use a framework too, As someone said you can use Spring Framework and use their annotations. Here you have a tutorial:

Dependency Injection with the Spring Framework

John
  • 576
  • 1
  • 6
  • 14
  • 2-3 guys above said that I must use a framework. Im confused. – Kaloyan Roussev Aug 12 '13 at 11:26
  • @J.Kowalski a framework is code that somebody wrote. You can write that code on your own if you think that those libraries that exist are not good for you. – zapl Aug 12 '13 at 11:27
  • I dont understand what is the difference between instatiating a class and using its methods (i dont need a framework for that) and using dependency injection and what is better about it. – Kaloyan Roussev Aug 12 '13 at 11:29
  • Yes, you can use a framework but you don't have to. It simplifies a bit the things. You can read the first article, it is explaining very well what dependency injection means. – John Aug 12 '13 at 11:32
  • can someone help me out here - http://stackoverflow.com/questions/18183894/testng-factory-annotation-not-enough-knowledge-on-dependency-injection – Kaloyan Roussev Aug 12 '13 at 11:41
3

You don't have to use a framework but it will help you write cleaner code. For instance, if you want to inject a mock into a class without using a framework, you need some way to to that either by adding getters/setters, pass it to the constructor or use public variables.

public class AClassToTest{
   private A aDependentClass; 

   public void aMethodToTest(){
      int i = aDependentClass.someDependentMethod();
      ..
      ..
   }
}

In the above code, aMethodToTest() is dependent of what aDependentClass.someDependentMethod() returns. This means that you should create a mock of class A and mock what someDependentMethod returns. This is still possible without using a framework, for example by adding a getter and setter so that you can set the object A in your testclass:

@Test
public void testAMethodToTest(){
   //here you must set the object A in your AClassToTest object
   //Create a mock of A with desired values
   //and set it using a setter
}

If you for example use spring for dependency injection, it will allow you to set the mock objects in it's IOC container by using it's @Autowire notation. Then you do not need to have setters/getters for your mocked objects which will give you cleaner code

polyglot
  • 2,031
  • 2
  • 20
  • 28
John Snow
  • 5,214
  • 4
  • 37
  • 44
0

For dependency injection Guice is great (personal opinion). At least I find it way better than what Spring provides. It is very lightweight too.

https://code.google.com/p/google-guice/

le-doude
  • 3,345
  • 2
  • 25
  • 55