Does merely parameterizing a dependency meet the requirements for Inversion of Control?

When I find a concrete dependency inside an extension method, I have been attempting to remove the (concrete) dependency by parameterising it like so

// original implementation
public static List<Address> GetDuplicate(this Address address)
{
    var repository = new AddressRepository();
    return repository.FindAddress(address.Street, address.Town);
}

// 'Fixed' implementation using 'parameter injection'
public static List<Address> GetDuplicate(this Address address, IAddressRepository repository = null)
{
    repository = repository ?? new AddressRepository();
    return repository.FindAddress(address.Street, address.Town);
}

My idea was that this would invert control of the collaborating class, AddressRepository, by making sure it could be passed in by the code using the method.

Anyway when a colleague saw this they said that the code still relied on a concrete implementation and was not using IOC. Although I can see there are some downsides of the code, I’m not sure I agree that it isn’t Inversion of Control since

  1. The code depends on an abstraction (IAddressRepository)
  2. Calling code can provide a different implementation.

What principle is violated with respect to IOC? Which part of the code in particular violates that principle? Simply put, is ‘parameter injection’ actually a thing?

5

…does the parameterisation of the dependency meet the requirements for “inversion of control”?

No. That’s just Dependency Injection.

Look very carefully at the Wikipedia definition for Inversion of Control:

Inversion of Control (IoC) describes a design in which custom-written portions of a computer program receive the flow of control from a generic, reusable library [or framework].

In traditional programming, the custom code that expresses the purpose of the program calls into reusable libraries to take care of generic tasks, but with inversion of control, it is the reusable code [the library or framework] that calls into the custom, or task-specific, code.

In other words, Inversion of Control is characterized by the Hollywood Principle: Don’t call us, we’ll call you.

I can think of at least two ways Inversion of Control can occur in everyday use. One way is by making use of the Subscriber Pattern. In event-driven UI’s, we don’t call the UI framework when a user clicks on a button. Rather, we subscribe to a Click event, and the UI framework calls us:

// Subscribe to Click event.
this.button1.Click += new System.EventHandler(this.button1_Click);

// Handler for click event
private void button1_Click(object sender, MyEventArgs e)
{
    // Do something when user clicks button
}

Another example of Inversion of Control is sort methods that make use of a comparator to determine the behavior of the sort with different data types. The MSDN documentation for the Linq OrderBy method gives a good example of this.

First, we create a “comparer.” A comparer is a class that satisfies the IComparer interface:

public class IntegerComparer : IComparer<int>
{
    public int Compare(int i1, int i2)
    {
        return i1 - i2;
    }
}

Given an unsorted array of:

string[] unsortedArray = { "three", "six", "nine", "twelve", "fifteen", "eighteen" };

We can then use the comparer to sort the array by the number of characters in each string:

sortedArray = unsortedArray.OrderBy(a => a.Length, new IntegerComparer());

which yields the result:

six
nine
three
twelve
fifteen
eighteen

Once again, we have inverted control by relegating generic behavior to the framework, and having the framework call your custom code to get specific behavior.

Further Reading

Inversion of Control (Martin Fowler’s Bliki)

6

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