Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I need a framework in order to use Dependency Injection? [duplicate]

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

like image 884
Kaloyan Roussev Avatar asked Aug 12 '13 11:08

Kaloyan Roussev


People also ask

Do I need a framework for dependency injection?

Dependency Injection is a pattern, not any particular framework. You could even implement it manually, but that would be too much trouble.

Do you need a DI framework?

We should use the dependency framework because of the following: It helps us in managing the complex dependencies easily. It makes the unit testing easy by enabling us to pass all the dependencies from outside so that we can easily use the mocked objects. It easily manages the scope(lifecycle) of the object.

Which framework is used for dependency injection?

. NET supports the dependency injection (DI) software design pattern, which is a technique for achieving Inversion of Control (IoC) between classes and their dependencies. Dependency injection in . NET is a built-in part of the framework, along with configuration, logging, and the options pattern.

What does dependency injection require?

Dependency injection helps to develop testable code, allowing developers to write unit tests easily. You can use mock databases with dependency injection, and test your application without affecting the actual database. Here's an example.


2 Answers

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

like image 82
John Avatar answered Oct 12 '22 20:10

John


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

like image 38
John Snow Avatar answered Oct 12 '22 20:10

John Snow