Dependency inversion always includes dependency injection? [closed]

This is a question on a home work. I’ve been up and down my notes and I can’t even see how the two are related. Googling this gives me questions/answers on one or the other, but never both.

14

Dependency Injection is a technique for implementing Dependency Inversion.

The Inversion of Control page on Wikipedia actually says it a little better, even though Dependency Inversion and Inversion of Control may not be exactly the same thing.

Here is what it says:

Implementation Techniques

In object-oriented programming, there are several basic techniques to
implement inversion of control. These are:

  • using a factory pattern
  • using a service locator pattern
  • using a dependency injection, for example:

    • Constructor injection
    • Parameter injection
    • Setter injection
    • Interface injection
  • using a contextualized lookup
  • using Template method design pattern
  • using Strategy design pattern

So no, Dependency Injection is not the only way to accomplish Dependency Inversion.

2

Answer: No. Here’s the difference:

Dependency Inversion is a principle.

High-level modules should not depend on low-level modules. Both should
depend on abstractions. Abstractions should not depend on details.
Details should depend on abstractions.

Dependency Injection is a technique, whereupon the dependencies — rather, their abstractions — are explicitly injected into an object, typically into the constructor, but sometimes exposed as properties. This can be done with Poor Man’s DI or with an IoC Container.

Example of Poor Man’s DI:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public class ConsumingClass
{
private readonly ISomeService SomeService;
public ConsumingClass(ISomeService someService)
{
SomeService = someService ?? new SomeServiceConcrete().
}
/* use SomeService elsewhere in the class */
}
</code>
<code>public class ConsumingClass { private readonly ISomeService SomeService; public ConsumingClass(ISomeService someService) { SomeService = someService ?? new SomeServiceConcrete(). } /* use SomeService elsewhere in the class */ } </code>
public class ConsumingClass
{
    private readonly ISomeService SomeService;

    public ConsumingClass(ISomeService someService)
    {
        SomeService = someService ?? new SomeServiceConcrete().
    }

    /* use SomeService elsewhere in the class */
}

Example of DI with IoC:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public class ConsumingClass
{
private readonly ISomeService SomeService;
public ConsumingClass(ISomeService someService)
{
SomeService = someService.
}
/* use SomeService elsewhere in the class */
}
</code>
<code>public class ConsumingClass { private readonly ISomeService SomeService; public ConsumingClass(ISomeService someService) { SomeService = someService. } /* use SomeService elsewhere in the class */ } </code>
public class ConsumingClass
{
    private readonly ISomeService SomeService;

    public ConsumingClass(ISomeService someService)
    {
        SomeService = someService.
    }

    /* use SomeService elsewhere in the class */
}

The Service Locator Pattern, in contrast to Dependency Injection, requires the object to ask a service for its dependencies. This is considered an anti-pattern. The details of why is beyond the scope of the question and is left as an exercise for the reader.

Example of Service Locator:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public class ConsumingClass
{
private readonly ISomeService SomeService;
public ConsumingClass()
{
SomeService = SomeServiceLocator.GetInstance<ISomeService>();
}
/* use SomeService elsewhere in the class */
}
</code>
<code>public class ConsumingClass { private readonly ISomeService SomeService; public ConsumingClass() { SomeService = SomeServiceLocator.GetInstance<ISomeService>(); } /* use SomeService elsewhere in the class */ } </code>
public class ConsumingClass
{
    private readonly ISomeService SomeService;

    public ConsumingClass()
    {
        SomeService = SomeServiceLocator.GetInstance<ISomeService>();
    }

    /* use SomeService elsewhere in the class */
}

2

The wiki page on Dependency inversion principle tells :

Various patterns such as Plugin, Service Locator, or Dependency
Injection are then employed to facilitate the run-time provisioning of
the chosen low-level component implementation to the high-level
component.

Therefore, the answer is : dependency injection is a technique to apply the dependency inversion principle, but it is not the only one.

There are other techniques when you want to use this principle (the linked wiki page lists plugins and service locator as additional techniques).

The dependency injection is a specific kind of inversion of control in a way that you somehow inject a lower level object into higher level object. That way, the higher level object depends on an abstraction – and not on the concrete implementation of a lower level object. In other words, it access an object through it’s (usually abstract) interface.

5

The simple answer to your question is “No, Dependency Injection is not always present when Dependency Inversion is used.”

Simply put, “Dependency Inversion” is the principle and “Dependency Injection” is just one method of implementation. As noted in another answer, there are other implementations of the principle including Service Locators and Plug-In Frameworks.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật