When to use Dependency Injection

前端 未结 8 923
野性不改
野性不改 2020-12-01 00:19

I\'ve had a certain feeling these last couple of days that dependency-injection should really be called \"I can\'t make up my mind\"-pattern. I know this might sound silly,

相关标签:
8条回答
  • 2020-12-01 00:25

    I just understood tonight. For me, dependancy injection is a method for instantiate objects which require a lot of parameters to work in a specific context.

    When should you use dependancy injection? You can use dependancy injection if you instanciate in a static way an object. For example, if you use a class which can convert objects into XML file or JSON file and if you need only the XML file. You will have to instanciate the object and configure a lot of thing if you don't use dependancy injection.

    When should you not use depandancy injection? If an object is instanciated with request parameters (after a submission form), you should not use depandancy injection because the object is not instanciated in a static way.

    0 讨论(0)
  • 2020-12-01 00:30

    Aside from loose coupling, testing of any type is achieved with much greater ease thanks to DI. You can put replace an existing dependency of a class under test with a mock, a dummy or even another version. If a class is created with its dependencies directly instantiated it can often be difficult or even impossible to "stub" them out if required.

    0 讨论(0)
  • 2020-12-01 00:35

    DI is very useful for decoupling your system. If all you're using it for is to decouple the database implementation from the rest of your application, then either your application is pretty simple or you need to do a lot more analysis on the problem domain and discover what components within your problem domain are the most likely to change and the components within your system that have a large amount of coupling.

    DI is most useful when you're aiming for code reuse, versatility and robustness to changes in your problem domain.

    How relevant it is to your project depends upon the expected lifespan of your code. Depending on the type of work you're doing zero reuse from one project to the next for the majority of code you're writing might actually be quite acceptable.

    An example for use the use of DI is in creating an application that can be deployed for several clients using DI to inject customisations for the client, which could also be described as the GOF Strategy pattern. Many of the GOF patterns can be facilitated with the use of a DI framework.

    DI is more relevant to Enterprise application development in which you have a large amount of code, complicated business requirements and an expectation (or hope) that the system will be maintained for many years or decades.

    0 讨论(0)
  • 2020-12-01 00:35

    While I semi-agree with you with the DB example, one of the large things that I found helpful to use DI is to help me test the layer I build on top of the database.

    Here's an example...

    You have your database.

    You have your code that accesses the database and returns objects

    You have business domain objects that take the previous item's objects and do some logic with them.

    If you merge the data access with your business domain logic, your domain objects can become difficult to test. DI allows you to inject your own data access objects into your domain so that you don't depend on the database for testing or possibly demonstrations (ran a demo where some data was pulled in from xml instead of a database).

    Abstracting 3rd party components and frameworks like this would also help you.

    Aside from the testing example, there's a few places where DI can be used through a Design by Contract approach. You may find it appropriate to create a processing engine of sorts that calls methods of the objects you're injecting into it. While it may not truly "process it" it runs the methods that have different implementation in each object you provide.

    I saw an example of this where the every business domain object had a "Save" function that the was called after it was injected into the processor. The processor modified the component with configuration information and Save handled the object's primary state. In essence, DI supplemented the polymorphic method implementation of the objects that conformed to the Interface.

    0 讨论(0)
  • 2020-12-01 00:39

    I think that DI is worth using when you have many services/components whose implementations must be selected at runtime based on external configuration. (Note that such configuration can take the form of an XML file or a combination of code annotations and separate classes; choose what is more convenient.)

    Otherwise, I would simply use a ServiceLocator, which is much "lighter" and easier to understand than a whole DI framework.

    For unit testing, I prefer to use a mocking API that can mock objects on demand, instead of requiring them to be "injected" into the tested unit from a test. For Java, one such library is my own, JMockit.

    0 讨论(0)
  • 2020-12-01 00:44

    Two words, unit testing.

    One of the most compelling reasons for DI is to allow easier unit testing without having to hit a database and worry about setting up 'test' data.

    0 讨论(0)
提交回复
热议问题