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
- The code depends on an abstraction (IAddressRepository)
- 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?